-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add selector subscriptions (#551)
This non-breaking PR adds [`reselect`](https://npmjs.com/package/reselect)-style selectors as an optional parameter to controller messenger event subscriptions. We call subscriptions with selectors **selector subscriptions**. This enables subscribing to subsets of large event payloads, for example `*:stateChange` events. ```typescript // ControllerMessenger.subscribe selector signature subscribe<E extends Event['type'], V>( eventType: E, handler: (nextValue: V, previousValue: V | undefined) => void, selector: (payload: ExtractEventPayload<Event, E>) => V, ): void; ``` When a selector subscription is triggered, the corresponding selector is passed the entire event payload, and returns a single value. If the value has changed since the last occurrence of the event, the handler is called with the new value and the previous value, or `undefined` if no previous value exists. Since controller state is immutable, the event handler can efficiently diff the selector return values and trigger other logic dependent on changes to subsets of complex event payloads (e.g., other events). This feature is motivated by our frequent need to subscribe to controller substate(s). Every controller has a generic `*:stateChange` event, but parsing that state is often error-prone and/or annoying. By adding selector subscriptions, we can performantly parse complex controller state in one place, and emit more specific events as necessary. This implementation was chosen due to our existing widespread use of selectors in our UIs, and in order to avoid the introduction of querying DSLs such as JSONPath and GraphQL at this time. In addition to adding selector subscriptions, this PR modifies an error messages and does some minor, non-breaking type touchup. Closes #528
- Loading branch information
Showing
3 changed files
with
275 additions
and
20 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.