diff --git a/src/sink.test.ts b/src/sink.test.ts index 96ebe38..0d8a942 100644 --- a/src/sink.test.ts +++ b/src/sink.test.ts @@ -4,19 +4,29 @@ const foo = new Updux({ initial: 0, mutations: { doIt: () => (state: number) => { - console.log(state); return state + 1; }, + doTheThing: () => (state: number) => { + return state + 3; + }, }, }); const bar = new Updux<{foo: number}>({ subduxes: {foo}, - mutations: { - doIt: () => (state: any) => state, - }, }); +bar.addMutation( + foo.actions.doTheThing, + (_, action) => state => { + return { + ...state, + baz: bar.subduxUpreducer(action)(state), + }; + }, + true, +); + bar.addMutation( foo.actions.doIt, () => (state: any) => ({...state, bar: 'yay'}), @@ -37,3 +47,10 @@ test('sink mutations', () => { bar: 'yay', }); }); + +test('sink mutation and subduxUpreducer', () => { + expect(bar.reducer(undefined, bar.actions.doTheThing())).toEqual({ + foo: 0, + baz: {foo: 3}, + }); +}); diff --git a/src/updux.ts b/src/updux.ts index 76a268a..6443db2 100644 --- a/src/updux.ts +++ b/src/updux.ts @@ -188,6 +188,27 @@ export class Updux { return buildMutations(this.localMutations, this.subduxes); } + /** + * Returns the upreducer made of the merge of all sudbuxes reducers, without + * the local mutations. Useful, for example, for sink mutations. + * @example + * ``` + * import todo from './todo'; // updux for a single todo + * import Updux from 'updux'; + * import u from 'updeep'; + * + * const todos = new Updux({ initial: [], subduxes: { '*': todo } }); + * todos.addMutation( + * todo.actions.done, + * ({todo_id},action) => u.map( u.if( u.is('id',todo_id) ), todos.subduxUpreducer(action) ) + * true + * ); + * ``` + */ + @computed get subduxUpreducer() { + return buildUpreducer(this.initial, buildMutations({}, this.subduxes)); + } + /** * Same as doing * @@ -248,7 +269,8 @@ export class Updux { * If a local mutation was already associated to the action, * it will be replaced by the new one. * @param isSink - * If `true`, disables the subduxes mutations for this action. + * If `true`, disables the subduxes mutations for this action. To + * conditionally run the subduxes mutations, check out [[subduxUpreducer]]. * @example * ``` * updux.addMutation( add, inc => state => state + inc );