From 5a5ae4d01afd0c9aa51090342d7f699daf4fbafc Mon Sep 17 00:00:00 2001 From: Rodrigo Pastro Date: Thu, 12 Nov 2020 16:14:47 -0500 Subject: [PATCH] fix(client): One cleanup per subscription (#67) * Prevent canceller from being invoked twice for same subscription * apply suggestions * fix: remove canceller on close Co-authored-by: enisdenjo --- src/client.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 1e8558b5..950c2f9a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -243,12 +243,14 @@ export function createClient(options: ClientOptions): Client { state.socket.addEventListener('close', listener); function listener(event: CloseEvent) { + cancellerRef.current = null; state.locks--; state.socket?.removeEventListener('close', listener); return reject(event); } cancellerRef.current = () => { + cancellerRef.current = null; cleanup?.(); state.locks--; if (!state.locks) { @@ -369,12 +371,14 @@ export function createClient(options: ClientOptions): Client { socket.addEventListener('close', listener); function listener(event: CloseEvent) { + cancellerRef.current = null; state.locks--; socket.removeEventListener('close', listener); return reject(event); } cancellerRef.current = () => { + cancellerRef.current = null; cleanup?.(); state.locks--; if (!state.locks) { @@ -539,8 +543,7 @@ export function createClient(options: ClientOptions): Client { } })() .catch(sink.error) - .then(sink.complete) // resolves on cancel or normal closure - .finally(() => (cancellerRef.current = null)); // when this promise settles there is nothing to cancel + .then(sink.complete); // resolves on cancel or normal closure return () => { cancellerRef.current?.();