This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathutils.js
54 lines (50 loc) · 1.29 KB
/
utils.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
// http://www.alloyteam.com/2012/11/javascript-throttle/
export function throttle (fn, delay, mustRunDelay = 0) {
if (delay == null) return fn;
/* istanbul ignore next */
const timestampProvider =
typeof performance === 'object' ? performance : Date;
let timer = null;
let tStart;
return function () {
const tCurr = timestampProvider.now();
if (timer != null) clearTimeout(timer);
if (!tStart) {
tStart = tCurr;
}
if (mustRunDelay !== 0 && tCurr - tStart >= mustRunDelay) {
fn.apply(this, arguments);
tStart = tCurr;
} else {
const context = this;
const args = [...arguments];
timer = setTimeout(function () {
timer = null;
return fn.apply(context, args);
}, delay);
}
};
}
export const PASSIVE_OPTS = (function () {
let value = false;
try {
window.addEventListener('test', noop, {
get passive() {
value = true;
return true;
}
});
window.removeEventListener('test', noop);
} catch (e) {
/* istanbul ignore next */
value = false;
}
return value && { passive: true };
/* istanbul ignore next */
function noop() {}
})();
export function create(prototype, properties) {
const obj = Object.create(prototype);
Object.assign(obj, properties);
return obj;
}