-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Memoize prop transformation functions and specify their arguments
Meant to be similar to reselect's `createSelector`, but instead of reaching into redux `state`, it reaches into the component's `this`.
- Loading branch information
1 parent
13685d6
commit 7015a57
Showing
3 changed files
with
65 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This memoizes a function by remembering its last args and its last result | ||
// and returning the last result if the args are the same as the last call. | ||
export function memoize(func) { | ||
var lastArgs = null; | ||
var lastResult; | ||
|
||
function argsDifferent(args) { | ||
return lastArgs === null || | ||
lastArgs.length != args.length || | ||
args.some((arg, idx) => { return arg !== lastArgs[idx] }); | ||
} | ||
|
||
return function(...args) { | ||
if(argsDifferent(args)) { | ||
lastArgs = args; | ||
lastResult = func(...args); | ||
} | ||
return lastResult | ||
} | ||
} | ||
|
||
// This memoizes `func` and returns a function that calls | ||
// the memoized `func` with the arguments returned by `argFunc` | ||
export function createMemoizedFunction(argFunc, func) { | ||
var memoized = memoize(func); | ||
return function() { | ||
return memoized(...argFunc()); | ||
} | ||
} |