Skip to content

Dependent selectors #300

Closed
Closed

Description

@TiddoLangerak

I'm running into issues where selectors are dependent on other selectors, in such a way that it triggers many cache misses. E.g. consider this state:

{
  companies : {
    1 : { 
        name : 'x',
        customers: [1,2]
     },
     2 : {
         name : 'Y',
         customers : [2,3]
     }
  },
  customers : {
    1 : {
        name : 'Person 1',
        age : 25
    }, /* etc */
  }
}

Now suppose I want to write a selector that gives the average age of the customers for a given company. Initially, I would do something like this:

function company(id) {
  return state => state.companies[id];
}
const customers = state => state.customers;

function customersForCompany(companyId) {
  return createSelector(
    company(companyId),
    customers,
    (company, customers) => company.customers.map(id => customers[id])
}

function avgCustomerAge(companyId) {
  return createSelector(
    customersForCompany(companyId),
    (customers) => avg(customers.map(customer => customer.age))
}

const avgCustomerAgeForX = avgCustomerAge(1);

Technically this works, but it isn't ideal: when any customer changes, then the average will be recalculated - even if it was a completely unrelated customer. I want my selector to only recalculate when relevant data changes.

With a bit of puzzling, I came with the following solution, but I can't help but have the feeling that it's not ideal:

function company(id) {
  return state => state.companies[id];
}
function customer(id) {
  return state => state.customers[id];
}
function customers(ids) {
  // Instead of directly filtering the state.customers, we create selectors for each id.
  // By using these as input selectors, reselect will only recompute the result when any
  // of the specific customers change.
  return createSelector(
    ids.map(id => customer(id)),
    (...customers) => customers
  );
}

function customersForCompany(companyId) {
  // Instead of selecting the customers directly here, we select a selector for the customers.
  // As long as the company stays the same, we'll get exactly the same customers selector,
  // which is memoized for the specific customers only
  const selector = createSelector(
    company(id),
    company => customers(company.customers)
  );
  // And wrap it, to hide the "Selector of selector" part.
  return state => selector(state)(state);
}

function avgCustomerAge(companyId) {
  return state => {
    customersForCompany(companyId),
    (customers) => avg(customers.map(customer => customer.age))
  }
}

This works as intended, but it's not exactly straightforward. Is there any better way to achieve this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions