@telperion/messenger 8.4.0 → 8.5.0
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/.build-manifest.json +416 -0
- package/README.md +30 -1
- package/broadcast/default-channel-name.cjs +6 -4
- package/broadcast/default-channel-name.cjs.map +1 -0
- package/broadcast/default-channel-name.d.cts +3 -0
- package/broadcast/default-channel-name.d.ts +3 -1
- package/broadcast/default-channel-name.js +2 -1
- package/broadcast/default-channel-name.js.map +1 -1
- package/broadcast/index.cjs +16 -13
- package/broadcast/index.cjs.map +1 -0
- package/broadcast/index.d.cts +11 -0
- package/broadcast/index.d.ts +11 -5
- package/broadcast/index.js +5 -6
- package/broadcast/index.js.map +1 -1
- package/broadcast/message-client.cjs +15 -13
- package/broadcast/message-client.cjs.map +1 -0
- package/broadcast/message-client.d.cts +15 -0
- package/broadcast/message-client.d.ts +9 -6
- package/broadcast/message-client.js +7 -5
- package/broadcast/message-client.js.map +1 -1
- package/broadcast/message-host.cjs +14 -12
- package/broadcast/message-host.cjs.map +1 -0
- package/broadcast/message-host.d.cts +14 -0
- package/broadcast/message-host.d.ts +9 -4
- package/broadcast/message-host.js +7 -5
- package/broadcast/message-host.js.map +1 -1
- package/broadcast/message-service.cjs +11 -9
- package/broadcast/message-service.cjs.map +1 -0
- package/broadcast/message-service.d.cts +15 -0
- package/broadcast/message-service.d.ts +10 -3
- package/broadcast/message-service.js +7 -5
- package/broadcast/message-service.js.map +1 -1
- package/chrome/default-connection-name.cjs +6 -4
- package/chrome/default-connection-name.cjs.map +1 -0
- package/chrome/default-connection-name.d.cts +3 -0
- package/chrome/default-connection-name.d.ts +3 -1
- package/chrome/default-connection-name.js +2 -1
- package/chrome/default-connection-name.js.map +1 -1
- package/chrome/index.cjs +14 -11
- package/chrome/index.cjs.map +1 -0
- package/chrome/index.d.cts +10 -0
- package/chrome/index.d.ts +10 -4
- package/chrome/index.js +4 -5
- package/chrome/index.js.map +1 -1
- package/chrome/message-client.cjs +18 -16
- package/chrome/message-client.cjs.map +1 -0
- package/chrome/message-client.d.cts +17 -0
- package/chrome/message-client.d.ts +8 -6
- package/chrome/message-client.js +6 -4
- package/chrome/message-client.js.map +1 -1
- package/chrome/message-host.cjs +15 -13
- package/chrome/message-host.cjs.map +1 -0
- package/chrome/message-host.d.cts +14 -0
- package/chrome/message-host.d.ts +9 -4
- package/chrome/message-host.js +6 -4
- package/chrome/message-host.js.map +1 -1
- package/fesm/messenger.mjs +125 -0
- package/fesm/messenger.mjs.map +1 -0
- package/iframe/channel-path-splitter.cjs +6 -4
- package/iframe/channel-path-splitter.cjs.map +1 -0
- package/iframe/channel-path-splitter.d.cts +3 -0
- package/iframe/channel-path-splitter.d.ts +3 -1
- package/iframe/channel-path-splitter.js +2 -1
- package/iframe/channel-path-splitter.js.map +1 -1
- package/iframe/default-channel-name.cjs +6 -4
- package/iframe/default-channel-name.cjs.map +1 -0
- package/iframe/default-channel-name.d.cts +3 -0
- package/iframe/default-channel-name.d.ts +3 -1
- package/iframe/default-channel-name.js +2 -1
- package/iframe/default-channel-name.js.map +1 -1
- package/iframe/iframe.type.cjs +3 -2
- package/iframe/iframe.type.cjs.map +1 -0
- package/iframe/iframe.type.d.cts +6 -0
- package/iframe/iframe.type.d.ts +6 -4
- package/iframe/iframe.type.js +0 -1
- package/iframe/iframe.type.js.map +1 -1
- package/iframe/index.cjs +16 -13
- package/iframe/index.cjs.map +1 -0
- package/iframe/index.d.cts +13 -0
- package/iframe/index.d.ts +13 -6
- package/iframe/index.js +5 -6
- package/iframe/index.js.map +1 -1
- package/iframe/message-client.cjs +26 -24
- package/iframe/message-client.cjs.map +1 -0
- package/iframe/message-client.d.cts +19 -0
- package/iframe/message-client.d.ts +9 -7
- package/iframe/message-client.js +7 -5
- package/iframe/message-client.js.map +1 -1
- package/iframe/message-host.cjs +30 -28
- package/iframe/message-host.cjs.map +1 -0
- package/iframe/message-host.d.cts +23 -0
- package/iframe/message-host.d.ts +10 -6
- package/iframe/message-host.js +8 -6
- package/iframe/message-host.js.map +1 -1
- package/iframe/message-service.cjs +13 -11
- package/iframe/message-service.cjs.map +1 -0
- package/iframe/message-service.d.cts +18 -0
- package/iframe/message-service.d.ts +11 -4
- package/iframe/message-service.js +7 -5
- package/iframe/message-service.js.map +1 -1
- package/iframe/source-id-splitter.cjs +6 -4
- package/iframe/source-id-splitter.cjs.map +1 -0
- package/iframe/source-id-splitter.d.cts +3 -0
- package/iframe/source-id-splitter.d.ts +3 -1
- package/iframe/source-id-splitter.js +2 -1
- package/iframe/source-id-splitter.js.map +1 -1
- package/iframe/upcoming-message.cjs +3 -2
- package/iframe/upcoming-message.cjs.map +1 -0
- package/iframe/upcoming-message.d.cts +6 -0
- package/iframe/upcoming-message.d.ts +3 -1
- package/iframe/upcoming-message.js +0 -1
- package/iframe/upcoming-message.js.map +1 -1
- package/index.cjs +22 -13
- package/index.cjs.map +1 -0
- package/index.d.cts +8 -0
- package/index.d.ts +8 -7
- package/index.js +5 -6
- package/index.js.map +1 -1
- package/listen.decorator.cjs +11 -8
- package/listen.decorator.cjs.map +1 -0
- package/listen.decorator.d.cts +8 -0
- package/listen.decorator.d.ts +3 -1
- package/listen.decorator.js +4 -2
- package/listen.decorator.js.map +1 -1
- package/listener-storage.type.cjs +3 -2
- package/listener-storage.type.cjs.map +1 -0
- package/listener-storage.type.d.cts +5 -0
- package/listener-storage.type.d.ts +4 -1
- package/listener-storage.type.js +0 -1
- package/listener-storage.type.js.map +1 -1
- package/message-client.cjs +4 -4
- package/message-client.cjs.map +1 -0
- package/message-client.d.cts +26 -0
- package/message-client.d.ts +8 -5
- package/message-client.js +2 -2
- package/message-client.js.map +1 -1
- package/message-host.cjs +20 -18
- package/message-host.cjs.map +1 -0
- package/message-host.d.cts +29 -0
- package/message-host.d.ts +8 -5
- package/message-host.js +5 -3
- package/message-host.js.map +1 -1
- package/message-response.type.cjs +3 -2
- package/message-response.type.cjs.map +1 -0
- package/message-response.type.d.cts +18 -0
- package/message-response.type.d.ts +8 -6
- package/message-response.type.js +0 -1
- package/message-response.type.js.map +1 -1
- package/message.interface.cjs +3 -2
- package/message.interface.cjs.map +1 -0
- package/message.interface.d.cts +8 -0
- package/message.interface.d.ts +3 -1
- package/message.interface.js +0 -1
- package/message.interface.js.map +1 -1
- package/package.json +387 -75
- package/request.decorator.cjs +32 -14
- package/request.decorator.cjs.map +1 -0
- package/request.decorator.d.cts +55 -0
- package/request.decorator.d.ts +55 -1
- package/request.decorator.js +26 -9
- package/request.decorator.js.map +1 -1
- package/rtc/channel-controller.cjs +6 -4
- package/rtc/channel-controller.cjs.map +1 -0
- package/rtc/channel-controller.d.cts +8 -0
- package/rtc/channel-controller.d.ts +3 -2
- package/rtc/channel-controller.js +2 -1
- package/rtc/channel-controller.js.map +1 -1
- package/rtc/default-channel-name.cjs +6 -4
- package/rtc/default-channel-name.cjs.map +1 -0
- package/rtc/default-channel-name.d.cts +4 -0
- package/rtc/default-channel-name.d.ts +3 -1
- package/rtc/default-channel-name.js +2 -1
- package/rtc/default-channel-name.js.map +1 -1
- package/rtc/index.cjs +16 -13
- package/rtc/index.cjs.map +1 -0
- package/rtc/index.d.cts +12 -0
- package/rtc/index.d.ts +12 -5
- package/rtc/index.js +5 -6
- package/rtc/index.js.map +1 -1
- package/rtc/initializer.cjs +23 -18
- package/rtc/initializer.cjs.map +1 -0
- package/rtc/initializer.d.cts +20 -0
- package/rtc/initializer.d.ts +7 -3
- package/rtc/initializer.js +11 -7
- package/rtc/initializer.js.map +1 -1
- package/rtc/message-client.cjs +63 -31
- package/rtc/message-client.cjs.map +1 -0
- package/rtc/message-client.d.cts +75 -0
- package/rtc/message-client.d.ts +17 -9
- package/rtc/message-client.js +40 -10
- package/rtc/message-client.js.map +1 -1
- package/rtc/message-host.cjs +40 -28
- package/rtc/message-host.cjs.map +1 -0
- package/rtc/message-host.d.cts +70 -0
- package/rtc/message-host.d.ts +12 -8
- package/rtc/message-host.js +18 -8
- package/rtc/message-host.js.map +1 -1
- package/rtc/message-service.cjs +17 -15
- package/rtc/message-service.cjs.map +1 -0
- package/rtc/message-service.d.cts +46 -0
- package/rtc/message-service.d.ts +11 -4
- package/rtc/message-service.js +11 -9
- package/rtc/message-service.js.map +1 -1
- package/rtc/rtc.type.cjs +3 -2
- package/rtc/rtc.type.cjs.map +1 -0
- package/rtc/rtc.type.d.cts +14 -0
- package/rtc/rtc.type.d.ts +6 -4
- package/rtc/rtc.type.js +0 -1
- package/rtc/rtc.type.js.map +1 -1
- package/selectors.cjs +18 -10
- package/selectors.cjs.map +1 -0
- package/selectors.d.cts +9 -0
- package/selectors.d.ts +9 -7
- package/selectors.js +8 -7
- package/selectors.js.map +1 -1
- package/worker/index.cjs +16 -13
- package/worker/index.cjs.map +1 -0
- package/worker/index.d.cts +12 -0
- package/worker/index.d.ts +12 -5
- package/worker/index.js +5 -6
- package/worker/index.js.map +1 -1
- package/worker/initializer.cjs +11 -8
- package/worker/initializer.cjs.map +1 -0
- package/worker/initializer.d.cts +15 -0
- package/worker/initializer.d.ts +8 -5
- package/worker/initializer.js +5 -3
- package/worker/initializer.js.map +1 -1
- package/worker/message-client.cjs +19 -17
- package/worker/message-client.cjs.map +1 -0
- package/worker/message-client.d.cts +133 -0
- package/worker/message-client.d.ts +10 -8
- package/worker/message-client.js +9 -7
- package/worker/message-client.js.map +1 -1
- package/worker/message-host.cjs +17 -15
- package/worker/message-host.cjs.map +1 -0
- package/worker/message-host.d.cts +135 -0
- package/worker/message-host.d.ts +10 -6
- package/worker/message-host.js +8 -6
- package/worker/message-host.js.map +1 -1
- package/worker/message-service.cjs +11 -9
- package/worker/message-service.cjs.map +1 -0
- package/worker/message-service.d.cts +124 -0
- package/worker/message-service.d.ts +11 -4
- package/worker/message-service.js +6 -4
- package/worker/message-service.js.map +1 -1
package/worker/initializer.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { noop } from
|
|
2
|
-
import { promisify } from
|
|
1
|
+
import { noop } from '@telperion/js-utils/function/noop';
|
|
2
|
+
import { promisify } from '@telperion/js-utils/promise/promisify';
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* Initializes a Web Worker or the worker context for message communication.
|
|
5
6
|
* @param prevWorker The previous worker promise to deinitialize
|
|
@@ -7,7 +8,7 @@ import { promisify } from "@telperion/js-utils/promise/promisify";
|
|
|
7
8
|
* @param handler The message event handler to attach
|
|
8
9
|
* @returns A promise resolving to the initialized Worker or undefined if in worker context
|
|
9
10
|
*/
|
|
10
|
-
|
|
11
|
+
function initializer(prevWorker, worker, handler) {
|
|
11
12
|
// Deinitialize previous worker if any
|
|
12
13
|
prevWorker.then(pw => {
|
|
13
14
|
if (pw) {
|
|
@@ -35,4 +36,5 @@ export function initializer(prevWorker, worker, handler) {
|
|
|
35
36
|
return newWorker;
|
|
36
37
|
}
|
|
37
38
|
|
|
39
|
+
export { initializer };
|
|
38
40
|
//# sourceMappingURL=initializer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"file":"initializer.js","sources":["../../../../libs/messenger/src/worker/initializer.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAQA;;;;;;AAMG;SACa,WAAW,CACzB,UAA+B,EAC/B,MAAuB,EACvB,OAAsC,EAAA;;AAGtC,IAAA,UAAU,CAAC,IAAI,CAAC,EAAE,IAAG;QACnB,IAAI,EAAE,EAAE;AACN,YAAA,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC;QAC5C;aAAO;AACL,YAAA,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC;QACzC;AACF,IAAA,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;;AAGd,IAAA,MAAM,OAAO,GAAuD,OAAO,MAAM,KAAK,UAAU,GAAG,MAAM,EAAE,GAAG,MAAM;;AAEpH,IAAA,MAAM,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC;;AAGpC,IAAA,SAAS,CAAC,IAAI,CAAC,MAAM,IAAG;QACtB,IAAI,MAAM,EAAE;;AAEV,YAAA,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC;QAC7C;aAAO;;AAEL,YAAA,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC;QACtC;AACF,IAAA,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;AAEd,IAAA,OAAO,SAAS;AAClB;;;;"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var uniqueId = require('@telperion/js-utils/unique-id');
|
|
4
|
+
var noop = require('@telperion/js-utils/function/noop');
|
|
5
|
+
var rxjs = require('rxjs');
|
|
6
|
+
var messageClient = require('../message-client.cjs');
|
|
7
|
+
var selectors = require('../selectors.cjs');
|
|
8
|
+
var worker_initializer = require('./initializer.cjs');
|
|
9
|
+
|
|
10
10
|
const WORKER = Symbol('Client Worker');
|
|
11
11
|
const HANDLER = Symbol('Client Message Handler');
|
|
12
12
|
const INSTANCE_ID = Symbol('Client Instance ID');
|
|
@@ -55,7 +55,7 @@ const INSTANCE_ID = Symbol('Client Instance ID');
|
|
|
55
55
|
* // Later, switch to a different worker
|
|
56
56
|
* client.initialize(new Worker('./different-worker.js'));
|
|
57
57
|
*/
|
|
58
|
-
class WorkerMessageClient extends
|
|
58
|
+
class WorkerMessageClient extends messageClient.MessageClient {
|
|
59
59
|
/**
|
|
60
60
|
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
61
61
|
* @private
|
|
@@ -69,7 +69,7 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
69
69
|
/**
|
|
70
70
|
* Observable stream of message responses from the worker or main thread
|
|
71
71
|
*/
|
|
72
|
-
[
|
|
72
|
+
[selectors.RESPONSES$] = new rxjs.Subject();
|
|
73
73
|
/**
|
|
74
74
|
* Creates a new WorkerMessageClient instance
|
|
75
75
|
*
|
|
@@ -117,7 +117,7 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
117
117
|
if (!this[WORKER]) {
|
|
118
118
|
this[WORKER] = Promise.resolve(undefined);
|
|
119
119
|
}
|
|
120
|
-
this[WORKER] =
|
|
120
|
+
this[WORKER] = worker_initializer.initializer(this[WORKER], worker, this[HANDLER]);
|
|
121
121
|
}
|
|
122
122
|
/**
|
|
123
123
|
* Sends a message to the worker or main thread
|
|
@@ -126,7 +126,7 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
126
126
|
* @template T - Type of the message body
|
|
127
127
|
* @internal Used by @Request decorator
|
|
128
128
|
*/
|
|
129
|
-
[
|
|
129
|
+
[selectors.SEND](message) {
|
|
130
130
|
this[WORKER].then(worker => {
|
|
131
131
|
if (worker) {
|
|
132
132
|
// Main thread: send to worker
|
|
@@ -136,14 +136,14 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
136
136
|
// Worker thread: send to main thread
|
|
137
137
|
postMessage(message);
|
|
138
138
|
}
|
|
139
|
-
}).catch(
|
|
139
|
+
}).catch(noop.noop);
|
|
140
140
|
}
|
|
141
141
|
/**
|
|
142
142
|
* Handles incoming messages and forwards them to the responses stream
|
|
143
143
|
* @private
|
|
144
144
|
*/
|
|
145
145
|
[HANDLER] = (event) => {
|
|
146
|
-
this[
|
|
146
|
+
this[selectors.RESPONSES$].next(event.data);
|
|
147
147
|
};
|
|
148
148
|
/**
|
|
149
149
|
* Generates a unique message ID for tracking request-response pairs
|
|
@@ -151,8 +151,10 @@ class WorkerMessageClient extends message_client_1.MessageClient {
|
|
|
151
151
|
* @returns Unique message identifier
|
|
152
152
|
* @internal Used by @Request decorator
|
|
153
153
|
*/
|
|
154
|
-
[
|
|
155
|
-
return
|
|
154
|
+
[selectors.GET_NEW_ID]() {
|
|
155
|
+
return uniqueId.uniqueId('messenger-worker-message-' + this[INSTANCE_ID]);
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
|
+
|
|
158
159
|
exports.WorkerMessageClient = WorkerMessageClient;
|
|
160
|
+
//# sourceMappingURL=message-client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-client.cjs","sources":["../../../../libs/messenger/src/worker/message-client.ts"],"sourcesContent":[null],"names":["MessageClient","RESPONSES$","Subject","initializer","SEND","noop","GET_NEW_ID","uniqueId"],"mappings":";;;;;;;;;AASA,MAAM,MAAM,GAAG,MAAM,CAAC,eAAe,CAAC;AACtC,MAAM,OAAO,GAAG,MAAM,CAAC,wBAAwB,CAAC;AAChD,MAAM,WAAW,GAAG,MAAM,CAAC,oBAAoB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CG;AACG,MAAO,mBAAoB,SAAQA,2BAAa,CAAA;AACpD;;;AAGG;IACK,CAAC,MAAM,IAAyB,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;AAElE;;;AAGG;AACK,IAAA,CAAC,WAAW,IAAI,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC;AAE9D;;AAEG;AACI,IAAA,CAACC,oBAAU,IAAI,IAAIC,YAAO,EAAmB;AAEpD;;;;;;;;;AASG;AACH,IAAA,WAAA,CAAY,MAAwB,EAAA;AAClC,QAAA,KAAK,EAAE;AAEP,QAAA,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;IACzB;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;AACH,IAAA,UAAU,CAAC,MAAwB,EAAA;;AAEjC,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;YACjB,IAAI,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;QAC3C;AAEA,QAAA,IAAI,CAAC,MAAM,CAAC,GAAGC,8BAAW,CACxB,IAAI,CAAC,MAAM,CAAC,EACZ,MAAM,EACN,IAAI,CAAC,OAAO,CAAC,CACd;IACH;AAEA;;;;;;AAMG;IACI,CAACC,cAAI,CAAC,CAAI,OAAmB,EAAA;QAClC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,IAAG;YACzB,IAAI,MAAM,EAAE;;AAEV,gBAAA,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;YAC7B;iBAAO;;gBAEJ,WAAmB,CAAC,OAAO,CAAC;YAC/B;AACF,QAAA,CAAC,CAAC,CAAC,KAAK,CAACC,SAAI,CAAC;IAChB;AAEA;;;AAGG;AACK,IAAA,CAAC,OAAO,IAAI,CAAC,KAAoC,KAAI;QAC3D,IAAI,CAACJ,oBAAU,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;AACnC,IAAA,CAAC;AAED;;;;;AAKG;AACO,IAAA,CAACK,oBAAU,CAAC,GAAA;QACpB,OAAOC,iBAAQ,CAAC,2BAA2B,GAAG,IAAI,CAAC,WAAW,CAAC,CAAW;IAC5E;AACD;;;;"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Subject } from 'rxjs';
|
|
2
|
+
import { MessageClient } from '../message-client.cjs';
|
|
3
|
+
import { MessageResponse } from '../message-response.type.cjs';
|
|
4
|
+
import { Message } from '../message.interface.cjs';
|
|
5
|
+
import { RESPONSES$, SEND, GET_NEW_ID } from '../selectors.cjs';
|
|
6
|
+
import { ClientWorkerArg } from './initializer.cjs';
|
|
7
|
+
|
|
8
|
+
declare const WORKER: unique symbol;
|
|
9
|
+
declare const HANDLER: unique symbol;
|
|
10
|
+
declare const INSTANCE_ID: unique symbol;
|
|
11
|
+
/**
|
|
12
|
+
* WorkerMessageClient
|
|
13
|
+
*
|
|
14
|
+
* Client implementation for Web Worker communication. Sends messages to and receives
|
|
15
|
+
* responses from Web Workers using the Worker postMessage API.
|
|
16
|
+
*
|
|
17
|
+
* This class can be used in two contexts:
|
|
18
|
+
* - **Main thread**: Provide a Worker instance to communicate with that specific worker
|
|
19
|
+
* - **Worker thread**: Omit the worker parameter to communicate with the main thread via self
|
|
20
|
+
*
|
|
21
|
+
* The worker parameter supports multiple types for flexibility:
|
|
22
|
+
* - Direct Worker instance
|
|
23
|
+
* - Promise that resolves to a Worker (for async worker initialization)
|
|
24
|
+
* - Function that returns a Worker (for lazy initialization)
|
|
25
|
+
* - Function that returns a Promise<Worker> (for async lazy initialization)
|
|
26
|
+
*
|
|
27
|
+
* The `initialize()` method allows dynamic worker management, enabling you to switch
|
|
28
|
+
* workers at runtime or re-establish connections after errors.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // In main thread - communicate with a specific worker
|
|
32
|
+
* const worker = new Worker('./worker.js');
|
|
33
|
+
* const client = new WorkerMessageClient(worker);
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* // In worker thread - communicate with main thread
|
|
37
|
+
* const client = new WorkerMessageClient();
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // With async worker initialization
|
|
41
|
+
* const workerPromise = import('./worker.js').then(m => new m.MyWorker());
|
|
42
|
+
* const client = new WorkerMessageClient(workerPromise);
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // With lazy initialization
|
|
46
|
+
* const client = new WorkerMessageClient(() =>
|
|
47
|
+
* document.querySelector('[data-worker]') ? new Worker('./worker.js') : undefined
|
|
48
|
+
* );
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Re-initialize with a different worker
|
|
52
|
+
* const client = new WorkerMessageClient();
|
|
53
|
+
* // Later, switch to a different worker
|
|
54
|
+
* client.initialize(new Worker('./different-worker.js'));
|
|
55
|
+
*/
|
|
56
|
+
declare class WorkerMessageClient extends MessageClient {
|
|
57
|
+
/**
|
|
58
|
+
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
private [WORKER];
|
|
62
|
+
/**
|
|
63
|
+
* Unique identifier for this client instance to prevent ID collisions
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
private [INSTANCE_ID];
|
|
67
|
+
/**
|
|
68
|
+
* Observable stream of message responses from the worker or main thread
|
|
69
|
+
*/
|
|
70
|
+
[RESPONSES$]: Subject<MessageResponse>;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new WorkerMessageClient instance
|
|
73
|
+
*
|
|
74
|
+
* @param worker - Optional worker configuration:
|
|
75
|
+
* - Worker: Direct worker instance (main thread)
|
|
76
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
77
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
78
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
79
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
80
|
+
*/
|
|
81
|
+
constructor(worker?: ClientWorkerArg);
|
|
82
|
+
/**
|
|
83
|
+
* Initializes or re-initializes the worker connection.
|
|
84
|
+
*
|
|
85
|
+
* This method sets up message listeners for the provided worker. If a worker
|
|
86
|
+
* was previously initialized, it cleans up the old connection before establishing
|
|
87
|
+
* the new one. This allows for dynamic worker management, such as:
|
|
88
|
+
* - Switching between different workers at runtime
|
|
89
|
+
* - Re-establishing connections after worker errors
|
|
90
|
+
* - Lazy initialization when the worker is conditionally needed
|
|
91
|
+
*
|
|
92
|
+
* @param worker - Optional worker configuration:
|
|
93
|
+
* - Worker: Direct worker instance (main thread)
|
|
94
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
95
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
96
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
97
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* // Switch to a different worker dynamically
|
|
101
|
+
* const client = new WorkerMessageClient(oldWorker);
|
|
102
|
+
* client.initialize(newWorker); // Cleans up old listener, sets up new one
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Re-initialize after worker error
|
|
106
|
+
* worker.onerror = () => {
|
|
107
|
+
* client.initialize(new Worker('./worker.js'));
|
|
108
|
+
* };
|
|
109
|
+
*/
|
|
110
|
+
initialize(worker?: ClientWorkerArg): void;
|
|
111
|
+
/**
|
|
112
|
+
* Sends a message to the worker or main thread
|
|
113
|
+
*
|
|
114
|
+
* @param message - The message to send
|
|
115
|
+
* @template T - Type of the message body
|
|
116
|
+
* @internal Used by @Request decorator
|
|
117
|
+
*/
|
|
118
|
+
[SEND]<T>(message: Message<T>): void;
|
|
119
|
+
/**
|
|
120
|
+
* Handles incoming messages and forwards them to the responses stream
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
private [HANDLER];
|
|
124
|
+
/**
|
|
125
|
+
* Generates a unique message ID for tracking request-response pairs
|
|
126
|
+
*
|
|
127
|
+
* @returns Unique message identifier
|
|
128
|
+
* @internal Used by @Request decorator
|
|
129
|
+
*/
|
|
130
|
+
protected [GET_NEW_ID](): string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { WorkerMessageClient };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Subject } from
|
|
2
|
-
import { MessageClient } from
|
|
3
|
-
import { MessageResponse } from
|
|
4
|
-
import { Message } from
|
|
5
|
-
import {
|
|
6
|
-
import { ClientWorkerArg } from
|
|
1
|
+
import { Subject } from 'rxjs';
|
|
2
|
+
import { MessageClient } from '../message-client.js';
|
|
3
|
+
import { MessageResponse } from '../message-response.type.js';
|
|
4
|
+
import { Message } from '../message.interface.js';
|
|
5
|
+
import { RESPONSES$, SEND, GET_NEW_ID } from '../selectors.js';
|
|
6
|
+
import { ClientWorkerArg } from './initializer.js';
|
|
7
|
+
|
|
7
8
|
declare const WORKER: unique symbol;
|
|
8
9
|
declare const HANDLER: unique symbol;
|
|
9
10
|
declare const INSTANCE_ID: unique symbol;
|
|
@@ -52,7 +53,7 @@ declare const INSTANCE_ID: unique symbol;
|
|
|
52
53
|
* // Later, switch to a different worker
|
|
53
54
|
* client.initialize(new Worker('./different-worker.js'));
|
|
54
55
|
*/
|
|
55
|
-
|
|
56
|
+
declare class WorkerMessageClient extends MessageClient {
|
|
56
57
|
/**
|
|
57
58
|
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
58
59
|
* @private
|
|
@@ -128,4 +129,5 @@ export declare class WorkerMessageClient extends MessageClient {
|
|
|
128
129
|
*/
|
|
129
130
|
protected [GET_NEW_ID](): string;
|
|
130
131
|
}
|
|
131
|
-
|
|
132
|
+
|
|
133
|
+
export { WorkerMessageClient };
|
package/worker/message-client.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { uniqueId } from
|
|
2
|
-
import { noop } from
|
|
3
|
-
import { Subject } from
|
|
4
|
-
import { MessageClient } from
|
|
5
|
-
import {
|
|
6
|
-
import { initializer } from
|
|
1
|
+
import { uniqueId } from '@telperion/js-utils/unique-id';
|
|
2
|
+
import { noop } from '@telperion/js-utils/function/noop';
|
|
3
|
+
import { Subject } from 'rxjs';
|
|
4
|
+
import { MessageClient } from '../message-client.js';
|
|
5
|
+
import { RESPONSES$, SEND, GET_NEW_ID } from '../selectors.js';
|
|
6
|
+
import { initializer } from './initializer.js';
|
|
7
|
+
|
|
7
8
|
const WORKER = Symbol('Client Worker');
|
|
8
9
|
const HANDLER = Symbol('Client Message Handler');
|
|
9
10
|
const INSTANCE_ID = Symbol('Client Instance ID');
|
|
@@ -52,7 +53,7 @@ const INSTANCE_ID = Symbol('Client Instance ID');
|
|
|
52
53
|
* // Later, switch to a different worker
|
|
53
54
|
* client.initialize(new Worker('./different-worker.js'));
|
|
54
55
|
*/
|
|
55
|
-
|
|
56
|
+
class WorkerMessageClient extends MessageClient {
|
|
56
57
|
/**
|
|
57
58
|
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
58
59
|
* @private
|
|
@@ -153,4 +154,5 @@ export class WorkerMessageClient extends MessageClient {
|
|
|
153
154
|
}
|
|
154
155
|
}
|
|
155
156
|
|
|
157
|
+
export { WorkerMessageClient };
|
|
156
158
|
//# sourceMappingURL=message-client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"file":"message-client.js","sources":["../../../../libs/messenger/src/worker/message-client.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;AASA,MAAM,MAAM,GAAG,MAAM,CAAC,eAAe,CAAC;AACtC,MAAM,OAAO,GAAG,MAAM,CAAC,wBAAwB,CAAC;AAChD,MAAM,WAAW,GAAG,MAAM,CAAC,oBAAoB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CG;AACG,MAAO,mBAAoB,SAAQ,aAAa,CAAA;AACpD;;;AAGG;IACK,CAAC,MAAM,IAAyB,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;AAElE;;;AAGG;AACK,IAAA,CAAC,WAAW,IAAI,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC;AAE9D;;AAEG;AACI,IAAA,CAAC,UAAU,IAAI,IAAI,OAAO,EAAmB;AAEpD;;;;;;;;;AASG;AACH,IAAA,WAAA,CAAY,MAAwB,EAAA;AAClC,QAAA,KAAK,EAAE;AAEP,QAAA,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;IACzB;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;AACH,IAAA,UAAU,CAAC,MAAwB,EAAA;;AAEjC,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;YACjB,IAAI,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;QAC3C;AAEA,QAAA,IAAI,CAAC,MAAM,CAAC,GAAG,WAAW,CACxB,IAAI,CAAC,MAAM,CAAC,EACZ,MAAM,EACN,IAAI,CAAC,OAAO,CAAC,CACd;IACH;AAEA;;;;;;AAMG;IACI,CAAC,IAAI,CAAC,CAAI,OAAmB,EAAA;QAClC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,IAAG;YACzB,IAAI,MAAM,EAAE;;AAEV,gBAAA,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;YAC7B;iBAAO;;gBAEJ,WAAmB,CAAC,OAAO,CAAC;YAC/B;AACF,QAAA,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;IAChB;AAEA;;;AAGG;AACK,IAAA,CAAC,OAAO,IAAI,CAAC,KAAoC,KAAI;QAC3D,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;AACnC,IAAA,CAAC;AAED;;;;;AAKG;AACO,IAAA,CAAC,UAAU,CAAC,GAAA;QACpB,OAAO,QAAQ,CAAC,2BAA2B,GAAG,IAAI,CAAC,WAAW,CAAC,CAAW;IAC5E;AACD;;;;"}
|
package/worker/message-host.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rxjs = require('rxjs');
|
|
4
|
+
var messageHost = require('../message-host.cjs');
|
|
5
|
+
var selectors = require('../selectors.cjs');
|
|
6
|
+
var worker_initializer = require('./initializer.cjs');
|
|
7
|
+
var noop = require('@telperion/js-utils/function/noop');
|
|
8
|
+
|
|
9
9
|
const WORKER = Symbol('Host Worker');
|
|
10
10
|
const HANDLER = Symbol('Host Message Handler');
|
|
11
11
|
const REQUESTS$ = Symbol('Host Requests Stream');
|
|
@@ -54,12 +54,12 @@ const REQUESTS$ = Symbol('Host Requests Stream');
|
|
|
54
54
|
* // Later, switch to a different worker
|
|
55
55
|
* host.initialize(new Worker('./different-worker.js'));
|
|
56
56
|
*/
|
|
57
|
-
class WorkerMessageHost extends
|
|
57
|
+
class WorkerMessageHost extends messageHost.MessageHost {
|
|
58
58
|
/**
|
|
59
59
|
* Observable stream of incoming messages from the worker or main thread
|
|
60
60
|
* @private
|
|
61
61
|
*/
|
|
62
|
-
[REQUESTS$] = new
|
|
62
|
+
[REQUESTS$] = new rxjs.Subject();
|
|
63
63
|
/**
|
|
64
64
|
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
65
65
|
* @private
|
|
@@ -78,7 +78,7 @@ class WorkerMessageHost extends message_host_1.MessageHost {
|
|
|
78
78
|
constructor(worker) {
|
|
79
79
|
super();
|
|
80
80
|
this.initialize(worker);
|
|
81
|
-
this[
|
|
81
|
+
this[selectors.LISTEN](this[REQUESTS$]);
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
84
|
* Initializes or re-initializes the worker connection.
|
|
@@ -113,7 +113,7 @@ class WorkerMessageHost extends message_host_1.MessageHost {
|
|
|
113
113
|
if (!this[WORKER]) {
|
|
114
114
|
this[WORKER] = Promise.resolve(undefined);
|
|
115
115
|
}
|
|
116
|
-
this[WORKER] =
|
|
116
|
+
this[WORKER] = worker_initializer.initializer(this[WORKER], worker, this[HANDLER]);
|
|
117
117
|
}
|
|
118
118
|
/**
|
|
119
119
|
* Sends a response message back to the worker or main thread
|
|
@@ -121,7 +121,7 @@ class WorkerMessageHost extends message_host_1.MessageHost {
|
|
|
121
121
|
* @param message - The response message to send
|
|
122
122
|
* @internal Used by the message handling system to send responses
|
|
123
123
|
*/
|
|
124
|
-
[
|
|
124
|
+
[selectors.RESPONSE](message) {
|
|
125
125
|
this[WORKER].then(worker => {
|
|
126
126
|
if (worker) {
|
|
127
127
|
worker.postMessage(message);
|
|
@@ -129,7 +129,7 @@ class WorkerMessageHost extends message_host_1.MessageHost {
|
|
|
129
129
|
else {
|
|
130
130
|
postMessage(message);
|
|
131
131
|
}
|
|
132
|
-
}).catch(
|
|
132
|
+
}).catch(noop.noop);
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
135
|
* Terminates the worker connection and cleans up message listeners.
|
|
@@ -152,7 +152,7 @@ class WorkerMessageHost extends message_host_1.MessageHost {
|
|
|
152
152
|
else {
|
|
153
153
|
removeEventListener('message', this[HANDLER]);
|
|
154
154
|
}
|
|
155
|
-
}).catch(
|
|
155
|
+
}).catch(noop.noop);
|
|
156
156
|
}
|
|
157
157
|
/**
|
|
158
158
|
* Handles incoming messages and forwards them to the requests stream
|
|
@@ -162,4 +162,6 @@ class WorkerMessageHost extends message_host_1.MessageHost {
|
|
|
162
162
|
this[REQUESTS$].next(event.data);
|
|
163
163
|
};
|
|
164
164
|
}
|
|
165
|
+
|
|
165
166
|
exports.WorkerMessageHost = WorkerMessageHost;
|
|
167
|
+
//# sourceMappingURL=message-host.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-host.cjs","sources":["../../../../libs/messenger/src/worker/message-host.ts"],"sourcesContent":[null],"names":["MessageHost","Subject","LISTEN","initializer","RESPONSE","noop"],"mappings":";;;;;;;;AAQA,MAAM,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC;AACpC,MAAM,OAAO,GAAG,MAAM,CAAC,sBAAsB,CAAC;AAC9C,MAAM,SAAS,GAAG,MAAM,CAAC,sBAAsB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CG;AACG,MAAO,iBAAkB,SAAQA,uBAAW,CAAA;AAChD;;;AAGG;AACK,IAAA,CAAC,SAAS,IAAI,IAAIC,YAAO,EAAW;AAE5C;;;AAGG;IACK,CAAC,MAAM,IAAyB,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;AAElE;;;;;;;;;AASG;AACH,IAAA,WAAA,CAAY,MAAe,EAAA;AACzB,QAAA,KAAK,EAAE;AAEP,QAAA,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;QACvB,IAAI,CAACC,gBAAM,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC/B;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;AACH,IAAA,UAAU,CAAC,MAAuB,EAAA;;AAEhC,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;YACjB,IAAI,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;QAC3C;AAEA,QAAA,IAAI,CAAC,MAAM,CAAC,GAAGC,8BAAW,CACxB,IAAI,CAAC,MAAM,CAAC,EACZ,MAAM,EACN,IAAI,CAAC,OAAO,CAAC,CACd;IACH;AAEA;;;;;AAKG;IACO,CAACC,kBAAQ,CAAC,CAAC,OAAwB,EAAA;QAC3C,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,IAAG;YACzB,IAAI,MAAM,EAAE;AACV,gBAAA,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;YAC7B;iBAAO;gBACJ,WAAmB,CAAC,OAAO,CAAC;YAC/B;AACF,QAAA,CAAC,CAAC,CAAC,KAAK,CAACC,SAAI,CAAC;IAChB;AAEA;;;;;;;;;;;;AAYG;IACI,SAAS,GAAA;QACd,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,IAAG;YACzB,IAAI,MAAM,EAAE;gBACV,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YACtD;iBAAO;gBACL,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/C;AACF,QAAA,CAAC,CAAC,CAAC,KAAK,CAACA,SAAI,CAAC;IAChB;AAEA;;;AAGG;AACK,IAAA,CAAC,OAAO,IAAI,CAAC,KAA4B,KAAI;QACnD,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;AAClC,IAAA,CAAC;AACF;;;;"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { MessageHost } from '../message-host.cjs';
|
|
2
|
+
import { MessageResponse } from '../message-response.type.cjs';
|
|
3
|
+
import { RESPONSE } from '../selectors.cjs';
|
|
4
|
+
import { ClientWorkerArg } from './initializer.cjs';
|
|
5
|
+
import 'rxjs';
|
|
6
|
+
import '../message.interface.cjs';
|
|
7
|
+
|
|
8
|
+
declare const WORKER: unique symbol;
|
|
9
|
+
declare const HANDLER: unique symbol;
|
|
10
|
+
declare const REQUESTS$: unique symbol;
|
|
11
|
+
/**
|
|
12
|
+
* WorkerMessageHost
|
|
13
|
+
*
|
|
14
|
+
* Host implementation for Web Worker communication. Receives messages from and sends
|
|
15
|
+
* responses to Web Workers using the Worker postMessage API.
|
|
16
|
+
*
|
|
17
|
+
* This class can be used in two contexts:
|
|
18
|
+
* - **Main thread**: Provide a Worker instance to receive messages from that specific worker
|
|
19
|
+
* - **Worker thread**: Omit the worker parameter to receive messages from the main thread via self
|
|
20
|
+
*
|
|
21
|
+
* The worker parameter supports multiple types for flexibility:
|
|
22
|
+
* - Direct Worker instance
|
|
23
|
+
* - Promise that resolves to a Worker (for async worker initialization)
|
|
24
|
+
* - Function that returns a Worker (for lazy initialization)
|
|
25
|
+
* - Function that returns a Promise<Worker> (for async lazy initialization)
|
|
26
|
+
*
|
|
27
|
+
* The `initialize()` method allows dynamic worker management, enabling you to switch
|
|
28
|
+
* workers at runtime or re-establish connections after errors.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // In main thread - receive messages from a specific worker
|
|
32
|
+
* const worker = new Worker('./worker.js');
|
|
33
|
+
* const host = new WorkerMessageHost(worker);
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* // In worker thread - receive messages from main thread
|
|
37
|
+
* const host = new WorkerMessageHost();
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // With async worker initialization
|
|
41
|
+
* const workerPromise = import('./worker.js').then(m => new m.MyWorker());
|
|
42
|
+
* const host = new WorkerMessageHost(workerPromise);
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // With lazy initialization
|
|
46
|
+
* const host = new WorkerMessageHost(() =>
|
|
47
|
+
* document.querySelector('[data-worker]') ? new Worker('./worker.js') : undefined
|
|
48
|
+
* );
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Re-initialize with a different worker
|
|
52
|
+
* const host = new WorkerMessageHost();
|
|
53
|
+
* // Later, switch to a different worker
|
|
54
|
+
* host.initialize(new Worker('./different-worker.js'));
|
|
55
|
+
*/
|
|
56
|
+
declare class WorkerMessageHost extends MessageHost {
|
|
57
|
+
/**
|
|
58
|
+
* Observable stream of incoming messages from the worker or main thread
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
private [REQUESTS$];
|
|
62
|
+
/**
|
|
63
|
+
* Promise resolving to the Worker instance or undefined if running in worker context
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
private [WORKER];
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new WorkerMessageHost instance
|
|
69
|
+
*
|
|
70
|
+
* @param worker - Optional worker configuration:
|
|
71
|
+
* - Worker: Direct worker instance (main thread)
|
|
72
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
73
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
74
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
75
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
76
|
+
*/
|
|
77
|
+
constructor(worker?: Worker);
|
|
78
|
+
/**
|
|
79
|
+
* Initializes or re-initializes the worker connection.
|
|
80
|
+
*
|
|
81
|
+
* This method sets up message listeners for the provided worker. If a worker
|
|
82
|
+
* was previously initialized, it cleans up the old connection before establishing
|
|
83
|
+
* the new one. This allows for dynamic worker management, such as:
|
|
84
|
+
* - Switching between different workers at runtime
|
|
85
|
+
* - Re-establishing connections after worker errors
|
|
86
|
+
* - Lazy initialization when the worker is conditionally needed
|
|
87
|
+
*
|
|
88
|
+
* @param worker - Optional worker configuration:
|
|
89
|
+
* - Worker: Direct worker instance (main thread)
|
|
90
|
+
* - Promise<Worker>: Promise resolving to worker (async initialization)
|
|
91
|
+
* - () => Worker: Function returning worker (lazy initialization)
|
|
92
|
+
* - () => Promise<Worker>: Function returning promise (async lazy initialization)
|
|
93
|
+
* - undefined: Omit for worker thread context (uses self)
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* // Switch to a different worker dynamically
|
|
97
|
+
* const host = new WorkerMessageHost(oldWorker);
|
|
98
|
+
* host.initialize(newWorker); // Cleans up old listener, sets up new one
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* // Re-initialize after worker error
|
|
102
|
+
* worker.onerror = () => {
|
|
103
|
+
* host.initialize(new Worker('./worker.js'));
|
|
104
|
+
* };
|
|
105
|
+
*/
|
|
106
|
+
initialize(worker: ClientWorkerArg): void;
|
|
107
|
+
/**
|
|
108
|
+
* Sends a response message back to the worker or main thread
|
|
109
|
+
*
|
|
110
|
+
* @param message - The response message to send
|
|
111
|
+
* @internal Used by the message handling system to send responses
|
|
112
|
+
*/
|
|
113
|
+
protected [RESPONSE](message: MessageResponse): void;
|
|
114
|
+
/**
|
|
115
|
+
* Terminates the worker connection and cleans up message listeners.
|
|
116
|
+
*
|
|
117
|
+
* This method should be called when you're done with the host to prevent memory leaks.
|
|
118
|
+
* It removes the message event listener from either the Worker instance (main thread)
|
|
119
|
+
* or the global scope (worker thread).
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* // Clean up when done
|
|
123
|
+
* const host = new WorkerMessageHost(worker);
|
|
124
|
+
* // ... use the host ...
|
|
125
|
+
* host.terminate(); // Clean up listeners
|
|
126
|
+
*/
|
|
127
|
+
terminate(): void;
|
|
128
|
+
/**
|
|
129
|
+
* Handles incoming messages and forwards them to the requests stream
|
|
130
|
+
* @private
|
|
131
|
+
*/
|
|
132
|
+
private [HANDLER];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export { WorkerMessageHost };
|
package/worker/message-host.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { MessageHost } from
|
|
2
|
-
import { MessageResponse } from
|
|
3
|
-
import { RESPONSE } from
|
|
4
|
-
import { ClientWorkerArg } from
|
|
1
|
+
import { MessageHost } from '../message-host.js';
|
|
2
|
+
import { MessageResponse } from '../message-response.type.js';
|
|
3
|
+
import { RESPONSE } from '../selectors.js';
|
|
4
|
+
import { ClientWorkerArg } from './initializer.js';
|
|
5
|
+
import 'rxjs';
|
|
6
|
+
import '../message.interface.js';
|
|
7
|
+
|
|
5
8
|
declare const WORKER: unique symbol;
|
|
6
9
|
declare const HANDLER: unique symbol;
|
|
7
10
|
declare const REQUESTS$: unique symbol;
|
|
@@ -50,7 +53,7 @@ declare const REQUESTS$: unique symbol;
|
|
|
50
53
|
* // Later, switch to a different worker
|
|
51
54
|
* host.initialize(new Worker('./different-worker.js'));
|
|
52
55
|
*/
|
|
53
|
-
|
|
56
|
+
declare class WorkerMessageHost extends MessageHost {
|
|
54
57
|
/**
|
|
55
58
|
* Observable stream of incoming messages from the worker or main thread
|
|
56
59
|
* @private
|
|
@@ -128,4 +131,5 @@ export declare class WorkerMessageHost extends MessageHost {
|
|
|
128
131
|
*/
|
|
129
132
|
private [HANDLER];
|
|
130
133
|
}
|
|
131
|
-
|
|
134
|
+
|
|
135
|
+
export { WorkerMessageHost };
|
package/worker/message-host.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { Subject } from
|
|
2
|
-
import { MessageHost } from
|
|
3
|
-
import { LISTEN, RESPONSE } from
|
|
4
|
-
import { initializer } from
|
|
5
|
-
import { noop } from
|
|
1
|
+
import { Subject } from 'rxjs';
|
|
2
|
+
import { MessageHost } from '../message-host.js';
|
|
3
|
+
import { LISTEN, RESPONSE } from '../selectors.js';
|
|
4
|
+
import { initializer } from './initializer.js';
|
|
5
|
+
import { noop } from '@telperion/js-utils/function/noop';
|
|
6
|
+
|
|
6
7
|
const WORKER = Symbol('Host Worker');
|
|
7
8
|
const HANDLER = Symbol('Host Message Handler');
|
|
8
9
|
const REQUESTS$ = Symbol('Host Requests Stream');
|
|
@@ -51,7 +52,7 @@ const REQUESTS$ = Symbol('Host Requests Stream');
|
|
|
51
52
|
* // Later, switch to a different worker
|
|
52
53
|
* host.initialize(new Worker('./different-worker.js'));
|
|
53
54
|
*/
|
|
54
|
-
|
|
55
|
+
class WorkerMessageHost extends MessageHost {
|
|
55
56
|
/**
|
|
56
57
|
* Observable stream of incoming messages from the worker or main thread
|
|
57
58
|
* @private
|
|
@@ -160,4 +161,5 @@ export class WorkerMessageHost extends MessageHost {
|
|
|
160
161
|
};
|
|
161
162
|
}
|
|
162
163
|
|
|
164
|
+
export { WorkerMessageHost };
|
|
163
165
|
//# sourceMappingURL=message-host.js.map
|