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

Expose selector's result to allow adhoc caching #448

Closed
wants to merge 3 commits into from

Conversation

jcready
Copy link

@jcready jcready commented Mar 24, 2020

When a selector returns a complex value it can be difficult to get caching right. This will expose a way for selectors to see their own previous result which provides a way for users to do things like return the previous result if the result of the current call will be shallowly equal to previous result.

See #441 for an example of the feature request. Here is how this change would facilitate the solution:

import { createSelector } from 'reselect';
import { shallowEqualArrays } from 'shallow-equal';

const currentUsersPosts = createSelector(
  [state => state.currentUser.id, state=> state.posts],
  (userId, posts) => {
    const result = posts.filter(p=>p.id === userId);
    const lastResult = currentUsersPosts.lastResult();
    if (shallowEqualArrays(lastResult, result)) return lastResult;
    return result;
  }
);

jcready added 2 commits March 24, 2020 11:32
When a selector returns a complex value it can be difficult to get
caching right. This will expose a way for selectors to see their own
previous result which provides a way for users to do things like
return the previous result if the result of the current call will be
shallowly equal to previous result.
@codecov
Copy link

codecov bot commented Mar 24, 2020

Codecov Report

Merging #448 into master will not change coverage by %.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #448   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           15        15           
=========================================
  Hits            15        15           
Impacted Files Coverage Δ
src/index.js 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c25739...9f04a19. Read the comment docs.

@markerikson
Copy link
Contributor

Superseded by #514 .

@jcready jcready deleted the patch-1 branch October 20, 2021 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants