From 7f1bcbddef4c6c5dcf2a2e9831393c83e424e47b Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 6 Sep 2022 11:35:47 -0400 Subject: [PATCH] add middleware wrapper --- src/Updux.js | 7 ++++++- src/middleware.js | 9 ++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Updux.js b/src/Updux.js index 939a717..3aaf8bd 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -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, ); } diff --git a/src/middleware.js b/src/middleware.js index de4d598..4c5dc15 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -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); }