augmentMiddlewareApi
This commit is contained in:
parent
f322691906
commit
5297aecba6
@ -22,34 +22,35 @@ export const augmentGetState = (originalGetState, selectors) => {
|
|||||||
}
|
}
|
||||||
return getState;
|
return getState;
|
||||||
};
|
};
|
||||||
export function buildEffectsMiddleware(
|
|
||||||
effects = [],
|
const augmentDispatch = (originalDispatch, actions) => {
|
||||||
actions = {},
|
|
||||||
selectors = {},
|
|
||||||
) {
|
|
||||||
return ({
|
|
||||||
getState: originalGetState,
|
|
||||||
dispatch: originalDispatch,
|
|
||||||
...rest
|
|
||||||
}) => {
|
|
||||||
const dispatch = (action) => originalDispatch(action);
|
const dispatch = (action) => originalDispatch(action);
|
||||||
|
|
||||||
for (const a in actions) {
|
for (const a in actions) {
|
||||||
dispatch[a] = (...args) => dispatch(actions[a](...args));
|
dispatch[a] = (...args) => dispatch(actions[a](...args));
|
||||||
}
|
}
|
||||||
|
return dispatch;
|
||||||
|
};
|
||||||
|
|
||||||
const getState = () => originalGetState();
|
const augmentMiddlewareApi = (api, actions, selectors) => {
|
||||||
for (const s in selectors) {
|
return {
|
||||||
getState[s] = (...args) => {
|
...api,
|
||||||
let result = selectors[s](originalGetState());
|
getState: augmentGetState(api.getState, selectors),
|
||||||
if (typeof result === 'function') return result(...args);
|
dispatch: augmentDispatch(api.dispatch, actions),
|
||||||
return result;
|
actions,
|
||||||
|
selectors,
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
let mws = effects.map((e) =>
|
export function buildEffectsMiddleware(
|
||||||
e({ getState, dispatch, actions, selectors, ...rest }),
|
effects = [],
|
||||||
);
|
actions = {},
|
||||||
|
selectors = {},
|
||||||
|
) {
|
||||||
|
return (api) => {
|
||||||
|
const newApi = augmentMiddlewareApi(api, actions, selectors);
|
||||||
|
|
||||||
|
let mws = effects.map((e) => e(newApi));
|
||||||
|
|
||||||
return (originalNext) => {
|
return (originalNext) => {
|
||||||
return mws.reduceRight((next, mw) => mw(next), 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