diff --git a/web/annotation_layer_builder.js b/web/annotation_layer_builder.js index c9205ec9e..481a3e1e4 100644 --- a/web/annotation_layer_builder.js +++ b/web/annotation_layer_builder.js @@ -50,7 +50,7 @@ import { PresentationModeState } from "./ui_utils.js"; class AnnotationLayerBuilder { #onAppend = null; - #onPresentationModeChanged = null; + #eventAbortController = null; /** * @param {AnnotationLayerBuilderOptions} options @@ -155,13 +155,15 @@ class AnnotationLayerBuilder { if (this.linkService.isInPresentationMode) { this.#updatePresentationModeState(PresentationModeState.FULLSCREEN); } - if (!this.#onPresentationModeChanged) { - this.#onPresentationModeChanged = evt => { - this.#updatePresentationModeState(evt.state); - }; + if (!this.#eventAbortController) { + this.#eventAbortController = new AbortController(); + this._eventBus?._on( "presentationmodechanged", - this.#onPresentationModeChanged + evt => { + this.#updatePresentationModeState(evt.state); + }, + { signal: this.#eventAbortController.signal } ); } } @@ -169,13 +171,8 @@ class AnnotationLayerBuilder { cancel() { this._cancelled = true; - if (this.#onPresentationModeChanged) { - this._eventBus?._off( - "presentationmodechanged", - this.#onPresentationModeChanged - ); - this.#onPresentationModeChanged = null; - } + this.#eventAbortController?.abort(); + this.#eventAbortController = null; } hide() { diff --git a/web/text_highlighter.js b/web/text_highlighter.js index 41721554d..ac682dba1 100644 --- a/web/text_highlighter.js +++ b/web/text_highlighter.js @@ -29,6 +29,8 @@ * either the text layer or XFA layer depending on the type of document. */ class TextHighlighter { + #eventAbortController = null; + /** * @param {TextHighlighterOptions} options */ @@ -37,7 +39,6 @@ class TextHighlighter { this.matches = []; this.eventBus = eventBus; this.pageIdx = pageIndex; - this._onUpdateTextLayerMatches = null; this.textDivs = null; this.textContentItemsStr = null; this.enabled = false; @@ -69,15 +70,18 @@ class TextHighlighter { throw new Error("TextHighlighter is already enabled."); } this.enabled = true; - if (!this._onUpdateTextLayerMatches) { - this._onUpdateTextLayerMatches = evt => { - if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) { - this._updateMatches(); - } - }; + + if (!this.#eventAbortController) { + this.#eventAbortController = new AbortController(); + this.eventBus._on( "updatetextlayermatches", - this._onUpdateTextLayerMatches + evt => { + if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) { + this._updateMatches(); + } + }, + { signal: this.#eventAbortController.signal } ); } this._updateMatches(); @@ -88,13 +92,10 @@ class TextHighlighter { return; } this.enabled = false; - if (this._onUpdateTextLayerMatches) { - this.eventBus._off( - "updatetextlayermatches", - this._onUpdateTextLayerMatches - ); - this._onUpdateTextLayerMatches = null; - } + + this.#eventAbortController?.abort(); + this.#eventAbortController = null; + this._updateMatches(/* reset = */ true); }