-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
functions.js
60 lines (51 loc) · 1.27 KB
/
functions.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @module ol/functions
*/
import {equals as arrayEquals} from './array.js';
/**
* Always returns true.
* @returns {boolean} true.
*/
export function TRUE() {
return true;
}
/**
* Always returns false.
* @returns {boolean} false.
*/
export function FALSE() {
return false;
}
/**
* A reusable function, used e.g. as a default for callbacks.
*
* @return {void} Nothing.
*/
export function VOID() {}
/**
* Wrap a function in another function that remembers the last return. If the
* returned function is called twice in a row with the same arguments and the same
* this object, it will return the value from the first call in the second call.
*
* @param {function(...any): ReturnType} fn The function to memoize.
* @return {function(...any): ReturnType} The memoized function.
* @template ReturnType
*/
export function memoizeOne(fn) {
let called = false;
/** @type ReturnType */
let lastResult;
/** @type Array<any> */
let lastArgs;
let lastThis;
return function() {
const nextArgs = Array.prototype.slice.call(arguments);
if (!called || this !== lastThis || !arrayEquals(nextArgs, lastArgs)) {
called = true;
lastThis = this;
lastArgs = nextArgs;
lastResult = fn.apply(this, arguments);
}
return lastResult;
};
}