-
Notifications
You must be signed in to change notification settings - Fork 6
/
not-idle.ts
48 lines (41 loc) · 1.13 KB
/
not-idle.ts
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
import { Base, Interaction } from './base';
export class NotIdle extends Base {
protected immediate = false;
private interactedWithin = false;
/**
* @param interactions set of interactions which will prevent being idle
*/
public when(interactions: Interaction[] | Interaction): this {
this.concatInteractions(interactions);
return this;
}
/**
* Adds default interactions to decide if user is not interacting with page.
*/
public whenInteractive(): this {
this.pushDefaultInteractions();
return this;
}
/**
* Runs not idle action immediately when an interaction happens
* so does not wait until end of the current period to run.
*
* - Does NOT run immediately by default
*/
public immediately(immediate = true): this {
this.immediate = immediate;
return this;
}
protected onInteraction = () => {
if (!this.interactedWithin && this.immediate) {
this.callback();
}
this.interactedWithin = true;
};
protected onInterval = () => {
if (this.interactedWithin && !this.immediate) {
this.callback();
}
this.interactedWithin = false;
};
}