From f8aa1905587c09f475ac61faeeb7389b309196c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Wed, 25 Aug 2021 19:43:21 +0000 Subject: [PATCH] service-worker.js linted (left only no-unused-vars errors) --- service-worker.js | 63 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/service-worker.js b/service-worker.js index 831008d..03dd10c 100644 --- a/service-worker.js +++ b/service-worker.js @@ -113,9 +113,9 @@ try { // load a plugin self.importScripts(`./plugins/${plugins[i]}.js`) // check if it loaded properly - var plugin = LibResilientPlugins.find(p=>p.name===plugins[i]) + var plugin = self.LibResilientPlugins.find(p=>p.name===plugins[i]) if (plugin === undefined) { - throw new Error(`Plugin not found: ${plugins[i]} (available plugins: ${LibResilientPlugins.map(p=>p.name).join(', ')})`) + throw new Error(`Plugin not found: ${plugins[i]} (available plugins: ${self.LibResilientPlugins.map(p=>p.name).join(', ')})`) } // make sure that the indirect flag is set if needed if (self.LibResilientConfig.plugins[plugin.name].indirect===true) { @@ -127,7 +127,7 @@ try { // make sure plugins used by the just-loaded plugin are also added to the list // but don't load a plugin twice if (typeof plugin.uses !== "undefined") { - for (p in plugin.uses) { + for (var p in plugin.uses) { if (plugins.indexOf(p) < 0) { // okay, this plugin has not been added to the plugins list yet // let's do that @@ -145,7 +145,7 @@ try { } // inform - self.log('service-worker', `DEBUG: Strategy in use: ${LibResilientPlugins.filter(p=>(!p.indirect)).map(p=>p.name).join(', ')}`) + self.log('service-worker', `DEBUG: Strategy in use: ${self.LibResilientPlugins.filter(p=>(!p.indirect)).map(p=>p.name).join(', ')}`) } catch(e) { // we only get a cryptic "Error while registering a service worker" @@ -176,7 +176,7 @@ let decrementActiveFetches = (clientId) => { // client has to be smart enough to know if that is just temporary // (and new fetches will fire in a moment, because a CSS file just // got fetched) or not - clients.get(clientId).then((client)=>{ + self.clients.get(clientId).then((client)=>{ client.postMessage({ allFetched: true }) @@ -227,31 +227,32 @@ let promiseTimeout = (time, timeout_resolves=false, error_message=false) => { * service worker restarts, if that's required */ let LibResilientResourceInfo = class { - - // actual values of the fields - // only used internally, and stored into the Indexed DB - values = { - url: '', // read only after initialization - clientId: null, - fetchError: null, - method: null, - state: null, // can be "error", "success", "running" - serviceWorker: 'COMMIT_UNKNOWN' // this will be replaced by commit sha in CI/CD; read-only - } - client = null; /** * constructor * needed to set the URL and clientId */ constructor(url, clientId) { + + // actual values of the fields + // only used internally, and stored into the Indexed DB + this.values = { + url: '', // read only after initialization + clientId: null, + fetchError: null, + method: null, + state: null, // can be "error", "success", "running" + serviceWorker: 'COMMIT_UNKNOWN' // this will be replaced by commit sha in CI/CD; read-only + } + this.client = null; + // set it this.values.url = url this.values.clientId = clientId // we might not have a non-empty clientId if it's a cross-origin fetch if (clientId) { // get the client from Client API based on clientId - clients.get(clientId).then((client)=>{ + self.clients.get(clientId).then((client)=>{ // set the client this.client = client // Send a message to the client. @@ -378,11 +379,11 @@ let libresilientFetch = (plugin, url, reqInfo) => { */ let callOnLibResilientPlugin = (call, args) => { // find the first plugin implementing the method - for (i=0; i{ + var LibResilientPluginsRun = self.LibResilientPlugins.filter((plugin)=>{ return ( (!plugin.indirect) && (useStashed || typeof plugin.stash !== 'function') ) }) @@ -459,7 +460,7 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas reqInfo.update({state:"success"}) // get the plugin that was used to fetch content - plugin = LibResilientPlugins.find(p=>p.name===reqInfo.method) + plugin = self.LibResilientPlugins.find(p=>p.name===reqInfo.method) // if it's a stashing plugin... if (typeof plugin.stash === 'function') { @@ -495,7 +496,7 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas || ( stashedResponse.headers.get('X-LibResilient-ETag') !== response.headers.get('X-LibResilient-ETag') ) ) { // inform! self.log('service-worker', 'fetched version method or ETag differs from stashed for:', url) - clients.get(reqInfo.clientId).then((client)=>{ + self.clients.get(reqInfo.clientId).then((client)=>{ client.postMessage({ url: url, fetchedDiffers: true @@ -507,8 +508,8 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas // do we want to stash? if (doStash) { // find the first stashing plugin - for (i=0; i{ // original response will be needed further down return response @@ -591,12 +592,12 @@ self.addEventListener('fetch', event => { // so let's also send the plugin list, why not // // *sigh* JS is great *sigh* - clients + self.clients .get(clientId) .then((client)=>{ client.postMessage({ clientId: clientId, - plugins: LibResilientPlugins.filter(p=>(!p.indirect)).map((p)=>{return p.name}), + plugins: self.LibResilientPlugins.filter(p=>(!p.indirect)).map((p)=>{return p.name}), serviceWorker: 'COMMIT_UNKNOWN' }) })