From 3a4cfd45a6515fac5a4205d0c2503864efbd33bf Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 4 Jul 2023 23:03:49 -0400 Subject: [PATCH] fix: need to loop over all protomux message args and await any promises --- src/index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 4193a1d..0daea8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -576,10 +576,17 @@ async function handleCreateProtomuxMessage(aq: ActiveQuery) { }); 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( (item: any) => item?.constructor.name.toLowerCase() !== "channel", ); + aq.sendUpdate({ action: "onmessage", args, @@ -622,3 +629,11 @@ function getSwarmToSwarmId(swarm: any) { } function noop() {} + +function isPromise(obj: Promise) { + return ( + !!obj && + (typeof obj === "object" || typeof obj === "function") && + typeof obj.then === "function" + ); +}