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): Dispose of subscription on complete or error messages #23

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
fix: actually dispose on error or complete
enisdenjo committed Oct 1, 2020

Verified

This commit was signed with the committer’s verified signature.
enisdenjo Denis Badurina
commit 273f565f63e8d5c6b169e77ef41e63da9b39595b
14 changes: 12 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -424,6 +424,7 @@ export function createClient(options: ClientOptions): Client {
on: emitter.on,
subscribe(payload, sink) {
const id = generateID();
const cancellerRef: CancellerRef = { current: null };

const messageHandler = ({ data }: MessageEvent) => {
const message = memoParseMessage(data);
@@ -438,19 +439,28 @@ export function createClient(options: ClientOptions): Client {
case MessageType.Error: {
if (message.id === id) {
sink.error(message.payload);
// the canceller must be set at this point
// because you cannot receive a message
// if there is no existing connection
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
cancellerRef.current!();
}
return;
}
case MessageType.Complete: {
if (message.id === id) {
sink.complete();
// the canceller must be set at this point
// because you cannot receive a message
// if there is no existing connection
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
cancellerRef.current!();
}
return;
}
}
};

const cancellerRef: CancellerRef = { current: null };
(async () => {
for (;;) {
try {
@@ -512,7 +522,7 @@ export function createClient(options: ClientOptions): Client {
})()
.catch(sink.error)
.then(sink.complete) // resolves on cancel or normal closure
.finally(cancellerRef.current);
.finally(() => (cancellerRef.current = null)); // when this promise settles there is nothing to cancel

return () => {
if (cancellerRef.current) {
50 changes: 50 additions & 0 deletions src/tests/client.ts
Original file line number Diff line number Diff line change
@@ -281,6 +281,56 @@ describe('subscription operation', () => {

expect(generateIDFn).toBeCalled();
});

it('should dispose of the subscription on complete', async () => {
const client = createClient({ url });

const completeFn = jest.fn();
client.subscribe(
{
query: `{
getValue
}`,
},
{
next: noop,
error: () => {
fail(`Unexpected error call`);
},
complete: completeFn,
},
);
await wait(20);

expect(completeFn).toBeCalled();

await wait(20);
expect(server.webSocketServer.clients.size).toBe(0);
});

it('should dispose of the subscription on error', async () => {
const client = createClient({ url });

const errorFn = jest.fn();
client.subscribe(
{
query: `{
iDontExist
}`,
},
{
next: noop,
error: errorFn,
complete: noop,
},
);
await wait(20);

expect(errorFn).toBeCalled();

await wait(20);
expect(server.webSocketServer.clients.size).toBe(0);
});
});

describe('"concurrency"', () => {