-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidle.ts
53 lines (45 loc) · 1.09 KB
/
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
49
50
51
52
53
import { Base, Interaction } from './base';
export class Idle extends Base {
private repetitive = false;
/**
* @param interactions set of interactions which will prevent being idle
*/
public whenNot(interactions: Interaction[] | Interaction): this {
this.concatInteractions(interactions);
return this;
}
/**
* Adds default interactions to decide if user is not interacting with page.
*/
public whenNotInteractive(): this {
this.pushDefaultInteractions();
return this;
}
/**
* Repeat calling the callback for each timeout frame.
*
* - Does NOT repeat by default
*/
public repeat(repeat = true): this {
this.repetitive = repeat;
return this;
}
/**
* Restart away timer
* Can be used incase of something different than events on `EventTarget`s
*/
public restart(): this {
this.onInteraction();
return this;
}
protected onInteraction = () => {
this.clearInterval();
this.setInterval();
};
protected onInterval = () => {
this.callback();
if (!this.repetitive) {
this.clearInterval();
}
};
}