// noinspection JSUnresolvedVariable let cacheName = 'peh-pluralponies-pwa'; let filesToCache = [["%CacheData%"]]; self.addEventListener('install', function(e) { e.waitUntil( caches.open(cacheName).then(function(cache) { return cache.addAll(filesToCache); }) ); }); self.addEventListener('fetch', function(e) { e.respondWith( caches.match(e.request).then(function(response) { return response || fetch(e.request); }) ); }); self.addEventListener('sync', function(e) { if (e.tag === 'data-sync') { e.waitUntil(repeatRefresh()); } }) self.addEventListener('periodicsync', function(e) { if (e.tag === 'data-sync') { e.waitUntil(repeatRefresh()); } }) function sleep(ms) { return new Promise((res, _rej) => { setTimeout(res, ms); }) } function repeatRefresh() { return new Promise(async (_res, _rej) => { await repeatRefreshInternal(); }) } async function repeatRefreshInternal() { await refresh(); sleep(300000).then(async () => { await repeatRefreshInternal(); }); } async function fetchPlus(resource, options = {}) { const { timeout = 8000 } = options; const controller = new AbortController(); const id = setTimeout(() => controller.abort(), timeout); const response = await fetch(resource, { ...options, signal: controller.signal }); clearTimeout(id); return response; } function refresh() { return new Promise(async (res, rej) => { let valuesToGet = JSON.parse(await localforage.getItem("values-to-get")); let keys = Object.keys(valuesToGet); let index = 2; await getNewValue(res, keys, index, valuesToGet); }) } async function getNewValue(res, keys, index, valuesToGet) { if (!keys[0]) { await localforage.setItem("refresh", new Date().toISOString()); console.log("Done refreshing in the background at " + new Date().toISOString()) res(); return; } try { await localforage.setItem(keys[0], (await (await fetchPlus(valuesToGet[keys[0]]["url"], { timeout: 3000 })).text())); keys.shift(); if (!keys[0]) { await localforage.setItem("refresh", new Date().toISOString()); console.log("Done refreshing in the background at " + new Date().toISOString()) res(); return; } setTimeout(async () => { index++; await getNewValue(res, keys, index, valuesToGet); }, valuesToGet[keys[0]]["limited"] ? 550 : 0); } catch (e) { for (let key of Object.keys(valuesToGet)) { if (await localforage.getItem(key) === null) { throw new Error("App requested key '" + key + "' but it can't be retrieved at the moment"); } } await localforage.setItem("refresh", new Date().toISOString()); console.log("Done refreshing in the background at " + new Date().toISOString()) res(); } }