-
Notifications
You must be signed in to change notification settings - Fork 8.6k
State containers #52384
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
Merged
Merged
State containers #52384
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7d9821f
feat: 🎸 add state containers
streamich c32036f
docs: ✏️ add state container demos
streamich 1c8a803
docs: ✏️ refrech state container docs
streamich 6922997
chore: 🤖 install default comparator
streamich 5443f77
chore: 🤖 remove old state container implementation
streamich c488031
feat: 🎸 add selectors
streamich 92cf60a
chore: 🤖 move Ensure tyep to type utils
streamich 2990b97
fix: 🐛 fix useSelector() types and demo CLI command
streamich d68f30c
test: 💍 add tests for state container demos
streamich 4f8a58d
feat: 🎸 add ReacursiveReadonly to kbn-utility-types
streamich ddc92a4
feat: 🎸 shallow freeze state when not in production
streamich fbe3ab6
test: 💍 fix Jest tests
streamich 8f1785b
refactor: 💡 remove .state and use BehaviourSubject
streamich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { result as counterResult } from './state_containers/counter'; | ||
| import { result as todomvcResult } from './state_containers/todomvc'; | ||
|
|
||
| describe('demos', () => { | ||
| describe('state containers', () => { | ||
| test('counter demo works', () => { | ||
| expect(counterResult).toBe(10); | ||
| }); | ||
|
|
||
| test('TodoMVC demo works', () => { | ||
| expect(todomvcResult).toEqual([ | ||
| { id: 0, text: 'Learning state containers', completed: true }, | ||
| { id: 1, text: 'Learning transitions...', completed: true }, | ||
| ]); | ||
| }); | ||
| }); | ||
| }); |
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
69 changes: 69 additions & 0 deletions
69
src/plugins/kibana_utils/demos/state_containers/todomvc.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { createStateContainer, PureTransition } from '../../public/state_containers'; | ||
|
|
||
| export interface TodoItem { | ||
| text: string; | ||
| completed: boolean; | ||
| id: number; | ||
| } | ||
|
|
||
| export type TodoState = TodoItem[]; | ||
|
|
||
| export const defaultState: TodoState = [ | ||
| { | ||
| id: 0, | ||
| text: 'Learning state containers', | ||
| completed: false, | ||
| }, | ||
| ]; | ||
|
|
||
| export interface TodoActions { | ||
| add: PureTransition<TodoState, [TodoItem]>; | ||
| edit: PureTransition<TodoState, [TodoItem]>; | ||
| delete: PureTransition<TodoState, [number]>; | ||
| complete: PureTransition<TodoState, [number]>; | ||
| completeAll: PureTransition<TodoState, []>; | ||
| clearCompleted: PureTransition<TodoState, []>; | ||
| } | ||
|
|
||
| export const pureTransitions: TodoActions = { | ||
| add: state => todo => [...state, todo], | ||
| edit: state => todo => state.map(item => (item.id === todo.id ? { ...item, ...todo } : item)), | ||
| delete: state => id => state.filter(item => item.id !== id), | ||
| complete: state => id => | ||
| state.map(item => (item.id === id ? { ...item, completed: true } : item)), | ||
| completeAll: state => () => state.map(item => ({ ...item, completed: true })), | ||
| clearCompleted: state => () => state.filter(({ completed }) => !completed), | ||
| }; | ||
|
|
||
| const container = createStateContainer<TodoState, TodoActions>(defaultState, pureTransitions); | ||
|
|
||
| container.transitions.add({ | ||
| id: 1, | ||
| text: 'Learning transitions...', | ||
| completed: false, | ||
| }); | ||
| container.transitions.complete(0); | ||
| container.transitions.complete(1); | ||
|
|
||
| console.log(container.get()); // eslint-disable-line | ||
|
|
||
| export const result = container.get(); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # State containers | ||
|
|
||
| State containers are Redux-store-like objects meant to help you manage state in | ||
| your services or apps. | ||
|
|
||
| - State containers are strongly typed, you will get TypeScript autocompletion suggestions from | ||
| your editor when accessing state, executing transitions and using React helpers. | ||
| - State containers can be easily hooked up with your React components. | ||
| - State containers can be used without React, too. | ||
| - State containers provide you central place where to store state, instead of spreading | ||
| state around multiple RxJs observables, which you need to coordinate. With state | ||
| container you can always access the latest state snapshot synchronously. | ||
| - Unlike Redux, state containers are less verbose, see example below. | ||
|
|
||
|
|
||
| ## Example | ||
|
|
||
| ```ts | ||
| import { createStateContainer } from 'src/plugins/kibana_utils'; | ||
|
|
||
| const container = createStateContainer(0, { | ||
| increment: (cnt: number) => (by: number) => cnt + by, | ||
| double: (cnt: number) => () => cnt * 2, | ||
| }); | ||
|
|
||
| container.transitions.increment(5); | ||
| container.transitions.double(); | ||
| console.log(container.get()); // 10 | ||
| ``` | ||
|
|
||
|
|
||
| ## Demos | ||
|
|
||
| See demos [here](../../demos/state_containers/). | ||
|
|
||
| You can run them with | ||
|
|
||
| ``` | ||
| npx -q ts-node src/plugins/kibana_utils/demos/state_containers/counter.ts | ||
| npx -q ts-node src/plugins/kibana_utils/demos/state_containers/todomvc.ts | ||
| ``` | ||
|
|
||
|
|
||
| ## Reference | ||
|
|
||
| - [Creating a state container](./creation.md). | ||
| - [State transitions](./transitions.md). | ||
| - [Using with React](./react.md). | ||
| - [Using without React`](./no_react.md). | ||
| - [Parallels with Redux](./redux.md). | ||
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
2 changes: 1 addition & 1 deletion
2
...lugins/kibana_utils/docs/store/getters.md → ...a_utils/docs/state_containers/no_react.md
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # React | ||
|
|
||
| `createStateContainerReactHelpers` factory allows you to easily use state containers with React. | ||
|
|
||
|
|
||
| ## Example | ||
|
|
||
|
|
||
| ```ts | ||
| import { createStateContainer, createStateContainerReactHelpers } from 'src/plugins/kibana_utils'; | ||
|
|
||
| const container = createStateContainer({}, {}); | ||
| export const { | ||
| Provider, | ||
| Consumer, | ||
| context, | ||
| useContainer, | ||
| useState, | ||
| useTransitions, | ||
| useSelector, | ||
| connect, | ||
| } = createStateContainerReactHelpers<typeof container>(); | ||
| ``` | ||
|
|
||
| Wrap your app with `<Provider>`. | ||
|
|
||
| ```tsx | ||
| <Provider value={container}> | ||
| <MyApplication /> | ||
| </Provider> | ||
| ``` | ||
|
|
||
|
|
||
| ## Reference | ||
|
|
||
| - [`useContainer()`](./react/use_container.md) | ||
| - [`useState()`](./react/use_state.md) | ||
| - [`useSelector()`](./react/use_selector.md) | ||
| - [`useTransitions()`](./react/use_transitions.md) | ||
| - [`connect()()`](./react/connect.md) | ||
| - [Context](./react/context.md) |
22 changes: 22 additions & 0 deletions
22
src/plugins/kibana_utils/docs/state_containers/react/connect.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # `connect()()` higher order component | ||
|
|
||
| Use `connect()()` higher-order-component to inject props from state into your component. | ||
|
|
||
| ```tsx | ||
| interface Props { | ||
| name: string; | ||
| punctuation: '.' | ',' | '!', | ||
| } | ||
| const Demo: React.FC<Props> = ({ name, punctuation }) => | ||
| <div>Hello, {name}{punctuation}</div>; | ||
|
|
||
| const store = createStateContainer({ userName: 'John' }); | ||
| const { Provider, connect } = createStateContainerReactHelpers(store); | ||
|
|
||
| const mapStateToProps = ({ userName }) => ({ name: userName }); | ||
| const DemoConnected = connect<Props, 'name'>(mapStateToProps)(Demo); | ||
|
|
||
| <Provider> | ||
| <DemoConnected punctuation="!" /> | ||
| </Provider> | ||
| ``` |
24 changes: 24 additions & 0 deletions
24
src/plugins/kibana_utils/docs/state_containers/react/context.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # React context | ||
|
|
||
| `createStateContainerReactHelpers` returns `<Provider>` and `<Consumer>` components | ||
| as well as `context` React context object. | ||
|
|
||
| ```ts | ||
| export const { | ||
| Provider, | ||
| Consumer, | ||
| context, | ||
| } = createStateContainerReactHelpers<typeof container>(); | ||
| ``` | ||
|
|
||
| `<Provider>` and `<Consumer>` are just regular React context components. | ||
|
|
||
| ```tsx | ||
| <Provider value={container}> | ||
| <div> | ||
| <Consumer>{container => | ||
| <pre>{JSON.stringify(container.get())}</pre> | ||
| }</Consumer> | ||
| </div> | ||
| </Provider> | ||
| ``` |
10 changes: 10 additions & 0 deletions
10
src/plugins/kibana_utils/docs/state_containers/react/use_container.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # `useContainer` hook | ||
|
|
||
| `useContainer` React hook will simply return you `container` object from React context. | ||
|
|
||
| ```tsx | ||
| const Demo = () => { | ||
| const store = useContainer(); | ||
| return <div>{store.get().isDarkMode ? '🌑' : '☀️'}</div>; | ||
| }; | ||
| ``` |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.