This commit is contained in:
kphanipavan
2024-01-27 02:07:11 +00:00
parent 883f5b89c9
commit 52cf6b6b74
19 changed files with 51917 additions and 50628 deletions

View File

@@ -144,7 +144,7 @@ _flutter.loader = null;
const serviceWorkerActivation = navigator.serviceWorker
.register(url)
.then(this._getNewServiceWorker)
.then((serviceWorkerRegistration) => this._getNewServiceWorker(serviceWorkerRegistration, serviceWorkerVersion))
.then(this._waitForServiceWorkerActivation);
// Timeout race promise
@@ -156,53 +156,47 @@ _flutter.loader = null;
}
/**
* Returns the latest service worker for the given `serviceWorkerRegistrationPromise`.
* Returns the latest service worker for the given `serviceWorkerRegistration`.
*
* This might return the current service worker, if there's no new service worker
* awaiting to be installed/updated.
*
* @param {Promise<ServiceWorkerRegistration>} serviceWorkerRegistrationPromise
* @param {ServiceWorkerRegistration} serviceWorkerRegistration
* @param {String} serviceWorkerVersion
* @returns {Promise<ServiceWorker>}
*/
async _getNewServiceWorker(serviceWorkerRegistrationPromise) {
const reg = await serviceWorkerRegistrationPromise;
if (!reg.active && (reg.installing || reg.waiting)) {
async _getNewServiceWorker(serviceWorkerRegistration, serviceWorkerVersion) {
if (!serviceWorkerRegistration.active && (serviceWorkerRegistration.installing || serviceWorkerRegistration.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
console.debug("Installing/Activating first service worker.");
return reg.installing || reg.waiting;
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
return serviceWorkerRegistration.installing || serviceWorkerRegistration.waiting;
} else if (!serviceWorkerRegistration.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
return reg.update().then((newReg) => {
console.debug("Updating service worker.");
return newReg.installing || newReg.waiting || newReg.active;
});
const newRegistration = await serviceWorkerRegistration.update();
console.debug("Updating service worker.");
return newRegistration.installing || newRegistration.waiting || newRegistration.active;
} else {
console.debug("Loading from existing service worker.");
return reg.active;
return serviceWorkerRegistration.active;
}
}
/**
* Returns a Promise that resolves when the `latestServiceWorker` changes its
* Returns a Promise that resolves when the `serviceWorker` changes its
* state to "activated".
*
* @param {Promise<ServiceWorker>} latestServiceWorkerPromise
* @param {ServiceWorker} serviceWorker
* @returns {Promise<void>}
*/
async _waitForServiceWorkerActivation(latestServiceWorkerPromise) {
const serviceWorker = await latestServiceWorkerPromise;
async _waitForServiceWorkerActivation(serviceWorker) {
if (!serviceWorker || serviceWorker.state == "activated") {
if (!serviceWorker) {
return Promise.reject(
new Error("Cannot activate a null service worker!")
);
throw new Error("Cannot activate a null service worker!");
} else {
console.debug("Service worker already active.");
return Promise.resolve();
return;
}
}
return new Promise((resolve, _) => {