diff --git a/src/effects.ts b/src/effects.ts index 4d76595..ad5076a 100644 --- a/src/effects.ts +++ b/src/effects.ts @@ -22,34 +22,35 @@ export const augmentGetState = (originalGetState, selectors) => { } return getState; }; + +const augmentDispatch = (originalDispatch, actions) => { + const dispatch = (action) => originalDispatch(action); + + for (const a in actions) { + dispatch[a] = (...args) => dispatch(actions[a](...args)); + } + return dispatch; +}; + +const augmentMiddlewareApi = (api, actions, selectors) => { + return { + ...api, + getState: augmentGetState(api.getState, selectors), + dispatch: augmentDispatch(api.dispatch, actions), + actions, + selectors, + }; +}; + export function buildEffectsMiddleware( effects = [], actions = {}, selectors = {}, ) { - return ({ - getState: originalGetState, - dispatch: originalDispatch, - ...rest - }) => { - const dispatch = (action) => originalDispatch(action); + return (api) => { + const newApi = augmentMiddlewareApi(api, actions, selectors); - for (const a in actions) { - dispatch[a] = (...args) => dispatch(actions[a](...args)); - } - - 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; - }; - } - - let mws = effects.map((e) => - e({ getState, dispatch, actions, selectors, ...rest }), - ); + let mws = effects.map((e) => e(newApi)); return (originalNext) => { return mws.reduceRight((next, mw) => mw(next), originalNext); diff --git a/src/middleware.test.todo b/src/middleware.test.todo deleted file mode 100644 index 8e11cfa..0000000 --- a/src/middleware.test.todo +++ /dev/null @@ -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()); -});