effects with function
This commit is contained in:
parent
55f0d5cd13
commit
06b4bf62c4
@ -314,6 +314,9 @@ export default class Updux<
|
|||||||
addEffect(actionCreator:
|
addEffect(actionCreator:
|
||||||
AggregateActions<ResolveActions<T_LocalActions>, T_Subduxes>,
|
AggregateActions<ResolveActions<T_LocalActions>, T_Subduxes>,
|
||||||
effect: EffectMiddleware): EffectMiddleware;
|
effect: EffectMiddleware): EffectMiddleware;
|
||||||
|
addEffect(guardFunc: (action:
|
||||||
|
AggregateActions<ResolveActions<T_LocalActions>, T_Subduxes>[keyof AggregateActions<ResolveActions<T_LocalActions>, T_Subduxes>]) => boolean,
|
||||||
|
effect: EffectMiddleware): EffectMiddleware;
|
||||||
addEffect(effect: EffectMiddleware): EffectMiddleware;
|
addEffect(effect: EffectMiddleware): EffectMiddleware;
|
||||||
addEffect(...args) {
|
addEffect(...args) {
|
||||||
let effect;
|
let effect;
|
||||||
@ -323,10 +326,13 @@ export default class Updux<
|
|||||||
else {
|
else {
|
||||||
const [actionCreator, originalEffect] = args;
|
const [actionCreator, originalEffect] = args;
|
||||||
|
|
||||||
|
const test = actionCreator.hasOwnProperty('match') ?
|
||||||
|
actionCreator.match : actionCreator;
|
||||||
|
|
||||||
effect = (api) => next => {
|
effect = (api) => next => {
|
||||||
const e = originalEffect(api)(next);
|
const e = originalEffect(api)(next);
|
||||||
return action => {
|
return action => {
|
||||||
const func = actionCreator.match(action) ? e : next;
|
const func = test(action) ? e : next;
|
||||||
return func(action);
|
return func(action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,8 +117,35 @@ test('addEffect with actionCreator', () => {
|
|||||||
const next = vi.fn();
|
const next = vi.fn();
|
||||||
const spy = vi.fn();
|
const spy = vi.fn();
|
||||||
|
|
||||||
const mw = dux.addEffect(dux.actions.foo, (api) => (next) => (action) =>
|
const mw = dux.addEffect(
|
||||||
next(spy(action))
|
dux.actions.foo,
|
||||||
|
(api) => (next) => (action) => next(spy(action)),
|
||||||
|
);
|
||||||
|
|
||||||
|
mw({})(next)(dux.actions.bar());
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(spy).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
next.mockReset();
|
||||||
|
mw({})(next)(dux.actions.foo());
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(spy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('addEffect with function', () => {
|
||||||
|
const dux = new Updux({
|
||||||
|
actions: {
|
||||||
|
foo: () => {},
|
||||||
|
bar: () => {},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const next = vi.fn();
|
||||||
|
const spy = vi.fn();
|
||||||
|
|
||||||
|
const mw = dux.addEffect(
|
||||||
|
(action) => action.type[0] === 'f',
|
||||||
|
(api) => (next) => (action) => next(spy(action)),
|
||||||
);
|
);
|
||||||
|
|
||||||
mw({})(next)(dux.actions.bar());
|
mw({})(next)(dux.actions.bar());
|
||||||
|
Loading…
Reference in New Issue
Block a user