From 134ecaa4e3d8b7b92e8aa65af32ff3d7a13b93d8 Mon Sep 17 00:00:00 2001 From: Ian Silva Date: Wed, 23 Oct 2024 09:32:28 -0300 Subject: [PATCH] fix(Realtime): unsubscribe from all callbacks if no callback is provided --- .../realtime/src/services/channel/channel.test.ts | 11 ++++++++++- packages/realtime/src/services/channel/channel.ts | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/realtime/src/services/channel/channel.test.ts b/packages/realtime/src/services/channel/channel.test.ts index 4c0f758..b711880 100644 --- a/packages/realtime/src/services/channel/channel.test.ts +++ b/packages/realtime/src/services/channel/channel.test.ts @@ -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(); @@ -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', () => { diff --git a/packages/realtime/src/services/channel/channel.ts b/packages/realtime/src/services/channel/channel.ts index 4ead125..7067a4b 100644 --- a/packages/realtime/src/services/channel/channel.ts +++ b/packages/realtime/src/services/channel/channel.ts @@ -110,6 +110,11 @@ export class Channel extends Observable { event: string, callback?: Callback, ): void => { + if (!callback) { + this.observers[event]?.reset(); + return; + } + this.observers[event]?.unsubscribe(callback); };