Skip to content

Commit 1f314dd

Browse files
committed
Add wrapSelectors functionality
1 parent 3881a82 commit 1f314dd

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

src/core/system.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class Store {
7676
this.boundSystem = Object.assign({},
7777
this.getRootInjects(),
7878
this.getWrappedAndBoundActions(dispatch),
79-
this.getBoundSelectors(getState, this.getSystem),
79+
this.getWrappedAndBoundSelectors(getState, this.getSystem),
8080
this.getStateThunks(getState),
8181
this.getFn(),
8282
this.getConfigs()
@@ -176,6 +176,36 @@ export default class Store {
176176
})
177177
}
178178

179+
getWrappedAndBoundSelectors(getState, getSystem) {
180+
let selectorGroups = this.getBoundSelectors(getState, getSystem)
181+
return objMap(selectorGroups, (selectors, selectorGroupName) => {
182+
let stateName = [selectorGroupName.slice(0, -9)] // selectors = 9 chars
183+
let wrappers = this.system.statePlugins[stateName].wrapSelectors
184+
if(wrappers) {
185+
return objMap(selectors, (selector, selectorName) => {
186+
let wrap = wrappers[selectorName]
187+
if(!wrap) {
188+
return selector
189+
}
190+
191+
if(!Array.isArray(wrap)) {
192+
wrap = [wrap]
193+
}
194+
return wrap.reduce((acc, fn) => {
195+
let wrappedSelector = (...args) => {
196+
return fn(acc, this.getSystem())(getState().getIn(stateName), ...args)
197+
}
198+
if(!isFn(wrappedSelector)) {
199+
throw new TypeError("wrapSelector needs to return a function that returns a new function (ie the wrapped action)")
200+
}
201+
return wrappedSelector
202+
}, selector || Function.prototype)
203+
})
204+
}
205+
return selectors
206+
})
207+
}
208+
179209
getStates(state) {
180210
return Object.keys(this.system.statePlugins).reduce((obj, key) => {
181211
obj[key] = state.get(key)

test/core/system.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,122 @@ describe("bound system", function(){
326326

327327
})
328328

329+
describe('wrapSelectors', () => {
330+
it("should wrap a selector and provide a reference to the original", function(){
331+
332+
// Given
333+
const system = new System({
334+
plugins: [
335+
{
336+
statePlugins: {
337+
doge: {
338+
selectors: {
339+
wow: () => (system) => {
340+
return "original"
341+
}
342+
}
343+
}
344+
}
345+
},
346+
{
347+
statePlugins: {
348+
doge: {
349+
wrapSelectors: {
350+
wow: (ori) => (system) => {
351+
// Then
352+
return ori() + " wrapper"
353+
}
354+
}
355+
}
356+
}
357+
}
358+
]
359+
})
360+
361+
// When
362+
var res = system.getSystem().dogeSelectors.wow(1)
363+
expect(res).toEqual("original wrapper")
364+
365+
})
366+
367+
it("should provide a live reference to the system to a wrapper", function(done){
368+
369+
// Given
370+
const mySystem = new System({
371+
plugins: [
372+
{
373+
statePlugins: {
374+
doge: {
375+
selectors: {
376+
wow: () => (system) => {
377+
return "original"
378+
}
379+
}
380+
}
381+
}
382+
},
383+
{
384+
statePlugins: {
385+
doge: {
386+
wrapSelectors: {
387+
wow: (ori, system) => () => {
388+
// Then
389+
expect(mySystem.getSystem()).toEqual(system.getSystem())
390+
done()
391+
return ori() + " wrapper"
392+
}
393+
}
394+
}
395+
}
396+
}
397+
]
398+
})
399+
400+
mySystem.getSystem().dogeSelectors.wow(1)
401+
})
402+
403+
it("should provide the state as the first argument to the inner function", function(done){
404+
405+
// Given
406+
const mySystem = new System({
407+
state: {
408+
doge: {
409+
abc: "123"
410+
}
411+
},
412+
plugins: [
413+
{
414+
statePlugins: {
415+
doge: {
416+
selectors: {
417+
wow: () => (system) => {
418+
return "original"
419+
}
420+
}
421+
}
422+
}
423+
},
424+
{
425+
statePlugins: {
426+
doge: {
427+
wrapSelectors: {
428+
wow: (ori, system) => (dogeState) => {
429+
// Then
430+
expect(dogeState.toJS().abc).toEqual('123')
431+
done()
432+
return ori() + " wrapper"
433+
}
434+
}
435+
}
436+
}
437+
}
438+
]
439+
})
440+
441+
mySystem.getSystem().dogeSelectors.wow(1)
442+
})
443+
})
444+
329445
})
330446

331447
})

0 commit comments

Comments
 (0)