Merge branch 'filtered-effect'
This commit is contained in:
commit
a04ebfcdfe
32
src/Updux.ts
32
src/Updux.ts
@ -59,7 +59,7 @@ type ResolveActions<
|
|||||||
[ActionType in keyof A]: ActionType extends string
|
[ActionType in keyof A]: ActionType extends string
|
||||||
? ResolveAction<ActionType, A[ActionType]>
|
? ResolveAction<ActionType, A[ActionType]>
|
||||||
: never;
|
: never;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Reaction<S = any, M extends MiddlewareAPI = MiddlewareAPI> = (
|
type Reaction<S = any, M extends MiddlewareAPI = MiddlewareAPI> = (
|
||||||
api: M,
|
api: M,
|
||||||
@ -311,8 +311,36 @@ export default class Updux<
|
|||||||
this.#defaultMutation = { mutation, terminal };
|
this.#defaultMutation = { mutation, terminal };
|
||||||
}
|
}
|
||||||
|
|
||||||
addEffect(effect: EffectMiddleware) {
|
addEffect(actionCreator:
|
||||||
|
AggregateActions<ResolveActions<T_LocalActions>, T_Subduxes>,
|
||||||
|
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(...args) {
|
||||||
|
let effect;
|
||||||
|
if (args.length === 1) {
|
||||||
|
effect = args[0];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const [actionCreator, originalEffect] = args;
|
||||||
|
|
||||||
|
const test = actionCreator.hasOwnProperty('match') ?
|
||||||
|
actionCreator.match : actionCreator;
|
||||||
|
|
||||||
|
effect = (api) => next => {
|
||||||
|
const e = originalEffect(api)(next);
|
||||||
|
return action => {
|
||||||
|
const func = test(action) ? e : next;
|
||||||
|
return func(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.#localEffects.push(effect);
|
this.#localEffects.push(effect);
|
||||||
|
|
||||||
|
return effect;
|
||||||
}
|
}
|
||||||
|
|
||||||
get effectsMiddleware() {
|
get effectsMiddleware() {
|
||||||
|
@ -106,5 +106,57 @@ test('subdux', () => {
|
|||||||
expect(seen).toEqual(1);
|
expect(seen).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('addEffect with actionCreator', () => {
|
||||||
|
const dux = new Updux({
|
||||||
|
actions: {
|
||||||
|
foo: null,
|
||||||
|
bar: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const next = vi.fn();
|
||||||
|
const spy = vi.fn();
|
||||||
|
|
||||||
|
const mw = dux.addEffect(
|
||||||
|
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());
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(spy).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
next.mockReset();
|
||||||
|
mw({})(next)(dux.actions.foo());
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(spy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
// TODO subdux effects
|
// TODO subdux effects
|
||||||
// TODO allow to subscribe / unsubscribe effects?
|
// TODO allow to subscribe / unsubscribe effects?
|
||||||
|
Loading…
Reference in New Issue
Block a user