1
0
mirror of https://git.tuxpa.in/a/code-server.git synced 2024-12-26 12:15:26 +00:00

Merge pull request #4038 from sdissegna-maystreet/main

[#4013] backported https://github.com/microsoft/vscode/pull/131408
This commit is contained in:
Joe Previte 2021-08-25 10:29:02 -07:00 committed by GitHub
commit 15b39e6811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -242,7 +242,28 @@ const workerReady = new Promise(async (resolve, reject) => {
} }
}; };
navigator.serviceWorker.addEventListener('message', versionHandler); navigator.serviceWorker.addEventListener('message', versionHandler);
assertIsDefined(registration.active).postMessage({ channel: 'version' });
const postVersionMessage = () => {
assertIsDefined(navigator.serviceWorker.controller).postMessage({ channel: 'version' });
};
// At this point, either the service worker is ready and
// became our controller, or we need to wait for it.
// Note that navigator.serviceWorker.controller could be a
// controller from a previously loaded service worker.
const currentController = navigator.serviceWorker.controller;
if (currentController && currentController.scriptURL.endsWith(swPath)) {
// service worker already loaded & ready to receive messages
postVersionMessage();
} else {
// either there's no controlling service worker, or it's an old one:
// wait for it to change before posting the message
const onControllerChange = () => {
navigator.serviceWorker.removeEventListener('controllerchange', onControllerChange);
postVersionMessage();
};
navigator.serviceWorker.addEventListener('controllerchange', onControllerChange);
}
}, },
error => { error => {
reject(new Error(`Could not register service workers: ${error}.`)); reject(new Error(`Could not register service workers: ${error}.`));