diff --git a/packages/pdfjs-viewer/src/history.ts b/packages/pdfjs-viewer/src/history.ts index cd31dcd8a..21c2966d4 100644 --- a/packages/pdfjs-viewer/src/history.ts +++ b/packages/pdfjs-viewer/src/history.ts @@ -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); +}