@ohos-ports/serwist-window 9.5.12-beta.1 → 9.5.12-beta.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.
@@ -84,11 +84,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
84
84
  //#region ServiceWorker
85
85
  /**
86
86
  * A ServiceWorker handle whose message channel is a real
87
- * `worker_threads` MessagePort pair. `postMessage()` by the page goes out
88
- * on the worker side (`_channel.port2`), and messages
89
- * posted from the worker side surface as `message` events on the
90
- * containing ServiceWorkerContainer with `source` set to this instance,
91
- * matching the browser contract.
87
+ * `worker_threads` MessagePort pair. The page side of the channel is
88
+ * `port2`, the emulated SW side is `port1`: `postMessage()` by the page
89
+ * is posted on `port2`, so it emerges on the `port1` listener installed
90
+ * by `register()` and surfaces as a container-level `message` event
91
+ * (with any transferred reply ports intact). Messages posted by the
92
+ * emulated SW on the transferred reply port emerge on the page's own
93
+ * channel, matching the browser contract.
92
94
  */
93
95
  class ShimServiceWorker extends ShimEventTarget {
94
96
  _channel;
@@ -104,8 +106,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
104
106
  }
105
107
 
106
108
  postMessage(data, transfer) {
107
- // Sent from the page, delivered on the worker side.
108
- this._channel.port1.postMessage(data, transfer ?? []);
109
+ // Sent from the page, delivered on the worker (SW) side. In Node.js
110
+ // `worker_threads` semantics, `port2.postMessage()` delivers to the
111
+ // entangled peer `port1` — the port the registration listens on.
112
+ // (Posting on `port1` would deliver to `port2`, which has no
113
+ // listener, silently dropping the message and any transferred
114
+ // reply ports.)
115
+ this._channel.port2.postMessage(data, transfer ?? []);
109
116
  }
110
117
 
111
118
  _setState(state) {
@@ -172,9 +179,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
172
179
 
173
180
  const channel = new MessageChannel();
174
181
  const sw = new ShimServiceWorker(resolvedURL, channel);
175
- // Messages posted from the worker side emerge on port1 and surface
176
- // as container-level `message` events with `source` set to the
177
- // worker, matching the browser contract.
182
+ // The page posts messages on `port2` (see ShimServiceWorker
183
+ // .postMessage), so they emerge here on `port1` and surface as
184
+ // container-level `message` events with `source` set to the
185
+ // worker. Any transferred reply ports arrive via `event.ports`,
186
+ // so SW-side code (tests, message handlers) can reply through
187
+ // `event.ports[0].postMessage()` and reach the page's own
188
+ // `messageSW()` channel — matching the browser contract.
178
189
  channel.port1.onmessage = (event) => {
179
190
  this._dispatch("message", {
180
191
  data: event.data,
@@ -199,6 +210,10 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
199
210
  registration.installing = null;
200
211
  sw._setState("installed");
201
212
 
213
+ // Use a small observable delay between lifecycle phases so
214
+ // `registration.waiting` (like a real browser's waiting worker)
215
+ // stays observable to page-side polling before activation
216
+ // proceeds.
202
217
  setTimeout(() => {
203
218
  registration.waiting = null;
204
219
  registration.active = sw;
@@ -208,8 +223,8 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
208
223
  this.#controller = sw;
209
224
  sw._setState("activated");
210
225
  this._dispatch("controllerchange", { target: this });
211
- }, 0);
212
- }, 0);
226
+ }, 25);
227
+ }, 25);
213
228
  }, 0);
214
229
 
215
230
  return registration;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohos-ports/serwist-window",
3
- "version": "9.5.12-beta.1",
3
+ "version": "9.5.12-beta.2",
4
4
  "type": "module",
5
5
  "description": "Simplifies communications with Serwist packages running in the service worker",
6
6
  "files": [
@@ -82,11 +82,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
82
82
  //#region ServiceWorker
83
83
  /**
84
84
  * A ServiceWorker handle whose message channel is a real
85
- * `worker_threads` MessagePort pair. `postMessage()` by the page goes out
86
- * on the worker side (`_channel.port2`), and messages posted from the
87
- * worker side surface as `message` events on the containing
88
- * ServiceWorkerContainer with `source` set to this instance, matching the
89
- * browser contract.
85
+ * `worker_threads` MessagePort pair. The page side of the channel is
86
+ * `port2`, the emulated SW side is `port1`: `postMessage()` by the page
87
+ * is posted on `port2`, so it emerges on the `port1` listener installed
88
+ * by `register()` and surfaces as a container-level `message` event
89
+ * (with any transferred reply ports intact). Messages posted by the
90
+ * emulated SW on the transferred reply port emerge on the page's own
91
+ * channel, matching the browser contract.
90
92
  */
91
93
  class ShimServiceWorker extends ShimEventTarget {
92
94
  scriptURL: string;
@@ -103,8 +105,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
103
105
  }
104
106
 
105
107
  postMessage(data: any, transfer?: any[]): void {
106
- // Sent from the page, delivered on the worker side.
107
- this._channel.port1.postMessage(data, transfer ?? []);
108
+ // Sent from the page, delivered on the worker (SW) side. In Node.js
109
+ // `worker_threads` semantics, `port2.postMessage()` delivers to the
110
+ // entangled peer `port1` — the port the registration listens on.
111
+ // (Posting on `port1` would deliver to `port2`, which has no
112
+ // listener, silently dropping the message and any transferred
113
+ // reply ports.)
114
+ this._channel.port2.postMessage(data, transfer ?? []);
108
115
  }
109
116
 
110
117
  _setState(state: string): void {
@@ -201,6 +208,10 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
201
208
  registration.installing = null;
202
209
  sw._setState("installed");
203
210
 
211
+ // Use a small observable delay between lifecycle phases so
212
+ // `registration.waiting` (like a real browser's waiting worker)
213
+ // stays observable to page-side polling before activation
214
+ // proceeds.
204
215
  setTimeout(() => {
205
216
  registration.waiting = null;
206
217
  registration.active = sw;
@@ -210,8 +221,8 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
210
221
  this.#controller = sw;
211
222
  sw._setState("activated");
212
223
  this._dispatch("controllerchange", { target: this });
213
- }, 0);
214
- }, 0);
224
+ }, 25);
225
+ }, 25);
215
226
  }, 0);
216
227
 
217
228
  return registration;