diff --git a/src/Updux.ts b/src/Updux.ts index b9358a9..d35b81c 100644 --- a/src/Updux.ts +++ b/src/Updux.ts @@ -116,6 +116,7 @@ export default class Updux< this.initial, this.#localMutations, this.#defaultMutation, + this.#subduxes, ) as any as ( state: undefined | typeof this.initial, action: Action, diff --git a/src/mutations.test.ts b/src/mutations.test.ts index 9f19df8..5858b63 100644 --- a/src/mutations.test.ts +++ b/src/mutations.test.ts @@ -56,18 +56,25 @@ test('default mutation', () => { test('mutation of a subdux', () => { const baz = createAction('baz'); + const noop = createAction('noop'); + const stopit = createAction('stopit'); const bar = new Updux({ initial: 0, actions: { baz, + stopit, }, }); bar.addMutation(baz, () => () => 1); + bar.addMutation(stopit, () => () => 2); const foo = new Updux({ subduxes: { bar }, }); + foo.addMutation(stopit, () => (state) => state, true); + expect(foo.reducer(undefined, noop())).toHaveProperty('bar', 0); expect(foo.reducer(undefined, baz())).toHaveProperty('bar', 1); + expect(foo.reducer(undefined, stopit())).toHaveProperty('bar', 0); }); diff --git a/src/reducer.ts b/src/reducer.ts index 12b431c..9ca2571 100644 --- a/src/reducer.ts +++ b/src/reducer.ts @@ -16,8 +16,7 @@ export function buildReducer( defaultMutation?: Omit, subduxes: Record = {}, ) { - // const subReducers = - // ? R.mapValues(subduxes, R.prop('reducer')); + const subReducers = R.mapValues(subduxes, R.prop('reducer')); // TODO matcherMutation // TODO defaultMutation @@ -48,6 +47,16 @@ export function buildReducer( )(state); } + if (!terminal && Object.keys(subduxes).length > 0) { + // subduxes + state = R.merge( + state, + R.mapValues(subReducers, (reducer, slice) => + (reducer as any)(state[slice], action), + ), + ); + } + return state; };