2023-06-26 01:29:50 -08:00
|
|
|
import type {Dispatch, Middleware} from 'redux';
|
2023-07-25 01:39:51 -08:00
|
|
|
import type {HyperActions, HyperState} from '../../typings/hyper';
|
2016-10-10 02:26:47 -08:00
|
|
|
/**
|
|
|
|
|
* 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) {
|
2021-03-28 11:54:27 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
2016-07-13 12:44:24 -08:00
|
|
|
action.effect();
|
|
|
|
|
delete action.effect;
|
|
|
|
|
}
|
2021-03-28 12:13:49 -08:00
|
|
|
// 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;
|