summaryrefslogtreecommitdiff
path: root/app/sw.src.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-08-21 17:31:56 +0200
committerMinteck <contact@minteck.org>2022-08-21 17:31:56 +0200
commita2df9a69dcc14cb70118cda2ded499055e7ee358 (patch)
tree6dd283e4e9452d38bce81ddaaae49b5335755842 /app/sw.src.js
parent84dd0735820b16b60f600284d35183d76547a71f (diff)
downloadpluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.tar.gz
pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.tar.bz2
pluralconnect-a2df9a69dcc14cb70118cda2ded499055e7ee358.zip
m. update
Diffstat (limited to 'app/sw.src.js')
-rw-r--r--app/sw.src.js110
1 files changed, 110 insertions, 0 deletions
diff --git a/app/sw.src.js b/app/sw.src.js
new file mode 100644
index 0000000..243e425
--- /dev/null
+++ b/app/sw.src.js
@@ -0,0 +1,110 @@
+// 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