-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutils.js
More file actions
62 lines (58 loc) · 2.01 KB
/
Copy pathutils.js
File metadata and controls
62 lines (58 loc) · 2.01 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
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
/**
* Limit the number of calls to `fn` to 1 per `time` maximum.
* First call is immediate if `time` have been waited already.
* All other calls before end of `time` window will lead to 1 exececution at the end of window.
* @param {(...args: unknown[]) => void} fn - function to be called
* @param {number} time - ms
* @returns {(...args: unknown[]) => void} lambda function to be called to call `fn`
* @example
* let f = callRateLimiter((arg) => console.log('called', arg), 1000);
* 00:00:00 f(1);f(2);f(3);f(4);
* 00:00:00 called 1
* 00:00:01 called 4
*/
export function callRateLimiter(fn, time) {
let timer = undefined;
let lastCall = undefined;
return (...args) => {
// first call or last call was far in the past: let's exec
if (!lastCall || Date.now() - lastCall > time) {
lastCall = Date.now();
fn.call(null, ...args);
return;
}
// exec already planed, replace it with new arguments
if (timer) {
clearTimeout(timer);
}
// plan an exec for near future
timer = setTimeout(() => {
lastCall = Date.now();
fn.call(null, ...args);
timer = null;
}, time - (Date.now() - lastCall));
};
}
/**
* Given a string, it will attempt to update the tab title if the `document` object exists
* @param {string} title - name that should be updating the browser tab
* @returns {void}
*/
export function setBrowserTabTitle(title = undefined) {
if (document && title) {
document.title = title;
}
}