[add] service worker & offline reader
This commit is contained in:
Vendored
+1
File diff suppressed because one or more lines are too long
@@ -0,0 +1,50 @@
|
||||
function _slicedToArray(t,n){return _arrayWithHoles(t)||_iterableToArrayLimit(t,n)||_unsupportedIterableToArray(t,n)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,n){if(t){if("string"==typeof t)return _arrayLikeToArray(t,n);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(t,n):void 0}}function _arrayLikeToArray(t,n){(null==n||n>t.length)&&(n=t.length);for(var r=0,e=new Array(n);r<n;r++)e[r]=t[r];return e}function _iterableToArrayLimit(t,n){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var e,o,u=[],i=!0,a=!1;try{for(r=r.call(t);!(i=(e=r.next()).done)&&(u.push(e.value),!n||u.length!==n);i=!0);}catch(t){a=!0,o=t}finally{try{i||null==r.return||r.return()}finally{if(a)throw o}}return u}}function _arrayWithHoles(t){if(Array.isArray(t))return t}function _typeof(t){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},_typeof(t)}!function(t,n){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).idbKeyval={})}(this,(function(t){"use strict";function n(t){return new Promise((function(n,r){t.oncomplete=t.onsuccess=function(){return n(t.result)},t.onabort=t.onerror=function(){return r(t.error)}}))}function r(t,r){var e=indexedDB.open(t);e.onupgradeneeded=function(){return e.result.createObjectStore(r)};var o=n(e);return function(t,n){return o.then((function(e){return n(e.transaction(r,t).objectStore(r))}))}}var e;function o(){return e||(e=r("keyval-store","keyval")),e}function u(t,r){return t.openCursor().onsuccess=function(){this.result&&(r(this.result),this.result.continue())},n(t.transaction)}t.clear=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:o();return t("readwrite",(function(t){return t.clear(),n(t.transaction)}))},t.createStore=r,t.del=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o();return r("readwrite",(function(r){return r.delete(t),n(r.transaction)}))},t.delMany=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o();return r("readwrite",(function(r){return t.forEach((function(t){return r.delete(t)})),n(r.transaction)}))},t.entries=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:o();return t("readonly",(function(r){if(r.getAll&&r.getAllKeys)return Promise.all([n(r.getAllKeys()),n(r.getAll())]).then((function(t){var n=_slicedToArray(t,2),r=n[0],e=n[1];return r.map((function(t,n){return[t,e[n]]}))}));var e=[];return t("readonly",(function(t){return u(t,(function(t){return e.push([t.key,t.value])})).then((function(){return e}))}))}))},t.get=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o();return r("readonly",(function(r){return n(r.get(t))}))},t.getMany=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o();return r("readonly",(function(r){return Promise.all(t.map((function(t){return n(r.get(t))})))}))},t.keys=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:o();return t("readonly",(function(t){if(t.getAllKeys)return n(t.getAllKeys());var r=[];return u(t,(function(t){return r.push(t.key)})).then((function(){return r}))}))},t.promisifyRequest=n,t.set=function(t,r){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:o();return e("readwrite",(function(e){return e.put(r,t),n(e.transaction)}))},t.setMany=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o();return r("readwrite",(function(r){return t.forEach((function(t){return r.put(t[1],t[0])})),n(r.transaction)}))},t.update=function(t,r){var e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:o();return e("readwrite",(function(e){return new Promise((function(o,u){e.get(t).onsuccess=function(){try{e.put(r(this.result),t),o(n(e.transaction))}catch(t){u(t)}}}))}))},t.values=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:o();return t("readonly",(function(t){if(t.getAll)return n(t.getAll());var r=[];return u(t,(function(t){return r.push(t.value)})).then((function(){return r}))}))},Object.defineProperty(t,"__esModule",{value:!0})}));
|
||||
|
||||
/**
|
||||
* Custom IDB Convenience Functions Wrapper
|
||||
**/
|
||||
const IDB = (function () {
|
||||
let { get, del, entries, update, keys } = idbKeyval;
|
||||
|
||||
return {
|
||||
async set(key, newValue) {
|
||||
let changeObj = {};
|
||||
await update(key, (oldValue) => {
|
||||
if (oldValue != null) changeObj.oldValue = oldValue;
|
||||
changeObj.newValue = newValue;
|
||||
return newValue;
|
||||
});
|
||||
return changeObj;
|
||||
},
|
||||
|
||||
get(key, defaultValue) {
|
||||
return get(key).then((resp) => {
|
||||
return defaultValue && resp == null ? defaultValue : resp;
|
||||
});
|
||||
},
|
||||
|
||||
del(key) {
|
||||
return del(key);
|
||||
},
|
||||
|
||||
find(keyRegExp, includeValues = false) {
|
||||
if (!(keyRegExp instanceof RegExp)) throw new Error("Invalid RegExp");
|
||||
|
||||
if (!includeValues)
|
||||
return keys().then((allKeys) =>
|
||||
allKeys.filter((key) => keyRegExp.test(key))
|
||||
);
|
||||
|
||||
return entries().then((allItems) => {
|
||||
const matchingKeys = allItems.filter((keyVal) =>
|
||||
keyRegExp.test(keyVal[0])
|
||||
);
|
||||
return matchingKeys.reduce((obj, keyVal) => {
|
||||
const [key, val] = keyVal;
|
||||
obj[key] = val;
|
||||
return obj;
|
||||
}, {});
|
||||
});
|
||||
},
|
||||
};
|
||||
})();
|
||||
Vendored
+15
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,60 @@
|
||||
const SW = (function () {
|
||||
// Helper Function
|
||||
function randomID() {
|
||||
return "00000000000000000000000000000000".replace(/[018]/g, (c) =>
|
||||
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))))
|
||||
.toString(16)
|
||||
.toUpperCase()
|
||||
);
|
||||
}
|
||||
|
||||
// Variables
|
||||
let swInstance = null;
|
||||
let outstandingMessages = {};
|
||||
|
||||
navigator.serviceWorker.addEventListener("message", ({ data }) => {
|
||||
let { id } = data;
|
||||
data = data.data;
|
||||
|
||||
console.log("[SW] Received Message:", { id, data });
|
||||
if (!outstandingMessages[id])
|
||||
return console.warn("[SW] Invalid Outstanding Message:", { id, data });
|
||||
|
||||
outstandingMessages[id](data);
|
||||
delete outstandingMessages[id];
|
||||
});
|
||||
|
||||
async function install() {
|
||||
// Register Service Worker
|
||||
swInstance = await navigator.serviceWorker.register("/sw.js");
|
||||
swInstance.onupdatefound = (data) =>
|
||||
console.log("[SW.install] Update Found:", data);
|
||||
|
||||
// Wait for Registration / Update
|
||||
let serviceWorker =
|
||||
swInstance.installing || swInstance.waiting || swInstance.active;
|
||||
|
||||
// Await Installation
|
||||
await new Promise((resolve) => {
|
||||
serviceWorker.onstatechange = (data) => {
|
||||
console.log("[SW.install] State Change:", serviceWorker.state);
|
||||
if (serviceWorker.state == "activated") resolve();
|
||||
};
|
||||
if (serviceWorker.state == "activated") resolve();
|
||||
});
|
||||
}
|
||||
|
||||
function send(data) {
|
||||
if (!swInstance?.active) return Promise.reject("Inactive Service Worker");
|
||||
let id = randomID();
|
||||
|
||||
let msgPromise = new Promise((resolve) => {
|
||||
outstandingMessages[id] = resolve;
|
||||
});
|
||||
|
||||
swInstance.active.postMessage({ id, data });
|
||||
return msgPromise;
|
||||
}
|
||||
|
||||
return { install, send };
|
||||
})();
|
||||
Reference in New Issue
Block a user