Possibility of implementing setters
Part of the power of getter libraries like Haskell's Lens is that they also include a powerful mechanism for setters as well.
As an outside observer who is in no way an expert on Redux or Reselect, I wonder if it's possible to implement setters in Reselect.
Here's what I dreamed up:
const postSelector = state => state.posts;
const titleSelector = createSelector(
[postSelector],
posts => posts.reduce((acc, post) => acc.push({id: post.id, title: post.title}), [])
);
const titleCaseSetter = createSetter(
[UPDATE_POST, MARK_POST],
[titleSelector],
titles => titles.map(title => title.title.toUpperCase())
);
Where createSetter is something like this:
function createSetter( actions, selectors, setterFunction )
{
if( !Array.isArray( actions ) )
actions = [actions];
var setterEval = setterFunction(...selectors);
for( let i = 0; i < actions.length; i++ )
{
dispatch({
type: actions[i],
...setterEval
});
}
}
Thoughts? I'm not sure if it's even possible but it seemed like a cool idea.
Oops, createSetter wouldn't dispatch immediately, but return a function that could be given to dispatch. Basically a setter would be a higher-order-action.
Hey @CooCooCaCha, thanks for the suggestion! I'm not sure I understand this fully, but I am intrigued. I wonder if including the posts reducer in the example would make it clearer to me?
@ellbee I apologize for taking so long to reply.
The posts reducer is super simple like the todo reducer in the redux examples. I don't know if there is a better way to accomplish this in redux but I'll try to explain a bit more.
Imagine I have a post data type that looks like this:
var post = {
title: [title text],
body: [body text],
published: [boolean, is this post published]
}
And my reducer is basically a list of posts and has some simple actions like ADD_POST, UPDATE_POST, and PUBLISH_POST.
So the idea of getters is that you take a data-structure and create a new data-structure derived form the original. For example, I could take my list of posts and derive a list of titles. This way I have an easier data-structure to work with in my app. This is basically what reselect already does.
Setters are a lot like getters but with the requirement that the resulting data-structure has to map 1-to-1 with the original. Since they map back to the original data-structure, I can manipulate the result of my setter and the original data-structure will change too.
I'm going to change the example above to make more sense. Let's say I'm managing my blog and I want a button that publishes posts. I also want to make sure that my post titles are capitalized properly before they are published.
const postSelector = state => state.posts;
const titleSelector = createSelector(
[postSelector],
posts => posts.reduce((acc, post) => acc.push({id: post.id, title: post.title}), [])
);
const publishSetter = createSetter(
[UPDATE_POST, PUBLISH_POST],
[titleSelector],
titles => titles.map(title => title.title.toUpperCase())
);
createSetter returns a function that I can then dispatch. So, for example, let's say I have a publish button and it has an onClick handler that looks like this:
onClick() {
this.props.dispatch(publishSetter());
}
I can then reuse this setter wherever I may have a publish button. You are effectively composing your Redux actions into higher-order-actions.
I hope this makes sense!
Also, the post objects would have to have an ID like so:
var post = {
id: [integer],
title: [title text],
body: [body text],
published: [boolean, is this post published]
}
Since both UPDATE_POST and PUBLISH_POST require an ID you can use the same selector for both. This is why the titleSelector above returns a title and an ID.
Hi @CooCooCaCha, no need to apologise! Thanks for the extra details, I get it now.
Instead of implementing this in reselect, another approach that might be worth considering is to create a "setter" middleware that dispatches multiple actions, and a higher-order action creator that can compose other action creators together. I realise that's a vague explanation, I'll try to knock up an example tomorrow and see what you think.
I've been playing around with the proposed setters API using the 'posts' example above. Are there any clear wins for using the 'setters' version over doing something like this?
publishSetter() {
const { props: { dispatch, titles } } = this;
titles.forEach(title => {
dispatch(updatePostActionCreator(title.id, "title", title.toUpperCase()));
dispatch(publishPostActionCreator(title.id));
});
}
@CooCooCaCha @ellbee I would suggest not to combine setters into reselect. To achieve something like "setters" I would propose to create utility libraries/functions which can create your actions creators as well as the corresponding selector (using reselect) based on a central data definition.
For example we are currently working on a an abstract form definition (don't know if we will be able to open source it) which lets you specify your form "schema" like this (pseudo code)
let loginFormSchema = {
username: {
description: ...
defaultValue: ....
validators: [ required ]
},
password: {
...
}
}
Using a factory function (eg. createFormDescription(yourSchema)) this will create your form action creators (setting a formvalue...) and selectors (selectors for checking if the form or a specific field is valid, retrieving the form values...).
So I think the same approach should be usable in your case. First you will need some kind of generic reducer. This reducer should support update actions which allow you to specify the data which should be updated by some kind of path specification. Having this reducer and the corresponding actionCreator it should be straight forward to create some kind of accessor functionality.
e.g.
let titleAccessors = createAccessor('/posts/title');
let titles = titleAccesors.selectAll(state);
let title = titleAccessor.selectById(5)(state);
dispatch(titleAccessors.update(5, 'new Title'));
I think this will result in a simplified version of a client only graphql. What do you think of this approach?
Closing due to inactivity. I think that this currently falls outside of the scope of Reselect.