augmentedAPI is available to subduxes
This commit is contained in:
parent
7f6716cf89
commit
ac97b6912c
@ -233,6 +233,37 @@ test('addEffect with unknown actionCreator adds it', () => {
|
||||
|
||||
expect(dux.actions.foo()).toMatchObject({ type: 'foo' });
|
||||
|
||||
});
|
||||
|
||||
test('effects of subduxes', () => {
|
||||
const foo = new Updux({
|
||||
initialState: 12,
|
||||
actions: {
|
||||
bar: null,
|
||||
},
|
||||
selectors: {
|
||||
addHundred: (x) => x + 100
|
||||
}
|
||||
})
|
||||
.addMutation(createAction('setFoo', withPayload<number>()), (state) => () => state)
|
||||
.addEffect(({ type: t }) => t === 'doit', (api) => next => action => {
|
||||
api.dispatch.setFoo(api.getState.addHundred())
|
||||
});
|
||||
|
||||
const dux = new Updux({
|
||||
subduxes: {
|
||||
foo
|
||||
}
|
||||
});
|
||||
|
||||
const store = dux.createStore();
|
||||
|
||||
store.dispatch({ type: "doit" });
|
||||
|
||||
expect(store.getState()).toMatchObject({ foo: 112 })
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
// TODO subdux effects
|
||||
|
@ -14,14 +14,14 @@ export interface EffectMiddleware<D, A = rtk.AnyAction> {
|
||||
export function buildEffects(localEffects, subduxes = {}) {
|
||||
return [
|
||||
...localEffects,
|
||||
...(Object.entries(subduxes) as any).flatMap(([slice, { effects }]) => {
|
||||
...(Object.entries(subduxes) as any).flatMap(([slice, { effects, actions, selectors }]) => {
|
||||
if (!effects) return [];
|
||||
return effects.map(
|
||||
(effect) => (api) =>
|
||||
effect({
|
||||
effect(augmentMiddlewareApi({
|
||||
...api,
|
||||
getState: () => api.getState()[slice],
|
||||
}),
|
||||
}, actions, selectors))
|
||||
);
|
||||
}),
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user