* Refactor index.ts by checking if ret[1].buffer exists before assigning it to args[0].buffer, updating the update function to decode instead of encode, sending message if the action is "send", updating onmessage to include only relevant arguments, and adding a noop function.

This commit is contained in:
Derrick Hammer 2023-04-08 20:07:10 -04:00
parent 64ec4bb91c
commit 7206624cb5
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 25 additions and 5 deletions

View File

@ -530,9 +530,11 @@ async function createProtomuxMessage(aq: ActiveQuery) {
}); });
const ret = await defers[action]?.promise; const ret = await defers[action]?.promise;
if (ret[1]) { if (ret[1]) {
args[0].buffer = Buffer.from(ret[1].buffer); if (ret[1].buffer) {
args[0].buffer = b4a.from(ret[1].buffer);
}
args[0].start = ret[1].start; args[0].start = ret[1].start;
args[0].end = ret[1].end; args[0].end = ret[1].end;
} }
@ -548,18 +550,34 @@ async function createProtomuxMessage(aq: ActiveQuery) {
return update("encode", args); return update("encode", args);
}, },
async decode(...args: any) { async decode(...args: any) {
return update("encode", args); return update("decode", args);
}, },
}; };
}; };
aq.setReceiveUpdate?.((data) => { aq.setReceiveUpdate?.((data) => {
defers[data.action]?.resolve(data.args[0]); if (data.action === "send") {
message.send(...data.args);
}
defers[data.action]?.resolve(data.args);
}); });
if (data.onmessage) {
data.onmessage = (...args: any) => {
args = args.filter(
(item: any) => item?.constructor.name.toLowerCase() !== "channel"
);
aq.sendUpdate({
action: "onmessage",
args,
});
};
}
const message = channel.addMessage({ const message = channel.addMessage({
encoding: handleEncoding(data.encoding ?? false), encoding: handleEncoding(data.encoding ?? false),
onmessage: data.encoding ?? undefined, onmessage: data.onmessage ?? noop,
}); });
aq.sendUpdate({ aq.sendUpdate({
@ -586,3 +604,5 @@ function getSwarmToSwarmId(swarm: any) {
return false; return false;
} }
function noop() {}