Skip to content

Commit 96cdf00

Browse files
authored
fix(system): allow wrapping selectors in chain (#7304)
Refs #7157
1 parent 385ef75 commit 96cdf00

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

src/core/system.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,23 +392,46 @@ function systemExtend(dest={}, src={}) {
392392
if(isObject(statePlugins)) {
393393
for(let namespace in statePlugins) {
394394
const namespaceObj = statePlugins[namespace]
395-
if(!isObject(namespaceObj) || !isObject(namespaceObj.wrapActions)) {
395+
if(!isObject(namespaceObj)) {
396396
continue
397397
}
398-
const { wrapActions } = namespaceObj
399-
for(let actionName in wrapActions) {
400-
let action = wrapActions[actionName]
401-
402-
// This should only happen if dest is the first plugin, since invocations after that will ensure its an array
403-
if(!Array.isArray(action)) {
404-
action = [action]
405-
wrapActions[actionName] = action // Put the value inside an array
406-
}
407398

408-
if(src && src.statePlugins && src.statePlugins[namespace] && src.statePlugins[namespace].wrapActions && src.statePlugins[namespace].wrapActions[actionName]) {
409-
src.statePlugins[namespace].wrapActions[actionName] = wrapActions[actionName].concat(src.statePlugins[namespace].wrapActions[actionName])
399+
const { wrapActions, wrapSelectors } = namespaceObj
400+
401+
// process action wrapping
402+
if (isObject(wrapActions)) {
403+
for(let actionName in wrapActions) {
404+
let action = wrapActions[actionName]
405+
406+
// This should only happen if dest is the first plugin, since invocations after that will ensure its an array
407+
if(!Array.isArray(action)) {
408+
action = [action]
409+
wrapActions[actionName] = action // Put the value inside an array
410+
}
411+
412+
if(src && src.statePlugins && src.statePlugins[namespace] && src.statePlugins[namespace].wrapActions && src.statePlugins[namespace].wrapActions[actionName]) {
413+
src.statePlugins[namespace].wrapActions[actionName] = wrapActions[actionName].concat(src.statePlugins[namespace].wrapActions[actionName])
414+
}
415+
410416
}
417+
}
418+
419+
// process selector wrapping
420+
if (isObject(wrapSelectors)) {
421+
for(let selectorName in wrapSelectors) {
422+
let selector = wrapSelectors[selectorName]
423+
424+
// This should only happen if dest is the first plugin, since invocations after that will ensure its an array
425+
if(!Array.isArray(selector)) {
426+
selector = [selector]
427+
wrapSelectors[selectorName] = selector // Put the value inside an array
428+
}
411429

430+
if(src && src.statePlugins && src.statePlugins[namespace] && src.statePlugins[namespace].wrapSelectors && src.statePlugins[namespace].wrapSelectors[selectorName]) {
431+
src.statePlugins[namespace].wrapSelectors[selectorName] = wrapSelectors[selectorName].concat(src.statePlugins[namespace].wrapSelectors[selectorName])
432+
}
433+
434+
}
412435
}
413436
}
414437
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import System from "core/system"
2+
3+
describe("wrapSelectors", () => {
4+
it("should wrap correctly when registering multiple plugins targeting the same selector", function() {
5+
const probeBase = {
6+
statePlugins: {
7+
probe: {
8+
selectors: {
9+
selectProbe: () => {
10+
return "base"
11+
}
12+
}
13+
}
14+
}
15+
}
16+
const probeWrap1 = {
17+
statePlugins: {
18+
probe: {
19+
wrapSelectors: {
20+
selectProbe: (oriSelector) => (state, ...args) => {
21+
const selectedValue = oriSelector(state, ...args)
22+
return `${selectedValue}wrap1`
23+
}
24+
}
25+
}
26+
}
27+
}
28+
const probeWrap2 = {
29+
statePlugins: {
30+
probe: {
31+
wrapSelectors: {
32+
selectProbe: (oriSelector) => (state, ...args) => {
33+
const selectedValue = oriSelector(state, ...args)
34+
return `${selectedValue}wrap2`
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
const system = new System({ plugins: [probeBase, probeWrap1, probeWrap2] })
42+
43+
expect(system.getSystem().probeSelectors.selectProbe()).toEqual("basewrap1wrap2")
44+
})
45+
})

0 commit comments

Comments
 (0)