Merge pull request #18193 from calixteman/rm_observer

Disconnect the resize observer and remove scroll listener when unbinding window events
This commit is contained in:
calixteman 2024-05-30 22:51:05 +02:00 committed by GitHub
commit 4d9c25a41a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 4 deletions

View file

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

View file

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

View file

@ -309,7 +309,21 @@ class PDFViewer {
this.renderingQueue = options.renderingQueue; 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.presentationModeState = PresentationModeState.UNKNOWN;
this._resetView(); 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 * Helper function to start monitoring the scroll event and converting them into
* PDF.js friendly one: with scroll debounce and scroll direction. * PDF.js friendly one: with scroll debounce and scroll direction.
*/ */
function watchScroll(viewAreaElement, callback) { function watchScroll(viewAreaElement, callback, abortSignal = undefined) {
const debounceScroll = function (evt) { const debounceScroll = function (evt) {
if (rAF) { if (rAF) {
return; return;
@ -189,7 +189,10 @@ function watchScroll(viewAreaElement, callback) {
}; };
let rAF = null; let rAF = null;
viewAreaElement.addEventListener("scroll", debounceScroll, true); viewAreaElement.addEventListener("scroll", debounceScroll, {
useCapture: true,
signal: abortSignal,
});
return state; return state;
} }