Skip to content

Commit ea8853c

Browse files
committed
Use onmessage/postMessage instead of BroadcastChannel in diff updater
1 parent 95b99ef commit ea8853c

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/js/assets.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,8 @@ async function diffUpdater() {
12441244
ubolog('Diff updater: cycle start');
12451245
return new Promise(resolve => {
12461246
let pendingOps = 0;
1247-
const bc = new globalThis.BroadcastChannel('diffUpdater');
12481247
const terminate = error => {
12491248
worker.terminate();
1250-
bc.close();
12511249
resolve();
12521250
if ( typeof error !== 'string' ) { return; }
12531251
ubolog(`Diff updater: terminate because ${error}`);
@@ -1260,14 +1258,15 @@ async function diffUpdater() {
12601258
if ( metadata.diffPath === data.patchPath ) { return; }
12611259
assetCacheSetDetails(data.assetKey, metadata);
12621260
};
1263-
bc.onmessage = ev => {
1261+
const worker = new Worker('js/diff-updater.js');
1262+
worker.onmessage = ev => {
12641263
const data = ev.data || {};
12651264
if ( data.what === 'ready' ) {
12661265
ubolog('Diff updater: hard updating', toHardUpdate.map(v => v.assetKey).join());
12671266
while ( toHardUpdate.length !== 0 ) {
12681267
const assetDetails = toHardUpdate.shift();
12691268
assetDetails.fetch = true;
1270-
bc.postMessage(assetDetails);
1269+
worker.postMessage(assetDetails);
12711270
pendingOps += 1;
12721271
}
12731272
return;
@@ -1284,7 +1283,7 @@ async function diffUpdater() {
12841283
data.text = result.content || '';
12851284
data.status = undefined;
12861285
checkAndCorrectDiffPath(data);
1287-
bc.postMessage(data);
1286+
worker.postMessage(data);
12881287
});
12891288
return;
12901289
}
@@ -1320,15 +1319,14 @@ async function diffUpdater() {
13201319
if ( pendingOps === 0 && toSoftUpdate.length !== 0 ) {
13211320
ubolog('Diff updater: soft updating', toSoftUpdate.map(v => v.assetKey).join());
13221321
while ( toSoftUpdate.length !== 0 ) {
1323-
bc.postMessage(toSoftUpdate.shift());
1322+
worker.postMessage(toSoftUpdate.shift());
13241323
pendingOps += 1;
13251324
}
13261325
}
13271326
if ( pendingOps !== 0 ) { return; }
13281327
ubolog('Diff updater: cycle complete');
13291328
terminate();
13301329
};
1331-
const worker = new Worker('js/diff-updater.js');
13321330
}).catch(reason => {
13331331
ubolog(`Diff updater: ${reason}`);
13341332
});

src/js/diff-updater.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,21 +265,21 @@ async function fetchAndApplyAllPatches(assetDetails) {
265265

266266
/******************************************************************************/
267267

268-
const bc = new globalThis.BroadcastChannel('diffUpdater');
268+
const self = globalThis;
269269

270-
bc.onmessage = ev => {
270+
self.onmessage = ev => {
271271
const message = ev.data || {};
272272
switch ( message.what ) {
273273
case 'update':
274274
fetchAndApplyAllPatches(message).then(response => {
275-
bc.postMessage(response);
275+
self.postMessage(response);
276276
}).catch(error => {
277-
bc.postMessage({ what: 'broken', error });
277+
self.postMessage({ what: 'broken', error });
278278
});
279279
break;
280280
}
281281
};
282282

283-
bc.postMessage({ what: 'ready' });
283+
self.postMessage({ what: 'ready' });
284284

285285
/******************************************************************************/

0 commit comments

Comments
 (0)