Skip to content

Commit

Permalink
Merge pull request #254 from GetStream/vishal/disconnect-fix
Browse files Browse the repository at this point in the history
Fixing disconnect method
  • Loading branch information
vishalnarkhede committed Feb 10, 2020
2 parents 57af48d + ebbac35 commit 600da6c
Show file tree
Hide file tree
Showing 4 changed files with 33 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
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,23 @@ describe('Chat', function() {
p = anonClient.addDevice('deviceID', 'apn');
await expect(p).to.be.rejectedWith(errorMsg);
});

it('disconnect should always return promise irrespective of wsConnection status', async function() {
const client2 = await getTestClientForUser('bob');
const chan = client2.channel('messaging', uuidv4());
await chan.watch();
let disconnect = client2.disconnect();
await expect(disconnect).to.be.fulfilled;

// Lets try disconnect second time on same client.
disconnect = client2.disconnect();
await expect(disconnect).to.be.fulfilled;

// Lets try deleting websocket connection object.
client2.wsConnection = null;
disconnect = client2.disconnect();
await expect(disconnect).to.be.fulfilled;
});
});

describe('Permissions', function() {
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 600da6c

Please sign in to comment.