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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type Selector<S, R> = (state: S) => R;

export type OutputSelector<S, R, C> = Selector<S, R> & {
resultFunc: C;
lastResult: () => R;
recomputations: () => number;
resetRecomputations: () => number;
}
Expand All @@ -12,6 +13,7 @@ export type ParametricSelector<S, P, R> = (state: S, props: P, ...args: any[]) =

export type OutputParametricSelector<S, P, R, C> = ParametricSelector<S, P, R> & {
resultFunc: C;
lastResult: () => R;
recomputations: () => number;
resetRecomputations: () => number;
}
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function getDependencies(funcs) {

export function createSelectorCreator(memoize, ...memoizeOptions) {
return (...funcs) => {
let lastResult
let recomputations = 0
const resultFunc = funcs.pop()
const dependencies = getDependencies(funcs)
Expand All @@ -75,11 +76,13 @@ export function createSelectorCreator(memoize, ...memoizeOptions) {
}

// apply arguments instead of spreading for performance.
return memoizedResultFunc.apply(null, params)
lastResult = memoizedResultFunc.apply(null, params)
return lastResult
})

selector.resultFunc = resultFunc
selector.dependencies = dependencies
selector.lastResult = () => lastResult
selector.recomputations = () => recomputations
selector.resetRecomputations = () => recomputations = 0
return selector
Expand Down