Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (60 loc) · 1.7 KB
/
Copy pathindex.js
File metadata and controls
69 lines (60 loc) · 1.7 KB
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
61
62
63
64
65
66
67
68
69
/** @type {WeakMap<Function, number>} */
const runtimes = new WeakMap();
/**
* @param {unknown} fn The function to validate.
* @param {string} operation The calling function's name for the error message.
*/
function validateFn(fn, operation) {
if (typeof fn !== "function") {
throw new TypeError(`${operation} requires a function`);
}
}
/**
* @param {unknown} timeResolution The timeResolution to parse.
* @returns {number} The parsed and defaulted resolution.
*/
function parseTimeResolution(timeResolution) {
if (typeof timeResolution === "number") {
return timeResolution || 1;
}
if (typeof timeResolution === "string") {
return parseInt(timeResolution, 10) || 1;
}
return 1;
}
/**
* Retrieve the last time a function was run.
*
* @param {Function} fn The function to inspect.
* @param {string | number} [timeResolution] Resolution of returned timestamp.
* @returns {number | undefined} Timestamp of the last time the function was captured.
*/
export default function lastRun(fn, timeResolution) {
validateFn(fn, "lastRun");
const time = runtimes.get(fn);
if (time == null) {
return;
}
const resolution = parseTimeResolution(timeResolution);
return time - (time % resolution);
}
/**
* Stores a timestamp for the given function.
*
* @param {Function} fn The function to track.
* @param {number} [timestamp] Override of current time.
*/
export function capture(fn, timestamp) {
validateFn(fn, "capture");
timestamp = timestamp || Date.now();
runtimes.set(fn, timestamp);
}
/**
* Drops a timestamp for the given function.
*
* @param {Function} fn The function to untrack.
*/
export function release(fn) {
validateFn(fn, "release");
runtimes.delete(fn);
}