Skip to content

Commit

Permalink
Fixing disconnect method
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Feb 8, 2020
1 parent 57af48d commit 48c5a1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ export class StreamChat {
if (this.wsConnection) {
return this.wsConnection.disconnect();
}

return Promise.resolve();
}

setAnonymousUser() {
Expand Down
18 changes: 13 additions & 5 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ export class StableWSConnection {

let isClosedPromise;
// and finally close...
if (this.ws && this.ws.close) {
// Assigning to local here because we will remove it from this before the
// promise resolves.
const { ws } = this;
// Assigning to local here because we will remove it from this before the
// promise resolves.
const { ws } = this;
if (ws && ws.close && ws.readyState === ws.OPEN) {
isClosedPromise = new Promise(resolve => {
ws.onclose = () => {
this.logger(
Expand All @@ -148,7 +148,6 @@ export class StableWSConnection {
resolve();
};
});

this.logger(
'info',
`connection:disconnect() - Manually closed connection by calling client.disconnect()`,
Expand All @@ -158,6 +157,15 @@ export class StableWSConnection {
);

ws.close(1000, 'Manually closed connection by calling client.disconnect()');
} else {
this.logger(
'info',
`connection:disconnect() - ws connection doesn't exist or it is already closed.`,
{
tags: ['connection'],
},
);
isClosedPromise = Promise.resolve();
}

delete this.ws;
Expand Down
2 changes: 1 addition & 1 deletion types/stream-chat/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export class StableWSConnection {
eventCallback: (event: ConnectionChangeEvent) => void,
);
connect(): Promise<void>;
disconnect(): void;
disconnect(): Promise<void>;
onlineStatusChanged(event: OnlineStatusEvent): void;
onopen(wsID: number): void;
onmessage(wsID: number, event: WebSocket.MessageEvent): void;
Expand Down

0 comments on commit 48c5a1a

Please sign in to comment.