Skip to content

Commit

Permalink
Merge pull request #108 from SuperViz/fix(realtime)/not-unsubscribing…
Browse files Browse the repository at this point in the history
…-to-event

fix(Realtime): unsubscribe from all callbacks if no callback is provided
  • Loading branch information
carlossantos74 authored Oct 23, 2024
2 parents 17953a4 + 134ecaa commit 80b0814
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.useFakeTimers();

Expand Down Expand Up @@ -77,6 +77,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 @@ -109,6 +109,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 80b0814

Please sign in to comment.