From 1df6ac5329373042106da881cad3c8b07a56418c Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 9 Mar 2023 15:32:41 -0500 Subject: [PATCH] subdux mutations are go --- src/buildUpreducer.todo | 45 ----------------------------------------- src/mutations.test.todo | 32 ----------------------------- 2 files changed, 77 deletions(-) delete mode 100644 src/buildUpreducer.todo delete mode 100644 src/mutations.test.todo diff --git a/src/buildUpreducer.todo b/src/buildUpreducer.todo deleted file mode 100644 index 39672ac..0000000 --- a/src/buildUpreducer.todo +++ /dev/null @@ -1,45 +0,0 @@ -import u from 'updeep'; -import { mapValues } from 'lodash-es'; - -export function buildUpreducer( - initial, - mutations, - subduxes = {}, - wrapper = undefined, -) { - const subReducers = - Object.keys(subduxes).length > 0 - ? mapValues(subduxes, ({ upreducer }) => upreducer) - : null; - - const upreducer = (action) => (state) => { - if (!action?.type) - throw new Error('upreducer called with a bad action'); - - let newState = state ?? initial; - - if (subReducers) { - if (subduxes['*']) { - newState = u.updateIn( - '*', - subduxes['*'].upreducer(action), - newState, - ); - } else { - const update = mapValues(subReducers, (upReducer) => - upReducer(action), - ); - - newState = u(update, newState); - } - } - - const a = mutations[action.type] || mutations['+']; - - if (!a) return newState; - - return a(action.payload, action)(newState); - }; - - return wrapper ? wrapper(upreducer) : upreducer; -} diff --git a/src/mutations.test.todo b/src/mutations.test.todo deleted file mode 100644 index dccf11b..0000000 --- a/src/mutations.test.todo +++ /dev/null @@ -1,32 +0,0 @@ -import schema from 'json-schema-shorthand'; -import u from 'updeep'; - -import { action } from './actions.js'; - -import { Updux, dux } from './Updux.js'; - - -test('strings and generators', async () => { - const actionA = action('a'); - - const foo = dux({ - actions: { - b: null, - a: actionA, - }, - }); - - // as a string and defined - expect(() => foo.setMutation('a', () => {})).not.toThrow(); - - // as a generator and defined - expect(() => foo.setMutation(actionA, () => {})).not.toThrow(); - - // as a string, not defined - expect(() => foo.setMutation('c', () => {})).toThrow(); - - foo.setMutation(action('d'), () => {}); - - expect(foo.actions.d).toBeTypeOf('function'); -}); -