augmentMiddlewareApi
This commit is contained in:
parent
f322691906
commit
5297aecba6
@ -22,34 +22,35 @@ export const augmentGetState = (originalGetState, selectors) => {
|
||||
}
|
||||
return getState;
|
||||
};
|
||||
export function buildEffectsMiddleware(
|
||||
effects = [],
|
||||
actions = {},
|
||||
selectors = {},
|
||||
) {
|
||||
return ({
|
||||
getState: originalGetState,
|
||||
dispatch: originalDispatch,
|
||||
...rest
|
||||
}) => {
|
||||
|
||||
const augmentDispatch = (originalDispatch, actions) => {
|
||||
const dispatch = (action) => originalDispatch(action);
|
||||
|
||||
for (const a in actions) {
|
||||
dispatch[a] = (...args) => dispatch(actions[a](...args));
|
||||
}
|
||||
return dispatch;
|
||||
};
|
||||
|
||||
const getState = () => originalGetState();
|
||||
for (const s in selectors) {
|
||||
getState[s] = (...args) => {
|
||||
let result = selectors[s](originalGetState());
|
||||
if (typeof result === 'function') return result(...args);
|
||||
return result;
|
||||
const augmentMiddlewareApi = (api, actions, selectors) => {
|
||||
return {
|
||||
...api,
|
||||
getState: augmentGetState(api.getState, selectors),
|
||||
dispatch: augmentDispatch(api.dispatch, actions),
|
||||
actions,
|
||||
selectors,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
let mws = effects.map((e) =>
|
||||
e({ getState, dispatch, actions, selectors, ...rest }),
|
||||
);
|
||||
export function buildEffectsMiddleware(
|
||||
effects = [],
|
||||
actions = {},
|
||||
selectors = {},
|
||||
) {
|
||||
return (api) => {
|
||||
const newApi = augmentMiddlewareApi(api, actions, selectors);
|
||||
|
||||
let mws = effects.map((e) => e(newApi));
|
||||
|
||||
return (originalNext) => {
|
||||
return mws.reduceRight((next, mw) => mw(next), originalNext);
|
||||
|
@ -1,48 +0,0 @@
|
||||
import { test, expect, vi } from 'vitest';
|
||||
|
||||
import { buildMiddleware } from './middleware.js';
|
||||
import { action } from './actions.js';
|
||||
|
||||
test('buildMiddleware, effects', async () => {
|
||||
const effectMock = vi.fn();
|
||||
|
||||
const mw = buildMiddleware([
|
||||
['*', (api) => (next) => (action) => effectMock()],
|
||||
]);
|
||||
|
||||
mw({})(() => {})({});
|
||||
|
||||
expect(effectMock).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
test('buildMiddleware, augmented api', async () => {
|
||||
const myAction = action('myAction');
|
||||
|
||||
const mw = buildMiddleware(
|
||||
[
|
||||
[
|
||||
'*',
|
||||
(api) => (next) => (action) => {
|
||||
expect(api.getState.mySelector()).toEqual(13);
|
||||
api.dispatch(myAction());
|
||||
next();
|
||||
},
|
||||
],
|
||||
],
|
||||
{
|
||||
myAction,
|
||||
},
|
||||
{
|
||||
mySelector: (state) => state?.selected,
|
||||
},
|
||||
);
|
||||
|
||||
const dispatch = vi.fn();
|
||||
const getState = vi.fn(() => ({ selected: 13 }));
|
||||
const next = vi.fn();
|
||||
|
||||
mw({ dispatch, getState })(next)(myAction());
|
||||
|
||||
expect(next).toHaveBeenCalledOnce();
|
||||
expect(dispatch).toHaveBeenCalledWith(myAction());
|
||||
});
|
Loading…
Reference in New Issue
Block a user