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

Re-factor the MessageHandler-class event handler function #18888

Merged
Merged
Show file tree
Hide file tree
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
26 changes: 22 additions & 4 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1982,7 +1982,7 @@ class PDFPageProxy {
}

class LoopbackPort {
#listeners = new Set();
#listeners = new Map();

#deferred = Promise.resolve();

Expand All @@ -1992,21 +1992,39 @@ class LoopbackPort {
};

this.#deferred.then(() => {
for (const listener of this.#listeners) {
for (const [listener] of this.#listeners) {
listener.call(this, event);
}
});
}

addEventListener(name, listener) {
this.#listeners.add(listener);
addEventListener(name, listener, options = null) {
let rmAbort = null;
if (options?.signal instanceof AbortSignal) {
const { signal } = options;
if (signal.aborted) {
warn("LoopbackPort - cannot use an `aborted` signal.");
return;
}
const onAbort = () => this.removeEventListener(name, listener);
rmAbort = () => signal.removeEventListener("abort", onAbort);

signal.addEventListener("abort", onAbort);
}
this.#listeners.set(listener, rmAbort);
}

removeEventListener(name, listener) {
const rmAbort = this.#listeners.get(listener);
rmAbort?.();

this.#listeners.delete(listener);
}

terminate() {
for (const [, rmAbort] of this.#listeners) {
rmAbort?.();
}
this.#listeners.clear();
}
}
Expand Down
28 changes: 17 additions & 11 deletions src/shared/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ function wrapReason(reason) {
}

class MessageHandler {
#messageAC = new AbortController();

constructor(sourceName, targetName, comObj) {
this.sourceName = sourceName;
this.targetName = targetName;
Expand All @@ -80,8 +82,12 @@ class MessageHandler {
this.callbackCapabilities = Object.create(null);
this.actionHandler = Object.create(null);

this._onComObjOnMessage = event => {
const data = event.data;
comObj.addEventListener("message", this.#onMessage.bind(this), {
signal: this.#messageAC.signal,
});
}

#onMessage({ data }) {
if (data.targetName !== this.sourceName) {
return;
}
Expand Down Expand Up @@ -111,25 +117,26 @@ class MessageHandler {
throw new Error(`Unknown action from worker: ${data.action}`);
}
if (data.callbackId) {
const cbSourceName = this.sourceName;
const cbTargetName = data.sourceName;
const sourceName = this.sourceName,
targetName = data.sourceName,
comObj = this.comObj;

new Promise(function (resolve) {
resolve(action(data.data));
}).then(
function (result) {
comObj.postMessage({
sourceName: cbSourceName,
targetName: cbTargetName,
sourceName,
targetName,
callback: CallbackKind.DATA,
callbackId: data.callbackId,
data: result,
});
},
function (reason) {
comObj.postMessage({
sourceName: cbSourceName,
targetName: cbTargetName,
sourceName,
targetName,
callback: CallbackKind.ERROR,
callbackId: data.callbackId,
reason: wrapReason(reason),
Expand All @@ -143,8 +150,6 @@ class MessageHandler {
return;
}
action(data.data);
};
comObj.addEventListener("message", this._onComObjOnMessage);
}

on(actionName, handler) {
Expand Down Expand Up @@ -527,7 +532,8 @@ class MessageHandler {
}

destroy() {
this.comObj.removeEventListener("message", this._onComObjOnMessage);
this.#messageAC?.abort();
this.#messageAC = null;
}
}

Expand Down