Remove event listeners with signal in web/annotation_layer_builder.js and web/text_highlighter.js

This commit is contained in:
Jonas Jenwald 2024-04-21 09:41:21 +02:00
parent 2831caef13
commit 32b885c5ef
2 changed files with 26 additions and 28 deletions

View file

@ -50,7 +50,7 @@ import { PresentationModeState } from "./ui_utils.js";
class AnnotationLayerBuilder { class AnnotationLayerBuilder {
#onAppend = null; #onAppend = null;
#onPresentationModeChanged = null; #eventAbortController = null;
/** /**
* @param {AnnotationLayerBuilderOptions} options * @param {AnnotationLayerBuilderOptions} options
@ -155,13 +155,15 @@ class AnnotationLayerBuilder {
if (this.linkService.isInPresentationMode) { if (this.linkService.isInPresentationMode) {
this.#updatePresentationModeState(PresentationModeState.FULLSCREEN); this.#updatePresentationModeState(PresentationModeState.FULLSCREEN);
} }
if (!this.#onPresentationModeChanged) { if (!this.#eventAbortController) {
this.#onPresentationModeChanged = evt => { this.#eventAbortController = new AbortController();
this.#updatePresentationModeState(evt.state);
};
this._eventBus?._on( this._eventBus?._on(
"presentationmodechanged", "presentationmodechanged",
this.#onPresentationModeChanged evt => {
this.#updatePresentationModeState(evt.state);
},
{ signal: this.#eventAbortController.signal }
); );
} }
} }
@ -169,13 +171,8 @@ class AnnotationLayerBuilder {
cancel() { cancel() {
this._cancelled = true; this._cancelled = true;
if (this.#onPresentationModeChanged) { this.#eventAbortController?.abort();
this._eventBus?._off( this.#eventAbortController = null;
"presentationmodechanged",
this.#onPresentationModeChanged
);
this.#onPresentationModeChanged = null;
}
} }
hide() { hide() {

View file

@ -29,6 +29,8 @@
* either the text layer or XFA layer depending on the type of document. * either the text layer or XFA layer depending on the type of document.
*/ */
class TextHighlighter { class TextHighlighter {
#eventAbortController = null;
/** /**
* @param {TextHighlighterOptions} options * @param {TextHighlighterOptions} options
*/ */
@ -37,7 +39,6 @@ class TextHighlighter {
this.matches = []; this.matches = [];
this.eventBus = eventBus; this.eventBus = eventBus;
this.pageIdx = pageIndex; this.pageIdx = pageIndex;
this._onUpdateTextLayerMatches = null;
this.textDivs = null; this.textDivs = null;
this.textContentItemsStr = null; this.textContentItemsStr = null;
this.enabled = false; this.enabled = false;
@ -69,15 +70,18 @@ class TextHighlighter {
throw new Error("TextHighlighter is already enabled."); throw new Error("TextHighlighter is already enabled.");
} }
this.enabled = true; this.enabled = true;
if (!this._onUpdateTextLayerMatches) {
this._onUpdateTextLayerMatches = evt => { if (!this.#eventAbortController) {
if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) { this.#eventAbortController = new AbortController();
this._updateMatches();
}
};
this.eventBus._on( this.eventBus._on(
"updatetextlayermatches", "updatetextlayermatches",
this._onUpdateTextLayerMatches evt => {
if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) {
this._updateMatches();
}
},
{ signal: this.#eventAbortController.signal }
); );
} }
this._updateMatches(); this._updateMatches();
@ -88,13 +92,10 @@ class TextHighlighter {
return; return;
} }
this.enabled = false; this.enabled = false;
if (this._onUpdateTextLayerMatches) {
this.eventBus._off( this.#eventAbortController?.abort();
"updatetextlayermatches", this.#eventAbortController = null;
this._onUpdateTextLayerMatches
);
this._onUpdateTextLayerMatches = null;
}
this._updateMatches(/* reset = */ true); this._updateMatches(/* reset = */ true);
} }