-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[State Management] State containers improvements #54436
[State Management] State containers improvements #54436
Conversation
…agement/state-containers-improvements
@@ -35,7 +35,7 @@ export const createStateContainerReactHelpers = <Container extends StateContaine | |||
return value; | |||
}; | |||
|
|||
const useTransitions = () => useContainer().transitions; | |||
const useTransitions: () => Container['transitions'] = () => useContainer().transitions; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allowed to remove this type casting: https://github.com/elastic/kibana/pull/54436/files#diff-0474422dfd150c2b85e0efb48c007a20L196
@@ -9,7 +9,7 @@ | |||
```ts | |||
import { createStateContainer, createStateContainerReactHelpers } from 'src/plugins/kibana_utils'; | |||
|
|||
const container = createStateContainer({}, {}); | |||
const container = createStateContainer({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seemed to be reasonable to also make transitions optional. Just like selectors
defaultState: State, | ||
pureTransitions: PureTransitions | ||
): ReduxLikeStateContainer<State, PureTransitions>; | ||
export function createStateContainer< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not exactly sure, but I ended up adding overrides here because bumped into following with previous setup just with optional generics:
const container = createStateContainer({test: 'test}, {action: (s) => () => s});
container.get().test // ts works
container.transitions.action() // ts works
but
explicitly specify only State shape in generic:
type S = {test: string};
const container = createStateContainer<S>({test: 'test}, {action: (s) => () => s});
container.get().test // ts works
container.transitions.action() // doesn't work.. ts now think, that transitions are {}.
So adding those overrides instead of using optional generic params just made it explicit, that when generic params are specified explicitly - the number of specified params have to match number of arguments.
Pinging @elastic/kibana-app-arch (Team:AppArch) |
@@ -56,9 +74,13 @@ export const createStateContainer = < | |||
state$, | |||
getState: () => data$.getValue(), | |||
set: (state: State) => { | |||
data$.next(freeze(state)); | |||
container.dispatch({ type: $$setActionType, args: [state] }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes set()
to go through dispatch and not be skipped by added middlewares
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM. Just see one comment about BaseState
.
@streamich, Please see: #54436 (comment)
The error is:
|
In your RISON example you probably need to cast the object, maybe something like this encode(s as RisonValue); |
@streamich, used
Looks like it :( |
…agement/state-containers-improvements # Conflicts: # src/plugins/kibana_utils/public/state_containers/create_state_container.test.ts # src/plugins/kibana_utils/public/state_containers/create_state_container.ts # src/plugins/kibana_utils/public/state_containers/create_state_container_react_helpers.test.tsx # src/plugins/kibana_utils/public/state_containers/create_state_container_react_helpers.ts # src/plugins/kibana_utils/public/state_containers/types.ts
@elasticmachine merge upstream |
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
Some maintenance and minor fixes to state containers based on experience while working with them in elastic#53582 Patch unit tests to use current "terminology" (e.g. "transition" vs "mutation") Fix docs where "store" was used instead of "state container" Allow to create state container without transition. Fix freeze function to deeply freeze objects. Restrict State to BaseState with extends object. in set() function, make sure the flow goes through dispatch to make sure middleware see this update Improve type inference for useTransition() Improve type inference for createStateContainer(). Other issues noticed, but didn't fix in reasonable time: Can't use addMiddleware without explicit type casting elastic#54438 Transitions and Selectors allow any state, not bind to container's state elastic#54439
Some maintenance and minor fixes to state containers based on experience while working with them in #53582 Patch unit tests to use current "terminology" (e.g. "transition" vs "mutation") Fix docs where "store" was used instead of "state container" Allow to create state container without transition. Fix freeze function to deeply freeze objects. Restrict State to BaseState with extends object. in set() function, make sure the flow goes through dispatch to make sure middleware see this update Improve type inference for useTransition() Improve type inference for createStateContainer(). Other issues noticed, but didn't fix in reasonable time: Can't use addMiddleware without explicit type casting #54438 Transitions and Selectors allow any state, not bind to container's state #54439
…age-offset-floating-tooltip * 'master' of github.com:elastic/kibana: [Maps] refactor isPointsOnly, isLinesOnly, and isPolygonsOnly to make synchronous (elastic#54067) Fix icon path in tutorial introduction (elastic#49684) [State Management] State containers improvements (elastic#54436) Fix floating tools rendering logic (elastic#54505) Handle another double quote special case (elastic#54474) [Home][Tutorial] Add data UI for IBM MQ Filebeat module (elastic#54238) fix(package): upgrade transitive dependency elliptic to v6.5.2 (elastic#54476) [Graph] Fix various a11y issues (elastic#54097) # Conflicts: # src/legacy/core_plugins/console/public/np_ready/application/models/legacy_core_editor/legacy_core_editor.ts
Some maintenance and minor fixes to state containers based on experience while working with them in elastic#53582 Patch unit tests to use current "terminology" (e.g. "transition" vs "mutation") Fix docs where "store" was used instead of "state container" Allow to create state container without transition. Fix freeze function to deeply freeze objects. Restrict State to BaseState with extends object. in set() function, make sure the flow goes through dispatch to make sure middleware see this update Improve type inference for useTransition() Improve type inference for createStateContainer(). Other issues noticed, but didn't fix in reasonable time: Can't use addMiddleware without explicit type casting elastic#54438 Transitions and Selectors allow any state, not bind to container's state elastic#54439
Summary
Some maintenance and minor fixes to state containers based on experience while working with them in #53582
BaseState
with extends object.set()
function, make sure the flow goes throughdispatch
to make sure middleware see this updateOther issues I noticed, but didn't fix in reasonable time:
addMiddleware
without explicit type casting [State Management] State containers and redux middlewares types mismatch #54438Checklist
Use
strikethroughsto remove checklist items you don't feel are applicable to this PR.For maintainers