Compare commits
2 Commits
b42069231c
...
2832694cd5
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 2832694cd5 | |
Derrick Hammer | acade9801f |
19
src/index.ts
19
src/index.ts
|
@ -52,6 +52,7 @@ function idFactory(start = 1) {
|
||||||
const getSwarmId = idFactory();
|
const getSwarmId = idFactory();
|
||||||
const getSocketId = idFactory();
|
const getSocketId = idFactory();
|
||||||
const getChannelId = idFactory();
|
const getChannelId = idFactory();
|
||||||
|
const getMessageId = idFactory();
|
||||||
|
|
||||||
addHandler("presentSeed", handlePresentSeed);
|
addHandler("presentSeed", handlePresentSeed);
|
||||||
addHandler("join", handleJoin);
|
addHandler("join", handleJoin);
|
||||||
|
@ -514,7 +515,7 @@ async function createProtomuxMessage(aq: ActiveQuery) {
|
||||||
|
|
||||||
const data = aq.callerInput.data;
|
const data = aq.callerInput.data;
|
||||||
|
|
||||||
const defers: { [action: string]: DeferredPromise<any> } = {};
|
const defers = new Map<number, DeferredPromise<any>>();
|
||||||
|
|
||||||
const handleEncoding = (enabled: boolean) => {
|
const handleEncoding = (enabled: boolean) => {
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
|
@ -522,14 +523,16 @@ async function createProtomuxMessage(aq: ActiveQuery) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const update = async (action: string, args: any) => {
|
const update = async (action: string, args: any) => {
|
||||||
await defers[action]?.promise;
|
const messageId = getMessageId();
|
||||||
defers[action] = defer();
|
const d = defer();
|
||||||
|
defers.set(messageId, d);
|
||||||
aq.sendUpdate({
|
aq.sendUpdate({
|
||||||
|
id: messageId,
|
||||||
action,
|
action,
|
||||||
args,
|
args,
|
||||||
});
|
});
|
||||||
|
|
||||||
const ret = await defers[action]?.promise;
|
const ret = (await d.promise) as any;
|
||||||
if (ret[1]) {
|
if (ret[1]) {
|
||||||
if (ret[1].buffer) {
|
if (ret[1].buffer) {
|
||||||
args[0].buffer = b4a.from(ret[1].buffer);
|
args[0].buffer = b4a.from(ret[1].buffer);
|
||||||
|
@ -556,12 +559,12 @@ async function createProtomuxMessage(aq: ActiveQuery) {
|
||||||
};
|
};
|
||||||
|
|
||||||
aq.setReceiveUpdate?.((data) => {
|
aq.setReceiveUpdate?.((data) => {
|
||||||
if (["send", "destroy"].includes(data.action)) {
|
if (data.action === "send") {
|
||||||
message[data.action](...data.args);
|
message.send(...data.args);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defers[data.action]?.resolve(data.args);
|
defers.get(data.id)?.resolve(data.args);
|
||||||
|
defers.delete(data.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (data.onmessage) {
|
if (data.onmessage) {
|
||||||
|
|
Loading…
Reference in New Issue