Skip to content

Commit

Permalink
fix(client): send complete message and close only if socket is still …
Browse files Browse the repository at this point in the history
…open
  • Loading branch information
enisdenjo committed Aug 26, 2020
1 parent 5c01ed9 commit 49b75ce
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,23 @@ export function createClient(options: ClientOptions): Client {

return () => {
if (socket) {
socket.send(
stringifyMessage<MessageType.Complete>({
id: uuid,
type: MessageType.Complete,
}),
);
if (socket.readyState === WebSocket.OPEN) {
socket.send(
stringifyMessage<MessageType.Complete>({
id: uuid,
type: MessageType.Complete,
}),
);
}

socket.removeEventListener('message', handleMessage);

// equal to 1 because this sink is the last one.
// the deletion from the map happens afterwards
if (Object.entries(subscribedSinks).length === 1) {
socket.close(1000, 'Normal Closure');
if (socket.readyState === WebSocket.OPEN) {
socket.close(1000, 'Normal Closure');
}
socket = null;
}
}
Expand Down

0 comments on commit 49b75ce

Please sign in to comment.