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

Remove event listeners with AbortSignal in the AltTextManager class #18881

Merged
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
55 changes: 29 additions & 26 deletions web/alt_text_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@
* limitations under the License.
*/

import { DOMSVGFactory, shadow } from "pdfjs-lib";
import { DOMSVGFactory } from "pdfjs-lib";

class AltTextManager {
#boundUpdateUIState = this.#updateUIState.bind(this);

#boundSetPosition = this.#setPosition.bind(this);

#boundOnClick = this.#onClick.bind(this);
#clickAC = null;

#currentEditor = null;

Expand All @@ -46,6 +42,8 @@ class AltTextManager {

#previousAltText = null;

#resizeAC = null;

#svgElement = null;

#rectElement = null;
Expand Down Expand Up @@ -77,6 +75,8 @@ class AltTextManager {
this.#eventBus = eventBus;
this.#container = container;

const onUpdateUIState = this.#updateUIState.bind(this);

dialog.addEventListener("close", this.#close.bind(this));
dialog.addEventListener("contextmenu", event => {
if (event.target !== this.#textarea) {
Expand All @@ -85,22 +85,12 @@ class AltTextManager {
});
cancelButton.addEventListener("click", this.#finish.bind(this));
saveButton.addEventListener("click", this.#save.bind(this));
optionDescription.addEventListener("change", this.#boundUpdateUIState);
optionDecorative.addEventListener("change", this.#boundUpdateUIState);
optionDescription.addEventListener("change", onUpdateUIState);
optionDecorative.addEventListener("change", onUpdateUIState);

this.#overlayManager.register(dialog);
}

get _elements() {
return shadow(this, "_elements", [
this.#optionDescription,
this.#optionDecorative,
this.#textarea,
this.#saveButton,
this.#cancelButton,
]);
}

#createSVGElement() {
if (this.#svgElement) {
return;
Expand Down Expand Up @@ -138,12 +128,21 @@ class AltTextManager {
if (this.#currentEditor || !editor) {
return;
}

this.#createSVGElement();

this.#hasUsedPointer = false;
for (const element of this._elements) {
element.addEventListener("click", this.#boundOnClick);

this.#clickAC = new AbortController();
const clickOpts = { signal: this.#clickAC.signal },
onClick = this.#onClick.bind(this);
for (const element of [
this.#optionDescription,
this.#optionDecorative,
this.#textarea,
this.#saveButton,
this.#cancelButton,
]) {
element.addEventListener("click", onClick, clickOpts);
}

const { altText, decorative } = editor.altTextData;
Expand All @@ -160,7 +159,11 @@ class AltTextManager {
this.#currentEditor = editor;
this.#uiManager = uiManager;
this.#uiManager.removeEditListeners();
this.#eventBus._on("resize", this.#boundSetPosition);

this.#resizeAC = new AbortController();
this.#eventBus._on("resize", this.#setPosition.bind(this), {
signal: this.#resizeAC.signal,
});

try {
await this.#overlayManager.open(this.#dialog);
Expand Down Expand Up @@ -258,7 +261,8 @@ class AltTextManager {

this.#removeOnClickListeners();
this.#uiManager?.addEditListeners();
this.#eventBus._off("resize", this.#boundSetPosition);
this.#resizeAC?.abort();
this.#resizeAC = null;
this.#currentEditor.altTextFinish();
this.#currentEditor = null;
this.#uiManager = null;
Expand Down Expand Up @@ -295,9 +299,8 @@ class AltTextManager {
}

#removeOnClickListeners() {
for (const element of this._elements) {
element.removeEventListener("click", this.#boundOnClick);
}
this.#clickAC?.abort();
this.#clickAC = null;
}

destroy() {
Expand Down