Remove event listeners with signal in web/pdf_scripting_manager.js

This commit is contained in:
Jonas Jenwald 2024-04-18 20:34:44 +02:00
parent bf785f2180
commit 2831caef13

View file

@ -37,6 +37,8 @@ class PDFScriptingManager {
#docProperties = null; #docProperties = null;
#eventAbortController = null;
#eventBus = null; #eventBus = null;
#externalServices = null; #externalServices = null;
@ -108,46 +110,66 @@ class PDFScriptingManager {
await this.#destroyScripting(); await this.#destroyScripting();
return; return;
} }
const eventBus = this.#eventBus;
this._internalEvents.set("updatefromsandbox", event => { this.#eventAbortController = new AbortController();
if (event?.source === window) { const { signal } = this.#eventAbortController;
this.#updateFromSandbox(event.detail);
}
});
this._internalEvents.set("dispatcheventinsandbox", event => {
this.#scripting?.dispatchEventInSandbox(event.detail);
});
this._internalEvents.set("pagechanging", ({ pageNumber, previous }) => { eventBus._on(
if (pageNumber === previous) { "updatefromsandbox",
return; // The current page didn't change. event => {
} if (event?.source === window) {
this.#dispatchPageClose(previous); this.#updateFromSandbox(event.detail);
this.#dispatchPageOpen(pageNumber); }
}); },
this._internalEvents.set("pagerendered", ({ pageNumber }) => { { signal }
if (!this._pageOpenPending.has(pageNumber)) { );
return; // No pending "PageOpen" event for the newly rendered page. eventBus._on(
} "dispatcheventinsandbox",
if (pageNumber !== this.#pdfViewer.currentPageNumber) { event => {
return; // The newly rendered page is no longer the current one. this.#scripting?.dispatchEventInSandbox(event.detail);
} },
this.#dispatchPageOpen(pageNumber); { signal }
}); );
this._internalEvents.set("pagesdestroy", async () => {
await this.#dispatchPageClose(this.#pdfViewer.currentPageNumber);
await this.#scripting?.dispatchEventInSandbox({ eventBus._on(
id: "doc", "pagechanging",
name: "WillClose", ({ pageNumber, previous }) => {
}); if (pageNumber === previous) {
return; // The current page didn't change.
}
this.#dispatchPageClose(previous);
this.#dispatchPageOpen(pageNumber);
},
{ signal }
);
eventBus._on(
"pagerendered",
({ pageNumber }) => {
if (!this._pageOpenPending.has(pageNumber)) {
return; // No pending "PageOpen" event for the newly rendered page.
}
if (pageNumber !== this.#pdfViewer.currentPageNumber) {
return; // The newly rendered page is no longer the current one.
}
this.#dispatchPageOpen(pageNumber);
},
{ signal }
);
eventBus._on(
"pagesdestroy",
async () => {
await this.#dispatchPageClose(this.#pdfViewer.currentPageNumber);
this.#closeCapability?.resolve(); await this.#scripting?.dispatchEventInSandbox({
}); id: "doc",
name: "WillClose",
});
for (const [name, listener] of this._internalEvents) { this.#closeCapability?.resolve();
this.#eventBus._on(name, listener); },
} { signal }
);
try { try {
const docProperties = await this.#docProperties(pdfDocument); const docProperties = await this.#docProperties(pdfDocument);
@ -168,7 +190,7 @@ class PDFScriptingManager {
}, },
}); });
this.#eventBus.dispatch("sandboxcreated", { source: this }); eventBus.dispatch("sandboxcreated", { source: this });
} catch (error) { } catch (error) {
console.error(`setDocument: "${error.message}".`); console.error(`setDocument: "${error.message}".`);
@ -242,13 +264,6 @@ class PDFScriptingManager {
return this.#ready; return this.#ready;
} }
/**
* @private
*/
get _internalEvents() {
return shadow(this, "_internalEvents", new Map());
}
/** /**
* @private * @private
*/ */
@ -466,10 +481,8 @@ class PDFScriptingManager {
this.#willPrintCapability?.reject(new Error("Scripting destroyed.")); this.#willPrintCapability?.reject(new Error("Scripting destroyed."));
this.#willPrintCapability = null; this.#willPrintCapability = null;
for (const [name, listener] of this._internalEvents) { this.#eventAbortController?.abort();
this.#eventBus._off(name, listener); this.#eventAbortController = null;
}
this._internalEvents.clear();
this._pageOpenPending.clear(); this._pageOpenPending.clear();
this._visitedPages.clear(); this._visitedPages.clear();