Skip to content

[8.x] 🌊 Streams: Selectors for derived samples (#213638)#216611

Merged
kibanamachine merged 2 commits intoelastic:8.xfrom
kibanamachine:backport/8.x/pr-213638
Apr 1, 2025
Merged

[8.x] 🌊 Streams: Selectors for derived samples (#213638)#216611
kibanamachine merged 2 commits intoelastic:8.xfrom
kibanamachine:backport/8.x/pr-213638

Conversation

@kibanamachine
Copy link
Contributor

Backport

This will backport the following commits from main to 8.x:

Questions ?

Please refer to the Backport tool documentation

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)
@kibanamachine kibanamachine added the backport This PR is a backport of another PR label Apr 1, 2025
@kibanamachine kibanamachine enabled auto-merge (squash) April 1, 2025 09:53
@elasticmachine
Copy link
Contributor

@kibanamachine kibanamachine merged commit 442330a into elastic:8.x Apr 1, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport This PR is a backport of another PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants