From 80d18703c476841deb95e0d979453e0d25a1fc31 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 6 Mar 2023 10:09:39 -0500 Subject: [PATCH] wip --- docs/tutorial.md | 8 ++++---- package.json | 1 + src/Updux.ts | 26 +++++++++++++++++++++++++- src/tutorial.test.ts | 22 +++++++++++++++++++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/docs/tutorial.md b/docs/tutorial.md index 6ab9599..387200e 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -72,10 +72,10 @@ Mutations are the reducing functions associated to actions. They are defined via the `mutation` method: ```js -todosDux.mutation( addTodo, description => ({next_id: id, todos}) => ({ - next_id: 1 + id, - todos: [...todos, { description, id, done: false }] -})); + dux.mutation(addTodo, (state, description) => { + state.todos.unshift({ description, id: state.nextId, done: false }); + state.nextId++; + }); ``` ## Effects diff --git a/package.json b/package.json index 5f5155c..2a05480 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "type": "module", "dependencies": { + "@yanick/updeep-remeda": "^2.1.0", "immer": "^9.0.15", "json-schema-shorthand": "^2.0.0", "redux": "^4.2.0", diff --git a/src/Updux.ts b/src/Updux.ts index 56b9e4c..59c1456 100644 --- a/src/Updux.ts +++ b/src/Updux.ts @@ -2,8 +2,20 @@ import { createStore as reduxCreateStore, applyMiddleware, DeepPartial, + Action, } from 'redux'; -import { configureStore, Reducer } from '@reduxjs/toolkit'; +import { configureStore, Reducer, createAction } from '@reduxjs/toolkit'; +import { withPayload } from './actions'; + +type ActionCreator = ReturnType; + +type AggregateState = L; + +type Mutation = ( + state: S, + payload: ReturnType['payload'], + action: ReturnType, +) => S | void; export default class Updux< T_LocalState = Record, @@ -11,6 +23,10 @@ export default class Updux< > { #localInitial: T_LocalState; #localActions: T_LocalActions; + #localMutations: Record< + string, + Mutation> + > = {}; constructor( config: Partial<{ @@ -45,4 +61,12 @@ export default class Updux< return store; } + + // TODO force the actionCreator to be one of the actions? + mutation( + actionCreator: A, + mutation: Mutation>, + ) { + this.#localMutations[actionCreator.type] = mutation; + } } diff --git a/src/tutorial.test.ts b/src/tutorial.test.ts index d1992af..753e746 100644 --- a/src/tutorial.test.ts +++ b/src/tutorial.test.ts @@ -1,4 +1,5 @@ import Updux, { createAction, withPayload } from './index.js'; +import u from '@yanick/updeep-remeda'; const expectType = (value: T) => value; @@ -23,7 +24,7 @@ test('initial state', () => { expect(store.getState()).toEqual(initial); }); -test.only('actions', () => { +test('actions', () => { const addTodo = createAction('addTodo', withPayload()); const todoDone = createAction('todoDone'); @@ -39,3 +40,22 @@ test.only('actions', () => { payload: 'write tutorial', }); }); + +test('mutation', () => { + const addTodo = createAction('addTodo', withPayload()); + + type Todo = { + description: string; + id: number; + done: boolean; + }; + + const dux = new Updux({ + initial: { nextId: 0, todos: [] as Todo[] }, + }); + + dux.mutation(addTodo, (state, description) => { + state.todos.unshift({ description, id: state.nextId, done: false }); + state.nextId++; + }); +});