fix: need to loop over all protomux message args and await any promises

This commit is contained in:
Derrick Hammer 2023-07-04 23:03:49 -04:00
parent 411d35154f
commit 3a4cfd45a6
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 16 additions and 1 deletions

View File

@ -576,10 +576,17 @@ async function handleCreateProtomuxMessage(aq: ActiveQuery) {
}); });
if (data.onmessage) { if (data.onmessage) {
data.onmessage = (...args: any) => { data.onmessage = async (...args: any) => {
for (let i = 0; i < args.length; i++) {
if (isPromise(args[i])) {
args[i] = await args[i];
}
}
args = args.filter( args = args.filter(
(item: any) => item?.constructor.name.toLowerCase() !== "channel", (item: any) => item?.constructor.name.toLowerCase() !== "channel",
); );
aq.sendUpdate({ aq.sendUpdate({
action: "onmessage", action: "onmessage",
args, args,
@ -622,3 +629,11 @@ function getSwarmToSwarmId(swarm: any) {
} }
function noop() {} function noop() {}
function isPromise(obj: Promise<any>) {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function"
);
}