hyper/lib/utils/effects.ts

23 lines
792 B
TypeScript
Raw Normal View History

2023-06-26 01:29:50 -08:00
import type {Dispatch, Middleware} from 'redux';
2023-07-25 09:30:19 -08:00
2023-07-25 01:39:51 -08:00
import type {HyperActions, HyperState} from '../../typings/hyper';
/**
* Simple redux middleware that executes
* the `effect` field if provided in an action
* since this is preceded by the `plugins`
* middleware. It allows authors to interrupt,
* defer or add to existing side effects at will
* as the result of an action being triggered.
*/
2023-06-25 04:27:42 -08:00
const effectsMiddleware: Middleware<{}, HyperState, Dispatch<HyperActions>> = () => (next) => (action) => {
2016-07-13 12:44:24 -08:00
const ret = next(action);
if (action.effect) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
2016-07-13 12:44:24 -08:00
action.effect();
delete action.effect;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
2016-07-13 12:44:24 -08:00
return ret;
};
2020-01-02 08:49:57 -09:00
export default effectsMiddleware;