@metamask-previews/base-controller
Advanced tools
Comparing version 4.0.1-preview.fa529884 to 4.1.0-preview.1ef189c
@@ -9,2 +9,14 @@ # Changelog | ||
## [4.1.0] | ||
### Added | ||
- Add `registerInitialEventPayload` to `ControllerMessenger` and `RestrictedControllerMessenger` ([#3697](https://github.com/MetaMask/core/pull/3697)) | ||
- 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 ([#3697](https://github.com/MetaMask/core/pull/3697)) | ||
- 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. | ||
- Subscribers to the `stateChange` event of any `BaseControllerV2`-based controllers will now correctly handle the initial state change event ([#3702](https://github.com/MetaMask/core/pull/3702)) | ||
- Previously the initial state change would always result in this event firing, even for subscriptions with selectors where the selected value has not changed. Additionally, the `previousValue` returned was always set to `undefined` the first time. | ||
- `BaseControllerV2` has been updated to correctly compare with the previous value even for the first state change. The returned `previousValue` is also now guaranteed to be correct even for the initial state change. | ||
## [4.0.1] | ||
@@ -107,3 +119,4 @@ ### Changed | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/base-controller@4.0.1...HEAD | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/base-controller@4.1.0...HEAD | ||
[4.1.0]: https://github.com/MetaMask/core/compare/@metamask/base-controller@4.0.1...@metamask/base-controller@4.1.0 | ||
[4.0.1]: https://github.com/MetaMask/core/compare/@metamask/base-controller@4.0.0...@metamask/base-controller@4.0.1 | ||
@@ -110,0 +123,0 @@ [4.0.0]: https://github.com/MetaMask/core/compare/@metamask/base-controller@3.2.3...@metamask/base-controller@4.0.0 |
@@ -39,2 +39,6 @@ "use strict"; | ||
this.messagingSystem.registerActionHandler(`${name}:getState`, () => this.state); | ||
this.messagingSystem.registerInitialEventPayload({ | ||
eventType: `${name}:stateChange`, | ||
getPayload: () => [this.state, []], | ||
}); | ||
} | ||
@@ -41,0 +45,0 @@ /** |
@@ -101,2 +101,17 @@ import { RestrictedControllerMessenger } from './RestrictedControllerMessenger'; | ||
/** | ||
* Register a function for getting the initial payload for an event. | ||
* | ||
* This is used for events that represent a state change, where the payload is the state. | ||
* Registering a function for getting the payload allows event selectors to have a point of | ||
* comparison the first time state changes. | ||
* | ||
* @param args - The arguments to this function | ||
* @param args.eventType - The event type to register a payload for. | ||
* @param args.getPayload - A function for retrieving the event payload. | ||
*/ | ||
registerInitialEventPayload<EventType extends Event['type']>({ eventType, getPayload, }: { | ||
eventType: EventType; | ||
getPayload: () => ExtractEventPayload<Event, EventType>; | ||
}): void; | ||
/** | ||
* Publish an event. | ||
@@ -103,0 +118,0 @@ * |
@@ -7,3 +7,3 @@ "use strict"; | ||
}; | ||
var _ControllerMessenger_actions, _ControllerMessenger_events, _ControllerMessenger_eventPayloadCache; | ||
var _ControllerMessenger_actions, _ControllerMessenger_events, _ControllerMessenger_initialEventPayloadGetters, _ControllerMessenger_eventPayloadCache; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,2 +27,8 @@ exports.ControllerMessenger = void 0; | ||
/** | ||
* A map of functions for getting the initial event payload. | ||
* | ||
* Used only for events that represent state changes. | ||
*/ | ||
_ControllerMessenger_initialEventPayloadGetters.set(this, new Map()); | ||
/** | ||
* A cache of selector return values for their respective handlers. | ||
@@ -89,2 +95,16 @@ */ | ||
/** | ||
* Register a function for getting the initial payload for an event. | ||
* | ||
* This is used for events that represent a state change, where the payload is the state. | ||
* Registering a function for getting the payload allows event selectors to have a point of | ||
* comparison the first time state changes. | ||
* | ||
* @param args - The arguments to this function | ||
* @param args.eventType - The event type to register a payload for. | ||
* @param args.getPayload - A function for retrieving the event payload. | ||
*/ | ||
registerInitialEventPayload({ eventType, getPayload, }) { | ||
__classPrivateFieldGet(this, _ControllerMessenger_initialEventPayloadGetters, "f").set(eventType, getPayload); | ||
} | ||
/** | ||
* Publish an event. | ||
@@ -136,2 +156,9 @@ * | ||
subscribers.set(handler, selector); | ||
if (selector) { | ||
const getPayload = __classPrivateFieldGet(this, _ControllerMessenger_initialEventPayloadGetters, "f").get(eventType); | ||
if (getPayload) { | ||
const initialValue = selector(...getPayload()); | ||
__classPrivateFieldGet(this, _ControllerMessenger_eventPayloadCache, "f").set(handler, initialValue); | ||
} | ||
} | ||
} | ||
@@ -214,3 +241,3 @@ /** | ||
exports.ControllerMessenger = ControllerMessenger; | ||
_ControllerMessenger_actions = new WeakMap(), _ControllerMessenger_events = new WeakMap(), _ControllerMessenger_eventPayloadCache = new WeakMap(); | ||
_ControllerMessenger_actions = new WeakMap(), _ControllerMessenger_events = new WeakMap(), _ControllerMessenger_initialEventPayloadGetters = new WeakMap(), _ControllerMessenger_eventPayloadCache = new WeakMap(); | ||
//# sourceMappingURL=ControllerMessenger.js.map |
@@ -87,2 +87,19 @@ import type { ActionConstraint, ActionHandler, ControllerMessenger, EventConstraint, ExtractActionParameters, ExtractActionResponse, ExtractEventHandler, ExtractEventPayload, NamespacedName, NotNamespacedBy, SelectorEventHandler, SelectorFunction } from './ControllerMessenger'; | ||
/** | ||
* Register a function for getting the initial payload for an event. | ||
* | ||
* This is used for events that represent a state change, where the payload is the state. | ||
* Registering a function for getting the payload allows event selectors to have a point of | ||
* comparison the first time state changes. | ||
* | ||
* The event type *must* be in the current namespace | ||
* | ||
* @param args - The arguments to this function | ||
* @param args.eventType - The event type to register a payload for. | ||
* @param args.getPayload - A function for retrieving the event payload. | ||
*/ | ||
registerInitialEventPayload<EventType extends Event['type'] & NamespacedName<Namespace>>({ eventType, getPayload, }: { | ||
eventType: EventType; | ||
getPayload: () => ExtractEventPayload<Event, EventType>; | ||
}): void; | ||
/** | ||
* Publish an event. | ||
@@ -89,0 +106,0 @@ * |
@@ -123,2 +123,25 @@ "use strict"; | ||
/** | ||
* Register a function for getting the initial payload for an event. | ||
* | ||
* This is used for events that represent a state change, where the payload is the state. | ||
* Registering a function for getting the payload allows event selectors to have a point of | ||
* comparison the first time state changes. | ||
* | ||
* The event type *must* be in the current namespace | ||
* | ||
* @param args - The arguments to this function | ||
* @param args.eventType - The event type to register a payload for. | ||
* @param args.getPayload - A function for retrieving the event payload. | ||
*/ | ||
registerInitialEventPayload({ eventType, getPayload, }) { | ||
/* istanbul ignore if */ // Branch unreachable with valid types | ||
if (!__classPrivateFieldGet(this, _RestrictedControllerMessenger_instances, "m", _RestrictedControllerMessenger_isInCurrentNamespace).call(this, eventType)) { | ||
throw new Error(`Only allowed publishing events prefixed by '${__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerName, "f")}:'`); | ||
} | ||
__classPrivateFieldGet(this, _RestrictedControllerMessenger_controllerMessenger, "f").registerInitialEventPayload({ | ||
eventType, | ||
getPayload, | ||
}); | ||
} | ||
/** | ||
* Publish an event. | ||
@@ -125,0 +148,0 @@ * |
{ | ||
"name": "@metamask-previews/base-controller", | ||
"version": "4.0.1-preview.fa529884", | ||
"version": "4.1.0-preview.1ef189c", | ||
"description": "Provides scaffolding for controllers as well a communication system for all controllers", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@metamask/utils": "^8.2.0", | ||
"@metamask/utils": "^8.3.0", | ||
"immer": "^9.0.6" | ||
@@ -37,0 +37,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
161090
1477
Updated@metamask/utils@^8.3.0