@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.
Files changed (50) hide show
  1. package/README.md +54 -8
  2. package/broadcast/message-client.js.map +1 -1
  3. package/broadcast/message-host.cjs +3 -2
  4. package/broadcast/message-host.d.ts +2 -1
  5. package/broadcast/message-host.js +3 -2
  6. package/broadcast/message-host.js.map +1 -1
  7. package/chrome/message-client.cjs +1 -1
  8. package/chrome/message-client.js +1 -1
  9. package/chrome/message-client.js.map +1 -1
  10. package/chrome/message-host.cjs +4 -3
  11. package/chrome/message-host.d.ts +2 -1
  12. package/chrome/message-host.js +4 -3
  13. package/chrome/message-host.js.map +1 -1
  14. package/iframe/message-client.js.map +1 -1
  15. package/iframe/message-host.cjs +3 -2
  16. package/iframe/message-host.d.ts +2 -1
  17. package/iframe/message-host.js +3 -2
  18. package/iframe/message-host.js.map +1 -1
  19. package/message-host.cjs +12 -7
  20. package/message-host.d.ts +4 -3
  21. package/message-host.js +13 -8
  22. package/message-host.js.map +1 -1
  23. package/message-response.type.d.ts +2 -0
  24. package/message-response.type.js.map +1 -1
  25. package/message.interface.d.ts +1 -0
  26. package/message.interface.js.map +1 -1
  27. package/package.json +1 -1
  28. package/request.decorator.cjs +2 -2
  29. package/request.decorator.js +2 -2
  30. package/request.decorator.js.map +1 -1
  31. package/selectors.cjs +4 -2
  32. package/selectors.d.ts +3 -1
  33. package/selectors.js +3 -1
  34. package/selectors.js.map +1 -1
  35. package/worker/initializer.cjs +39 -0
  36. package/worker/initializer.d.ts +12 -0
  37. package/worker/initializer.js +38 -0
  38. package/worker/initializer.js.map +1 -0
  39. package/worker/message-client.cjs +14 -30
  40. package/worker/message-client.d.ts +19 -5
  41. package/worker/message-client.js +14 -30
  42. package/worker/message-client.js.map +1 -1
  43. package/worker/message-host.cjs +146 -24
  44. package/worker/message-host.d.ts +120 -3
  45. package/worker/message-host.js +146 -24
  46. package/worker/message-host.js.map +1 -1
  47. package/worker/message-service.cjs +111 -0
  48. package/worker/message-service.d.ts +110 -1
  49. package/worker/message-service.js +111 -0
  50. package/worker/message-service.js.map +1 -1
@@ -1,8 +1,117 @@
1
1
  import { WorkerMessageClient } from "./message-client";
2
- declare const WorkerMessageService_base: new (args_0: [worker?: Worker | undefined], args_1: [worker?: (Worker | undefined) | Promise<Worker | undefined> | (() => (Worker | undefined) | Promise<Worker | undefined>)]) => {
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;AAEnD,MAAM,OAAO,oBAAqB,SAAQ,KAAK,CAAC,iBAAiB,EAAE,mBAAmB,CAAC;IACrF,YAAY,MAAe;QACzB,KAAK,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAC5B,CAAC;CACF","file":"message-service.js","sourcesContent":["import { mixin } from \"@thalesrc/js-utils/class/mixin\";\nimport { WorkerMessageClient } from \"./message-client\";\nimport { WorkerMessageHost } from \"./message-host\";\n\nexport class WorkerMessageService extends mixin(WorkerMessageHost, WorkerMessageClient) {\n constructor(worker?: Worker) {\n super([worker], [worker]);\n }\n}\n"]}
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"]}