Bug 1681888 - [devtools] Remove defer usage in devtools/server/startup/worker.js. r=nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D100552
This commit is contained in:
Falguni Islam 2021-01-13 13:58:41 +00:00
Родитель 682f92fe6f
Коммит d6961d7fdf
1 изменённых файлов: 23 добавлений и 24 удалений

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

@ -18,27 +18,35 @@
// worker loader. To make sure the worker loader can access it, it needs to be
// defined before loading the worker loader script below.
let nextId = 0;
const rpcDeferreds = {};
this.rpc = function(method, ...params) {
const id = nextId++;
return new Promise((resolve, reject) => {
const id = nextId++;
this.addEventListener("message", function onMessageForRpc(event) {
const packet = JSON.parse(event.data);
if (packet.type !== "rpc" || packet.id !== id) {
return;
}
if (packet.error) {
reject(packet.error);
} else {
resolve(packet.result);
}
this.removeEventListener("message", onMessageForRpc);
});
postMessage(
JSON.stringify({
type: "rpc",
method,
params,
id,
})
);
const deferred = defer();
rpcDeferreds[id] = deferred;
return deferred.promise;
postMessage(
JSON.stringify({
type: "rpc",
method,
params,
id,
})
);
});
};
loadSubScript("resource://devtools/shared/worker/loader.js");
const defer = worker.require("devtools/shared/defer");
const { WorkerTargetActor } = worker.require(
"devtools/server/actors/targets/worker"
);
@ -134,14 +142,5 @@ this.addEventListener("message", async function(event) {
connections.delete(packet.forwardingPrefix);
}
break;
case "rpc":
const deferred = rpcDeferreds[packet.id];
delete rpcDeferreds[packet.id];
if (packet.error) {
deferred.reject(packet.error);
}
deferred.resolve(packet.result);
break;
}
});