@thalesrc/hermes 7.4.7 → 7.5.2
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.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/iframe/message-client.js.map +1 -1
- package/iframe/message-host.js.map +1 -1
- package/index.d.ts +2 -2
- package/index.js.map +1 -1
- package/message-host.js.map +1 -1
- package/message-response.type.d.ts +2 -0
- package/message-response.type.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/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 +147 -23
- package/worker/message-host.d.ts +123 -1
- package/worker/message-host.js +147 -23
- 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
|
@@ -1 +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;AAG9C,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;
|
|
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;AAG9C,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAwC,WAAW,EAAE,MAAM,eAAe,CAAC;AAClF,OAAO,EAAE,IAAI,EAAE,MAAM,kCAAkC,CAAC;AAExD,MAAM,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AACrC,MAAM,OAAO,GAAG,MAAM,CAAC,sBAAsB,CAAC,CAAC;AAC/C,MAAM,SAAS,GAAG,MAAM,CAAC,sBAAsB,CAAC,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,OAAO,iBAAkB,SAAQ,WAAW;IAChD;;;OAGG;IACK,CAAC,SAAS,CAAC,GAAG,IAAI,OAAO,EAAW,CAAC;IAE7C;;;OAGG;IACK,CAAC,MAAM,CAAC,GAAwB,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAEnE;;;;;;;;;OASG;IACH,YAAY,MAAe;QACzB,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACxB,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IAChC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,UAAU,CAAC,MAAuB;QAChC,8CAA8C;QAC9C,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YAClB,IAAI,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAC5C,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,GAAG,WAAW,CACxB,IAAI,CAAC,MAAM,CAAC,EACZ,MAAM,EACN,IAAI,CAAC,OAAO,CAAC,CACd,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACO,CAAC,QAAQ,CAAC,CAAC,OAAwB;QAC3C,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;YACzB,IAAI,MAAM,EAAE,CAAC;gBACX,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;YAC9B,CAAC;iBAAM,CAAC;gBACL,WAAmB,CAAC,OAAO,CAAC,CAAC;YAChC,CAAC;QACH,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;IAED;;;;;;;;;;;;OAYG;IACI,SAAS;QACd,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;YACzB,IAAI,MAAM,EAAE,CAAC;gBACX,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;YACvD,CAAC;iBAAM,CAAC;gBACN,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;YAChD,CAAC;QACH,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;IAED;;;OAGG;IACK,CAAC,OAAO,CAAC,GAAG,CAAC,KAA4B,EAAE,EAAE;QACnD,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC,CAAC;CACH","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\";\nimport { LISTEN, RESPONSE } from \"../selectors\";\nimport { ClientWorkerArg, ClientWorkerPromise, initializer } from \"./initializer\";\nimport { noop } from \"@thalesrc/js-utils/function/noop\";\n\nconst WORKER = Symbol('Host Worker');\nconst HANDLER = Symbol('Host Message Handler');\nconst REQUESTS$ = Symbol('Host Requests Stream');\n\n/**\n * WorkerMessageHost\n *\n * Host implementation for Web Worker communication. Receives messages from and sends\n * responses to 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 receive messages from that specific worker\n * - **Worker thread**: Omit the worker parameter to receive messages from 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 - receive messages from a specific worker\n * const worker = new Worker('./worker.js');\n * const host = new WorkerMessageHost(worker);\n *\n * @example\n * // In worker thread - receive messages from main thread\n * const host = new WorkerMessageHost();\n *\n * @example\n * // With async worker initialization\n * const workerPromise = import('./worker.js').then(m => new m.MyWorker());\n * const host = new WorkerMessageHost(workerPromise);\n *\n * @example\n * // With lazy initialization\n * const host = new WorkerMessageHost(() =>\n * document.querySelector('[data-worker]') ? new Worker('./worker.js') : undefined\n * );\n *\n * @example\n * // Re-initialize with a different worker\n * const host = new WorkerMessageHost();\n * // Later, switch to a different worker\n * host.initialize(new Worker('./different-worker.js'));\n */\nexport class WorkerMessageHost extends MessageHost {\n /**\n * Observable stream of incoming messages from the worker or main thread\n * @private\n */\n private [REQUESTS$] = new Subject<Message>();\n\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 * Creates a new WorkerMessageHost 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?: Worker) {\n super();\n\n this.initialize(worker);\n this[LISTEN](this[REQUESTS$]);\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 host = new WorkerMessageHost(oldWorker);\n * host.initialize(newWorker); // Cleans up old listener, sets up new one\n *\n * @example\n * // Re-initialize after worker error\n * worker.onerror = () => {\n * host.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 response message back to the worker or main thread\n *\n * @param message - The response message to send\n * @internal Used by the message handling system to send responses\n */\n protected [RESPONSE](message: MessageResponse) {\n this[WORKER].then(worker => {\n if (worker) {\n worker.postMessage(message);\n } else {\n (postMessage as any)(message);\n }\n }).catch(noop);\n }\n\n /**\n * Terminates the worker connection and cleans up message listeners.\n *\n * This method should be called when you're done with the host to prevent memory leaks.\n * It removes the message event listener from either the Worker instance (main thread)\n * or the global scope (worker thread).\n *\n * @example\n * // Clean up when done\n * const host = new WorkerMessageHost(worker);\n * // ... use the host ...\n * host.terminate(); // Clean up listeners\n */\n public terminate() {\n this[WORKER].then(worker => {\n if (worker) {\n worker.removeEventListener('message', this[HANDLER]);\n } else {\n removeEventListener('message', this[HANDLER]);\n }\n }).catch(noop);\n }\n\n /**\n * Handles incoming messages and forwards them to the requests stream\n * @private\n */\n private [HANDLER] = (event: MessageEvent<Message>) => {\n this[REQUESTS$].next(event.data);\n };\n}\n"]}
|
|
@@ -4,9 +4,120 @@ exports.WorkerMessageService = void 0;
|
|
|
4
4
|
const mixin_1 = require("@thalesrc/js-utils/class/mixin");
|
|
5
5
|
const message_client_1 = require("./message-client");
|
|
6
6
|
const message_host_1 = require("./message-host");
|
|
7
|
+
/**
|
|
8
|
+
* WorkerMessageService
|
|
9
|
+
*
|
|
10
|
+
* Bidirectional communication service for Web Workers that combines both client and host
|
|
11
|
+
* capabilities. This class extends both WorkerMessageClient and WorkerMessageHost through
|
|
12
|
+
* mixin composition, enabling full duplex communication - it can both send messages and
|
|
13
|
+
* respond to incoming requests simultaneously.
|
|
14
|
+
*
|
|
15
|
+
* This class is useful when you need a single instance to handle both:
|
|
16
|
+
* - **Sending messages**: Using the @Request decorator to call methods on the other side
|
|
17
|
+
* - **Receiving messages**: Using the @Response decorator to handle incoming requests
|
|
18
|
+
*
|
|
19
|
+
* Like its parent classes, this can be used in two contexts:
|
|
20
|
+
* - **Main thread**: Provide a Worker instance for bidirectional communication with that worker
|
|
21
|
+
* - **Worker thread**: Omit the worker parameter for bidirectional communication with main thread
|
|
22
|
+
*
|
|
23
|
+
* The worker parameter supports multiple types for flexibility:
|
|
24
|
+
* - Direct Worker instance
|
|
25
|
+
* - Promise that resolves to a Worker (for async worker initialization)
|
|
26
|
+
* - Function that returns a Worker (for lazy initialization)
|
|
27
|
+
* - Function that returns a Promise<Worker> (for async lazy initialization)
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // In main thread - full duplex communication with worker
|
|
31
|
+
* const worker = new Worker('./worker.js');
|
|
32
|
+
* const service = new WorkerMessageService(worker);
|
|
33
|
+
*
|
|
34
|
+
* class MainAPI extends WorkerMessageService {
|
|
35
|
+
* // Send messages to worker
|
|
36
|
+
* @Request()
|
|
37
|
+
* fetchData(): Observable<Data> {
|
|
38
|
+
* return null!;
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* // Respond to worker requests
|
|
42
|
+
* @Listen()
|
|
43
|
+
* saveToLocalStorage(data: any) {
|
|
44
|
+
* localStorage.setItem('data', JSON.stringify(data));
|
|
45
|
+
* return [];
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // In worker thread - full duplex communication with main thread
|
|
51
|
+
* const service = new WorkerMessageService();
|
|
52
|
+
*
|
|
53
|
+
* class WorkerAPI extends WorkerMessageService {
|
|
54
|
+
* // Respond to main thread requests
|
|
55
|
+
* @Listen()
|
|
56
|
+
* fetchData() {
|
|
57
|
+
* return from(fetch('/api/data').then(r => r.json()));
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* // Send messages to main thread
|
|
61
|
+
* @Request()
|
|
62
|
+
* saveToLocalStorage(data: any): Observable<void> {
|
|
63
|
+
* return null!;
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // With async worker initialization
|
|
69
|
+
* const workerPromise = import('./worker.js').then(m => new m.MyWorker());
|
|
70
|
+
* const service = new WorkerMessageService(workerPromise);
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // Re-initialize with a different worker
|
|
74
|
+
* const service = new WorkerMessageService();
|
|
75
|
+
* // Later, switch to a different worker
|
|
76
|
+
* service.initialize(new Worker('./different-worker.js'));
|
|
77
|
+
*/
|
|
7
78
|
class WorkerMessageService extends (0, mixin_1.mixin)(message_host_1.WorkerMessageHost, message_client_1.WorkerMessageClient) {
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new WorkerMessageService instance with bidirectional communication
|
|
81
|
+
*
|
|
82
|
+
* @param worker - Optional worker configuration:
|
|
83
|
+
* - Worker: Direct worker instance (main thread)
|
|
84
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
85
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
86
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
87
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
88
|
+
*/
|
|
8
89
|
constructor(worker) {
|
|
9
90
|
super([worker], [worker]);
|
|
91
|
+
this.initialize(worker);
|
|
10
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Initializes or re-initializes the worker connection for both client and host.
|
|
95
|
+
*
|
|
96
|
+
* This method sets up message listeners for both sending and receiving messages.
|
|
97
|
+
* If a worker was previously initialized, it cleans up the old connections before
|
|
98
|
+
* establishing new ones. This allows for dynamic worker management.
|
|
99
|
+
*
|
|
100
|
+
* @param worker - Optional worker configuration:
|
|
101
|
+
* - Worker: Direct worker instance (main thread)
|
|
102
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
103
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
104
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
105
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* // Switch to a different worker dynamically
|
|
109
|
+
* const service = new WorkerMessageService(oldWorker);
|
|
110
|
+
* service.initialize(newWorker); // Cleans up old listeners, sets up new ones
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* // Re-initialize after worker error
|
|
114
|
+
* worker.onerror = () => {
|
|
115
|
+
* service.initialize(new Worker('./worker.js'));
|
|
116
|
+
* };
|
|
117
|
+
*/
|
|
118
|
+
initialize = (worker) => {
|
|
119
|
+
message_host_1.WorkerMessageHost.prototype.initialize.call(this, worker);
|
|
120
|
+
message_client_1.WorkerMessageClient.prototype.initialize.call(this, worker);
|
|
121
|
+
};
|
|
11
122
|
}
|
|
12
123
|
exports.WorkerMessageService = WorkerMessageService;
|
|
@@ -1,8 +1,117 @@
|
|
|
1
1
|
import { WorkerMessageClient } from "./message-client";
|
|
2
|
-
|
|
2
|
+
import { ClientWorkerArg } from "./initializer";
|
|
3
|
+
declare const WorkerMessageService_base: new (args_0: [worker?: Worker | undefined], args_1: [worker?: ClientWorkerArg]) => {
|
|
4
|
+
initialize: (worker?: ClientWorkerArg) => void;
|
|
3
5
|
terminate: () => void;
|
|
4
6
|
} & WorkerMessageClient;
|
|
7
|
+
/**
|
|
8
|
+
* WorkerMessageService
|
|
9
|
+
*
|
|
10
|
+
* Bidirectional communication service for Web Workers that combines both client and host
|
|
11
|
+
* capabilities. This class extends both WorkerMessageClient and WorkerMessageHost through
|
|
12
|
+
* mixin composition, enabling full duplex communication - it can both send messages and
|
|
13
|
+
* respond to incoming requests simultaneously.
|
|
14
|
+
*
|
|
15
|
+
* This class is useful when you need a single instance to handle both:
|
|
16
|
+
* - **Sending messages**: Using the @Request decorator to call methods on the other side
|
|
17
|
+
* - **Receiving messages**: Using the @Response decorator to handle incoming requests
|
|
18
|
+
*
|
|
19
|
+
* Like its parent classes, this can be used in two contexts:
|
|
20
|
+
* - **Main thread**: Provide a Worker instance for bidirectional communication with that worker
|
|
21
|
+
* - **Worker thread**: Omit the worker parameter for bidirectional communication with main thread
|
|
22
|
+
*
|
|
23
|
+
* The worker parameter supports multiple types for flexibility:
|
|
24
|
+
* - Direct Worker instance
|
|
25
|
+
* - Promise that resolves to a Worker (for async worker initialization)
|
|
26
|
+
* - Function that returns a Worker (for lazy initialization)
|
|
27
|
+
* - Function that returns a Promise<Worker> (for async lazy initialization)
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // In main thread - full duplex communication with worker
|
|
31
|
+
* const worker = new Worker('./worker.js');
|
|
32
|
+
* const service = new WorkerMessageService(worker);
|
|
33
|
+
*
|
|
34
|
+
* class MainAPI extends WorkerMessageService {
|
|
35
|
+
* // Send messages to worker
|
|
36
|
+
* @Request()
|
|
37
|
+
* fetchData(): Observable<Data> {
|
|
38
|
+
* return null!;
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* // Respond to worker requests
|
|
42
|
+
* @Listen()
|
|
43
|
+
* saveToLocalStorage(data: any) {
|
|
44
|
+
* localStorage.setItem('data', JSON.stringify(data));
|
|
45
|
+
* return [];
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // In worker thread - full duplex communication with main thread
|
|
51
|
+
* const service = new WorkerMessageService();
|
|
52
|
+
*
|
|
53
|
+
* class WorkerAPI extends WorkerMessageService {
|
|
54
|
+
* // Respond to main thread requests
|
|
55
|
+
* @Listen()
|
|
56
|
+
* fetchData() {
|
|
57
|
+
* return from(fetch('/api/data').then(r => r.json()));
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* // Send messages to main thread
|
|
61
|
+
* @Request()
|
|
62
|
+
* saveToLocalStorage(data: any): Observable<void> {
|
|
63
|
+
* return null!;
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // With async worker initialization
|
|
69
|
+
* const workerPromise = import('./worker.js').then(m => new m.MyWorker());
|
|
70
|
+
* const service = new WorkerMessageService(workerPromise);
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // Re-initialize with a different worker
|
|
74
|
+
* const service = new WorkerMessageService();
|
|
75
|
+
* // Later, switch to a different worker
|
|
76
|
+
* service.initialize(new Worker('./different-worker.js'));
|
|
77
|
+
*/
|
|
5
78
|
export declare class WorkerMessageService extends WorkerMessageService_base {
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new WorkerMessageService instance with bidirectional communication
|
|
81
|
+
*
|
|
82
|
+
* @param worker - Optional worker configuration:
|
|
83
|
+
* - Worker: Direct worker instance (main thread)
|
|
84
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
85
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
86
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
87
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
88
|
+
*/
|
|
6
89
|
constructor(worker?: Worker);
|
|
90
|
+
/**
|
|
91
|
+
* Initializes or re-initializes the worker connection for both client and host.
|
|
92
|
+
*
|
|
93
|
+
* This method sets up message listeners for both sending and receiving messages.
|
|
94
|
+
* If a worker was previously initialized, it cleans up the old connections before
|
|
95
|
+
* establishing new ones. This allows for dynamic worker management.
|
|
96
|
+
*
|
|
97
|
+
* @param worker - Optional worker configuration:
|
|
98
|
+
* - Worker: Direct worker instance (main thread)
|
|
99
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
100
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
101
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
102
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Switch to a different worker dynamically
|
|
106
|
+
* const service = new WorkerMessageService(oldWorker);
|
|
107
|
+
* service.initialize(newWorker); // Cleans up old listeners, sets up new ones
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* // Re-initialize after worker error
|
|
111
|
+
* worker.onerror = () => {
|
|
112
|
+
* service.initialize(new Worker('./worker.js'));
|
|
113
|
+
* };
|
|
114
|
+
*/
|
|
115
|
+
initialize: (worker: ClientWorkerArg) => void;
|
|
7
116
|
}
|
|
8
117
|
export {};
|
|
@@ -1,10 +1,121 @@
|
|
|
1
1
|
import { mixin } from "@thalesrc/js-utils/class/mixin";
|
|
2
2
|
import { WorkerMessageClient } from "./message-client";
|
|
3
3
|
import { WorkerMessageHost } from "./message-host";
|
|
4
|
+
/**
|
|
5
|
+
* WorkerMessageService
|
|
6
|
+
*
|
|
7
|
+
* Bidirectional communication service for Web Workers that combines both client and host
|
|
8
|
+
* capabilities. This class extends both WorkerMessageClient and WorkerMessageHost through
|
|
9
|
+
* mixin composition, enabling full duplex communication - it can both send messages and
|
|
10
|
+
* respond to incoming requests simultaneously.
|
|
11
|
+
*
|
|
12
|
+
* This class is useful when you need a single instance to handle both:
|
|
13
|
+
* - **Sending messages**: Using the @Request decorator to call methods on the other side
|
|
14
|
+
* - **Receiving messages**: Using the @Response decorator to handle incoming requests
|
|
15
|
+
*
|
|
16
|
+
* Like its parent classes, this can be used in two contexts:
|
|
17
|
+
* - **Main thread**: Provide a Worker instance for bidirectional communication with that worker
|
|
18
|
+
* - **Worker thread**: Omit the worker parameter for bidirectional communication with main thread
|
|
19
|
+
*
|
|
20
|
+
* The worker parameter supports multiple types for flexibility:
|
|
21
|
+
* - Direct Worker instance
|
|
22
|
+
* - Promise that resolves to a Worker (for async worker initialization)
|
|
23
|
+
* - Function that returns a Worker (for lazy initialization)
|
|
24
|
+
* - Function that returns a Promise<Worker> (for async lazy initialization)
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // In main thread - full duplex communication with worker
|
|
28
|
+
* const worker = new Worker('./worker.js');
|
|
29
|
+
* const service = new WorkerMessageService(worker);
|
|
30
|
+
*
|
|
31
|
+
* class MainAPI extends WorkerMessageService {
|
|
32
|
+
* // Send messages to worker
|
|
33
|
+
* @Request()
|
|
34
|
+
* fetchData(): Observable<Data> {
|
|
35
|
+
* return null!;
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* // Respond to worker requests
|
|
39
|
+
* @Listen()
|
|
40
|
+
* saveToLocalStorage(data: any) {
|
|
41
|
+
* localStorage.setItem('data', JSON.stringify(data));
|
|
42
|
+
* return [];
|
|
43
|
+
* }
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // In worker thread - full duplex communication with main thread
|
|
48
|
+
* const service = new WorkerMessageService();
|
|
49
|
+
*
|
|
50
|
+
* class WorkerAPI extends WorkerMessageService {
|
|
51
|
+
* // Respond to main thread requests
|
|
52
|
+
* @Listen()
|
|
53
|
+
* fetchData() {
|
|
54
|
+
* return from(fetch('/api/data').then(r => r.json()));
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* // Send messages to main thread
|
|
58
|
+
* @Request()
|
|
59
|
+
* saveToLocalStorage(data: any): Observable<void> {
|
|
60
|
+
* return null!;
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // With async worker initialization
|
|
66
|
+
* const workerPromise = import('./worker.js').then(m => new m.MyWorker());
|
|
67
|
+
* const service = new WorkerMessageService(workerPromise);
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* // Re-initialize with a different worker
|
|
71
|
+
* const service = new WorkerMessageService();
|
|
72
|
+
* // Later, switch to a different worker
|
|
73
|
+
* service.initialize(new Worker('./different-worker.js'));
|
|
74
|
+
*/
|
|
4
75
|
export class WorkerMessageService extends mixin(WorkerMessageHost, WorkerMessageClient) {
|
|
76
|
+
/**
|
|
77
|
+
* Creates a new WorkerMessageService instance with bidirectional communication
|
|
78
|
+
*
|
|
79
|
+
* @param worker - Optional worker configuration:
|
|
80
|
+
* - Worker: Direct worker instance (main thread)
|
|
81
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
82
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
83
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
84
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
85
|
+
*/
|
|
5
86
|
constructor(worker) {
|
|
6
87
|
super([worker], [worker]);
|
|
88
|
+
this.initialize(worker);
|
|
7
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Initializes or re-initializes the worker connection for both client and host.
|
|
92
|
+
*
|
|
93
|
+
* This method sets up message listeners for both sending and receiving messages.
|
|
94
|
+
* If a worker was previously initialized, it cleans up the old connections before
|
|
95
|
+
* establishing new ones. This allows for dynamic worker management.
|
|
96
|
+
*
|
|
97
|
+
* @param worker - Optional worker configuration:
|
|
98
|
+
* - Worker: Direct worker instance (main thread)
|
|
99
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
100
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
101
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
102
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Switch to a different worker dynamically
|
|
106
|
+
* const service = new WorkerMessageService(oldWorker);
|
|
107
|
+
* service.initialize(newWorker); // Cleans up old listeners, sets up new ones
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* // Re-initialize after worker error
|
|
111
|
+
* worker.onerror = () => {
|
|
112
|
+
* service.initialize(new Worker('./worker.js'));
|
|
113
|
+
* };
|
|
114
|
+
*/
|
|
115
|
+
initialize = (worker) => {
|
|
116
|
+
WorkerMessageHost.prototype.initialize.call(this, worker);
|
|
117
|
+
WorkerMessageClient.prototype.initialize.call(this, worker);
|
|
118
|
+
};
|
|
8
119
|
}
|
|
9
120
|
|
|
10
121
|
//# sourceMappingURL=message-service.js.map
|
|
@@ -1 +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;
|
|
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;AAGnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,KAAK,CAAC,iBAAiB,EAAE,mBAAmB,CAAC;IACrF;;;;;;;;;OASG;IACH,YAAY,MAAe;QACzB,KAAK,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;QAE1B,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACa,UAAU,GAAG,CAAC,MAAuB,EAAQ,EAAE;QAC7D,iBAAiB,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC1D,mBAAmB,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC9D,CAAC,CAAC;CACH","file":"message-service.js","sourcesContent":["import { mixin } from \"@thalesrc/js-utils/class/mixin\";\nimport { WorkerMessageClient } from \"./message-client\";\nimport { WorkerMessageHost } from \"./message-host\";\nimport { ClientWorkerArg } from \"./initializer\";\n\n/**\n * WorkerMessageService\n *\n * Bidirectional communication service for Web Workers that combines both client and host\n * capabilities. This class extends both WorkerMessageClient and WorkerMessageHost through\n * mixin composition, enabling full duplex communication - it can both send messages and\n * respond to incoming requests simultaneously.\n *\n * This class is useful when you need a single instance to handle both:\n * - **Sending messages**: Using the @Request decorator to call methods on the other side\n * - **Receiving messages**: Using the @Response decorator to handle incoming requests\n *\n * Like its parent classes, this can be used in two contexts:\n * - **Main thread**: Provide a Worker instance for bidirectional communication with that worker\n * - **Worker thread**: Omit the worker parameter for bidirectional communication with main thread\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 * @example\n * // In main thread - full duplex communication with worker\n * const worker = new Worker('./worker.js');\n * const service = new WorkerMessageService(worker);\n *\n * class MainAPI extends WorkerMessageService {\n * // Send messages to worker\n * @Request()\n * fetchData(): Observable<Data> {\n * return null!;\n * }\n *\n * // Respond to worker requests\n * @Listen()\n * saveToLocalStorage(data: any) {\n * localStorage.setItem('data', JSON.stringify(data));\n * return [];\n * }\n * }\n *\n * @example\n * // In worker thread - full duplex communication with main thread\n * const service = new WorkerMessageService();\n *\n * class WorkerAPI extends WorkerMessageService {\n * // Respond to main thread requests\n * @Listen()\n * fetchData() {\n * return from(fetch('/api/data').then(r => r.json()));\n * }\n *\n * // Send messages to main thread\n * @Request()\n * saveToLocalStorage(data: any): Observable<void> {\n * return null!;\n * }\n * }\n *\n * @example\n * // With async worker initialization\n * const workerPromise = import('./worker.js').then(m => new m.MyWorker());\n * const service = new WorkerMessageService(workerPromise);\n *\n * @example\n * // Re-initialize with a different worker\n * const service = new WorkerMessageService();\n * // Later, switch to a different worker\n * service.initialize(new Worker('./different-worker.js'));\n */\nexport class WorkerMessageService extends mixin(WorkerMessageHost, WorkerMessageClient) {\n /**\n * Creates a new WorkerMessageService instance with bidirectional communication\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?: Worker) {\n super([worker], [worker]);\n\n this.initialize(worker);\n }\n\n /**\n * Initializes or re-initializes the worker connection for both client and host.\n *\n * This method sets up message listeners for both sending and receiving messages.\n * If a worker was previously initialized, it cleans up the old connections before\n * establishing new ones. This allows for dynamic worker management.\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 service = new WorkerMessageService(oldWorker);\n * service.initialize(newWorker); // Cleans up old listeners, sets up new ones\n *\n * @example\n * // Re-initialize after worker error\n * worker.onerror = () => {\n * service.initialize(new Worker('./worker.js'));\n * };\n */\n public override initialize = (worker: ClientWorkerArg): void => {\n WorkerMessageHost.prototype.initialize.call(this, worker);\n WorkerMessageClient.prototype.initialize.call(this, worker);\n };\n}\n"]}
|