2024-02-26 19:28:08 +00:00
|
|
|
```
|
2022-08-26 19:51:00 +00:00
|
|
|
|
2024-02-26 19:28:08 +00:00
|
|
|
```
|
2022-08-26 19:51:00 +00:00
|
|
|
|
2024-02-26 19:28:08 +00:00
|
|
|
--8<- "docs/tutorial-effects.test.ts:effects-1"
|
2022-08-29 15:56:30 +00:00
|
|
|
|
2024-08-08 13:28:44 +00:00
|
|
|
```
|
2022-08-29 15:56:30 +00:00
|
|
|
|
|
|
|
|
2024-02-26 19:28:08 +00:00
|
|
|
```
|
2022-08-29 15:56:30 +00:00
|
|
|
|
2022-08-30 15:00:00 +00:00
|
|
|
## Subduxes
|
|
|
|
|
|
|
|
Now that we have all the building blocks, we can embark on the last and
|
|
|
|
funkiest part of Updux: its recursive nature.
|
|
|
|
|
|
|
|
### Recap: the Todos dux, undivided
|
|
|
|
|
|
|
|
Upduxes can be divided into sub-upduxes that deal with the various parts of
|
|
|
|
the global state. This is better understood by working out an example, so
|
|
|
|
let's recap on the Todos dux we have so far:
|
|
|
|
|
2024-08-08 13:28:44 +00:00
|
|
|
```title="todos-monolith.ts"
|
2024-02-26 19:28:08 +00:00
|
|
|
--8<- "docs/tutorial-monolith.test.ts:mono"
|
|
|
|
```
|
2022-08-30 15:00:00 +00:00
|
|
|
|
|
|
|
This store has two main components: the `nextId`, and the `todos` collection.
|
|
|
|
The `todos` collection is itself composed of the individual `todo`s. Let's
|
|
|
|
create upduxes for each of those.
|
|
|
|
|
|
|
|
### NextId dux
|
|
|
|
|
2024-08-08 13:28:44 +00:00
|
|
|
```js title="nextId.ts"
|
2024-02-26 19:28:08 +00:00
|
|
|
--8<- "docs/nextId.ts:dux"
|
|
|
|
```
|
2022-08-30 15:00:00 +00:00
|
|
|
|
|
|
|
### Todo updux
|
|
|
|
|
2024-08-08 13:28:44 +00:00
|
|
|
```title="todo.ts"
|
2024-02-26 19:28:08 +00:00
|
|
|
--8<- "docs/todo.ts"
|
|
|
|
```
|
2022-08-30 15:00:00 +00:00
|
|
|
|
|
|
|
### Todos updux
|
|
|
|
|
2024-08-08 13:28:44 +00:00
|
|
|
```title="todos.ts"
|
2024-02-26 19:28:08 +00:00
|
|
|
--8<- "docs/todos.ts"
|
|
|
|
```
|
2022-08-30 15:00:00 +00:00
|
|
|
|
|
|
|
### Main store
|
|
|
|
|
2024-08-08 13:28:44 +00:00
|
|
|
```title="todoList.ts"
|
2024-02-26 19:28:08 +00:00
|
|
|
--8<- "docs/todoList.ts"
|
|
|
|
```
|
2022-08-30 15:00:00 +00:00
|
|
|
|
|
|
|
Tadah! We had to define the `addTodo` effect at the top level as it needs to
|
|
|
|
access the `getNextId` selector from `nextId` and the `addTodoWithId`
|
|
|
|
action from the `todos`.
|
|
|
|
|
|
|
|
Note that the `getNextId` selector still gets the
|
|
|
|
right value; when aggregating subduxes selectors Updux auto-wraps them to
|
2024-08-08 13:28:44 +00:00
|
|
|
access the right slice of the top object.
|
2022-08-30 15:00:00 +00:00
|
|
|
|
2022-08-30 18:15:47 +00:00
|
|
|
## Reactions
|
|
|
|
|
|
|
|
Reactions -- aka Redux's subscriptions -- can be added to a updux store via the initial config
|
|
|
|
or the method `addSubscription`. The signature of a reaction is:
|
|
|
|
|
|
|
|
```
|
|
|
|
(storeApi) => (state, previousState, unsubscribe) => {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Subscriptions registered for an updux and its subduxes are automatically
|
|
|
|
subscribed to the store when calling `createStore`.
|
|
|
|
|
|
|
|
The `state` passed to the subscriptions of the subduxes is the local state.
|
|
|
|
|
|
|
|
Also, all subscriptions are wrapped such that they are called only if the
|
|
|
|
local `state` changed since their last invocation.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```
|
|
|
|
const todos = new Updux({
|
|
|
|
initial: [],
|
|
|
|
actions: {
|
|
|
|
setNbrTodos: null,
|
2022-08-30 18:32:56 +00:00
|
|
|
addTodo: null,
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
addTodo: todo => todos => [ ...todos, todo ],
|
|
|
|
},
|
2022-08-30 18:15:47 +00:00
|
|
|
reactions: [
|
2022-08-30 18:32:56 +00:00
|
|
|
({dispatch}) => todos => dispatch.setNbrTodos(todos.length)
|
2022-08-30 18:15:47 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const myDux = new Updux({
|
|
|
|
initial: {
|
2022-08-30 18:32:56 +00:00
|
|
|
nbrTodos: 0
|
2022-08-30 18:15:47 +00:00
|
|
|
},
|
|
|
|
subduxes: {
|
|
|
|
todos,
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
setNbrTodos: nbrTodos => u({ nbrTodos })
|
|
|
|
}
|
2022-08-30 18:32:56 +00:00
|
|
|
});
|
2022-08-30 18:15:47 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
[immer]: https://www.npmjs.com/package/immer
|
|
|
|
[lodash]: https://www.npmjs.com/package/lodash
|
|
|
|
[ts-action]: https://www.npmjs.com/package/ts-action
|
2023-03-02 18:10:20 +00:00
|
|
|
[remeda]: remedajs.com/
|