Skip to content

Commit

Permalink
fix(Realtime): unsubscribe from all callbacks if no callback is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Raspincel committed Oct 23, 2024
1 parent 6307f8a commit 134ecaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/realtime/src/services/channel/channel.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LIMITS_MOCK } from '../../../__mocks__/limits.mock';
import { MOCK_OBSERVER_HELPER } from '../../../__mocks__/observer-helper.mock';
import { MOCK_LOCAL_PARTICIPANT } from '../../../__mocks__/participants.mock';
import { RealtimeChannelState } from '../../component/types';
import { IOC } from '../io';

import { Channel } from './channel';
import { RealtimeChannelState } from '../../component/types';

jest.mock('lodash/throttle', () => jest.fn((fn) => fn));
jest.useFakeTimers();
Expand Down Expand Up @@ -78,6 +78,15 @@ describe('Realtime Channel', () => {

expect(spy).toHaveBeenCalled();
});

test('should reset an event if no callback is provided', () => {
ChannelInstance['observers']['test'] = MOCK_OBSERVER_HELPER;
const spy = jest.spyOn(ChannelInstance['observers']['test'], 'reset' as any);

ChannelInstance.unsubscribe('test');

expect(spy).toHaveBeenCalled();
});
});

describe('subscribeToRealtimeEvents', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/realtime/src/services/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export class Channel extends Observable {
event: string,
callback?: Callback<T>,
): void => {
if (!callback) {
this.observers[event]?.reset();
return;
}

this.observers[event]?.unsubscribe(callback);
};

Expand Down

0 comments on commit 134ecaa

Please sign in to comment.