Skip to content

Commit

Permalink
events fix id mapping + refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzoum authored and dib542 committed Jul 8, 2022
1 parent 2e50a7a commit ff832b2
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/lib/web3/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ export function createSubscriptionManager(
id: number;
};
} = {};
const idListenerMap: {
[id: number]: typeof listeners[keyof typeof listeners];
let idListenerMap: {
[id: number]: typeof listeners[keyof typeof listeners] | null;
} = {};
const openListeners: Array<() => void> = [];
const closeListeners: Array<() => void> = [];
Expand Down Expand Up @@ -325,7 +325,12 @@ export function createSubscriptionManager(
currentSocket.addEventListener('message', bubbleOnMessage);
Object.entries(listeners).forEach(([paramQuery, group]) => {
// refresh ids after a new connection
idListenerMap[group.idUnsub] = null;
idListenerMap[group.id] = null;
group.id = createID();
group.idUnsub = createID();
idListenerMap[group.idUnsub] = group;
idListenerMap[group.id] = group;
// send pending requests
if (
group.status === QueryStatus.Disconnected &&
Expand Down Expand Up @@ -388,6 +393,7 @@ export function createSubscriptionManager(
function close() {
unsubscribeAll();
if (isOpen()) socket?.close();
idListenerMap = {};
socket = null;
}

Expand Down Expand Up @@ -428,21 +434,20 @@ export function createSubscriptionManager(
eventType: EventType,
options: SubscriptionOptions
) {
const id = createID(),
idUnsub = createID();
const paramQuery = getParamQuery(eventType, options);
listeners[paramQuery] = listeners[paramQuery] || {
const listenerGroup = listeners[paramQuery] || {
status: QueryStatus.Disconnected,
idUnsub: idUnsub,
idUnsub: createID(),
callBacks: [],
id: id,
id: createID(),
};
idListenerMap[id] = listeners[paramQuery];
idListenerMap[idUnsub] = listeners[paramQuery];
listeners[paramQuery].callBacks.push(onMessage);
listeners[paramQuery] = listenerGroup;
idListenerMap[listenerGroup.id] = listenerGroup;
idListenerMap[listenerGroup.idUnsub] = listenerGroup;
listenerGroup.callBacks.push(onMessage);
if (isOpen()) {
sendMessage('subscribe', paramQuery, id);
listeners[paramQuery].status = QueryStatus.Connecting;
sendMessage('subscribe', paramQuery, listenerGroup.id);
listenerGroup.status = QueryStatus.Connecting;
} else if (!socket) {
open();
}
Expand Down Expand Up @@ -548,6 +553,7 @@ export function createSubscriptionManager(
if (cb.status === QueryStatus.Disconnecting)
cb.status = QueryStatus.Disconnected;
});
idListenerMap = {};
return;
}
const listenerGroup = idListenerMap[id];
Expand Down

0 comments on commit ff832b2

Please sign in to comment.