-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: πΈ add state containers * docs: βοΈ add state container demos * docs: βοΈ refrech state container docs * chore: π€ install default comparator * chore: π€ remove old state container implementation * feat: πΈ add selectors * chore: π€ move Ensure tyep to type utils * fix: π fix useSelector() types and demo CLI command * test: π add tests for state container demos * feat: πΈ add ReacursiveReadonly to kbn-utility-types * feat: πΈ shallow freeze state when not in production * test: π fix Jest tests * refactor: π‘ remove .state and use BehaviourSubject
- Loading branch information
Showing
36 changed files
with
1,275 additions
and
750 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
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
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
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 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 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 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 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 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
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 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 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 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.