Skip to content

Commit 9875521

Browse files
fix: Keep delegated handlers when unregistering actions (#6395)
## Explanation Keep delegated handlers around even when unregistering actions. This helps with the resiliency of the messenger system in case actions are unregistered and registered elsewhere in the chain of delegations. This is also very useful for testing. Additionally adjusts the error message thrown when a delegated action handler cannot be found to match an undelegated one.
1 parent 44a7b1a commit 9875521

File tree

3 files changed

+5
-34
lines changed

3 files changed

+5
-34
lines changed

packages/messenger/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Keep delegated handlers when unregistering actions ([#6395](https://github.com/MetaMask/core/pull/6395))
13+
1014
## [0.1.0]
1115

1216
### Added

packages/messenger/src/Messenger.test.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,31 +1264,6 @@ describe('Messenger', () => {
12641264
actions: ['Source:getLength'],
12651265
});
12661266

1267-
expect(() => delegatedMessenger.call('Source:getLength', 'test')).toThrow(
1268-
`Cannot call 'Source:getLength', action not registered.`,
1269-
);
1270-
});
1271-
1272-
it('unregisters delegated action handlers when action is unregistered', () => {
1273-
type ExampleAction = {
1274-
type: 'Source:getLength';
1275-
handler: (input: string) => number;
1276-
};
1277-
const sourceMessenger = new Messenger<'Source', ExampleAction, never>({
1278-
namespace: 'Source',
1279-
});
1280-
const delegatedMessenger = new Messenger<
1281-
'Destination',
1282-
ExampleAction,
1283-
never
1284-
>({ namespace: 'Destination' });
1285-
sourceMessenger.delegate({
1286-
messenger: delegatedMessenger,
1287-
actions: ['Source:getLength'],
1288-
});
1289-
1290-
sourceMessenger.unregisterActionHandler('Source:getLength');
1291-
12921267
expect(() => delegatedMessenger.call('Source:getLength', 'test')).toThrow(
12931268
`A handler for Source:getLength has not been registered`,
12941269
);

packages/messenger/src/Messenger.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,6 @@ export class Messenger<
356356
actionType: ActionType,
357357
) {
358358
this.#actions.delete(actionType);
359-
const delegationTargets = this.#actionDelegationTargets.get(actionType);
360-
if (!delegationTargets) {
361-
return;
362-
}
363-
for (const messenger of delegationTargets) {
364-
messenger._internalUnregisterDelegatedActionHandler(actionType);
365-
}
366-
this.#actionDelegationTargets.delete(actionType);
367359
}
368360

369361
/**
@@ -763,7 +755,7 @@ export class Messenger<
763755
| undefined;
764756
if (!actionHandler) {
765757
throw new Error(
766-
`Cannot call '${actionType}', action not registered.`,
758+
`A handler for ${actionType} has not been registered`,
767759
);
768760
}
769761
return actionHandler(...args);

0 commit comments

Comments
 (0)