Unverified Commit 1c1c273b authored by Pavel Mazhnik's avatar Pavel Mazhnik Committed by GitHub

[web] remove unnecessary awaits from flutter.js (#130204)

Fixed types for `_getNewServiceWorker` and `_waitForServiceWorkerActivation` functions.  
These functions currently expect a Promise as an argument, but we're actually passing in an already resolved value:
```js
.then(this._getNewServiceWorker)
.then(this._waitForServiceWorkerActivation);
```
parent 847d1f04
...@@ -156,53 +156,46 @@ _flutter.loader = null; ...@@ -156,53 +156,46 @@ _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 * This might return the current service worker, if there's no new service worker
* awaiting to be installed/updated. * awaiting to be installed/updated.
* *
* @param {Promise<ServiceWorkerRegistration>} serviceWorkerRegistrationPromise * @param {ServiceWorkerRegistration} serviceWorkerRegistration
* @returns {Promise<ServiceWorker>} * @returns {Promise<ServiceWorker>}
*/ */
async _getNewServiceWorker(serviceWorkerRegistrationPromise) { async _getNewServiceWorker(serviceWorkerRegistration) {
const reg = await serviceWorkerRegistrationPromise; if (!serviceWorkerRegistration.active && (serviceWorkerRegistration.installing || serviceWorkerRegistration.waiting)) {
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing // No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate. // one for the first time. Simply wait for it to activate.
console.debug("Installing/Activating first service worker."); console.debug("Installing/Activating first service worker.");
return reg.installing || reg.waiting; return serviceWorkerRegistration.installing || serviceWorkerRegistration.waiting;
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) { } else if (!serviceWorkerRegistration.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we // When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update. // need to ask the service worker to update.
return reg.update().then((newReg) => { const newRegistration = await serviceWorkerRegistration.update();
console.debug("Updating service worker."); console.debug("Updating service worker.");
return newReg.installing || newReg.waiting || newReg.active; return newRegistration.installing || newRegistration.waiting || newRegistration.active;
});
} else { } else {
console.debug("Loading from existing service worker."); 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". * state to "activated".
* *
* @param {Promise<ServiceWorker>} latestServiceWorkerPromise * @param {ServiceWorker} serviceWorker
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async _waitForServiceWorkerActivation(latestServiceWorkerPromise) { async _waitForServiceWorkerActivation(serviceWorker) {
const serviceWorker = await latestServiceWorkerPromise;
if (!serviceWorker || serviceWorker.state == "activated") { if (!serviceWorker || serviceWorker.state == "activated") {
if (!serviceWorker) { if (!serviceWorker) {
return Promise.reject( throw new Error("Cannot activate a null service worker!");
new Error("Cannot activate a null service worker!")
);
} else { } else {
console.debug("Service worker already active."); console.debug("Service worker already active.");
return Promise.resolve(); return;
} }
} }
return new Promise((resolve, _) => { return new Promise((resolve, _) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment