summaryrefslogtreecommitdiff
path: root/app/sw.src.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2023-02-23 19:34:56 +0100
committerMinteck <contact@minteck.org>2023-02-23 19:34:56 +0100
commit3d1cd02f27518f1a04374c7c8320cd5d82ede6e9 (patch)
tree75be5fba4368472fb11c8015aee026b2b9a71888 /app/sw.src.js
parent8cc1f13c17fa2fb5a4410542d39e650e02945634 (diff)
downloadpluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.tar.gz
pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.tar.bz2
pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.zip
Updated 40 files, added 37 files, deleted 1103 files and renamed 3905 files (automated)
Diffstat (limited to 'app/sw.src.js')
-rw-r--r--app/sw.src.js110
1 files changed, 0 insertions, 110 deletions
diff --git a/app/sw.src.js b/app/sw.src.js
deleted file mode 100644
index 243e425..0000000
--- a/app/sw.src.js
+++ /dev/null
@@ -1,110 +0,0 @@
-// 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();
- }
-} \ No newline at end of file