Skip to content
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

Merged
merged 2 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ Returns an uninitialized application:
- `app.store` - the redux store returned from the application's `createStore` config.
- `app.getState()` - an alias to `app.store.getState()`.
- `app.dispatch()` - an alias to `app.store.dispatch()`.
- `app.dispatch.MOD.ACTION()` - dispatches `MOD`'s action named `ACTION`, passing-along arguments to that action. Same as `app.dispatch(app.actions.MOD.ACTION())`.
- `app.dispatch.MOD.ACTION()` - dispatches `MOD`'s action named `ACTION`, passing along arguments to that action. Same as `app.dispatch(app.actions.MOD.ACTION())`.
- `app.actions.MOD.ACTION()` - an alias for `MOD`'s action named `ACTION`. Same as `app.mods.MOD.actions.ACTION()`.
- `app.select.MOD.SELECTOR()` - calls `MOD`'s selector named `SELECTOR` with the app's current state bound as the first argument and passing along additional arguments. Same as `app.mods.MOD.selectors.SELECTOR(app.getState())`.
- `app.selectors.MOD.SELECTOR()` - an alias for `MOD`'s selector named `SELECTOR`. Same as `app.mods.MOD.selectors.SELECTOR()`.

#### `middleware`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const MiddleEnd = require('strange-middle-end');

(async () => {

const { FETCH_USER, INCREMENT } = MiddleEnd.createTypes({
const { INCREMENT, FETCH_USER } = MiddleEnd.createTypes({
INCREMENT: MiddleEnd.type.simple,
FETCH_USER: MiddleEnd.type.async
});
Expand Down Expand Up @@ -142,7 +142,7 @@ const MiddleEnd = require('strange-middle-end');

await app.dispatch.model.fetchUser({ id: 42 });

console.log(app.selectors.model.getUser(app.getState()));
console.log(app.select.model.getUser());

app.dispatch.counter.increment();

Expand Down
29 changes: 29 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exports.create = ({ createStore, mods = {} }) => {
middleEnd.getState;
middleEnd.dispatch;
middleEnd.actions;
middleEnd.select;
middleEnd.selectors;

// Setup any schemas
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baller! 🍺 ❤️

});
});

initializedProp('selectors', () => {

return internals.pickEach(middleEnd.mods, 'selectors');
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, huh. what cases does returning plain value cover? is that kinda a selector to access a constant?
less a question about this PR, more about redux practice generally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't design it for any specific use-case. I wanted select to be identical to selectors except with any functions in there bound to state, and that lead to me just ignoring unfamiliar values. I suppose the other option would be to strip them out for select, but it seems like it would make app.select and app.selectors less "reflective" of each other.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, gotcha, that makes sense. thanks for explaining! 🙏

};
}, {});
};
45 changes: 45 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,51 @@ describe('Core', () => {
expect(m.selectors.a).to.shallow.equal(m.mods.a.selectors);
expect(m.selectors.c).to.shallow.equal(m.mods.c.selectors);
});

it('has state-bound selectors from mod branches.', () => {

const m = MiddleEnd.create({
mods: {
a: {
selectors: {
getUpper: ({ a }) => a.toUpperCase()
}
},
b: {},
c: {
selectors: {
multiply: ({ c }, y) => c * y,
some: 'value',
group: {
null: null,
manyArgs: ({ c }, three, four, five) => `${c}${three}${four}${five}`
zemccartney marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
d: null
},
createStore: () => {

return Redux.createStore(() => ({
a: 'a',
c: 2
}));
}
});

m.initialize();

expect(m.select).to.only.contain(['a', 'c']);
expect(m.select.a).to.only.contain(['getUpper']);
expect(m.select.c).to.only.contain(['multiply', 'some', 'group']);
expect(m.select.c.group).to.only.contain(['null', 'manyArgs']);

expect(m.select.a.getUpper()).to.equal('A');
expect(m.select.c.multiply(15)).to.equal(30);
expect(m.select.c.some).to.equal('value');
expect(m.select.c.group.null).to.equal(null);
expect(m.select.c.group.manyArgs(3, 4, 5)).to.equal('2345');
});
});

describe('middleware', () => {
Expand Down