@thalesrc/hermes 7.3.0 → 7.5.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 +54 -8
- package/broadcast/message-client.js.map +1 -1
- package/broadcast/message-host.cjs +3 -2
- package/broadcast/message-host.d.ts +2 -1
- package/broadcast/message-host.js +3 -2
- package/broadcast/message-host.js.map +1 -1
- package/chrome/message-client.cjs +1 -1
- package/chrome/message-client.js +1 -1
- package/chrome/message-client.js.map +1 -1
- package/chrome/message-host.cjs +4 -3
- package/chrome/message-host.d.ts +2 -1
- package/chrome/message-host.js +4 -3
- package/chrome/message-host.js.map +1 -1
- package/iframe/message-client.js.map +1 -1
- package/iframe/message-host.cjs +3 -2
- package/iframe/message-host.d.ts +2 -1
- package/iframe/message-host.js +3 -2
- package/iframe/message-host.js.map +1 -1
- package/message-host.cjs +12 -7
- package/message-host.d.ts +4 -3
- package/message-host.js +13 -8
- package/message-host.js.map +1 -1
- package/message-response.type.d.ts +2 -0
- package/message-response.type.js.map +1 -1
- package/message.interface.d.ts +1 -0
- package/message.interface.js.map +1 -1
- package/package.json +1 -1
- package/request.decorator.cjs +2 -2
- package/request.decorator.js +2 -2
- package/request.decorator.js.map +1 -1
- package/selectors.cjs +4 -2
- package/selectors.d.ts +3 -1
- package/selectors.js +3 -1
- package/selectors.js.map +1 -1
- package/worker/initializer.cjs +39 -0
- package/worker/initializer.d.ts +12 -0
- package/worker/initializer.js +38 -0
- package/worker/initializer.js.map +1 -0
- package/worker/message-client.cjs +14 -30
- package/worker/message-client.d.ts +19 -5
- package/worker/message-client.js +14 -30
- package/worker/message-client.js.map +1 -1
- package/worker/message-host.cjs +146 -24
- package/worker/message-host.d.ts +120 -3
- package/worker/message-host.js +146 -24
- package/worker/message-host.js.map +1 -1
- package/worker/message-service.cjs +111 -0
- package/worker/message-service.d.ts +110 -1
- package/worker/message-service.js +111 -0
- package/worker/message-service.js.map +1 -1
package/request.decorator.cjs
CHANGED
|
@@ -8,12 +8,12 @@ function Request(path) {
|
|
|
8
8
|
descriptor.value = function (message) {
|
|
9
9
|
const messageId = this[selectors_1.GET_NEW_ID]();
|
|
10
10
|
this[selectors_1.SEND]({ body: message, id: messageId, path });
|
|
11
|
-
return this[selectors_1.RESPONSES$].pipe((0, operators_1.map)((data) => {
|
|
11
|
+
return this[selectors_1.RESPONSES$].pipe((0, operators_1.filter)(({ id }) => id === messageId), (0, operators_1.map)((data) => {
|
|
12
12
|
const { error } = data;
|
|
13
13
|
if (error)
|
|
14
14
|
throw error;
|
|
15
15
|
return data;
|
|
16
|
-
}), (0, operators_1.
|
|
16
|
+
}), (0, operators_1.takeWhile)((res) => !res.completed), (0, operators_1.map)(({ body }) => body));
|
|
17
17
|
};
|
|
18
18
|
return descriptor;
|
|
19
19
|
};
|
package/request.decorator.js
CHANGED
|
@@ -5,12 +5,12 @@ export function Request(path) {
|
|
|
5
5
|
descriptor.value = function (message) {
|
|
6
6
|
const messageId = this[GET_NEW_ID]();
|
|
7
7
|
this[SEND]({ body: message, id: messageId, path });
|
|
8
|
-
return this[RESPONSES$].pipe(map((data) => {
|
|
8
|
+
return this[RESPONSES$].pipe(filter(({ id }) => id === messageId), map((data) => {
|
|
9
9
|
const { error } = data;
|
|
10
10
|
if (error)
|
|
11
11
|
throw error;
|
|
12
12
|
return data;
|
|
13
|
-
}),
|
|
13
|
+
}), takeWhile((res) => !res.completed), map(({ body }) => body));
|
|
14
14
|
};
|
|
15
15
|
return descriptor;
|
|
16
16
|
};
|
package/request.decorator.js.map
CHANGED
|
@@ -1 +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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AAU,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 filter(({id}) => id === messageId),\n map((data) => {\n const { error } = data as ErrorMessageResponse;\n\n if (error) throw error;\n\n return data as SuccessfulMessageResponse;\n }),\n takeWhile((res) => !res.completed),\n map(({ body }: UncompletedMessageResponse) => body),\n );\n };\n\n return descriptor;\n };\n}\n"]}
|
package/selectors.cjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.RESPONSE = exports.TERMINATE_MESSAGE$ = exports.LISTEN = exports.GET_NEW_ID = exports.SEND = exports.RESPONSES$ = exports.MESSAGE_LISTENERS = void 0;
|
|
4
4
|
exports.MESSAGE_LISTENERS = Symbol('Message Listeners');
|
|
5
|
-
exports.SENDING_SERVICE = Symbol('Sending Service');
|
|
6
5
|
exports.RESPONSES$ = Symbol('Responses');
|
|
7
6
|
exports.SEND = Symbol('Send');
|
|
8
7
|
exports.GET_NEW_ID = Symbol('Get New Id');
|
|
8
|
+
exports.LISTEN = Symbol('Listen');
|
|
9
|
+
exports.TERMINATE_MESSAGE$ = Symbol('Terminate Message');
|
|
10
|
+
exports.RESPONSE = Symbol('Response');
|
package/selectors.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export declare const MESSAGE_LISTENERS: unique symbol;
|
|
2
|
-
export declare const SENDING_SERVICE: unique symbol;
|
|
3
2
|
export declare const RESPONSES$: unique symbol;
|
|
4
3
|
export declare const SEND: unique symbol;
|
|
5
4
|
export declare const GET_NEW_ID: unique symbol;
|
|
5
|
+
export declare const LISTEN: unique symbol;
|
|
6
|
+
export declare const TERMINATE_MESSAGE$: unique symbol;
|
|
7
|
+
export declare const RESPONSE: unique symbol;
|
package/selectors.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export const MESSAGE_LISTENERS = Symbol('Message Listeners');
|
|
2
|
-
export const SENDING_SERVICE = Symbol('Sending Service');
|
|
3
2
|
export const RESPONSES$ = Symbol('Responses');
|
|
4
3
|
export const SEND = Symbol('Send');
|
|
5
4
|
export const GET_NEW_ID = Symbol('Get New Id');
|
|
5
|
+
export const LISTEN = Symbol('Listen');
|
|
6
|
+
export const TERMINATE_MESSAGE$ = Symbol('Terminate Message');
|
|
7
|
+
export const RESPONSE = Symbol('Response');
|
|
6
8
|
|
|
7
9
|
//# sourceMappingURL=selectors.js.map
|
package/selectors.js.map
CHANGED
|
@@ -1 +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,
|
|
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,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;AAC/C,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAC9D,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC","file":"selectors.js","sourcesContent":["export const MESSAGE_LISTENERS = Symbol('Message Listeners');\nexport const RESPONSES$ = Symbol('Responses');\nexport const SEND = Symbol('Send');\nexport const GET_NEW_ID = Symbol('Get New Id');\nexport const LISTEN = Symbol('Listen');\nexport const TERMINATE_MESSAGE$ = Symbol('Terminate Message');\nexport const RESPONSE = Symbol('Response');\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initializer = initializer;
|
|
4
|
+
const noop_1 = require("@thalesrc/js-utils/function/noop");
|
|
5
|
+
const promisify_1 = require("@thalesrc/js-utils/promise/promisify");
|
|
6
|
+
/**
|
|
7
|
+
* Initializes a Web Worker or the worker context for message communication.
|
|
8
|
+
* @param prevWorker The previous worker promise to deinitialize
|
|
9
|
+
* @param worker The new worker argument (Worker instance, promise, or factory function)
|
|
10
|
+
* @param handler The message event handler to attach
|
|
11
|
+
* @returns A promise resolving to the initialized Worker or undefined if in worker context
|
|
12
|
+
*/
|
|
13
|
+
function initializer(prevWorker, worker, handler) {
|
|
14
|
+
// Deinitialize previous worker if any
|
|
15
|
+
prevWorker.then(pw => {
|
|
16
|
+
if (pw) {
|
|
17
|
+
pw.removeEventListener('message', handler);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
removeEventListener('message', handler);
|
|
21
|
+
}
|
|
22
|
+
}).catch(noop_1.noop);
|
|
23
|
+
// Resolve worker parameter: execute function if provided, otherwise use value directly
|
|
24
|
+
const _worker = typeof worker === 'function' ? worker() : worker;
|
|
25
|
+
// Ensure worker is wrapped in a promise for consistent async handling
|
|
26
|
+
const newWorker = (0, promisify_1.promisify)(_worker);
|
|
27
|
+
// Set up message listener once worker is resolved
|
|
28
|
+
newWorker.then(worker => {
|
|
29
|
+
if (worker) {
|
|
30
|
+
// Main thread context: listen to specific worker
|
|
31
|
+
worker.addEventListener('message', handler);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// Worker thread context: listen to messages from main thread
|
|
35
|
+
addEventListener('message', handler);
|
|
36
|
+
}
|
|
37
|
+
}).catch(noop_1.noop);
|
|
38
|
+
return newWorker;
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type ClientWorkerType = Worker | undefined;
|
|
2
|
+
export type ClientWorkerPromise = Promise<ClientWorkerType>;
|
|
3
|
+
export type ClientWorkerFactory = () => ClientWorkerType | ClientWorkerPromise;
|
|
4
|
+
export type ClientWorkerArg = ClientWorkerType | ClientWorkerPromise | ClientWorkerFactory;
|
|
5
|
+
/**
|
|
6
|
+
* Initializes a Web Worker or the worker context for message communication.
|
|
7
|
+
* @param prevWorker The previous worker promise to deinitialize
|
|
8
|
+
* @param worker The new worker argument (Worker instance, promise, or factory function)
|
|
9
|
+
* @param handler The message event handler to attach
|
|
10
|
+
* @returns A promise resolving to the initialized Worker or undefined if in worker context
|
|
11
|
+
*/
|
|
12
|
+
export declare function initializer(prevWorker: ClientWorkerPromise, worker: ClientWorkerArg, handler: (event: MessageEvent) => void): Promise<Worker | undefined>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { noop } from "@thalesrc/js-utils/function/noop";
|
|
2
|
+
import { promisify } from "@thalesrc/js-utils/promise/promisify";
|
|
3
|
+
/**
|
|
4
|
+
* Initializes a Web Worker or the worker context for message communication.
|
|
5
|
+
* @param prevWorker The previous worker promise to deinitialize
|
|
6
|
+
* @param worker The new worker argument (Worker instance, promise, or factory function)
|
|
7
|
+
* @param handler The message event handler to attach
|
|
8
|
+
* @returns A promise resolving to the initialized Worker or undefined if in worker context
|
|
9
|
+
*/
|
|
10
|
+
export function initializer(prevWorker, worker, handler) {
|
|
11
|
+
// Deinitialize previous worker if any
|
|
12
|
+
prevWorker.then(pw => {
|
|
13
|
+
if (pw) {
|
|
14
|
+
pw.removeEventListener('message', handler);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
removeEventListener('message', handler);
|
|
18
|
+
}
|
|
19
|
+
}).catch(noop);
|
|
20
|
+
// Resolve worker parameter: execute function if provided, otherwise use value directly
|
|
21
|
+
const _worker = typeof worker === 'function' ? worker() : worker;
|
|
22
|
+
// Ensure worker is wrapped in a promise for consistent async handling
|
|
23
|
+
const newWorker = promisify(_worker);
|
|
24
|
+
// Set up message listener once worker is resolved
|
|
25
|
+
newWorker.then(worker => {
|
|
26
|
+
if (worker) {
|
|
27
|
+
// Main thread context: listen to specific worker
|
|
28
|
+
worker.addEventListener('message', handler);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// Worker thread context: listen to messages from main thread
|
|
32
|
+
addEventListener('message', handler);
|
|
33
|
+
}
|
|
34
|
+
}).catch(noop);
|
|
35
|
+
return newWorker;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=initializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/worker/initializer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kCAAkC,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAOjE;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CACzB,UAA+B,EAC/B,MAAuB,EACvB,OAAsC;IAEtC,sCAAsC;IACtC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE;QACnB,IAAI,EAAE,EAAE,CAAC;YACP,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC7C,CAAC;aAAM,CAAC;YACN,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC1C,CAAC;IACH,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAEf,uFAAuF;IACvF,MAAM,OAAO,GAAuD,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;IACrH,sEAAsE;IACtE,MAAM,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IAErC,kDAAkD;IAClD,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;QACtB,IAAI,MAAM,EAAE,CAAC;YACX,iDAAiD;YACjD,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC;aAAM,CAAC;YACN,6DAA6D;YAC7D,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACvC,CAAC;IACH,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAEf,OAAO,SAAS,CAAC;AACnB,CAAC","file":"initializer.js","sourcesContent":["import { noop } from \"@thalesrc/js-utils/function/noop\";\nimport { promisify } from \"@thalesrc/js-utils/promise/promisify\";\n\nexport type ClientWorkerType = Worker | undefined;\nexport type ClientWorkerPromise = Promise<ClientWorkerType>;\nexport type ClientWorkerFactory = () => ClientWorkerType | ClientWorkerPromise;\nexport type ClientWorkerArg = ClientWorkerType | ClientWorkerPromise | ClientWorkerFactory;\n\n/**\n * Initializes a Web Worker or the worker context for message communication.\n * @param prevWorker The previous worker promise to deinitialize\n * @param worker The new worker argument (Worker instance, promise, or factory function)\n * @param handler The message event handler to attach\n * @returns A promise resolving to the initialized Worker or undefined if in worker context\n */\nexport function initializer(\n prevWorker: ClientWorkerPromise,\n worker: ClientWorkerArg,\n handler: (event: MessageEvent) => void\n) {\n // Deinitialize previous worker if any\n prevWorker.then(pw => {\n if (pw) {\n pw.removeEventListener('message', handler);\n } else {\n removeEventListener('message', handler);\n }\n }).catch(noop);\n\n // Resolve worker parameter: execute function if provided, otherwise use value directly\n const _worker: ClientWorkerType | ClientWorkerPromise | undefined = typeof worker === 'function' ? worker() : worker;\n // Ensure worker is wrapped in a promise for consistent async handling\n const newWorker = promisify(_worker);\n\n // Set up message listener once worker is resolved\n newWorker.then(worker => {\n if (worker) {\n // Main thread context: listen to specific worker\n worker.addEventListener('message', handler);\n } else {\n // Worker thread context: listen to messages from main thread\n addEventListener('message', handler);\n }\n }).catch(noop);\n\n return newWorker;\n}\n"]}
|
|
@@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WorkerMessageClient = void 0;
|
|
4
4
|
const unique_id_1 = require("@thalesrc/js-utils/unique-id");
|
|
5
5
|
const noop_1 = require("@thalesrc/js-utils/function/noop");
|
|
6
|
-
const promisify_1 = require("@thalesrc/js-utils/promise/promisify");
|
|
7
6
|
const rxjs_1 = require("rxjs");
|
|
8
7
|
const message_client_1 = require("../message-client");
|
|
9
8
|
const selectors_1 = require("../selectors");
|
|
9
|
+
const initializer_1 = require("./initializer");
|
|
10
|
+
const WORKER = Symbol('Client Worker');
|
|
11
|
+
const HANDLER = Symbol('Client Message Handler');
|
|
12
|
+
const INSTANCE_ID = Symbol('Client Instance ID');
|
|
10
13
|
/**
|
|
11
14
|
* WorkerMessageClient
|
|
12
15
|
*
|
|
@@ -57,12 +60,12 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
57
60
|
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
58
61
|
* @private
|
|
59
62
|
*/
|
|
60
|
-
|
|
63
|
+
[WORKER] = Promise.resolve(undefined);
|
|
61
64
|
/**
|
|
62
65
|
* Unique identifier for this client instance to prevent ID collisions
|
|
63
66
|
* @private
|
|
64
67
|
*/
|
|
65
|
-
|
|
68
|
+
[INSTANCE_ID] = '' + Math.floor(Math.random() * 10000);
|
|
66
69
|
/**
|
|
67
70
|
* Observable stream of message responses from the worker or main thread
|
|
68
71
|
*/
|
|
@@ -110,30 +113,11 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
110
113
|
* };
|
|
111
114
|
*/
|
|
112
115
|
initialize(worker) {
|
|
113
|
-
//
|
|
114
|
-
this
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
else {
|
|
119
|
-
removeEventListener('message', this.#handler);
|
|
120
|
-
}
|
|
121
|
-
}).catch(noop_1.noop);
|
|
122
|
-
// Resolve worker parameter: execute function if provided, otherwise use value directly
|
|
123
|
-
const _worker = typeof worker === 'function' ? worker() : worker;
|
|
124
|
-
// Ensure worker is wrapped in a promise for consistent async handling
|
|
125
|
-
this.#worker = (0, promisify_1.promisify)(_worker);
|
|
126
|
-
// Set up message listener once worker is resolved
|
|
127
|
-
this.#worker.then(worker => {
|
|
128
|
-
if (worker) {
|
|
129
|
-
// Main thread context: listen to specific worker
|
|
130
|
-
worker.addEventListener('message', this.#handler);
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
// Worker thread context: listen to messages from main thread
|
|
134
|
-
addEventListener('message', this.#handler);
|
|
135
|
-
}
|
|
136
|
-
}).catch(noop_1.noop);
|
|
116
|
+
// Ensure WORKER is initialized if not already
|
|
117
|
+
if (!this[WORKER]) {
|
|
118
|
+
this[WORKER] = Promise.resolve(undefined);
|
|
119
|
+
}
|
|
120
|
+
this[WORKER] = (0, initializer_1.initializer)(this[WORKER], worker, this[HANDLER]);
|
|
137
121
|
}
|
|
138
122
|
/**
|
|
139
123
|
* Sends a message to the worker or main thread
|
|
@@ -143,7 +127,7 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
143
127
|
* @internal Used by @Request decorator
|
|
144
128
|
*/
|
|
145
129
|
[selectors_1.SEND](message) {
|
|
146
|
-
this
|
|
130
|
+
this[WORKER].then(worker => {
|
|
147
131
|
if (worker) {
|
|
148
132
|
// Main thread: send to worker
|
|
149
133
|
worker.postMessage(message);
|
|
@@ -158,7 +142,7 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
158
142
|
* Handles incoming messages and forwards them to the responses stream
|
|
159
143
|
* @private
|
|
160
144
|
*/
|
|
161
|
-
|
|
145
|
+
[HANDLER] = (event) => {
|
|
162
146
|
this[selectors_1.RESPONSES$].next(event.data);
|
|
163
147
|
};
|
|
164
148
|
/**
|
|
@@ -168,7 +152,7 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
168
152
|
* @internal Used by @Request decorator
|
|
169
153
|
*/
|
|
170
154
|
[selectors_1.GET_NEW_ID]() {
|
|
171
|
-
return (0, unique_id_1.uniqueId)('hermes-worker-message-' + this
|
|
155
|
+
return (0, unique_id_1.uniqueId)('hermes-worker-message-' + this[INSTANCE_ID]);
|
|
172
156
|
}
|
|
173
157
|
}
|
|
174
158
|
exports.WorkerMessageClient = WorkerMessageClient;
|
|
@@ -3,10 +3,10 @@ import { MessageClient } from "../message-client";
|
|
|
3
3
|
import { MessageResponse } from "../message-response.type";
|
|
4
4
|
import { Message } from "../message.interface";
|
|
5
5
|
import { GET_NEW_ID, RESPONSES$, SEND } from "../selectors";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
import { ClientWorkerArg } from "./initializer";
|
|
7
|
+
declare const WORKER: unique symbol;
|
|
8
|
+
declare const HANDLER: unique symbol;
|
|
9
|
+
declare const INSTANCE_ID: unique symbol;
|
|
10
10
|
/**
|
|
11
11
|
* WorkerMessageClient
|
|
12
12
|
*
|
|
@@ -53,7 +53,16 @@ type ClientWorkerArg = ClientWorkerType | ClientWorkerPromise | ClientWorkerFact
|
|
|
53
53
|
* client.initialize(new Worker('./different-worker.js'));
|
|
54
54
|
*/
|
|
55
55
|
export declare class WorkerMessageClient extends MessageClient {
|
|
56
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
58
|
+
* @private
|
|
59
|
+
*/
|
|
60
|
+
private [WORKER];
|
|
61
|
+
/**
|
|
62
|
+
* Unique identifier for this client instance to prevent ID collisions
|
|
63
|
+
* @private
|
|
64
|
+
*/
|
|
65
|
+
private [INSTANCE_ID];
|
|
57
66
|
/**
|
|
58
67
|
* Observable stream of message responses from the worker or main thread
|
|
59
68
|
*/
|
|
@@ -106,6 +115,11 @@ export declare class WorkerMessageClient extends MessageClient {
|
|
|
106
115
|
* @internal Used by @Request decorator
|
|
107
116
|
*/
|
|
108
117
|
[SEND]<T>(message: Message<T>): void;
|
|
118
|
+
/**
|
|
119
|
+
* Handles incoming messages and forwards them to the responses stream
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
private [HANDLER];
|
|
109
123
|
/**
|
|
110
124
|
* Generates a unique message ID for tracking request-response pairs
|
|
111
125
|
*
|
package/worker/message-client.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { uniqueId } from "@thalesrc/js-utils/unique-id";
|
|
2
2
|
import { noop } from "@thalesrc/js-utils/function/noop";
|
|
3
|
-
import { promisify } from '@thalesrc/js-utils/promise/promisify';
|
|
4
3
|
import { Subject } from "rxjs";
|
|
5
4
|
import { MessageClient } from "../message-client";
|
|
6
5
|
import { GET_NEW_ID, RESPONSES$, SEND } from "../selectors";
|
|
6
|
+
import { initializer } from "./initializer";
|
|
7
|
+
const WORKER = Symbol('Client Worker');
|
|
8
|
+
const HANDLER = Symbol('Client Message Handler');
|
|
9
|
+
const INSTANCE_ID = Symbol('Client Instance ID');
|
|
7
10
|
/**
|
|
8
11
|
* WorkerMessageClient
|
|
9
12
|
*
|
|
@@ -54,12 +57,12 @@ export class WorkerMessageClient extends MessageClient {
|
|
|
54
57
|
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
55
58
|
* @private
|
|
56
59
|
*/
|
|
57
|
-
|
|
60
|
+
[WORKER] = Promise.resolve(undefined);
|
|
58
61
|
/**
|
|
59
62
|
* Unique identifier for this client instance to prevent ID collisions
|
|
60
63
|
* @private
|
|
61
64
|
*/
|
|
62
|
-
|
|
65
|
+
[INSTANCE_ID] = '' + Math.floor(Math.random() * 10000);
|
|
63
66
|
/**
|
|
64
67
|
* Observable stream of message responses from the worker or main thread
|
|
65
68
|
*/
|
|
@@ -107,30 +110,11 @@ export class WorkerMessageClient extends MessageClient {
|
|
|
107
110
|
* };
|
|
108
111
|
*/
|
|
109
112
|
initialize(worker) {
|
|
110
|
-
//
|
|
111
|
-
this
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
else {
|
|
116
|
-
removeEventListener('message', this.#handler);
|
|
117
|
-
}
|
|
118
|
-
}).catch(noop);
|
|
119
|
-
// Resolve worker parameter: execute function if provided, otherwise use value directly
|
|
120
|
-
const _worker = typeof worker === 'function' ? worker() : worker;
|
|
121
|
-
// Ensure worker is wrapped in a promise for consistent async handling
|
|
122
|
-
this.#worker = promisify(_worker);
|
|
123
|
-
// Set up message listener once worker is resolved
|
|
124
|
-
this.#worker.then(worker => {
|
|
125
|
-
if (worker) {
|
|
126
|
-
// Main thread context: listen to specific worker
|
|
127
|
-
worker.addEventListener('message', this.#handler);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
// Worker thread context: listen to messages from main thread
|
|
131
|
-
addEventListener('message', this.#handler);
|
|
132
|
-
}
|
|
133
|
-
}).catch(noop);
|
|
113
|
+
// Ensure WORKER is initialized if not already
|
|
114
|
+
if (!this[WORKER]) {
|
|
115
|
+
this[WORKER] = Promise.resolve(undefined);
|
|
116
|
+
}
|
|
117
|
+
this[WORKER] = initializer(this[WORKER], worker, this[HANDLER]);
|
|
134
118
|
}
|
|
135
119
|
/**
|
|
136
120
|
* Sends a message to the worker or main thread
|
|
@@ -140,7 +124,7 @@ export class WorkerMessageClient extends MessageClient {
|
|
|
140
124
|
* @internal Used by @Request decorator
|
|
141
125
|
*/
|
|
142
126
|
[SEND](message) {
|
|
143
|
-
this
|
|
127
|
+
this[WORKER].then(worker => {
|
|
144
128
|
if (worker) {
|
|
145
129
|
// Main thread: send to worker
|
|
146
130
|
worker.postMessage(message);
|
|
@@ -155,7 +139,7 @@ export class WorkerMessageClient extends MessageClient {
|
|
|
155
139
|
* Handles incoming messages and forwards them to the responses stream
|
|
156
140
|
* @private
|
|
157
141
|
*/
|
|
158
|
-
|
|
142
|
+
[HANDLER] = (event) => {
|
|
159
143
|
this[RESPONSES$].next(event.data);
|
|
160
144
|
};
|
|
161
145
|
/**
|
|
@@ -165,7 +149,7 @@ export class WorkerMessageClient extends MessageClient {
|
|
|
165
149
|
* @internal Used by @Request decorator
|
|
166
150
|
*/
|
|
167
151
|
[GET_NEW_ID]() {
|
|
168
|
-
return uniqueId('hermes-worker-message-' + this
|
|
152
|
+
return uniqueId('hermes-worker-message-' + this[INSTANCE_ID]);
|
|
169
153
|
}
|
|
170
154
|
}
|
|
171
155
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/hermes/src/worker/message-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,kCAAkC,CAAC;AACxD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/hermes/src/worker/message-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,kCAAkC,CAAC;AACxD,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;AAC5D,OAAO,EAAwC,WAAW,EAAE,MAAM,eAAe,CAAC;AAElF,MAAM,MAAM,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AACvC,MAAM,OAAO,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AACjD,MAAM,WAAW,GAAG,MAAM,CAAC,oBAA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AAI,CAAC,CAAC;IACjB,CAAC;IAED;;;OAGG;IACK,CAAC,OAAO,CAAC,GAAG,CAAC,KAAoC,EAAE,EAAE;QAC3D,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC,CAAA;IAED;;;;;OAKG;IACO,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/unique-id\";\nimport { noop } from \"@thalesrc/js-utils/function/noop\";\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\";\nimport { ClientWorkerArg, ClientWorkerPromise, initializer } from \"./initializer\";\n\nconst WORKER = Symbol('Client Worker');\nconst HANDLER = Symbol('Client Message Handler');\nconst INSTANCE_ID = Symbol('Client Instance ID');\n\n/**\n * WorkerMessageClient\n *\n * Client implementation for Web Worker communication. Sends messages to and receives\n * responses from Web Workers using the Worker postMessage API.\n *\n * This class can be used in two contexts:\n * - **Main thread**: Provide a Worker instance to communicate with that specific worker\n * - **Worker thread**: Omit the worker parameter to communicate with the main thread via self\n *\n * The worker parameter supports multiple types for flexibility:\n * - Direct Worker instance\n * - Promise that resolves to a Worker (for async worker initialization)\n * - Function that returns a Worker (for lazy initialization)\n * - Function that returns a Promise<Worker> (for async lazy initialization)\n *\n * The `initialize()` method allows dynamic worker management, enabling you to switch\n * workers at runtime or re-establish connections after errors.\n *\n * @example\n * // In main thread - communicate with a specific worker\n * const worker = new Worker('./worker.js');\n * const client = new WorkerMessageClient(worker);\n *\n * @example\n * // In worker thread - communicate with main thread\n * const client = new WorkerMessageClient();\n *\n * @example\n * // With async worker initialization\n * const workerPromise = import('./worker.js').then(m => new m.MyWorker());\n * const client = new WorkerMessageClient(workerPromise);\n *\n * @example\n * // With lazy initialization\n * const client = new WorkerMessageClient(() =>\n * document.querySelector('[data-worker]') ? new Worker('./worker.js') : undefined\n * );\n *\n * @example\n * // Re-initialize with a different worker\n * const client = new WorkerMessageClient();\n * // Later, switch to a different worker\n * client.initialize(new Worker('./different-worker.js'));\n */\nexport class WorkerMessageClient extends MessageClient {\n /**\n * Promise resolving to the Worker instance or undefined if running in worker context\n * @private\n */\n private [WORKER]: ClientWorkerPromise = Promise.resolve(undefined);\n\n /**\n * Unique identifier for this client instance to prevent ID collisions\n * @private\n */\n private [INSTANCE_ID] = '' + Math.floor(Math.random() * 10000);\n\n /**\n * Observable stream of message responses from the worker or main thread\n */\n public [RESPONSES$] = new Subject<MessageResponse>();\n\n /**\n * Creates a new WorkerMessageClient instance\n *\n * @param worker - Optional worker configuration:\n * - Worker: Direct worker instance (main thread)\n * - Promise<Worker>: Promise resolving to worker (async initialization)\n * - () => Worker: Function returning worker (lazy initialization)\n * - () => Promise<Worker>: Function returning promise (async lazy initialization)\n * - undefined: Omit for worker thread context (uses self)\n */\n constructor(worker?: ClientWorkerArg) {\n super();\n\n this.initialize(worker);\n }\n\n /**\n * Initializes or re-initializes the worker connection.\n *\n * This method sets up message listeners for the provided worker. If a worker\n * was previously initialized, it cleans up the old connection before establishing\n * the new one. This allows for dynamic worker management, such as:\n * - Switching between different workers at runtime\n * - Re-establishing connections after worker errors\n * - Lazy initialization when the worker is conditionally needed\n *\n * @param worker - Optional worker configuration:\n * - Worker: Direct worker instance (main thread)\n * - Promise<Worker>: Promise resolving to worker (async initialization)\n * - () => Worker: Function returning worker (lazy initialization)\n * - () => Promise<Worker>: Function returning promise (async lazy initialization)\n * - undefined: Omit for worker thread context (uses self)\n *\n * @example\n * // Switch to a different worker dynamically\n * const client = new WorkerMessageClient(oldWorker);\n * client.initialize(newWorker); // Cleans up old listener, sets up new one\n *\n * @example\n * // Re-initialize after worker error\n * worker.onerror = () => {\n * client.initialize(new Worker('./worker.js'));\n * };\n */\n initialize(worker?: ClientWorkerArg) {\n // Ensure WORKER is initialized if not already\n if (!this[WORKER]) {\n this[WORKER] = Promise.resolve(undefined);\n }\n\n this[WORKER] = initializer(\n this[WORKER],\n worker,\n this[HANDLER]\n );\n }\n\n /**\n * Sends a message to the worker or main thread\n *\n * @param message - The message to send\n * @template T - Type of the message body\n * @internal Used by @Request decorator\n */\n public [SEND]<T>(message: Message<T>) {\n this[WORKER].then(worker => {\n if (worker) {\n // Main thread: send to worker\n worker.postMessage(message);\n } else {\n // Worker thread: send to main thread\n (postMessage as any)(message);\n }\n }).catch(noop);\n }\n\n /**\n * Handles incoming messages and forwards them to the responses stream\n * @private\n */\n private [HANDLER] = (event: MessageEvent<MessageResponse>) => {\n this[RESPONSES$].next(event.data);\n }\n\n /**\n * Generates a unique message ID for tracking request-response pairs\n *\n * @returns Unique message identifier\n * @internal Used by @Request decorator\n */\n protected [GET_NEW_ID](): string {\n return uniqueId('hermes-worker-message-' + this[INSTANCE_ID]) as string;\n }\n}\n"]}
|