Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const PDFViewerApplication = {
_downloadUrl: "",
_eventBusAbortController: null,
_windowAbortController: null,
_globalAbortController: new AbortController(),
documentInfo: null,
metadata: null,
_contentDispositionFilename: null,
Expand Down Expand Up @@ -463,6 +464,7 @@ const PDFViewerApplication = {
enablePermissions: AppOptions.get("enablePermissions"),
pageColors,
mlManager: this.mlManager,
abortSignal: this._globalAbortController.signal,
});
this.pdfViewer = pdfViewer;

Expand All @@ -477,6 +479,7 @@ const PDFViewerApplication = {
renderingQueue: pdfRenderingQueue,
linkService: pdfLinkService,
pageColors,
abortSignal: this._globalAbortController.signal,
});
pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
}
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion web/pdf_thumbnail_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

/**
Expand All @@ -57,14 +59,19 @@ class PDFThumbnailViewer {
linkService,
renderingQueue,
pageColors,
abortSignal,
}) {
this.container = container;
this.eventBus = eventBus;
this.linkService = linkService;
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();
}

Expand Down
16 changes: 15 additions & 1 deletion web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
7 changes: 5 additions & 2 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down