feat(pdfjs): debounce saving view config

This commit is contained in:
Elian Doran 2025-12-29 16:19:21 +02:00
parent 84425e86e9
commit 359f398afa
No known key found for this signature in database

View File

@ -2,17 +2,7 @@ export default function interceptViewHistory() {
const originalSetItem = Storage.prototype.setItem;
Storage.prototype.setItem = function (key: string, value: string) {
if (key === "pdfjs.history") {
// Parse the history and remove entries that are not relevant.
const history = JSON.parse(value);
const fingerprint = window.PDFViewerApplication?.pdfDocument?.fingerprints?.[0];
if (fingerprint) {
history.files = history.files.filter((file: any) => file.fingerprint === fingerprint);
}
window.parent.postMessage({
type: "pdfjs-viewer-save-view-history",
data: JSON.stringify(history)
}, "*");
saveHistory(value);
return;
}
@ -28,3 +18,25 @@ export default function interceptViewHistory() {
return originalGetItem.call(this, key);
}
}
let saveTimeout: number | null = null;
function saveHistory(value: string) {
if (saveTimeout) {
clearTimeout(saveTimeout);
}
saveTimeout = window.setTimeout(() => {
// Parse the history and remove entries that are not relevant.
const history = JSON.parse(value);
const fingerprint = window.PDFViewerApplication?.pdfDocument?.fingerprints?.[0];
if (fingerprint) {
history.files = history.files.filter((file: any) => file.fingerprint === fingerprint);
}
window.parent.postMessage({
type: "pdfjs-viewer-save-view-history",
data: JSON.stringify(history)
}, "*");
saveTimeout = null;
}, 2_000);
}