Skip to content

Commit 6a04f54

Browse files
committed
Update clearActions to handle delegated actions
1 parent 62cf851 commit 6a04f54

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/messenger/src/Messenger.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,31 @@ describe('Messenger', () => {
186186
expect(pingCount).toBe(0);
187187
});
188188

189+
it('should throw when calling a delegated action after actions have been reset', () => {
190+
type PingAction = { type: 'Fixture:ping'; handler: () => void };
191+
const messenger = new Messenger<'Fixture', PingAction, never>({
192+
namespace: 'Fixture',
193+
});
194+
let pingCount = 0;
195+
messenger.registerActionHandler('Fixture:ping', () => {
196+
pingCount += 1;
197+
});
198+
const delegatedMessenger = new Messenger<'Destination', PingAction, never>({
199+
namespace: 'Destination',
200+
});
201+
messenger.delegate({
202+
messenger: delegatedMessenger,
203+
actions: ['Fixture:ping'],
204+
});
205+
206+
messenger.clearActions();
207+
208+
expect(() => {
209+
delegatedMessenger.call('Fixture:ping');
210+
}).toThrow('A handler for Fixture:ping has not been registered');
211+
expect(pingCount).toBe(0);
212+
});
213+
189214
it('should publish event to subscriber', () => {
190215
type MessageEvent = { type: 'Fixture:message'; payload: [string] };
191216
const messenger = new Messenger<'Fixture', never, MessageEvent>({

packages/messenger/src/Messenger.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ export class Messenger<
321321
* This prevents all actions from being called.
322322
*/
323323
clearActions() {
324-
this.#actions.clear();
324+
for (const actionType of this.#actions.keys()) {
325+
this.#unregisterActionHandler(actionType);
326+
}
325327
}
326328

327329
/**

0 commit comments

Comments
 (0)