Bug 1880162 - Split off portPromises from ProxyMessenger.ports, r=robwu

Differential Revision: https://phabricator.services.mozilla.com/D206231
This commit is contained in:
Tomislav Jovanovic 2024-04-05 19:19:33 +00:00
Родитель 4806eb02d3
Коммит 86374767c5
1 изменённых файлов: 20 добавлений и 11 удалений

Просмотреть файл

@ -245,8 +245,10 @@ let apiManager = new (class extends SchemaAPIManager {
// to relevant child messengers. Also handles Native messaging and GeckoView.
/** @typedef {typeof ProxyMessenger} NativeMessenger */
const ProxyMessenger = {
/** @type {Map<number, Partial<ParentPort>&Promise<ParentPort>>} */
/** @type {Map<number, ParentPort>} */
ports: new Map(),
/** @type {Map<number, Promise>} */
portPromises: new Map(),
init() {
this.conduit = new lazy.BroadcastConduit(ProxyMessenger, {
@ -363,17 +365,24 @@ const ProxyMessenger = {
}
// PortMessages that follow will need to wait for the port to be opened.
/** @type {callback} */
let resolvePort;
this.ports.set(arg.portId, new Promise(res => (resolvePort = res)));
let { promise, resolve, reject } = Promise.withResolvers();
this.portPromises.set(arg.portId, promise);
let kind = await this.normalizeArgs(arg, sender);
let all = await this.conduit.castPortConnect(kind, arg);
resolvePort();
try {
let kind = await this.normalizeArgs(arg, sender);
let all = await this.conduit.castPortConnect(kind, arg);
resolve();
// If there are no active onConnect listeners.
if (!all.some(x => x.value)) {
throw new ExtensionError(ERROR_NO_RECEIVERS);
// If there are no active onConnect listeners.
if (!all.some(x => x.value)) {
throw new ExtensionError(ERROR_NO_RECEIVERS);
}
} catch (err) {
// Throw _and_ reject with error, so everything awaiting this port fails.
reject(err);
throw err;
} finally {
this.portPromises.delete(arg.portId);
}
},
@ -387,7 +396,7 @@ const ProxyMessenger = {
// NOTE: the following await make sure we await for promised ports
// (ports that were not yet open when added to the Map,
// see recvPortConnect).
await this.ports.get(sender.portId);
await this.portPromises.get(sender.portId);
this.sendPortMessage(sender.portId, holder, !sender.source);
},