Add signal-support in the EventBus, and utilize it in the viewer (PR 17964 follow-up)

This mimics the `signal` option that's available for `addEventListener`, see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#signal).
This commit is contained in:
Jonas Jenwald 2024-04-18 16:53:56 +02:00
parent 5ad42c13ad
commit 702ee7b1e1
3 changed files with 148 additions and 130 deletions

View file

@ -143,6 +143,60 @@ describe("event_utils", function () {
expect(onceCount).toEqual(1); expect(onceCount).toEqual(1);
}); });
it("dispatch event to handlers with/without 'signal' option, aborted *before* dispatch", function () {
const eventBus = new EventBus();
const ac = new AbortController();
let multipleCount = 0,
noneCount = 0;
eventBus.on("test", function () {
multipleCount++;
});
eventBus.on(
"test",
function () {
noneCount++;
},
{ signal: ac.signal }
);
ac.abort();
eventBus.dispatch("test");
eventBus.dispatch("test");
eventBus.dispatch("test");
expect(multipleCount).toEqual(3);
expect(noneCount).toEqual(0);
});
it("dispatch event to handlers with/without 'signal' option, aborted *after* dispatch", function () {
const eventBus = new EventBus();
const ac = new AbortController();
let multipleCount = 0,
onceCount = 0;
eventBus.on("test", function () {
multipleCount++;
});
eventBus.on(
"test",
function () {
onceCount++;
},
{ signal: ac.signal }
);
eventBus.dispatch("test");
ac.abort();
eventBus.dispatch("test");
eventBus.dispatch("test");
expect(multipleCount).toEqual(3);
expect(onceCount).toEqual(1);
});
it("should not re-dispatch to DOM", async function () { it("should not re-dispatch to DOM", async function () {
if (isNodeJS) { if (isNodeJS) {
pending("Document is not supported in Node.js."); pending("Document is not supported in Node.js.");

View file

@ -157,7 +157,7 @@ const PDFViewerApplication = {
url: "", url: "",
baseUrl: "", baseUrl: "",
_downloadUrl: "", _downloadUrl: "",
_boundEvents: Object.create(null), _eventBusAbortController: null,
_windowAbortController: null, _windowAbortController: null,
documentInfo: null, documentInfo: null,
metadata: null, metadata: null,
@ -1832,75 +1832,87 @@ const PDFViewerApplication = {
}, },
bindEvents() { bindEvents() {
const { eventBus, _boundEvents } = this; if (this._eventBusAbortController) {
return;
}
this._eventBusAbortController = new AbortController();
_boundEvents.beforePrint = this.beforePrint.bind(this); const {
_boundEvents.afterPrint = this.afterPrint.bind(this); eventBus,
_eventBusAbortController: { signal },
} = this;
eventBus._on("resize", webViewerResize); eventBus._on("resize", webViewerResize, { signal });
eventBus._on("hashchange", webViewerHashchange); eventBus._on("hashchange", webViewerHashchange, { signal });
eventBus._on("beforeprint", _boundEvents.beforePrint); eventBus._on("beforeprint", this.beforePrint.bind(this), { signal });
eventBus._on("afterprint", _boundEvents.afterPrint); eventBus._on("afterprint", this.afterPrint.bind(this), { signal });
eventBus._on("pagerender", webViewerPageRender); eventBus._on("pagerender", webViewerPageRender, { signal });
eventBus._on("pagerendered", webViewerPageRendered); eventBus._on("pagerendered", webViewerPageRendered, { signal });
eventBus._on("updateviewarea", webViewerUpdateViewarea); eventBus._on("updateviewarea", webViewerUpdateViewarea, { signal });
eventBus._on("pagechanging", webViewerPageChanging); eventBus._on("pagechanging", webViewerPageChanging, { signal });
eventBus._on("scalechanging", webViewerScaleChanging); eventBus._on("scalechanging", webViewerScaleChanging, { signal });
eventBus._on("rotationchanging", webViewerRotationChanging); eventBus._on("rotationchanging", webViewerRotationChanging, { signal });
eventBus._on("sidebarviewchanged", webViewerSidebarViewChanged); eventBus._on("sidebarviewchanged", webViewerSidebarViewChanged, { signal });
eventBus._on("pagemode", webViewerPageMode); eventBus._on("pagemode", webViewerPageMode, { signal });
eventBus._on("namedaction", webViewerNamedAction); eventBus._on("namedaction", webViewerNamedAction, { signal });
eventBus._on("presentationmodechanged", webViewerPresentationModeChanged); eventBus._on("presentationmodechanged", webViewerPresentationModeChanged, {
eventBus._on("presentationmode", webViewerPresentationMode); signal,
});
eventBus._on("presentationmode", webViewerPresentationMode, { signal });
eventBus._on( eventBus._on(
"switchannotationeditormode", "switchannotationeditormode",
webViewerSwitchAnnotationEditorMode webViewerSwitchAnnotationEditorMode,
{ signal }
); );
eventBus._on( eventBus._on(
"switchannotationeditorparams", "switchannotationeditorparams",
webViewerSwitchAnnotationEditorParams webViewerSwitchAnnotationEditorParams,
{ signal }
); );
eventBus._on("print", webViewerPrint); eventBus._on("print", webViewerPrint, { signal });
eventBus._on("download", webViewerDownload); eventBus._on("download", webViewerDownload, { signal });
eventBus._on("firstpage", webViewerFirstPage); eventBus._on("firstpage", webViewerFirstPage, { signal });
eventBus._on("lastpage", webViewerLastPage); eventBus._on("lastpage", webViewerLastPage, { signal });
eventBus._on("nextpage", webViewerNextPage); eventBus._on("nextpage", webViewerNextPage, { signal });
eventBus._on("previouspage", webViewerPreviousPage); eventBus._on("previouspage", webViewerPreviousPage, { signal });
eventBus._on("zoomin", webViewerZoomIn); eventBus._on("zoomin", webViewerZoomIn, { signal });
eventBus._on("zoomout", webViewerZoomOut); eventBus._on("zoomout", webViewerZoomOut, { signal });
eventBus._on("zoomreset", webViewerZoomReset); eventBus._on("zoomreset", webViewerZoomReset, { signal });
eventBus._on("pagenumberchanged", webViewerPageNumberChanged); eventBus._on("pagenumberchanged", webViewerPageNumberChanged, { signal });
eventBus._on("scalechanged", webViewerScaleChanged); eventBus._on("scalechanged", webViewerScaleChanged, { signal });
eventBus._on("rotatecw", webViewerRotateCw); eventBus._on("rotatecw", webViewerRotateCw, { signal });
eventBus._on("rotateccw", webViewerRotateCcw); eventBus._on("rotateccw", webViewerRotateCcw, { signal });
eventBus._on("optionalcontentconfig", webViewerOptionalContentConfig); eventBus._on("optionalcontentconfig", webViewerOptionalContentConfig, {
eventBus._on("switchscrollmode", webViewerSwitchScrollMode); signal,
eventBus._on("scrollmodechanged", webViewerScrollModeChanged); });
eventBus._on("switchspreadmode", webViewerSwitchSpreadMode); eventBus._on("switchscrollmode", webViewerSwitchScrollMode, { signal });
eventBus._on("spreadmodechanged", webViewerSpreadModeChanged); eventBus._on("scrollmodechanged", webViewerScrollModeChanged, { signal });
eventBus._on("documentproperties", webViewerDocumentProperties); eventBus._on("switchspreadmode", webViewerSwitchSpreadMode, { signal });
eventBus._on("findfromurlhash", webViewerFindFromUrlHash); eventBus._on("spreadmodechanged", webViewerSpreadModeChanged, { signal });
eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount); eventBus._on("documentproperties", webViewerDocumentProperties, { signal });
eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState); eventBus._on("findfromurlhash", webViewerFindFromUrlHash, { signal });
eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount, {
signal,
});
eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState, {
signal,
});
if (AppOptions.get("pdfBug")) { if (AppOptions.get("pdfBug")) {
_boundEvents.reportPageStatsPDFBug = reportPageStatsPDFBug; eventBus._on("pagerendered", reportPageStatsPDFBug, { signal });
eventBus._on("pagechanging", reportPageStatsPDFBug, { signal });
eventBus._on("pagerendered", _boundEvents.reportPageStatsPDFBug);
eventBus._on("pagechanging", _boundEvents.reportPageStatsPDFBug);
} }
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus._on("fileinputchange", webViewerFileInputChange); eventBus._on("fileinputchange", webViewerFileInputChange, { signal });
eventBus._on("openfile", webViewerOpenFile); eventBus._on("openfile", webViewerOpenFile, { signal });
} }
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
// The `unbindEvents` method is unused in MOZCENTRAL builds,
// hence we don't need to unregister these event listeners.
eventBus._on( eventBus._on(
"annotationeditorstateschanged", "annotationeditorstateschanged",
webViewerAnnotationEditorStatesChanged webViewerAnnotationEditorStatesChanged,
{ signal }
); );
eventBus._on("reporttelemetry", webViewerReportTelemetry); eventBus._on("reporttelemetry", webViewerReportTelemetry, { signal });
} }
}, },
@ -2049,62 +2061,8 @@ const PDFViewerApplication = {
}, },
unbindEvents() { unbindEvents() {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { this._eventBusAbortController?.abort();
throw new Error("Not implemented: unbindEvents"); this._eventBusAbortController = null;
}
const { eventBus, _boundEvents } = this;
eventBus._off("resize", webViewerResize);
eventBus._off("hashchange", webViewerHashchange);
eventBus._off("beforeprint", _boundEvents.beforePrint);
eventBus._off("afterprint", _boundEvents.afterPrint);
eventBus._off("pagerender", webViewerPageRender);
eventBus._off("pagerendered", webViewerPageRendered);
eventBus._off("updateviewarea", webViewerUpdateViewarea);
eventBus._off("pagechanging", webViewerPageChanging);
eventBus._off("scalechanging", webViewerScaleChanging);
eventBus._off("rotationchanging", webViewerRotationChanging);
eventBus._off("sidebarviewchanged", webViewerSidebarViewChanged);
eventBus._off("pagemode", webViewerPageMode);
eventBus._off("namedaction", webViewerNamedAction);
eventBus._off("presentationmodechanged", webViewerPresentationModeChanged);
eventBus._off("presentationmode", webViewerPresentationMode);
eventBus._off("print", webViewerPrint);
eventBus._off("download", webViewerDownload);
eventBus._off("firstpage", webViewerFirstPage);
eventBus._off("lastpage", webViewerLastPage);
eventBus._off("nextpage", webViewerNextPage);
eventBus._off("previouspage", webViewerPreviousPage);
eventBus._off("zoomin", webViewerZoomIn);
eventBus._off("zoomout", webViewerZoomOut);
eventBus._off("zoomreset", webViewerZoomReset);
eventBus._off("pagenumberchanged", webViewerPageNumberChanged);
eventBus._off("scalechanged", webViewerScaleChanged);
eventBus._off("rotatecw", webViewerRotateCw);
eventBus._off("rotateccw", webViewerRotateCcw);
eventBus._off("optionalcontentconfig", webViewerOptionalContentConfig);
eventBus._off("switchscrollmode", webViewerSwitchScrollMode);
eventBus._off("scrollmodechanged", webViewerScrollModeChanged);
eventBus._off("switchspreadmode", webViewerSwitchSpreadMode);
eventBus._off("spreadmodechanged", webViewerSpreadModeChanged);
eventBus._off("documentproperties", webViewerDocumentProperties);
eventBus._off("findfromurlhash", webViewerFindFromUrlHash);
eventBus._off("updatefindmatchescount", webViewerUpdateFindMatchesCount);
eventBus._off("updatefindcontrolstate", webViewerUpdateFindControlState);
if (_boundEvents.reportPageStatsPDFBug) {
eventBus._off("pagerendered", _boundEvents.reportPageStatsPDFBug);
eventBus._off("pagechanging", _boundEvents.reportPageStatsPDFBug);
_boundEvents.reportPageStatsPDFBug = null;
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus._off("fileinputchange", webViewerFileInputChange);
eventBus._off("openfile", webViewerOpenFile);
}
_boundEvents.beforePrint = null;
_boundEvents.afterPrint = null;
}, },
unbindWindowEvents() { unbindWindowEvents() {

View file

@ -44,29 +44,21 @@ async function waitOnEventOrTimeout({ target, name, delay = 0 }) {
throw new Error("waitOnEventOrTimeout - invalid parameters."); throw new Error("waitOnEventOrTimeout - invalid parameters.");
} }
const { promise, resolve } = Promise.withResolvers(); const { promise, resolve } = Promise.withResolvers();
const ac = new AbortController();
function handler(type) { function handler(type) {
if (target instanceof EventBus) { ac.abort(); // Remove event listener.
target._off(name, eventHandler); clearTimeout(timeout);
} else {
target.removeEventListener(name, eventHandler);
}
if (timeout) {
clearTimeout(timeout);
}
resolve(type); resolve(type);
} }
const eventHandler = handler.bind(null, WaitOnType.EVENT); const evtMethod = target instanceof EventBus ? "_on" : "addEventListener";
if (target instanceof EventBus) { target[evtMethod](name, handler.bind(null, WaitOnType.EVENT), {
target._on(name, eventHandler); signal: ac.signal,
} else { });
target.addEventListener(name, eventHandler);
}
const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT); const timeout = setTimeout(handler.bind(null, WaitOnType.TIMEOUT), delay);
const timeout = setTimeout(timeoutHandler, delay);
return promise; return promise;
} }
@ -87,6 +79,7 @@ class EventBus {
this._on(eventName, listener, { this._on(eventName, listener, {
external: true, external: true,
once: options?.once, once: options?.once,
signal: options?.signal,
}); });
} }
@ -96,10 +89,7 @@ class EventBus {
* @param {Object} [options] * @param {Object} [options]
*/ */
off(eventName, listener, options = null) { off(eventName, listener, options = null) {
this._off(eventName, listener, { this._off(eventName, listener);
external: true,
once: options?.once,
});
} }
/** /**
@ -138,11 +128,25 @@ class EventBus {
* @ignore * @ignore
*/ */
_on(eventName, listener, options = null) { _on(eventName, listener, options = null) {
let rmAbort = null;
if (options?.signal instanceof AbortSignal) {
const { signal } = options;
if (signal.aborted) {
console.error("Cannot use an `aborted` signal.");
return;
}
const onAbort = () => this._off(eventName, listener);
rmAbort = () => signal.removeEventListener("abort", onAbort);
signal.addEventListener("abort", onAbort);
}
const eventListeners = (this.#listeners[eventName] ||= []); const eventListeners = (this.#listeners[eventName] ||= []);
eventListeners.push({ eventListeners.push({
listener, listener,
external: options?.external === true, external: options?.external === true,
once: options?.once === true, once: options?.once === true,
rmAbort,
}); });
} }
@ -155,7 +159,9 @@ class EventBus {
return; return;
} }
for (let i = 0, ii = eventListeners.length; i < ii; i++) { for (let i = 0, ii = eventListeners.length; i < ii; i++) {
if (eventListeners[i].listener === listener) { const evt = eventListeners[i];
if (evt.listener === listener) {
evt.rmAbort?.(); // Ensure that the `AbortSignal` listener is removed.
eventListeners.splice(i, 1); eventListeners.splice(i, 1);
return; return;
} }