Skip to content

Commit

Permalink
Merge pull request #651 from GetStream/vishal/channel-unsubscribe
Browse files Browse the repository at this point in the history
Return unsubscribe handler from `channel.on`
  • Loading branch information
vishalnarkhede committed Mar 19, 2021
2 parents 6468ec4 + fe1a3d2 commit 1ae1fca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ export class Channel<
ReactionType,
UserType
>,
): void;
): { unsubscribe: () => void };
on(
callback: EventHandler<
AttachmentType,
Expand All @@ -1391,7 +1391,7 @@ export class Channel<
ReactionType,
UserType
>,
): void;
): { unsubscribe: () => void };
on(
callbackOrString:
| EventHandler<
Expand All @@ -1413,7 +1413,7 @@ export class Channel<
ReactionType,
UserType
>,
): void {
): { unsubscribe: () => void } {
const key = callbackOrNothing ? (callbackOrString as string) : 'all';
const valid = isValidEventType(key);
if (!valid) {
Expand All @@ -1433,6 +1433,18 @@ export class Channel<
);

this.listeners[key].push(callback);

return {
unsubscribe: () => {
this._client.logger(
'info',
`Removing listener for ${key} event from channel ${this.cid}`,
{ tags: ['event', 'channel'], channel: this },
);

this.listeners[key] = this.listeners[key].filter((el) => el !== callback);
},
};
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,10 @@ export class StreamChat<
this.listeners[key].push(callback);
return {
unsubscribe: () => {
this.logger('info', `Removing listener for ${key} event`, {
tags: ['event', 'client'],
});

this.listeners[key] = this.listeners[key].filter((el) => el !== callback);
},
};
Expand Down
18 changes: 18 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateChannel } from './test-utils/generateChannel';
import { generateMember } from './test-utils/generateMember';
import { generateMsg } from './test-utils/generateMessage';
import { generateUser } from './test-utils/generateUser';
import { getClientWithUser } from './test-utils/getClient';
import { getOrCreateChannelApi } from './test-utils/getOrCreateChannelApi';

import { StreamChat } from '../../src/client';
Expand Down Expand Up @@ -396,3 +397,20 @@ describe('Ensure single channel per cid on client activeChannels state', () => {
expect(channelVish_copy1).to.be.equal(channelVish_copy2);
});
});

describe.only('event subscription and unsubscription', () => {
it('channel.on should return unsubscribe handler', async () => {
const client = await getClientWithUser();
const channel = client.channel('messaging', uuidv4());

const { unsubscribe: unsubscribe1 } = channel.on('message.new', () => {});
const { unsubscribe: unsubscribe2 } = channel.on(() => {});

expect(Object.values(channel.listeners).length).to.be.equal(2);

unsubscribe1();
expect(channel.listeners['message.new'].length).to.be.equal(0);
unsubscribe2();
expect(channel.listeners['all'].length).to.be.equal(0);
});
});

0 comments on commit 1ae1fca

Please sign in to comment.