-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement utility selectors bound to current state #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ exports.create = ({ createStore, mods = {} }) => { | |
middleEnd.getState; | ||
middleEnd.dispatch; | ||
middleEnd.actions; | ||
middleEnd.select; | ||
middleEnd.selectors; | ||
|
||
// Setup any schemas | ||
|
@@ -118,6 +119,19 @@ exports.create = ({ createStore, mods = {} }) => { | |
return internals.pickEach(middleEnd.mods, 'actions'); | ||
}); | ||
|
||
initializedProp('select', () => { | ||
|
||
const { selectors, getState } = middleEnd; | ||
|
||
// Convenient selection, | ||
// select.auth.isAuthenticated() versus selectors.auth.isAuthenticated(getState()) | ||
|
||
return internals.mapEachLeafFunction(selectors, (selector) => { | ||
|
||
return (...args) => selector(getState(), ...args); | ||
}); | ||
}); | ||
|
||
initializedProp('selectors', () => { | ||
|
||
return internals.pickEach(middleEnd.mods, 'selectors'); | ||
|
@@ -167,3 +181,18 @@ internals.pickEach = (obj, prop) => { | |
.filter(([, value]) => value && typeof value[prop] !== 'undefined') | ||
.reduce((collect, [key, value]) => ({ ...collect, [key]: value[prop] }), {}); | ||
}; | ||
|
||
internals.mapEachLeafFunction = (obj, map) => { | ||
|
||
return Object.entries(obj).reduce((collect, [key, value]) => { | ||
|
||
return { | ||
...collect, | ||
[key]: typeof value === 'function' ? | ||
map(value) : | ||
(value && typeof value === 'object') ? | ||
internals.mapEachLeafFunction(value, map) : | ||
value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, huh. what cases does returning plain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't design it for any specific use-case. I wanted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, gotcha, that makes sense. thanks for explaining! 🙏 |
||
}; | ||
}, {}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
baller! 🍺 ❤️