-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(base-controller): Fix selectors initial value comparison (#3697)
## Explanation The "selectors" feature of the `ControllerMessenger` did not work correctly for the first event published after subscribing, because the original value of the payload (at time of subscribing) was not available for comparison. To address this, a `registerInitialEventPayload` method has been added to the `ControllerMessenger` and `RestrictedControllerMessenger`. This allows registering a function that returns the "event payload state" at a given time, which is used internally by the `subscribe` method to correctly initialize the payload cache when a subscriber with a selector is added. This ensures that the selector works correctly even for the initial state change, in that it will return the correct previous value, and not fire at all if the selected piece of state is unchanged. In a later PR, `BaseControllerV2` can be updated to register an event payload for the `stateChange` event. This will fix this flaw for the `stateChange` event of each controller. ## References Fixes #3649 ## Changelog ### `@metamask/base-controller` - Added: Add `registerInitialEventPayload` to `ControllerMessenger` and `RestrictedControllerMessenger` - This allows registering an event payload function for an event, which has the benefit of ensuring the "subscription selector" feature works correctly the first time the event is fired after subscribing. - Fixed: Fix `subscribe` method selector support on first publish - An event with a registered initial event payload function will work better with selectors, in that it will correctly compare with the initial selected state and return the previous value the first time it's published. Without this, the initial published event will always return `undefined` as the previous value. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate
- Loading branch information
Showing
4 changed files
with
556 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.