(function () { const DB_NAME = "md-hub-cache"; const DB_VERSION = 1; const STORE_DOCUMENTS = "documents"; const STORE_CONTENT = "content"; function openDB() { return new Promise(function (resolve, reject) { const request = indexedDB.open(DB_NAME, DB_VERSION); request.onerror = function () { reject(request.error); }; request.onsuccess = function () { resolve(request.result); }; request.onupgradeneeded = function (event) { const db = event.target.result; if (!db.objectStoreNames.contains(STORE_DOCUMENTS)) { db.createObjectStore(STORE_DOCUMENTS, { keyPath: "path" }); } if (!db.objectStoreNames.contains(STORE_CONTENT)) { db.createObjectStore(STORE_CONTENT, { keyPath: "path" }); } }; }); } function cacheDocuments(documents) { return openDB().then(function (db) { return new Promise(function (resolve, reject) { const tx = db.transaction(STORE_DOCUMENTS, "readwrite"); const store = tx.objectStore(STORE_DOCUMENTS); documents.forEach(function (doc) { store.put(doc); }); tx.oncomplete = function () { resolve(); }; tx.onerror = function () { reject(tx.error); }; }); }); } function cacheContent(path, html, title) { return openDB().then(function (db) { return new Promise(function (resolve, reject) { const tx = db.transaction(STORE_CONTENT, "readwrite"); const store = tx.objectStore(STORE_CONTENT); store.put({ path: path, html: html, title: title, cachedAt: Date.now() }); tx.oncomplete = function () { resolve(); }; tx.onerror = function () { reject(tx.error); }; }); }); } function getCachedDocuments() { return openDB().then(function (db) { return new Promise(function (resolve, reject) { const tx = db.transaction(STORE_DOCUMENTS, "readonly"); const store = tx.objectStore(STORE_DOCUMENTS); const request = store.getAll(); request.onsuccess = function () { resolve(request.result); }; request.onerror = function () { reject(request.error); }; }); }); } function getCachedContent(path) { return openDB().then(function (db) { return new Promise(function (resolve, reject) { const tx = db.transaction(STORE_CONTENT, "readonly"); const store = tx.objectStore(STORE_CONTENT); const request = store.get(path); request.onsuccess = function () { resolve(request.result); }; request.onerror = function () { reject(request.error); }; }); }); } window.MDHubCache = { cacheDocuments: cacheDocuments, cacheContent: cacheContent, getCachedDocuments: getCachedDocuments, getCachedContent: getCachedContent, }; })();