UNPKG

@jorsek/ezd-client

Version:
98 lines 4.11 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const p_limit_1 = require("p-limit"); const workbox_window_1 = require("workbox-window"); exports.getOfflineStatus = () => { const val = window.localStorage.getItem("prefetch_status"); if (val === "complete") { return "COMPLETE"; } else { return "INCOMPLETE"; } }; exports.prefetchAll = ({ status_callback, client, parallelism = 10 }) => __awaiter(this, void 0, void 0, function* () { if (exports.getOfflineStatus() === "COMPLETE") { status_callback("Ready"); return; } const do_fetch = () => __awaiter(this, void 0, void 0, function* () { const paths = ["/"]; const hidden_fetcher = document.createElement("div"); hidden_fetcher.hidden = true; const limit = p_limit_1.default(parallelism); const sections = yield client.content.getSections(); let processed = 0; let total = 0; const section_promise_map = {}; const loadLeaf = (section, leaf) => __awaiter(this, void 0, void 0, function* () { try { const content = yield client.content.getContent(leaf.href); paths.push(`/content/${leaf.href}`); hidden_fetcher.innerHTML = content.content; processed++; status_callback(`Processing ${section}: ${processed}/${total}`); } catch (e) { // womp womp, still try and load stuff anyway } }); const handleTree = (section, tree) => { if (!section_promise_map[section]) { section_promise_map[section] = []; } section_promise_map[section].push(limit(() => loadLeaf(section, tree))); total++; for (const child of tree.children) { handleTree(section, child); } }; for (const section of sections) { const navTree = yield client.content.getNavTree(section.href); if (!section_promise_map[section.title]) { section_promise_map[section.title] = []; } section_promise_map[section.title].push(limit(() => loadLeaf(section.title, { href: section.href }))); total++; handleTree(section.title, navTree); } for (const [section_name, promises] of Object.entries(section_promise_map)) { yield Promise.all(promises); } return paths; // status_callback("Offline Ready!"); }); const wb = new workbox_window_1.Workbox("/service-worker.js"); wb.addEventListener("installed", (event) => { // @ts-ignore if (!event.isUpdate) { status_callback("First Service Worker load!"); window.location.reload(); } }); status_callback("Waiting for sw to control page..."); // tslint:disable-next-line: no-floating-promises wb.register(); yield wb.controlling; const urlsToCache = yield do_fetch(); status_callback(`Prefetching ${urlsToCache.length} content pages...`); // @ts-ignore const success = yield wb.messageSW({ type: "CACHE_URLS", payload: { urlsToCache, }, }); status_callback(`Prefetch ${success ? "success" : "failure"}`); window.localStorage.setItem("prefetch_status", "complete"); status_callback("All set!"); }); //# sourceMappingURL=offlineManager.js.map