add subduxUpreducer
This commit is contained in:
parent
0e03628502
commit
15e154b6ec
@ -4,19 +4,29 @@ const foo = new Updux<number>({
|
|||||||
initial: 0,
|
initial: 0,
|
||||||
mutations: {
|
mutations: {
|
||||||
doIt: () => (state: number) => {
|
doIt: () => (state: number) => {
|
||||||
console.log(state);
|
|
||||||
return state + 1;
|
return state + 1;
|
||||||
},
|
},
|
||||||
|
doTheThing: () => (state: number) => {
|
||||||
|
return state + 3;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const bar = new Updux<{foo: number}>({
|
const bar = new Updux<{foo: number}>({
|
||||||
subduxes: {foo},
|
subduxes: {foo},
|
||||||
mutations: {
|
|
||||||
doIt: () => (state: any) => state,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
bar.addMutation(
|
||||||
|
foo.actions.doTheThing,
|
||||||
|
(_, action) => state => {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
baz: bar.subduxUpreducer(action)(state),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
bar.addMutation(
|
bar.addMutation(
|
||||||
foo.actions.doIt,
|
foo.actions.doIt,
|
||||||
() => (state: any) => ({...state, bar: 'yay'}),
|
() => (state: any) => ({...state, bar: 'yay'}),
|
||||||
@ -37,3 +47,10 @@ test('sink mutations', () => {
|
|||||||
bar: 'yay',
|
bar: 'yay',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('sink mutation and subduxUpreducer', () => {
|
||||||
|
expect(bar.reducer(undefined, bar.actions.doTheThing())).toEqual({
|
||||||
|
foo: 0,
|
||||||
|
baz: {foo: 3},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
24
src/updux.ts
24
src/updux.ts
@ -188,6 +188,27 @@ export class Updux<S = any> {
|
|||||||
return buildMutations(this.localMutations, this.subduxes);
|
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
|
* Same as doing
|
||||||
*
|
*
|
||||||
@ -248,7 +269,8 @@ export class Updux<S = any> {
|
|||||||
* If a local mutation was already associated to the action,
|
* If a local mutation was already associated to the action,
|
||||||
* it will be replaced by the new one.
|
* it will be replaced by the new one.
|
||||||
* @param isSink
|
* @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
|
* @example
|
||||||
* ```
|
* ```
|
||||||
* updux.addMutation( add, inc => state => state + inc );
|
* updux.addMutation( add, inc => state => state + inc );
|
||||||
|
Loading…
Reference in New Issue
Block a user