-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebouncer.mjs
36 lines (29 loc) · 1 KB
/
debouncer.mjs
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
"use strict"
export const DEBOUNCER_CANCEL = 0;
export const DEBOUNCER_INSTANT = 1;
export const DEBOUNCER_LATER = 2;
export const DEBOUNCER_RUSH = 3;
// RUSH - runs immediately if there is awaiting
// LATER - runs in specified amount of time
// CANCEL - cancels if there is anything ongoing
// INSTANT - runs immediately
export const runIfInactive = (callback, timeInMs = 30) => {
const activator = (sureToActivate) => {
const isAwaiting = activator.isAwaiting;
if (isAwaiting) {
clearTimeout(isAwaiting - 1);
activator.isAwaiting = 0;
}
if (sureToActivate === DEBOUNCER_INSTANT || (isAwaiting && (sureToActivate === DEBOUNCER_RUSH))) {
return callback();
}
if (sureToActivate === DEBOUNCER_CANCEL) {
return;
}
activator.isAwaiting = setTimeout(()=> {
activator.isAwaiting = 0;
callback();
}, timeInMs) + 1;
}
return activator;
};