add middleware wrapper

This commit is contained in:
Yanick Champoux 2022-09-06 11:35:47 -04:00
parent 0b0ea7ea66
commit 7f1bcbddef
2 changed files with 12 additions and 4 deletions

View File

@ -22,9 +22,13 @@ export class Updux {
#selectors = {}; #selectors = {};
#effects = []; #effects = [];
#localReactions = []; #localReactions = [];
#middlewareWrapper;
constructor(config = {}) { constructor(config = {}) {
this.#config = config; this.#config = config;
this.#middlewareWrapper = config.middlewareWrapper;
this.#localInitial = config.initial; this.#localInitial = config.initial;
this.#subduxes = config.subduxes ?? {}; this.#subduxes = config.subduxes ?? {};
@ -148,7 +152,8 @@ export class Updux {
this.#effects, this.#effects,
this.actions, this.actions,
this.selectors, this.selectors,
this.subduxes, this.#subduxes,
this.#middlewareWrapper,
); );
} }

View File

@ -37,7 +37,7 @@ export function augmentMiddlewareApi(api, actions, selectors) {
} }
const sliceMw = (slice, mw) => (api) => { const sliceMw = (slice, mw) => (api) => {
const getSliceState = () => get(api.getState(), slice); const getSliceState = () => api.getState()[slice];
return mw({ ...api, getState: getSliceState }); return mw({ ...api, getState: getSliceState });
}; };
@ -65,10 +65,11 @@ export function buildMiddleware(
actions = {}, actions = {},
selectors = {}, selectors = {},
subduxes = {}, subduxes = {},
wrapper = undefined,
) { ) {
let inner = R.compact( let inner = R.compact(
Object.entries(subduxes).map((slice, [{ middleware }]) => Object.entries(subduxes).map(([slice, { middleware }]) =>
slice !== '*' && middleware ? sliceMw(slice, middleware) : null, ((slice !== '*' && middleware) ? sliceMw(slice, middleware) : null),
), ),
); );
@ -78,5 +79,7 @@ export function buildMiddleware(
let mws = [...local, ...inner]; let mws = [...local, ...inner];
if( wrapper ) mws = wrapper(mws);
return composeMw(mws); return composeMw(mws);
} }