augmentedAPI is available to subduxes

This commit is contained in:
Yanick Champoux 2024-08-13 18:56:31 -04:00
parent 7f6716cf89
commit ac97b6912c
2 changed files with 34 additions and 3 deletions

View File

@ -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

View File

@ -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))
);
}),
];