Use onmessage/postMessage instead of BroadcastChannel in diff updater

This commit is contained in:
Raymond Hill 2025-02-28 17:18:48 -05:00
parent 95b99ef4ac
commit ea8853cda3
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
2 changed files with 10 additions and 12 deletions

View file

@ -1244,10 +1244,8 @@ async function diffUpdater() {
ubolog('Diff updater: cycle start'); ubolog('Diff updater: cycle start');
return new Promise(resolve => { return new Promise(resolve => {
let pendingOps = 0; let pendingOps = 0;
const bc = new globalThis.BroadcastChannel('diffUpdater');
const terminate = error => { const terminate = error => {
worker.terminate(); worker.terminate();
bc.close();
resolve(); resolve();
if ( typeof error !== 'string' ) { return; } if ( typeof error !== 'string' ) { return; }
ubolog(`Diff updater: terminate because ${error}`); ubolog(`Diff updater: terminate because ${error}`);
@ -1260,14 +1258,15 @@ async function diffUpdater() {
if ( metadata.diffPath === data.patchPath ) { return; } if ( metadata.diffPath === data.patchPath ) { return; }
assetCacheSetDetails(data.assetKey, metadata); assetCacheSetDetails(data.assetKey, metadata);
}; };
bc.onmessage = ev => { const worker = new Worker('js/diff-updater.js');
worker.onmessage = ev => {
const data = ev.data || {}; const data = ev.data || {};
if ( data.what === 'ready' ) { if ( data.what === 'ready' ) {
ubolog('Diff updater: hard updating', toHardUpdate.map(v => v.assetKey).join()); ubolog('Diff updater: hard updating', toHardUpdate.map(v => v.assetKey).join());
while ( toHardUpdate.length !== 0 ) { while ( toHardUpdate.length !== 0 ) {
const assetDetails = toHardUpdate.shift(); const assetDetails = toHardUpdate.shift();
assetDetails.fetch = true; assetDetails.fetch = true;
bc.postMessage(assetDetails); worker.postMessage(assetDetails);
pendingOps += 1; pendingOps += 1;
} }
return; return;
@ -1284,7 +1283,7 @@ async function diffUpdater() {
data.text = result.content || ''; data.text = result.content || '';
data.status = undefined; data.status = undefined;
checkAndCorrectDiffPath(data); checkAndCorrectDiffPath(data);
bc.postMessage(data); worker.postMessage(data);
}); });
return; return;
} }
@ -1320,7 +1319,7 @@ async function diffUpdater() {
if ( pendingOps === 0 && toSoftUpdate.length !== 0 ) { if ( pendingOps === 0 && toSoftUpdate.length !== 0 ) {
ubolog('Diff updater: soft updating', toSoftUpdate.map(v => v.assetKey).join()); ubolog('Diff updater: soft updating', toSoftUpdate.map(v => v.assetKey).join());
while ( toSoftUpdate.length !== 0 ) { while ( toSoftUpdate.length !== 0 ) {
bc.postMessage(toSoftUpdate.shift()); worker.postMessage(toSoftUpdate.shift());
pendingOps += 1; pendingOps += 1;
} }
} }
@ -1328,7 +1327,6 @@ async function diffUpdater() {
ubolog('Diff updater: cycle complete'); ubolog('Diff updater: cycle complete');
terminate(); terminate();
}; };
const worker = new Worker('js/diff-updater.js');
}).catch(reason => { }).catch(reason => {
ubolog(`Diff updater: ${reason}`); ubolog(`Diff updater: ${reason}`);
}); });

View file

@ -265,21 +265,21 @@ async function fetchAndApplyAllPatches(assetDetails) {
/******************************************************************************/ /******************************************************************************/
const bc = new globalThis.BroadcastChannel('diffUpdater'); const self = globalThis;
bc.onmessage = ev => { self.onmessage = ev => {
const message = ev.data || {}; const message = ev.data || {};
switch ( message.what ) { switch ( message.what ) {
case 'update': case 'update':
fetchAndApplyAllPatches(message).then(response => { fetchAndApplyAllPatches(message).then(response => {
bc.postMessage(response); self.postMessage(response);
}).catch(error => { }).catch(error => {
bc.postMessage({ what: 'broken', error }); self.postMessage({ what: 'broken', error });
}); });
break; break;
} }
}; };
bc.postMessage({ what: 'ready' }); self.postMessage({ what: 'ready' });
/******************************************************************************/ /******************************************************************************/