-
Notifications
You must be signed in to change notification settings - Fork 14
/
memoizeLast.js
44 lines (42 loc) · 1.12 KB
/
memoizeLast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { is, Seq } from 'immutable';
/**
* Like memoize, but only caches the most recent value.
* It's often useful for caching expensive calculations in react components.
*
* @example
* const sum = memoizeLast((...nums) => nums.reduce((acc, n) => acc + n));
* sum(List.of(1, 2, 3))
* //> does work, returns 6
* sum(List.of(1, 2, 3))
* //> takes cached value, returns 6
* sum(List.of(4, 5, 6))
* //> does work, returns 15
* sum(List.of(1, 2, 3))
* //> does work again, returns 6
*
* @param {Function} operation
* @return {Function}
*/
export default function memoizeLast(operation) {
if (operation.length === 1) {
let prevArg1;
let prevResult1;
return function memoizedLast1(arg) {
if (prevArg1 === undefined || !is(arg, prevArg1)) {
prevResult1 = operation(arg);
prevArg1 = arg;
}
return prevResult1;
};
}
let prevArgsN;
let prevResultN;
return function memoizedLastN(...args) {
const argsN = Seq(args);
if (prevArgsN === undefined || !is(argsN, prevArgsN)) {
prevResultN = operation(...args);
prevArgsN = argsN;
}
return prevResultN;
};
}