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

Give transformers access to the full, combined reducer state #682

Merged
merged 1 commit into from
Feb 1, 2018

Conversation

dougkeen
Copy link
Contributor

For example, this is useful if you have a transformer that needs to filter items in one reducer based on values in another reducer.

Let's say we're building a chat app, where we'd like the 100 most recent messages and the sender info shown right when the app starts up (without making a trip to the server). We have a users reducer, which is a bunch of user data objects keyed by userId. We also have a messages reducer, which is an ordered list of message data objects, with a senderId property that refers to the userId of the sender. We'd like to build a transformer that persists only the top 100 messages in the messages reducer, along with all the users in the user reducer that correspond to the senders of those 100 messages.

Right now, this isn't possible. When persisting users in the users reducer, the transformer isn't able to access the messages reducer to figure out which users are senders of the top 100 messages.

With this PR, that transformer can be written as:

import _ from "lodash";

const top100MessagesTransform = createTransform(
    (subState, key, fullState) => {
      if (key === "messages") {
        return _.take(subState, 100);
      } else if (key === "users") {
        const messages = _.take(fullState.messages, 100);
        const senderIdSet = _.keyBy(messages, "senderId");
        return _.pickBy(subState, (user, userId) => _.has(senderIdSet, userId));
      } else {
        return subState;
      }
    },
    (state) => state,
    {whitelist: ["messages", "users"]},
);

@rt2zz rt2zz changed the base branch from master to pp February 1, 2018 07:28
@rt2zz rt2zz merged commit 099785a into rt2zz:pp Feb 1, 2018
rt2zz pushed a commit that referenced this pull request Feb 1, 2018
@jpshelley
Copy link

I have an issue currently with full state marked here - #871

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants