Skip to content

Commit

Permalink
fix: messageEvents object to manage uid and originating resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Dec 15, 2023
1 parent d87fd2e commit 22de227
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
frameId: uuid.generate(8),
connected: false,
listeners: new Map(),
messageEvents: {},
messageQueue: new Map(), // required per url already per url when isBrowser and indexeddb.
configQueue: new Map(),
maxReconnectDelay: 600000,
Expand Down Expand Up @@ -371,12 +372,12 @@
let isAwait
for (let socket of sockets) {
data.socketId = socket.id;
if (!this.messageEvents[uid])
this.addListener(uid, resolve)

if (socket.connected && this.serverOrganization && this.serverStorage && this.organizationBalance) {
if (data.status === 'resolve')
resolve(data)
else if (data.status !== 'queued')
this.addListener(uid, resolve)

if (data.status === 'await')
isAwait = true
Expand Down Expand Up @@ -409,21 +410,25 @@
organization_id: data.organization_id
})
}
} else if (!indexeddb)
} else if (!indexeddb && data.status === "queued")
this.messageQueue.set(uid, data);

}
});
},

addListener(uid, resolve) {
this.messageEvents[uid] = resolve
if (isBrowser) {
const self = this
window.addEventListener(uid, function (event) {
delete self.messageEvents[uid]
// here we have access to request and new data
resolve(event.detail);
}, { once: true });
} else {
process.once(uid, (data) => {
delete this.messageEvents[uid]
resolve(data);
});
}
Expand Down

0 comments on commit 22de227

Please sign in to comment.