add subduxUpreducer

typescript
Yanick Champoux 2019-11-05 19:24:03 -05:00
parent 0e03628502
commit 15e154b6ec
2 changed files with 44 additions and 5 deletions

View File

@ -4,19 +4,29 @@ const foo = new Updux<number>({
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},
});
});

View File

@ -188,6 +188,27 @@ export class Updux<S = any> {
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<S = any> {
* 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 );