Skip to content

Commit

Permalink
Simplify messaging logic and add acknowledgement
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaifroid committed Nov 16, 2023
1 parent 1586290 commit 9572170
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
6 changes: 6 additions & 0 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ self.addEventListener('message', function (event) {
if (event.data.action === 'init') {
// On 'init' message, we enable the fetchEventListener
fetchCaptureEnabled = true;
// Acdknowledge the init message to all clients
self.clients.matchAll().then(function (clientList) {
clientList.forEach(function (client) {
client.postMessage({ action: 'acknowledge' });
});
});
} else if (event.data.action === 'disable') {
// On 'disable' message, we disable the fetchEventListener
// Note that this code doesn't currently run because the app currently never sends a 'disable' message
Expand Down
46 changes: 26 additions & 20 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,47 +795,55 @@ function initServiceWorkerMessaging () {
if (params.contentInjectionMode === 'serviceworker') {
// Create a message listener
navigator.serviceWorker.onmessage = function (event) {
if (!selectedArchive) {
console.warn('Message from SW received, but no archive is selected!');
return;
}
if (event.data.error) {
console.error('Error in MessageChannel', event.data.error);
throw event.data.error;
}
if (event.data.action === 'askForContent') {
} else if (event.data.action === 'acknowledge') {
// The Service Worker is acknowledging receipt of init message
console.log('SW acknowledged init message');
initServiceWorkerHandle = true;
} else if (event.data.action === 'askForContent') {
// The Service Worker is asking for content. Check we have a loaded ZIM in this instance.
// DEV: This can happen if there are various instances of the app open in different tabs or windows, and no archive has been selected in this instance.
if (!selectedArchive) {
console.warn('Message from SW received, but no archive is selected!');
return;
}
// See below for explanation of this exception
const videoException = selectedArchive.zimType === 'zimit' && /\/\/youtubei.*player/.test(event.data.title);
// Check that the zimFileId in the messageChannel event data is the same as the one in the currently open archive
// Because the SW broadcasts its request to all open tabs or windows, we need to check that the request is for this instance
if (event.data.zimFileName !== selectedArchive.file.name) {
console.warn('SW request does not match this insstance', '[zimFileName:' + event.data.zimFileName + ' !== ' + selectedArchive.file.name + ']');
if (selectedArchive.zimType === 'zimit' && /\/\/youtubei.*player/.test(event.data.title)) {
if (event.data.zimFileName !== selectedArchive.file.name && !videoException) {
// Do nothing if the request is not for this instance
// console.debug('SW request does not match this instance', '[zimFileName:' + event.data.zimFileName + ' !== ' + selectedArchive.file.name + ']');
} else {
if (videoException) {
// DEV: This is a hack to allow YouTube videos to play in Zimit archives:
// Because links are embedded in a nested iframe, the SW cannot identify the top-level window from which to request the ZIM content
// Until we find a way to tell where it is coming from, we allow the request through and try to load the content
console.warn('>>> Allowing passthrough to process YouTube video <<<');
} else {
return;
// Until we find a way to tell where it is coming from, we allow the request through on all controlled clients and try to load the content
console.warn('>>> Allowing passthrough of SW request to process Zimit video <<<');
}
handleMessageChannelMessage(event);
}
handleMessageChannelMessage(event)
} else {
console.error('Invalid message received', event.data);
}
};
// Send the init message to the ServiceWorker
if (navigator.serviceWorker.controller) {
console.log('Initializing SW messaging...');
navigator.serviceWorker.controller.postMessage({
action: 'init'
});
} else if (initServiceWorkerHandle) {
} else if (!initServiceWorkerHandle) {
console.error('The Service Worker is active but is not controlling the current page! We have to reload.');
// Turn off failsafe, as this is a controlled reboot
settingsStore.setItem('lastPageLoad', 'rebooting', Infinity);
window.location.reload();
} else {
// If this is the first time we are initiating the SW, allow Promises to complete by delaying potential reload till next tick
console.debug('The Service Worker needs more time to load...');
initServiceWorkerHandle = setTimeout(initServiceWorkerMessaging, 0);
setTimeout(initServiceWorkerMessaging, 0);
}
}
}
Expand Down Expand Up @@ -1293,10 +1301,8 @@ function setLocalArchiveFromArchiveList () {
* Resets the CSS Cache (used only in jQuery mode)
*/
function resetCssCache () {
// Reset the cssCache. Must be done when archive changes.
if (selectedArchive.cssCache) {
selectedArchive.cssCache = new Map();
}
// Reset the cssCache if an archive is loaded
if (selectedArchive) selectedArchive.cssCache = new Map();
}

let webKitFileList = null
Expand Down

0 comments on commit 9572170

Please sign in to comment.