@thalesrc/hermes 6.1.1 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +417 -57
- package/broadcast/default-channel-name.cjs +4 -0
- package/broadcast/default-channel-name.js +2 -0
- package/broadcast/default-channel-name.js.map +1 -0
- package/broadcast/index.cjs +13 -0
- package/broadcast/index.js +2 -0
- package/broadcast/index.js.map +1 -0
- package/broadcast/message-client.cjs +26 -0
- package/broadcast/message-client.js +2 -0
- package/broadcast/message-client.js.map +1 -0
- package/broadcast/message-host.cjs +27 -0
- package/broadcast/message-host.js +2 -0
- package/broadcast/message-host.js.map +1 -0
- package/broadcast/message-service.cjs +13 -0
- package/broadcast/message-service.d.ts +1 -1
- package/broadcast/message-service.js +4 -2
- package/broadcast/message-service.js.map +1 -0
- package/chrome/default-connection-name.cjs +4 -0
- package/chrome/default-connection-name.js +2 -0
- package/chrome/default-connection-name.js.map +1 -0
- package/chrome/index.cjs +11 -0
- package/chrome/index.js +2 -0
- package/chrome/index.js.map +1 -0
- package/chrome/message-client.cjs +41 -0
- package/chrome/message-client.js +2 -0
- package/chrome/message-client.js.map +1 -0
- package/chrome/message-host.cjs +42 -0
- package/chrome/message-host.d.ts +1 -5
- package/chrome/message-host.js +11 -11
- package/chrome/message-host.js.map +1 -0
- package/iframe/channel-path-splitter.cjs +4 -0
- package/iframe/channel-path-splitter.js +2 -0
- package/iframe/channel-path-splitter.js.map +1 -0
- package/iframe/default-channel-name.cjs +4 -0
- package/iframe/default-channel-name.js +2 -0
- package/iframe/default-channel-name.js.map +1 -0
- package/iframe/iframe.type.cjs +2 -0
- package/iframe/iframe.type.js +2 -0
- package/iframe/iframe.type.js.map +1 -0
- package/iframe/index.cjs +13 -0
- package/iframe/index.js +2 -0
- package/iframe/index.js.map +1 -0
- package/iframe/message-client.cjs +50 -0
- package/iframe/message-client.d.ts +1 -5
- package/iframe/message-client.js +10 -10
- package/iframe/message-client.js.map +1 -0
- package/iframe/message-host.cjs +62 -0
- package/iframe/message-host.d.ts +1 -11
- package/iframe/message-host.js +20 -23
- package/iframe/message-host.js.map +1 -0
- package/iframe/message-service.cjs +13 -0
- package/iframe/message-service.d.ts +1 -1
- package/iframe/message-service.js +4 -2
- package/iframe/message-service.js.map +1 -0
- package/iframe/source-id-splitter.cjs +4 -0
- package/iframe/source-id-splitter.js +2 -0
- package/iframe/source-id-splitter.js.map +1 -0
- package/iframe/upcoming-message.cjs +2 -0
- package/iframe/upcoming-message.js +2 -0
- package/iframe/upcoming-message.js.map +1 -0
- package/index.cjs +13 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/listen.decorator.cjs +20 -0
- package/listen.decorator.js +4 -2
- package/listen.decorator.js.map +1 -0
- package/listener-storage.type.cjs +2 -0
- package/listener-storage.type.js +2 -0
- package/listener-storage.type.js.map +1 -0
- package/message-client.cjs +10 -0
- package/message-client.js +2 -0
- package/message-client.js.map +1 -0
- package/message-host.cjs +53 -0
- package/message-host.js +2 -0
- package/message-host.js.map +1 -0
- package/message-response.type.cjs +2 -0
- package/message-response.type.js +2 -0
- package/message-response.type.js.map +1 -0
- package/message.interface.cjs +2 -0
- package/message.interface.js +2 -0
- package/message.interface.js.map +1 -0
- package/package.json +32 -34
- package/request.decorator.cjs +20 -0
- package/request.decorator.js +3 -1
- package/request.decorator.js.map +1 -0
- package/selectors.cjs +8 -0
- package/selectors.js +2 -0
- package/selectors.js.map +1 -0
- package/worker/index.cjs +9 -0
- package/worker/index.js +2 -0
- package/worker/index.js.map +1 -0
- package/worker/message-client.cjs +40 -0
- package/worker/message-client.d.ts +1 -1
- package/worker/message-client.js +2 -0
- package/worker/message-client.js.map +1 -0
- package/worker/message-host.cjs +43 -0
- package/worker/message-host.js +2 -0
- package/worker/message-host.js.map +1 -0
- package/worker/message-service.cjs +12 -0
- package/worker/message-service.d.ts +1 -1
- package/worker/message-service.js +4 -2
- package/worker/message-service.js.map +1 -0
- package/.editorconfig +0 -9
- package/CODE_OF_CONDUCT.md +0 -76
- package/codecov.yml +0 -8
- package/jest.config.js +0 -22
- package/mixin.d.ts +0 -12
- package/mixin.js +0 -18
- package/setup-test.ts +0 -0
- package/tsconfig.lib.json +0 -12
- package/tsconfig.spec.json +0 -11
- package/tslint.json +0 -14
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IframeMessageService = void 0;
|
|
4
|
+
const mixin_1 = require("@thalesrc/js-utils/class/mixin");
|
|
5
|
+
const message_client_1 = require("./message-client");
|
|
6
|
+
const message_host_1 = require("./message-host");
|
|
7
|
+
const default_channel_name_1 = require("./default-channel-name");
|
|
8
|
+
class IframeMessageService extends (0, mixin_1.mixin)(message_host_1.IframeMessageHost, message_client_1.IframeMessageClient) {
|
|
9
|
+
constructor(channelName = default_channel_name_1.DEFAULT_CHANNEL_NAME, targetFrame) {
|
|
10
|
+
super([channelName, targetFrame], [channelName, targetFrame]);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.IframeMessageService = IframeMessageService;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IframeMessageClient } from "./message-client";
|
|
2
2
|
import { IFrame } from "./iframe.type";
|
|
3
|
-
declare const IframeMessageService_base: new (args_0: [channelName?: string, targetFrame?: IFrame], args_1: [channelName?: string, targetFrame?: IFrame]) => {
|
|
3
|
+
declare const IframeMessageService_base: new (args_0: [channelName?: string | undefined, targetFrame?: IFrame | undefined], args_1: [channelName?: string | undefined, targetFrame?: IFrame | undefined]) => {
|
|
4
4
|
terminate: () => void;
|
|
5
5
|
} & IframeMessageClient;
|
|
6
6
|
export declare class IframeMessageService extends IframeMessageService_base {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mixin } from "@thalesrc/js-utils/class/mixin";
|
|
2
2
|
import { IframeMessageClient } from "./message-client";
|
|
3
3
|
import { IframeMessageHost } from "./message-host";
|
|
4
4
|
import { DEFAULT_CHANNEL_NAME } from "./default-channel-name";
|
|
5
|
-
export class IframeMessageService extends
|
|
5
|
+
export class IframeMessageService extends mixin(IframeMessageHost, IframeMessageClient) {
|
|
6
6
|
constructor(channelName = DEFAULT_CHANNEL_NAME, targetFrame) {
|
|
7
7
|
super([channelName, targetFrame], [channelName, targetFrame]);
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=message-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/iframe/message-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAG9D,MAAM,OAAO,oBAAqB,SAAQ,KAAK,CAAC,iBAAiB,EAAE,mBAAmB,CAAC;IACrF,YACE,WAAW,GAAG,oBAAoB,EAClC,WAAoB;QAEpB,KAAK,CAAC,CAAC,WAAW,EAAE,WAAW,CAAC,EAAE,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC;IAChE,CAAC;CACF","file":"message-service.js","sourcesContent":["import { mixin } from \"@thalesrc/js-utils/class/mixin\";\nimport { IframeMessageClient } from \"./message-client\";\nimport { IframeMessageHost } from \"./message-host\";\nimport { DEFAULT_CHANNEL_NAME } from \"./default-channel-name\";\nimport { IFrame } from \"./iframe.type\";\n\nexport class IframeMessageService extends mixin(IframeMessageHost, IframeMessageClient) {\n constructor(\n channelName = DEFAULT_CHANNEL_NAME,\n targetFrame?: IFrame\n ) {\n super([channelName, targetFrame], [channelName, targetFrame]);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/iframe/source-id-splitter.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,kBAAkB,GAAG,+BAA+B,CAAC","file":"source-id-splitter.js","sourcesContent":["export const SOURCE_ID_SPLITTER = '__hermes_source_id_splitter__';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/iframe/upcoming-message.ts"],"names":[],"mappings":"","file":"upcoming-message.js","sourcesContent":["export interface UpcomingMessage<T = any> {\n data: T;\n sender: Window;\n}\n"]}
|
package/index.cjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageHost = exports.MessageClient = exports.Listen = exports.Request = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
var request_decorator_1 = require("./request.decorator");
|
|
6
|
+
Object.defineProperty(exports, "Request", { enumerable: true, get: function () { return request_decorator_1.Request; } });
|
|
7
|
+
var listen_decorator_1 = require("./listen.decorator");
|
|
8
|
+
Object.defineProperty(exports, "Listen", { enumerable: true, get: function () { return listen_decorator_1.Listen; } });
|
|
9
|
+
var message_client_1 = require("./message-client");
|
|
10
|
+
Object.defineProperty(exports, "MessageClient", { enumerable: true, get: function () { return message_client_1.MessageClient; } });
|
|
11
|
+
var message_host_1 = require("./message-host");
|
|
12
|
+
Object.defineProperty(exports, "MessageHost", { enumerable: true, get: function () { return message_host_1.MessageHost; } });
|
|
13
|
+
tslib_1.__exportStar(require("./selectors"), exports);
|
package/index.js
CHANGED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAG5C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,cAAc,aAAa,CAAC","file":"index.js","sourcesContent":["export { Request } from './request.decorator';\nexport { Listen } from './listen.decorator';\nexport { Message } from './message.interface';\nexport { MessageResponse } from './message-response.type';\nexport { MessageClient } from './message-client';\nexport { MessageHost } from './message-host';\nexport * from './selectors';\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Listen = Listen;
|
|
4
|
+
const selectors_1 = require("./selectors");
|
|
5
|
+
/**
|
|
6
|
+
* Decorate a generator method to listen messages of a domain and reply them by using `yield`
|
|
7
|
+
*
|
|
8
|
+
* @param path Acts like an api endpoint path of an XHR for extensions messaging
|
|
9
|
+
*/
|
|
10
|
+
function Listen(path) {
|
|
11
|
+
return function (target, key, descriptor) {
|
|
12
|
+
if (!target.constructor[selectors_1.MESSAGE_LISTENERS]) {
|
|
13
|
+
target.constructor[selectors_1.MESSAGE_LISTENERS] = new Map();
|
|
14
|
+
}
|
|
15
|
+
target.constructor[selectors_1.MESSAGE_LISTENERS].set(path ?? key, [
|
|
16
|
+
...(target.constructor[selectors_1.MESSAGE_LISTENERS].get(path ?? key) || []),
|
|
17
|
+
descriptor.value
|
|
18
|
+
]);
|
|
19
|
+
};
|
|
20
|
+
}
|
package/listen.decorator.js
CHANGED
|
@@ -9,9 +9,11 @@ export function Listen(path) {
|
|
|
9
9
|
if (!target.constructor[MESSAGE_LISTENERS]) {
|
|
10
10
|
target.constructor[MESSAGE_LISTENERS] = new Map();
|
|
11
11
|
}
|
|
12
|
-
target.constructor[MESSAGE_LISTENERS].set(path
|
|
13
|
-
...(target.constructor[MESSAGE_LISTENERS].get(path
|
|
12
|
+
target.constructor[MESSAGE_LISTENERS].set(path ?? key, [
|
|
13
|
+
...(target.constructor[MESSAGE_LISTENERS].get(path ?? key) || []),
|
|
14
14
|
descriptor.value
|
|
15
15
|
]);
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=listen.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/listen.decorator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,IAAa;IAClC,OAAO,UAAS,MAAM,EAAE,GAAG,EAAE,UAAU;QACrC,IAAI,CAAE,MAAM,CAAC,WAAmB,CAAC,iBAAiB,CAAC,EAAE,CAAC;YACnD,MAAM,CAAC,WAAmB,CAAC,iBAAiB,CAAC,GAAG,IAAI,GAAG,EAAE,CAAC;QAC7D,CAAC;QAEC,MAAM,CAAC,WAAmB,CAAC,iBAAiB,CAAqB,CAAC,GAAG,CAAC,IAAK,IAAI,GAAG,EAAE;YACpF,GAAG,CAAE,MAAM,CAAC,WAAmB,CAAC,iBAAiB,CAAC,CAAC,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;YAC1E,UAAU,CAAC,KAAK;SACV,CAAC,CAAC;IACZ,CAAC,CAAC;AACJ,CAAC","file":"listen.decorator.js","sourcesContent":["import { ListenerStorage } from './listener-storage.type';\nimport { MESSAGE_LISTENERS } from './selectors';\n\n/**\n * Decorate a generator method to listen messages of a domain and reply them by using `yield`\n *\n * @param path Acts like an api endpoint path of an XHR for extensions messaging\n */\nexport function Listen(path?: string): MethodDecorator {\n return function(target, key, descriptor) {\n if (!(target.constructor as any)[MESSAGE_LISTENERS]) {\n (target.constructor as any)[MESSAGE_LISTENERS] = new Map();\n }\n\n ((target.constructor as any)[MESSAGE_LISTENERS] as ListenerStorage).set(path! ?? key, [\n ...((target.constructor as any)[MESSAGE_LISTENERS].get(path ?? key) || []),\n descriptor.value\n ] as any);\n };\n}\n"]}
|
package/listener-storage.type.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/listener-storage.type.ts"],"names":[],"mappings":"","file":"listener-storage.type.js","sourcesContent":["import { Observable } from 'rxjs';\n\nexport type ListenerStorage<T = any> = Map<string, ((message: any) => Observable<T>)[]>;\n"]}
|
package/message-client.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/message-client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAE3D;;GAEG;AACH,MAAM,OAAgB,aAAa;CAiBlC","file":"message-client.js","sourcesContent":["import { Observable } from 'rxjs';\n\nimport { MessageResponse } from './message-response.type';\nimport { Message } from './message.interface';\nimport { GET_NEW_ID, RESPONSES$, SEND } from './selectors';\n\n/**\n * Message Client\n */\nexport abstract class MessageClient {\n /**\n * Emits all messages got from hosts\n */\n protected abstract readonly [RESPONSES$]: Observable<MessageResponse>;\n\n /**\n * Request decorators use this method to send messages\n *\n * @param message Message payload to send\n */\n protected abstract [SEND](message: Message): void;\n\n /**\n * Request decorators use this method to get unique message id all accross the platform\n */\n protected abstract [GET_NEW_ID](): string;\n}\n"]}
|
package/message-host.cjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageHost = void 0;
|
|
4
|
+
const js_utils_1 = require("@thalesrc/js-utils");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
const operators_1 = require("rxjs/operators");
|
|
7
|
+
const selectors_1 = require("./selectors");
|
|
8
|
+
/**
|
|
9
|
+
* Message Host
|
|
10
|
+
*/
|
|
11
|
+
class MessageHost {
|
|
12
|
+
/**
|
|
13
|
+
* Message Terminator Subject
|
|
14
|
+
*
|
|
15
|
+
* Use `next(messageId)` method to terminate a message connection
|
|
16
|
+
*/
|
|
17
|
+
terminateMessage$ = new rxjs_1.Subject();
|
|
18
|
+
/**
|
|
19
|
+
* Run this method to start listening the requests
|
|
20
|
+
*/
|
|
21
|
+
listen = (messages$) => {
|
|
22
|
+
for (const [path, listeners] of this.#getListeners()) {
|
|
23
|
+
messages$
|
|
24
|
+
.pipe((0, operators_1.filter)(({ path: messagePath }) => path === messagePath))
|
|
25
|
+
.subscribe(({ body, id }) => {
|
|
26
|
+
for (const listener of listeners) {
|
|
27
|
+
listener.call(this, body).pipe((0, operators_1.takeUntil)(this.terminateMessage$.pipe((0, operators_1.filter)(terminatedMessageId => terminatedMessageId === id)))).subscribe(result => {
|
|
28
|
+
this.response({ completed: false, id, body: result });
|
|
29
|
+
}, js_utils_1.noop, () => {
|
|
30
|
+
this.response({ completed: true, id });
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* All inherited listeners
|
|
38
|
+
*/
|
|
39
|
+
#getListeners() {
|
|
40
|
+
const map = new Map();
|
|
41
|
+
let currentProto = this['__proto__' + ''];
|
|
42
|
+
while (currentProto.constructor !== Object) {
|
|
43
|
+
if (Reflect.ownKeys(currentProto.constructor).includes(selectors_1.MESSAGE_LISTENERS)) {
|
|
44
|
+
for (const [key, handlers] of currentProto.constructor[selectors_1.MESSAGE_LISTENERS]) {
|
|
45
|
+
map.set(key, [...(map.get(key) || []), ...handlers]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
currentProto = currentProto.__proto__;
|
|
49
|
+
}
|
|
50
|
+
return map;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.MessageHost = MessageHost;
|
package/message-host.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/message-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAc,OAAO,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAKnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD;;GAEG;AACH,MAAM,OAAgB,WAAW;IAC/B;;;;OAIG;IACgB,iBAAiB,GAAG,IAAI,OAAO,EAAU,CAAC;IAE7D;;OAEG;IACgB,MAAM,GAAG,CAAC,SAA8B,EAAQ,EAAE;QACnE,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,IAAI,CAAC,aAAa,EAAE,EAAE,CAAC;YACrD,SAAS;iBACN,IAAI,CAAC,MAAM,CAAC,CAAC,EAAC,IAAI,EAAE,WAAW,EAAC,EAAE,EAAE,CAAC,IAAI,KAAK,WAAW,CAAC,CAAC;iBAC3D,SAAS,CAAC,CAAC,EAAC,IAAI,EAAE,EAAE,EAAC,EAAE,EAAE;gBACxB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;oBAChC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAqB,CAAC,IAAI,CACjD,SAAS,CAAC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,EAAE,CAAC,mBAAmB,KAAK,EAAE,CAAC,CAAC,CAAC,CAClG,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;wBACnB,IAAI,CAAC,QAAQ,CAAC,EAAC,SAAS,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAC,CAAC,CAAC;oBACtD,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE;wBACZ,IAAI,CAAC,QAAQ,CAAC,EAAC,SAAS,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;oBACxC,CAAC,CAAC,CAAC;gBACL,CAAC;YACH,CAAC,CAAC,CAAC;QACP,CAAC;IACH,CAAC,CAAA;IASD;;OAEG;IACH,aAAa;QACX,MAAM,GAAG,GAAoB,IAAI,GAAG,EAAE,CAAC;QAEvC,IAAI,YAAY,GAAI,IAAY,CAAC,WAAW,GAAG,EAAE,CAAC,CAAC;QAEnD,OAAO,YAAY,CAAC,WAAW,KAAK,MAAM,EAAE,CAAC;YAC3C,IAAI,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC;gBAC1E,KAAK,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,IAAI,YAAY,CAAC,WAAW,CAAC,iBAAiB,CAAoB,EAAE,CAAC;oBAC7F,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC;gBACvD,CAAC;YACH,CAAC;YAED,YAAY,GAAG,YAAY,CAAC,SAAS,CAAC;QACxC,CAAC;QAED,OAAO,GAAG,CAAC;IACb,CAAC;CACF","file":"message-host.js","sourcesContent":["import { noop } from '@thalesrc/js-utils';\nimport { Observable, Subject } from 'rxjs';\nimport { filter, takeUntil } from 'rxjs/operators';\n\nimport { ListenerStorage } from './listener-storage.type';\nimport { SuccessfulMessageResponse } from './message-response.type';\nimport { Message } from './message.interface';\nimport { MESSAGE_LISTENERS } from './selectors';\n\n\n/**\n * Message Host\n */\nexport abstract class MessageHost {\n /**\n * Message Terminator Subject\n *\n * Use `next(messageId)` method to terminate a message connection\n */\n protected readonly terminateMessage$ = new Subject<string>();\n\n /**\n * Run this method to start listening the requests\n */\n protected readonly listen = (messages$: Observable<Message>): void => {\n for (const [path, listeners] of this.#getListeners()) {\n messages$\n .pipe(filter(({path: messagePath}) => path === messagePath))\n .subscribe(({body, id}) => {\n for (const listener of listeners) {\n (listener.call(this, body) as Observable<any>).pipe(\n takeUntil(this.terminateMessage$.pipe(filter(terminatedMessageId => terminatedMessageId === id))),\n ).subscribe(result => {\n this.response({completed: false, id, body: result});\n }, noop, () => {\n this.response({completed: true, id });\n });\n }\n });\n }\n }\n\n /**\n * Build a reponse method to send the responses to the requests by using the communication methods of the platform\n *\n * @param message Incoming response message\n */\n protected abstract response<T = any>(message: SuccessfulMessageResponse<T>): void;\n\n /**\n * All inherited listeners\n */\n #getListeners(): ListenerStorage {\n const map: ListenerStorage = new Map();\n\n let currentProto = (this as any)['__proto__' + ''];\n\n while (currentProto.constructor !== Object) {\n if (Reflect.ownKeys(currentProto.constructor).includes(MESSAGE_LISTENERS)) {\n for (const [key, handlers] of currentProto.constructor[MESSAGE_LISTENERS] as ListenerStorage) {\n map.set(key, [...(map.get(key) || []), ...handlers]);\n }\n }\n\n currentProto = currentProto.__proto__;\n }\n\n return map;\n }\n}\n"]}
|
package/message-response.type.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/message-response.type.ts"],"names":[],"mappings":"","file":"message-response.type.js","sourcesContent":["import { Message } from './message.interface';\n\ntype Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;\n\nexport type UncompletedMessageResponse<T = any> = Omit<Message<T>, 'path'> & { completed: false; };\ntype CompletedMessageResponse = Omit<Message, 'body' | 'path'> & { completed: true; };\n\nexport type ErrorMessageResponse = {error: any};\nexport type SuccessfulMessageResponse<T = any> = UncompletedMessageResponse<T> | CompletedMessageResponse;\n\nexport type MessageResponse<T = any> = SuccessfulMessageResponse<T> | ErrorMessageResponse;\n"]}
|
package/message.interface.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/message.interface.ts"],"names":[],"mappings":"","file":"message.interface.js","sourcesContent":["export interface Message<T = any> {\n path: string;\n body: T;\n id: string;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thalesrc/hermes",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.1",
|
|
4
4
|
"description": "Javascript messaging library",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"typings": "index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build": "tsc -p ./tsconfig.lib.json",
|
|
9
|
-
"test": "jest",
|
|
10
|
-
"test:coverage": "jest --collectCoverage",
|
|
11
|
-
"lint": "tslint -p tsconfig.json && tslint -p tsconfig.spec.json",
|
|
12
|
-
"prepublishOnly": "npm version $PACKAGE_VERSION --no-git-tag-version && tsc -p ./tsconfig.lib.json"
|
|
13
|
-
},
|
|
14
|
-
"repository": {
|
|
15
|
-
"type": "git",
|
|
16
|
-
"url": "git+https://github.com/thalesrc/hermes.git"
|
|
17
|
-
},
|
|
18
5
|
"keywords": [
|
|
19
6
|
"javascript",
|
|
20
7
|
"messaging",
|
|
@@ -28,28 +15,39 @@
|
|
|
28
15
|
"process",
|
|
29
16
|
"webworkers"
|
|
30
17
|
],
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
"url": "https://github.com/thalesrc/
|
|
18
|
+
"homepage": "https://github.com/thalesrc/thalesrc/tree/main/libs/hermes#readme",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/thalesrc/thalesrc.git",
|
|
22
|
+
"directory": "libs/hermes"
|
|
35
23
|
},
|
|
36
|
-
"homepage": "https://github.com/thalesrc/hermes#readme",
|
|
37
24
|
"dependencies": {
|
|
38
|
-
"@thalesrc/js-utils": "^
|
|
39
|
-
"rxjs": "^7.8.1"
|
|
25
|
+
"@thalesrc/js-utils": "^3.4.0",
|
|
26
|
+
"rxjs": "^7.8.1",
|
|
27
|
+
"tslib": "^2.3.0"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"module": "./index.js",
|
|
31
|
+
"main": "./index.cjs",
|
|
32
|
+
"typings": "./index.d.ts",
|
|
33
|
+
"bugs": "https://github.com/thalesrc/thalesrc/issues",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"author": {
|
|
36
|
+
"name": "Ali Şahin Özçelik",
|
|
37
|
+
"email": "alisahinozcelik@gmail.com",
|
|
38
|
+
"url": "https://alisah.in"
|
|
40
39
|
},
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"cpy-cli": "^5.0.0",
|
|
45
|
-
"jest": "^29.7.0",
|
|
46
|
-
"replace": "^1.2.2",
|
|
47
|
-
"rxjs-marbles": "^7.0.1",
|
|
48
|
-
"ts-jest": "^29.1.1",
|
|
49
|
-
"tslint": "^5.20.1",
|
|
50
|
-
"typescript": "^5.3.2"
|
|
40
|
+
"funding": {
|
|
41
|
+
"type": "patreon",
|
|
42
|
+
"url": "https://www.patreon.com/alisahin"
|
|
51
43
|
},
|
|
52
|
-
"
|
|
53
|
-
"
|
|
44
|
+
"exports": {
|
|
45
|
+
".": {
|
|
46
|
+
"default": "./index.cjs",
|
|
47
|
+
"import": "./index.js",
|
|
48
|
+
"node": "./index.cjs",
|
|
49
|
+
"require": "./index.cjs",
|
|
50
|
+
"types": "./index.d.ts"
|
|
51
|
+
}
|
|
54
52
|
}
|
|
55
|
-
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Request = Request;
|
|
4
|
+
const operators_1 = require("rxjs/operators");
|
|
5
|
+
const selectors_1 = require("./selectors");
|
|
6
|
+
function Request(path) {
|
|
7
|
+
return function (target, key, descriptor) {
|
|
8
|
+
descriptor.value = function (message) {
|
|
9
|
+
const messageId = this[selectors_1.GET_NEW_ID]();
|
|
10
|
+
this[selectors_1.SEND]({ body: message, id: messageId, path });
|
|
11
|
+
return this[selectors_1.RESPONSES$].pipe((0, operators_1.map)((data) => {
|
|
12
|
+
const { error } = data;
|
|
13
|
+
if (error)
|
|
14
|
+
throw error;
|
|
15
|
+
return data;
|
|
16
|
+
}), (0, operators_1.filter)(({ id }) => id === messageId), (0, operators_1.takeWhile)((res) => !res.completed), (0, operators_1.map)(({ body }) => body));
|
|
17
|
+
};
|
|
18
|
+
return descriptor;
|
|
19
|
+
};
|
|
20
|
+
}
|
package/request.decorator.js
CHANGED
|
@@ -10,8 +10,10 @@ export function Request(path) {
|
|
|
10
10
|
if (error)
|
|
11
11
|
throw error;
|
|
12
12
|
return data;
|
|
13
|
-
}), filter(({ id }) => id === messageId), takeWhile((
|
|
13
|
+
}), filter(({ id }) => id === messageId), takeWhile((res) => !res.completed), map(({ body }) => body));
|
|
14
14
|
};
|
|
15
15
|
return descriptor;
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=request.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/request.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGxD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAG3D,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,OAAO,UAAS,MAAM,EAAE,GAAG,EAAE,UAAwC;QACnE,UAAU,CAAC,KAAK,GAAG,UAA8B,OAAY;YAC3D,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YAErC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAC,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAC,CAAC,CAAC;YAEjD,OAAO,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAC1B,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;gBACX,MAAM,EAAC,KAAK,EAAC,GAAG,IAA4B,CAAC;gBAE7C,IAAI,KAAK;oBAAE,MAAM,KAAK,CAAC;gBAEvB,OAAO,IAAiC,CAAC;YAC3C,CAAC,CAAC,EACF,MAAM,CAAC,CAAC,EAAC,EAAE,EAAC,EAAE,EAAE,CAAC,EAAE,KAAK,SAAS,CAAC,EAClC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,EAClC,GAAG,CAAC,CAAC,EAAC,IAAI,EAA6B,EAAE,EAAE,CAAC,IAAI,CAAC,CAClD,CAAC;QACJ,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC","file":"request.decorator.js","sourcesContent":["import { filter, map, takeWhile } from 'rxjs/operators';\n\nimport { MessageClient } from './message-client';\nimport { GET_NEW_ID, RESPONSES$, SEND } from './selectors';\nimport { ErrorMessageResponse, SuccessfulMessageResponse, UncompletedMessageResponse } from './message-response.type';\n\nexport function Request(path: string): MethodDecorator {\n return function(target, key, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> {\n descriptor.value = function(this: MessageClient, message: any) {\n const messageId = this[GET_NEW_ID]();\n\n this[SEND]({body: message, id: messageId, path});\n\n return this[RESPONSES$].pipe(\n map((data) => {\n const {error} = data as ErrorMessageResponse;\n\n if (error) throw error;\n\n return data as SuccessfulMessageResponse;\n }),\n filter(({id}) => id === messageId),\n takeWhile((res) => !res.completed),\n map(({body}: UncompletedMessageResponse) => body),\n );\n };\n\n return descriptor;\n };\n}\n"]}
|
package/selectors.cjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GET_NEW_ID = exports.SEND = exports.RESPONSES$ = exports.SENDING_SERVICE = exports.MESSAGE_LISTENERS = void 0;
|
|
4
|
+
exports.MESSAGE_LISTENERS = Symbol('Message Listeners');
|
|
5
|
+
exports.SENDING_SERVICE = Symbol('Sending Service');
|
|
6
|
+
exports.RESPONSES$ = Symbol('Responses');
|
|
7
|
+
exports.SEND = Symbol('Send');
|
|
8
|
+
exports.GET_NEW_ID = Symbol('Get New Id');
|
package/selectors.js
CHANGED
package/selectors.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/selectors.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAC7D,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AACzD,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AAC9C,MAAM,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AACnC,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC","file":"selectors.js","sourcesContent":["export const MESSAGE_LISTENERS = Symbol('Message Listeners');\nexport const SENDING_SERVICE = Symbol('Sending Service');\nexport const RESPONSES$ = Symbol('Responses');\nexport const SEND = Symbol('Send');\nexport const GET_NEW_ID = Symbol('Get New Id');\n"]}
|
package/worker/index.cjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Listen = exports.Request = exports.WorkerMessageService = void 0;
|
|
4
|
+
var message_service_1 = require("./message-service");
|
|
5
|
+
Object.defineProperty(exports, "WorkerMessageService", { enumerable: true, get: function () { return message_service_1.WorkerMessageService; } });
|
|
6
|
+
var request_decorator_1 = require("../request.decorator");
|
|
7
|
+
Object.defineProperty(exports, "Request", { enumerable: true, get: function () { return request_decorator_1.Request; } });
|
|
8
|
+
var listen_decorator_1 = require("../listen.decorator");
|
|
9
|
+
Object.defineProperty(exports, "Listen", { enumerable: true, get: function () { return listen_decorator_1.Listen; } });
|
package/worker/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/worker/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC","file":"index.js","sourcesContent":["export { WorkerMessageService } from './message-service';\nexport { Request } from '../request.decorator';\nexport { Listen } from '../listen.decorator';\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkerMessageClient = void 0;
|
|
4
|
+
const js_utils_1 = require("@thalesrc/js-utils");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
const message_client_1 = require("../message-client");
|
|
7
|
+
const selectors_1 = require("../selectors");
|
|
8
|
+
const WORKER = Symbol('Worker');
|
|
9
|
+
const HANDLER = Symbol('Handler');
|
|
10
|
+
const INSTANCE_ID = Symbol('Instance Id');
|
|
11
|
+
class WorkerMessageClient extends message_client_1.MessageClient {
|
|
12
|
+
[selectors_1.RESPONSES$] = new rxjs_1.Subject();
|
|
13
|
+
[WORKER];
|
|
14
|
+
[INSTANCE_ID] = Date.now();
|
|
15
|
+
constructor(worker) {
|
|
16
|
+
super();
|
|
17
|
+
this[WORKER] = worker;
|
|
18
|
+
if (worker) {
|
|
19
|
+
worker.addEventListener('message', this[HANDLER]);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
addEventListener('message', this[HANDLER]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
[selectors_1.SEND](message) {
|
|
26
|
+
if (this[WORKER]) {
|
|
27
|
+
this[WORKER].postMessage(message);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
postMessage(message);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
[HANDLER] = (event) => {
|
|
34
|
+
this[selectors_1.RESPONSES$].next(event.data);
|
|
35
|
+
};
|
|
36
|
+
[selectors_1.GET_NEW_ID]() {
|
|
37
|
+
return (0, js_utils_1.uniqueId)('hermes-worker-message-' + this[INSTANCE_ID]);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.WorkerMessageClient = WorkerMessageClient;
|
|
@@ -11,7 +11,7 @@ declare const HANDLER: unique symbol;
|
|
|
11
11
|
declare const INSTANCE_ID: unique symbol;
|
|
12
12
|
export declare class WorkerMessageClient extends MessageClient {
|
|
13
13
|
[RESPONSES$]: Subject<MessageResponse>;
|
|
14
|
-
protected [WORKER]: Worker;
|
|
14
|
+
protected [WORKER]: Worker | undefined;
|
|
15
15
|
private [INSTANCE_ID];
|
|
16
16
|
constructor(worker?: Worker);
|
|
17
17
|
[SEND]<T>(message: Message<T>): void;
|
package/worker/message-client.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/worker/message-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGlD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAM5D,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AAChC,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAClC,MAAM,WAAW,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAE1C,MAAM,OAAO,mBAAoB,SAAQ,aAAa;IAC7C,CAAC,UAAU,CAAC,GAAG,IAAI,OAAO,EAAmB,CAAC;IAC3C,CAAC,MAAM,CAAC,CAAqB;IAC/B,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAEnC,YAAY,MAAe;QACzB,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;QAEtB,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QACpD,CAAC;aAAM,CAAC;YACN,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IAEM,CAAC,IAAI,CAAC,CAAI,OAAmB;QAClC,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QACpC,CAAC;aAAM,CAAC;YACL,WAAmB,CAAC,OAAO,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IAES,CAAC,OAAO,CAAC,GAAG,CAAC,KAAoC,EAAE,EAAE;QAC7D,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC,CAAA;IAES,CAAC,UAAU,CAAC;QACpB,OAAO,QAAQ,CAAC,wBAAwB,GAAG,IAAI,CAAC,WAAW,CAAC,CAAW,CAAC;IAC1E,CAAC;CACF","file":"message-client.js","sourcesContent":["import { uniqueId } from \"@thalesrc/js-utils\";\nimport { Subject } from \"rxjs\";\nimport { MessageClient } from \"../message-client\";\nimport { MessageResponse } from \"../message-response.type\";\nimport { Message } from \"../message.interface\";\nimport { GET_NEW_ID, RESPONSES$, SEND } from \"../selectors\";\n\ninterface MessageEvent<T> {\n data: T;\n}\n\nconst WORKER = Symbol('Worker');\nconst HANDLER = Symbol('Handler');\nconst INSTANCE_ID = Symbol('Instance Id');\n\nexport class WorkerMessageClient extends MessageClient {\n public [RESPONSES$] = new Subject<MessageResponse>();\n protected [WORKER]: Worker | undefined;\n private [INSTANCE_ID] = Date.now();\n\n constructor(worker?: Worker) {\n super();\n\n this[WORKER] = worker;\n\n if (worker) {\n worker.addEventListener('message', this[HANDLER]);\n } else {\n addEventListener('message', this[HANDLER]);\n }\n }\n\n public [SEND]<T>(message: Message<T>) {\n if (this[WORKER]) {\n this[WORKER].postMessage(message);\n } else {\n (postMessage as any)(message);\n }\n }\n\n protected [HANDLER] = (event: MessageEvent<MessageResponse>) => {\n this[RESPONSES$].next(event.data);\n }\n\n protected [GET_NEW_ID](): string {\n return uniqueId('hermes-worker-message-' + this[INSTANCE_ID]) as string;\n }\n}\n"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkerMessageHost = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const message_host_1 = require("../message-host");
|
|
6
|
+
const REQUESTS$ = Symbol('Requests');
|
|
7
|
+
const HANDLER = Symbol('Handler');
|
|
8
|
+
const WORKER = Symbol('Worker');
|
|
9
|
+
class WorkerMessageHost extends message_host_1.MessageHost {
|
|
10
|
+
[REQUESTS$] = new rxjs_1.Subject();
|
|
11
|
+
[WORKER];
|
|
12
|
+
constructor(worker) {
|
|
13
|
+
super();
|
|
14
|
+
if (worker) {
|
|
15
|
+
this[WORKER] = worker;
|
|
16
|
+
worker.addEventListener('message', this[HANDLER]);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
addEventListener('message', this[HANDLER]);
|
|
20
|
+
}
|
|
21
|
+
this.listen(this[REQUESTS$]);
|
|
22
|
+
}
|
|
23
|
+
response(message) {
|
|
24
|
+
if (this[WORKER]) {
|
|
25
|
+
this[WORKER].postMessage(message);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
postMessage(message);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
terminate() {
|
|
32
|
+
if (this[WORKER]) {
|
|
33
|
+
this[WORKER].removeEventListener('message', this[HANDLER]);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
removeEventListener('message', this[HANDLER]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
[HANDLER] = (event) => {
|
|
40
|
+
this[REQUESTS$].next(event.data);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
exports.WorkerMessageHost = WorkerMessageHost;
|
package/worker/message-host.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/worker/message-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAQ9C,MAAM,SAAS,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AACrC,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAClC,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AAEhC,MAAM,OAAO,iBAAkB,SAAQ,WAAW;IACxC,CAAC,SAAS,CAAC,GAAG,IAAI,OAAO,EAAW,CAAC;IACrC,CAAC,MAAM,CAAC,CAAqB;IAErC,YAAY,MAAe;QACzB,KAAK,EAAE,CAAC;QAER,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;YACtB,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QACpD,CAAC;aAAM,CAAC;YACN,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7C,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IAC/B,CAAC;IAES,QAAQ,CAAC,OAAwB;QACzC,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QACpC,CAAC;aAAM,CAAC;YACL,WAAmB,CAAC,OAAO,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IAEM,SAAS;QACd,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,CAAC,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7D,CAAC;aAAM,CAAC;YACN,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IAEO,CAAC,OAAO,CAAC,GAAG,CAAC,KAA4B,EAAE,EAAE;QACnD,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC,CAAA;CACF","file":"message-host.js","sourcesContent":["import { Subject } from \"rxjs\";\nimport { MessageHost } from \"../message-host\";\nimport { MessageResponse } from \"../message-response.type\";\nimport { Message } from \"../message.interface\";\n\ninterface MessageEvent<T> {\n data: T;\n}\n\nconst REQUESTS$ = Symbol('Requests');\nconst HANDLER = Symbol('Handler');\nconst WORKER = Symbol('Worker');\n\nexport class WorkerMessageHost extends MessageHost {\n private [REQUESTS$] = new Subject<Message>();\n private [WORKER]: Worker | undefined;\n\n constructor(worker?: Worker) {\n super();\n\n if (worker) {\n this[WORKER] = worker;\n worker.addEventListener('message', this[HANDLER]);\n } else {\n addEventListener('message', this[HANDLER]);\n }\n\n this.listen(this[REQUESTS$]);\n }\n\n protected response(message: MessageResponse) {\n if (this[WORKER]) {\n this[WORKER].postMessage(message);\n } else {\n (postMessage as any)(message);\n }\n }\n\n public terminate() {\n if (this[WORKER]) {\n this[WORKER].removeEventListener('message', this[HANDLER]);\n } else {\n removeEventListener('message', this[HANDLER]);\n }\n }\n\n private [HANDLER] = (event: MessageEvent<Message>) => {\n this[REQUESTS$].next(event.data);\n }\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkerMessageService = void 0;
|
|
4
|
+
const mixin_1 = require("@thalesrc/js-utils/class/mixin");
|
|
5
|
+
const message_client_1 = require("./message-client");
|
|
6
|
+
const message_host_1 = require("./message-host");
|
|
7
|
+
class WorkerMessageService extends (0, mixin_1.mixin)(message_host_1.WorkerMessageHost, message_client_1.WorkerMessageClient) {
|
|
8
|
+
constructor(worker) {
|
|
9
|
+
super([worker], [worker]);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.WorkerMessageService = WorkerMessageService;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { WorkerMessageClient } from "./message-client";
|
|
2
|
-
declare const WorkerMessageService_base: new (args_0: [worker?: Worker], args_1: [worker?: Worker]) => {
|
|
2
|
+
declare const WorkerMessageService_base: new (args_0: [worker?: Worker | undefined], args_1: [worker?: Worker | undefined]) => {
|
|
3
3
|
terminate: () => void;
|
|
4
4
|
} & WorkerMessageClient;
|
|
5
5
|
export declare class WorkerMessageService extends WorkerMessageService_base {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mixin } from "@thalesrc/js-utils/class/mixin";
|
|
2
2
|
import { WorkerMessageClient } from "./message-client";
|
|
3
3
|
import { WorkerMessageHost } from "./message-host";
|
|
4
|
-
export class WorkerMessageService extends
|
|
4
|
+
export class WorkerMessageService extends mixin(WorkerMessageHost, WorkerMessageClient) {
|
|
5
5
|
constructor(worker) {
|
|
6
6
|
super([worker], [worker]);
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=message-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/worker/message-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnD,MAAM,OAAO,oBAAqB,SAAQ,KAAK,CAAC,iBAAiB,EAAE,mBAAmB,CAAC;IACrF,YAAY,MAAe;QACzB,KAAK,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAC5B,CAAC;CACF","file":"message-service.js","sourcesContent":["import { mixin } from \"@thalesrc/js-utils/class/mixin\";\nimport { WorkerMessageClient } from \"./message-client\";\nimport { WorkerMessageHost } from \"./message-host\";\n\nexport class WorkerMessageService extends mixin(WorkerMessageHost, WorkerMessageClient) {\n constructor(worker?: Worker) {\n super([worker], [worker]);\n }\n}\n"]}
|