Skip to content

Commit

Permalink
fix(wrapSelectors): allow chain of wrapping selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed May 24, 2021
1 parent ea0f2b5 commit 6f70609
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions src/system/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,55 @@ 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 ).
*/
const { statePlugins } = dest;
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 */
Expand Down

0 comments on commit 6f70609

Please # to comment.