From 6f70609ed3a1b5e371dfbb3634a128225b77b3c9 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Mon, 24 May 2021 13:53:13 +0200 Subject: [PATCH] fix(wrapSelectors): allow chain of wrapping selectors --- src/system/helpers.js | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/system/helpers.js b/src/system/helpers.js index e7988f6a..ffbbc5a9 100644 --- a/src/system/helpers.js +++ b/src/system/helpers.js @@ -37,7 +37,7 @@ export const systemExtend = (dest = {}, src = {}) => { } /** - * Account for wrapActions, make it an array and append to it. + * Account for wrapActions/wrapSelectors make it an array and append to it. * Modifies `src`. * 80% of this code is just safe traversal. We need to address that ( ie: use a lib ). */ @@ -45,25 +45,47 @@ export const systemExtend = (dest = {}, src = {}) => { if (isPlainObject(statePlugins)) { keys(statePlugins).forEach((namespace) => { const namespaceObj = statePlugins[namespace]; - if (!isPlainObject(namespaceObj) || !isPlainObject(namespaceObj.wrapActions)) { + if (!isPlainObject(namespaceObj)) { return; } - const { wrapActions } = namespaceObj; - keys(wrapActions).forEach((actionName) => { - let action = wrapActions[actionName]; - - // this should only happen if dest is the first plugin, since invocations after that will ensure its an array - if (isNotArray(action)) { - action = [action]; - wrapActions[actionName] = action; // put the value inside an array - } - - if (hasPath(['statePlugins', namespace, 'wrapActions', actionName], src)) { - src.statePlugins[namespace].wrapActions[actionName] = wrapActions[actionName].concat( - src.statePlugins[namespace].wrapActions[actionName] - ); - } - }); + const { wrapActions, wrapSelectors } = namespaceObj; + // process action wrapping + if (isPlainObject(wrapActions)) { + keys(wrapActions).forEach((actionName) => { + let action = wrapActions[actionName]; + + // this should only happen if dest is the first plugin, since invocations after that will ensure its an array + if (isNotArray(action)) { + action = [action]; + wrapActions[actionName] = action; // put the value inside an array + } + + if (hasPath(['statePlugins', namespace, 'wrapActions', actionName], src)) { + src.statePlugins[namespace].wrapActions[actionName] = wrapActions[actionName].concat( + src.statePlugins[namespace].wrapActions[actionName] + ); + } + }); + } + + // process selector wrapping + if (isPlainObject(wrapSelectors)) { + keys(wrapSelectors).forEach((selectorName) => { + let selector = wrapSelectors[selectorName]; + + // this should only happen if dest is the first plugin, since invocations after that will ensure its an array + if (isNotArray(selector)) { + selector = [selector]; + wrapSelectors[selectorName] = selector; // put the value inside an array + } + + if (hasPath(['statePlugins', namespace, 'wrapSelectors', selectorName], src)) { + src.statePlugins[namespace].wrapSelectors[selectorName] = wrapSelectors[ + selectorName + ].concat(src.statePlugins[namespace].wrapSelectors[selectorName]); + } + }); + } }); } /* eslint-enable no-param-reassign */