[8.x] 🌊 Streams: Selectors for derived samples (#213638)#216611
Merged
kibanamachine merged 2 commits intoelastic:8.xfrom Apr 1, 2025
Merged
[8.x] 🌊 Streams: Selectors for derived samples (#213638)#216611kibanamachine merged 2 commits intoelastic:8.xfrom
kibanamachine merged 2 commits intoelastic:8.xfrom
Conversation
Simplified massively from first state and just plugging in reselect in
places where that's suitable (here to calculate the currently relevant
sample documents).
Also does a drive-by layout fix.
~Introduces a new xstate helper for derived data.~
~In most cases, the actor and state machine model of xstate is great,
but for derived data using pure functions, the semantics of the
`useMemo` hook with defined dependencies is often easier to understand
and eliminates the risk of forgetting to update the derived data
correctly in some cases.~
~It's about using the right tool for the right job - you don't need to
choose between the dependency list of useMemo and the actor model of
xstate, you can use what fits the case, without compromising
performance.~
~This is the API:~
```ts
const myActorContext = withMemoizedSelectors(
createActorContext(myMachine),
{
derivedView: createSelector(
[
(ctx: MyContextType) => {
return ctx.dependency1;
},
(ctx: MyContextType) =>
ctx.dependency2,
],
(dependency1, dependency2) => {
return // expensive calculation only running when necessary
}
),
},
(context) => (context.subMachine ? [context.subMachine] : []) // optional subscribe to changes of submachines as well
);
// in react use useMemoizedSelector hook
// this will cause the component to rerender if the selector is returning a new value
myActorContext.useMemoizedSelector('derivedView')
```
~This is using reselect to declare the dependencies similar to a react
useMemo hook - the actual selector will only run if the dependencies
change, leading to similar semantics as useMemo, with the additional
benefit that if the value is used in multiple places, it's still just
calculated once. The component calling `withMemoizedSelectors` only
re-renders if the value returned by the selector changes. The selector
itself only re-runs if one of the declared dependencies changes.~
~Everything is type-safe by capturing the types of the reselect selector
object via inferred type param and using it in the `useMemoizedSelector`
type.~
(cherry picked from commit c5e0b05)
Contributor
💔 Build Failed
Failed CI Stepscc @flash1293 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport
This will backport the following commits from
mainto8.x:Questions ?
Please refer to the Backport tool documentation