vomebook commited on
Commit ·
b93fb73
1
Parent(s): 22f4bc7
Add cross-tab reader store notifications
Browse files- static/reader-store.js +11 -6
- static/reader.js +1 -0
static/reader-store.js
CHANGED
|
@@ -6,6 +6,11 @@
|
|
| 6 |
const MAX_ENTRIES = 200;
|
| 7 |
const SCHEMA_VERSION = 1;
|
| 8 |
let databasePromise = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
function openDatabase() {
|
| 11 |
if (databasePromise) return databasePromise;
|
|
@@ -86,7 +91,7 @@
|
|
| 86 |
};
|
| 87 |
};
|
| 88 |
};
|
| 89 |
-
tx.oncomplete = resolve;
|
| 90 |
tx.onerror = () => reject(tx.error);
|
| 91 |
tx.onabort = () => reject(tx.error);
|
| 92 |
});
|
|
@@ -107,15 +112,15 @@
|
|
| 107 |
});
|
| 108 |
}
|
| 109 |
|
| 110 |
-
async function remove(url) { await transaction("readwrite", (store) => store.delete(url)); }
|
| 111 |
-
async function clearHistory() { await transaction("readwrite", (store) => store.clear()); }
|
| 112 |
async function putBookmark(entry) {
|
| 113 |
entry = normalizeBookmarkEntry(entry); if (!entry) throw new TypeError("Invalid reader bookmark entry");
|
| 114 |
const database = await openDatabase();
|
| 115 |
await new Promise((resolve, reject) => {
|
| 116 |
const tx = database.transaction(BOOKMARK_STORE_NAME, "readwrite");
|
| 117 |
tx.objectStore(BOOKMARK_STORE_NAME).put(entry);
|
| 118 |
-
tx.oncomplete = resolve; tx.onerror = () => reject(tx.error); tx.onabort = () => reject(tx.error);
|
| 119 |
});
|
| 120 |
}
|
| 121 |
async function listBookmarks(url) {
|
|
@@ -151,8 +156,8 @@
|
|
| 151 |
await new Promise((resolve, reject) => {
|
| 152 |
const tx = database.transaction(BOOKMARK_STORE_NAME, "readwrite");
|
| 153 |
tx.objectStore(BOOKMARK_STORE_NAME).delete(id);
|
| 154 |
-
tx.oncomplete = resolve; tx.onerror = () => reject(tx.error); tx.onabort = () => reject(tx.error);
|
| 155 |
});
|
| 156 |
}
|
| 157 |
-
root.VoiceOfMLReaderStore = Object.freeze({ DB_NAME, MAX_ENTRIES, SCHEMA_VERSION, get, put, list, remove, clearHistory, putBookmark, listBookmarks, listAllBookmarks, removeBookmark });
|
| 158 |
})(typeof self !== "undefined" ? self : window);
|
|
|
|
| 6 |
const MAX_ENTRIES = 200;
|
| 7 |
const SCHEMA_VERSION = 1;
|
| 8 |
let databasePromise = null;
|
| 9 |
+
const subscribers = new Set();
|
| 10 |
+
const storeChannel = typeof root.BroadcastChannel === "function" ? new root.BroadcastChannel("voiceofml-reader-store-v1") : null;
|
| 11 |
+
if (storeChannel) storeChannel.onmessage = (event) => { for (const listener of subscribers) listener({ ...event.data, remote: true }); };
|
| 12 |
+
function notify(change) { try { storeChannel?.postMessage(change); } catch (_) {} for (const listener of subscribers) listener({ ...change, remote: false }); }
|
| 13 |
+
function subscribe(listener) { if (typeof listener !== "function") throw new TypeError("Reader store subscriber must be a function"); subscribers.add(listener); return () => subscribers.delete(listener); }
|
| 14 |
|
| 15 |
function openDatabase() {
|
| 16 |
if (databasePromise) return databasePromise;
|
|
|
|
| 91 |
};
|
| 92 |
};
|
| 93 |
};
|
| 94 |
+
tx.oncomplete = () => { notify({ type: "history", url: entry.url, lastReadAt: entry.lastReadAt }); resolve(); };
|
| 95 |
tx.onerror = () => reject(tx.error);
|
| 96 |
tx.onabort = () => reject(tx.error);
|
| 97 |
});
|
|
|
|
| 112 |
});
|
| 113 |
}
|
| 114 |
|
| 115 |
+
async function remove(url) { await transaction("readwrite", (store) => store.delete(url)); notify({ type: "history-remove", url }); }
|
| 116 |
+
async function clearHistory() { await transaction("readwrite", (store) => store.clear()); notify({ type: "history-clear" }); }
|
| 117 |
async function putBookmark(entry) {
|
| 118 |
entry = normalizeBookmarkEntry(entry); if (!entry) throw new TypeError("Invalid reader bookmark entry");
|
| 119 |
const database = await openDatabase();
|
| 120 |
await new Promise((resolve, reject) => {
|
| 121 |
const tx = database.transaction(BOOKMARK_STORE_NAME, "readwrite");
|
| 122 |
tx.objectStore(BOOKMARK_STORE_NAME).put(entry);
|
| 123 |
+
tx.oncomplete = () => { notify({ type: "bookmark", url: entry.url, id: entry.id }); resolve(); }; tx.onerror = () => reject(tx.error); tx.onabort = () => reject(tx.error);
|
| 124 |
});
|
| 125 |
}
|
| 126 |
async function listBookmarks(url) {
|
|
|
|
| 156 |
await new Promise((resolve, reject) => {
|
| 157 |
const tx = database.transaction(BOOKMARK_STORE_NAME, "readwrite");
|
| 158 |
tx.objectStore(BOOKMARK_STORE_NAME).delete(id);
|
| 159 |
+
tx.oncomplete = () => { notify({ type: "bookmark-remove", id }); resolve(); }; tx.onerror = () => reject(tx.error); tx.onabort = () => reject(tx.error);
|
| 160 |
});
|
| 161 |
}
|
| 162 |
+
root.VoiceOfMLReaderStore = Object.freeze({ DB_NAME, MAX_ENTRIES, SCHEMA_VERSION, get, put, list, remove, clearHistory, putBookmark, listBookmarks, listAllBookmarks, removeBookmark, subscribe });
|
| 163 |
})(typeof self !== "undefined" ? self : window);
|
static/reader.js
CHANGED
|
@@ -55,6 +55,7 @@ try { const cached = sessionStorage.getItem(`reader-resolve:${readerId}`); if (c
|
|
| 55 |
const readerLifecycle = readerRuntime.state.lifecycle;
|
| 56 |
readerRuntime.update("source", { id: readerId, url: sourceUrl, contentUrl, downloadUrl, extension, metadata: resolvedReaderData });
|
| 57 |
readerRuntime.events.on("phase", ({ phase }) => { document.documentElement.dataset.readerPhase = phase; });
|
|
|
|
| 58 |
function setReaderPhase(phase) { return readerRuntime.setPhase(phase); }
|
| 59 |
function setReaderStage(stage) { return readerRuntime.setStage(stage); }
|
| 60 |
function classifyReaderError(error, fallback = "READER_PARSE") { const value = `${error?.name || ""} ${error?.message || error || ""}`; if (/AbortError|timeout|network|fetch|HTTP\s*\d+/i.test(value)) return "READER_NETWORK"; if (/EPUB_INVALID|corrupt|damage|truncated|central directory|end of data|invalid zip/i.test(value)) return "READER_CORRUPT"; return fallback; }
|
|
|
|
| 55 |
const readerLifecycle = readerRuntime.state.lifecycle;
|
| 56 |
readerRuntime.update("source", { id: readerId, url: sourceUrl, contentUrl, downloadUrl, extension, metadata: resolvedReaderData });
|
| 57 |
readerRuntime.events.on("phase", ({ phase }) => { document.documentElement.dataset.readerPhase = phase; });
|
| 58 |
+
const unsubscribeReaderStore = VoiceOfMLReaderStore.subscribe?.((change) => { if (!change?.remote || readerLifecycle.disposed) return; if (change.type === "history-clear" || change.type === "history-remove" || change.url === sourceUrl) { if (!document.querySelector("#history-panel").hidden) renderHistory(); } if ((change.type === "bookmark" || change.type === "bookmark-remove") && (!change.url || change.url === sourceUrl) && !document.querySelector("#bookmarks-panel").hidden) renderBookmarks(); });
|
| 59 |
function setReaderPhase(phase) { return readerRuntime.setPhase(phase); }
|
| 60 |
function setReaderStage(stage) { return readerRuntime.setStage(stage); }
|
| 61 |
function classifyReaderError(error, fallback = "READER_PARSE") { const value = `${error?.name || ""} ${error?.message || error || ""}`; if (/AbortError|timeout|network|fetch|HTTP\s*\d+/i.test(value)) return "READER_NETWORK"; if (/EPUB_INVALID|corrupt|damage|truncated|central directory|end of data|invalid zip/i.test(value)) return "READER_CORRUPT"; return fallback; }
|