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 = {};
#effects = [];
#localReactions = [];
#middlewareWrapper;
constructor(config = {}) {
this.#config = config;
this.#middlewareWrapper = config.middlewareWrapper;
this.#localInitial = config.initial;
this.#subduxes = config.subduxes ?? {};
@ -148,7 +152,8 @@ export class Updux {
this.#effects,
this.actions,
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 getSliceState = () => get(api.getState(), slice);
const getSliceState = () => api.getState()[slice];
return mw({ ...api, getState: getSliceState });
};
@ -65,10 +65,11 @@ export function buildMiddleware(
actions = {},
selectors = {},
subduxes = {},
wrapper = undefined,
) {
let inner = R.compact(
Object.entries(subduxes).map((slice, [{ middleware }]) =>
slice !== '*' && middleware ? sliceMw(slice, middleware) : null,
Object.entries(subduxes).map(([slice, { middleware }]) =>
((slice !== '*' && middleware) ? sliceMw(slice, middleware) : null),
),
);
@ -78,5 +79,7 @@ export function buildMiddleware(
let mws = [...local, ...inner];
if( wrapper ) mws = wrapper(mws);
return composeMw(mws);
}