Skip to content

Commit

Permalink
perf(client): Focus subscription message listeners on id (enisdenjo…
Browse files Browse the repository at this point in the history
…#150)

Co-authored-by: Denis Badurina <denis@domonda.com>
  • Loading branch information
michelepra and enisdenjo committed Mar 29, 2021
1 parent d99982b commit 32c2268
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/client.ts
Expand Up @@ -293,15 +293,30 @@ export function createClient(options: ClientOptions): Client {

// websocket status emitter, subscriptions are handled differently
const emitter = (() => {
const message = (() => {
const listeners: { [key: string]: EventMessageListener } = {};
return {
on(id: string, listener: EventMessageListener) {
listeners[id] = listener;
return () => {
delete listeners[id];
};
},
emit(message: Message) {
if ('id' in message) listeners[message.id]?.(message);
},
};
})();
const listeners: { [event in Event]: EventListener<event>[] } = {
connecting: on?.connecting ? [on.connecting] : [],
connected: on?.connected ? [on.connected] : [],
message: on?.message ? [on.message] : [],
message: on?.message ? [message.emit, on.message] : [message.emit],
closed: on?.closed ? [on.closed] : [],
error: on?.error ? [on.error] : [],
};

return {
onMessage: message.on,
on<E extends Event>(event: E, listener: EventListener<E>) {
const l = listeners[event] as EventListener<E>[];
l.push(listener);
Expand Down Expand Up @@ -518,28 +533,24 @@ export function createClient(options: ClientOptions): Client {
// if completed while waiting for connect, release the connection lock right away
if (completed) return release();

const unlisten = emitter.on('message', (message) => {
const unlisten = emitter.onMessage(id, (message) => {
switch (message.type) {
case MessageType.Next: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (message.id === id) sink.next(message.payload as any);
sink.next(message.payload as any);
return;
}
case MessageType.Error: {
if (message.id === id) {
completed = true;
sink.error(message.payload);
releaser();
// TODO-db-201025 calling releaser will complete the sink, meaning that both the `error` and `complete` will be
// called. neither promises or observables care; once they settle, additional calls to the resolvers will be ignored
}
completed = true;
sink.error(message.payload);
releaser();
// TODO-db-201025 calling releaser will complete the sink, meaning that both the `error` and `complete` will be
// called. neither promises or observables care; once they settle, additional calls to the resolvers will be ignored
return;
}
case MessageType.Complete: {
if (message.id === id) {
completed = true;
releaser(); // release completes the sink
}
completed = true;
releaser(); // release completes the sink
return;
}
}
Expand Down

0 comments on commit 32c2268

Please sign in to comment.