-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoizeOne.ts
42 lines (40 loc) · 1.48 KB
/
MemoizeOne.ts
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
function customIsEqual(args1: any[], args2: any[]) {
if (args1.length !== args2.length) return false;
for (let i = 0; i < args1.length; i++) {
if (args1[i] !== args2[i]) return false;
}
return true;
}
function memoizeOne<T extends (...args: any[]) => any>(
func: T,
isEqual?: (args: Parameters<T>, lastArgs: Parameters<T>) => boolean
) {
let lastArgs: null | Parameters<T> = null;
let savedResult: ReturnType<T> | null = null;
let lastThis: any | null = null;
return function (...newArgs: Parameters<T>) {
if (lastArgs) {
const isBothArgsEqual = isEqual
? isEqual(newArgs, lastArgs)
: customIsEqual(newArgs, lastArgs);
if (isBothArgsEqual && lastThis === this) {
//if args are equal and this on which function is called is also same then directly return the result
return savedResult as ReturnType<T>;
} else {
// if args are no same as previous then store current args and this(context) and call original function and save result
lastArgs = newArgs;
lastThis = this;
const result = func.apply(this, newArgs);
savedResult = result;
return savedResult as ReturnType<T>;
}
} else {
// if args do not exists then store current args and this(context) and call original function and save result
lastArgs = newArgs;
lastThis = this;
const result = func.apply(this, newArgs);
savedResult = result;
return savedResult as ReturnType<T>;
}
};
}