1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

Disconnect the resize observer and remove scroll listener when unbinding window events

This commit is contained in:
Calixte Denizet 2024-05-30 18:53:07 +02:00
parent fdb3617e0f
commit 4430b6b703
4 changed files with 35 additions and 4 deletions

View file

@ -159,6 +159,7 @@ const PDFViewerApplication = {
_downloadUrl: "",
_eventBusAbortController: null,
_windowAbortController: null,
_globalAbortController: new AbortController(),
documentInfo: null,
metadata: null,
_contentDispositionFilename: null,
@ -463,6 +464,7 @@ const PDFViewerApplication = {
enablePermissions: AppOptions.get("enablePermissions"),
pageColors,
mlManager: this.mlManager,
abortSignal: this._globalAbortController.signal,
});
this.pdfViewer = pdfViewer;
@ -477,6 +479,7 @@ const PDFViewerApplication = {
renderingQueue: pdfRenderingQueue,
linkService: pdfLinkService,
pageColors,
abortSignal: this._globalAbortController.signal,
});
pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
}
@ -2092,6 +2095,10 @@ const PDFViewerApplication = {
unbindWindowEvents() {
this._windowAbortController?.abort();
this._windowAbortController = null;
if (AppOptions.get("isInAutomation")) {
this._globalAbortController?.abort();
this._globalAbortController = null;
}
},
_accumulateTicks(ticks, prop) {

View file

@ -42,6 +42,8 @@ const THUMBNAIL_SELECTED_CLASS = "selected";
* @property {Object} [pageColors] - Overwrites background and foreground colors
* with user defined ones in order to improve readability in high contrast
* mode.
* @property {AbortSignal} [abortSignal] - The AbortSignal for the window
* events.
*/
/**
@ -57,6 +59,7 @@ class PDFThumbnailViewer {
linkService,
renderingQueue,
pageColors,
abortSignal,
}) {
this.container = container;
this.eventBus = eventBus;
@ -64,7 +67,11 @@ class PDFThumbnailViewer {
this.renderingQueue = renderingQueue;
this.pageColors = pageColors || null;
this.scroll = watchScroll(this.container, this.#scrollUpdated.bind(this));
this.scroll = watchScroll(
this.container,
this.#scrollUpdated.bind(this),
abortSignal
);
this.#resetView();
}

View file

@ -309,7 +309,21 @@ class PDFViewer {
this.renderingQueue = options.renderingQueue;
}
this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this));
const { abortSignal } = options;
abortSignal?.addEventListener(
"abort",
() => {
this.#resizeObserver.disconnect();
this.#resizeObserver = null;
},
{ once: true }
);
this.scroll = watchScroll(
this.container,
this._scrollUpdate.bind(this),
abortSignal
);
this.presentationModeState = PresentationModeState.UNKNOWN;
this._resetView();

View file

@ -155,7 +155,7 @@ function scrollIntoView(element, spot, scrollMatches = false) {
* Helper function to start monitoring the scroll event and converting them into
* PDF.js friendly one: with scroll debounce and scroll direction.
*/
function watchScroll(viewAreaElement, callback) {
function watchScroll(viewAreaElement, callback, abortSignal = undefined) {
const debounceScroll = function (evt) {
if (rAF) {
return;
@ -189,7 +189,10 @@ function watchScroll(viewAreaElement, callback) {
};
let rAF = null;
viewAreaElement.addEventListener("scroll", debounceScroll, true);
viewAreaElement.addEventListener("scroll", debounceScroll, {
useCapture: true,
signal: abortSignal,
});
return state;
}