Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): One cleanup per subscription #67

Merged
merged 4 commits into from
Nov 12, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export function createClient(options: ClientOptions): Client {
}

cancellerRef.current = () => {
cancellerRef.current = null;
cleanup?.();
state.locks--;
if (!state.locks) {
Expand Down Expand Up @@ -375,6 +376,7 @@ export function createClient(options: ClientOptions): Client {
}

cancellerRef.current = () => {
cancellerRef.current = null;
cleanup?.();
state.locks--;
if (!state.locks) {
Expand Down Expand Up @@ -497,6 +499,7 @@ export function createClient(options: ClientOptions): Client {
// either the canceller will be called and the promise resolved
// or the socket closed and the promise rejected
await throwOnCloseOrWaitForCancel(() => {
cancellerRef.current = null;
enisdenjo marked this conversation as resolved.
Show resolved Hide resolved
// if not completed already, send complete message to server on cancel
if (!completed) {
socket.send(
Expand Down Expand Up @@ -539,8 +542,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?.();
Expand Down