new Updux()
- - + + @@ -62,42 +62,42 @@ @@ -119,33 +119,33 @@ - +Classes
- Updux
diff --git a/.gitignore b/.gitignore index c56c6e5..31e46d1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ pnpm-debug.log yarn-error.log GPUCache/ updux-2.0.0.tgz +pnpm-lock.yaml diff --git a/Taskfile.yaml b/Taskfile.yaml index e97b9a2..a9d7b7c 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -2,7 +2,27 @@ version: '3' +vars: + PARENT_BRANCH: main + tasks: + build: tsc + + checks: + deps: [lint, test, build] + + integrate: + deps: [checks] + cmds: + - git is-clean + # did we had tests? + - git diff-ls {{.PARENT_BRANCH}} | grep test + - git checkout {{.PARENT_BRANCH}} + - git weld - + + test: vitest run src + test:dev: vitest src + lint:fix:delta: vars: FILES: diff --git a/docs/recipes.md b/docs/recipes.md index 3dd308d..ef7369d 100644 --- a/docs/recipes.md +++ b/docs/recipes.md @@ -4,7 +4,7 @@ Say you have a `todos` state that is an array of `todo` sub-states, with some actions that should percolate to all todos, and some that should only -percolate to one. One way to model this is via updux's splat subduxes +percolate to one. One way to model this is via updux's splat subduxes (backed by `updeep`'s own '*'-key behavior). ``` @@ -55,7 +55,7 @@ const updux = new Updux({ add: (inc=1) => state => ({ counter: state.counter + inc }) } }); - + ``` Converting it to Immer would look like: @@ -68,10 +68,10 @@ import { produce } from 'immer'; const updux = new Updux({ initial: { counter: 0 }, mutations: { - add: (inc=1) => produce( draft => draft.counter += inc ) } + add: (inc=1) => produce( draft => draft.counter += inc ) } } }); - + ``` But since typing `produce` over and over is no fun, `groomMutations` @@ -86,11 +86,8 @@ const updux = new Updux({ initial: { counter: 0 }, groomMutations: mutation => (...args) => produce( mutation(...args) ), mutations: { - add: (inc=1) => draft => draft.counter += inc + add: (inc=1) => draft => draft.counter += inc } }); - + ``` - - - diff --git a/docs/recipes.test.js b/docs/recipes.test.js index 8560675..ef0a13a 100644 --- a/docs/recipes.test.js +++ b/docs/recipes.test.js @@ -51,4 +51,3 @@ test( "tutorial", async () => { ]); }); - diff --git a/docs/tutorial-reactions.test.js b/docs/tutorial-reactions.test.js index 61dfe85..cdba1d9 100644 --- a/docs/tutorial-reactions.test.js +++ b/docs/tutorial-reactions.test.js @@ -37,4 +37,3 @@ test( "basic tests", async () => { expect(store.getState().nbrTodos).toEqual(2); }); - diff --git a/docs/tutorial.md b/docs/tutorial.md index 6ecb348..387200e 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -4,22 +4,23 @@ This tutorial walks you through the features of `Updux` using the time-honored example of the implementation of Todo list store. We'll be using -[updeep](https://www.npmjs.com/package/updeep) to +[@yanick/updeep-remeda](https://www.npmjs.com/package/@yanick/updeep-remeda) to help with immutability and deep merging, but that's totally optional. If `updeep` is not your bag, -it can easily be substitued with, say, [immer][], [lodash][], or even +it can easily be substitued with, say, [immer][], +[remeda][], +[lodash][], or even plain JavaScript. ## Definition of the state To begin with, let's define that has nothing but an initial state. - ```js -import { Updux } from 'updux'; +import Updux from 'updux'; const todosDux = new Updux({ initial: { - next_id: 1, + nextId: 1, todos: [], } }); @@ -32,22 +33,26 @@ initial state will be automatically set: ```js const store = todosDux.createStore(); -console.log(store.getState()); // prints { next_id: 1, todos: [] } +console.log(store.getState()); // prints { nextId: 1, todos: [] } ``` ## Add actions This is all good, but a little static. Let's add actions! - ```js +import { createAction } from 'updux'; + +const addTodo = createAction('addTodo'); +const todoDone = createAction('todoDone'); + const todosDux = new Updux({ initial: { - next_id: 1, + nextId: 1, todos: [], }, - { - addTodo: null, - todoDone: null, + actions: { + addTodo, + todoDone, } }); ``` @@ -55,22 +60,22 @@ const todosDux = new Updux({ ### Accessing actions Once an action is defined, its creator is accessible via the `actions` accessor. - +This is not yet terribly exciting, but it'll get better once we begin using +subduxes. ```js console.log( todosDux.actions.addTodo('write tutorial') ); - // prints { type: 'addTodo', payload: 'write tutorial' } + // => { type: 'addTodo', payload: 'write tutorial' } ``` ### Adding a mutation Mutations are the reducing functions associated to actions. They -are defined via the `setMutation` method: +are defined via the `mutation` method: - - ```js -todosDux.setMutation( 'addTodo', description => ({next_id: id, todos}) => ({ - next_id: 1 + id, - todos: [...todos, { description, id, done: false }] -})); +```js + dux.mutation(addTodo, (state, description) => { + state.todos.unshift({ description, id: state.nextId, done: false }); + state.nextId++; + }); ``` ## Effects @@ -158,9 +163,9 @@ todosDux.setSelector('getTodoById', getTodoById); ### Accessing selectors -The `getState` method of a dux store is augmented +The `getState` method of a dux store is augmented with its selectors, with the first call for the state already -called in for you. +called in for you. ```js const store = todosDux.createStore(); @@ -204,7 +209,7 @@ const todosDux = new Updux({ addTodoWithId: todo => u.updateIn( 'todos', todos => [ ...todos, todo] ), incrementNextId: () => u({ nextId: fp.add(1) }), - todoDone: (id) => u.updateIn('todos', + todoDone: (id) => u.updateIn('todos', u.map( u.if( fp.matches({id}), todo => u({done: true}, todo) ) ) ), }, @@ -404,4 +409,4 @@ const myDux = new Updux({ [immer]: https://www.npmjs.com/package/immer [lodash]: https://www.npmjs.com/package/lodash [ts-action]: https://www.npmjs.com/package/ts-action - +[remeda]: remedajs.com/ diff --git a/out/Updux.html b/out/Updux.html index 44126a5..c1e8090 100644 --- a/out/Updux.html +++ b/out/Updux.html @@ -19,7 +19,7 @@