From 45feb10c4640a7bacb031c70d68334a9ba315e7e Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Fri, 25 May 2018 14:37:46 +0300 Subject: [PATCH] refactor(service-worker): avoid unnecessary operations and remove unused code (#24127) PR Close #24127 --- packages/service-worker/worker/src/driver.ts | 33 +++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/service-worker/worker/src/driver.ts b/packages/service-worker/worker/src/driver.ts index 6814053df9..1dc99259b3 100644 --- a/packages/service-worker/worker/src/driver.ts +++ b/packages/service-worker/worker/src/driver.ts @@ -225,16 +225,21 @@ export class Driver implements Debuggable, UpdateSource { // Initialization is the only event which is sent directly from the SW to itself, // and thus `event.source` is not a Client. Handle it here, before the check // for Client sources. - if (data.action === 'INITIALIZE' && this.initialized === null) { - // Initialize the SW. - this.initialized = this.initialize(); + if (data.action === 'INITIALIZE') { + // Only initialize if not already initialized (or initializing). + if (this.initialized === null) { + // Initialize the SW. + this.initialized = this.initialize(); - // Wait until initialization is properly scheduled, then trigger idle - // events to allow it to complete (assuming the SW is idle). - event.waitUntil((async() => { - await this.initialized; - await this.idle.trigger(); - })()); + // Wait until initialization is properly scheduled, then trigger idle + // events to allow it to complete (assuming the SW is idle). + event.waitUntil((async() => { + await this.initialized; + await this.idle.trigger(); + })()); + } + + return; } // Only messages from true clients are accepted past this point (this is essentially @@ -725,16 +730,6 @@ export class Driver implements Debuggable, UpdateSource { private async setupUpdate(manifest: Manifest, hash: string): Promise { const newVersion = new AppVersion(this.scope, this.adapter, this.db, this.idle, manifest, hash); - // Try to determine a version that's safe to update from. - let updateFrom: AppVersion|undefined = undefined; - - // It's always safe to update from a version, even a broken one, as it will still - // only have valid resources cached. If there is no latest version, though, this - // update will have to install as a fresh version. - if (this.latestHash !== null) { - updateFrom = this.versions.get(this.latestHash); - } - // Firstly, check if the manifest version is correct. if (manifest.configVersion !== SUPPORTED_CONFIG_VERSION) { await this.deleteAllCaches();