-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimeTrack.js
More file actions
63 lines (52 loc) · 1.38 KB
/
TimeTrack.js
File metadata and controls
63 lines (52 loc) · 1.38 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
63
import { createNanoEvents } from "./functions.js";
export class TimeTrack {
constructor(timestamp, paused) {
this.memory_ = 0;
this.paused_ = false;
this.eventEmitter_ = createNanoEvents();
if (paused) {
this.pause();
}
if (timestamp) {
this.setMillis(timestamp);
} else {
this.setMillis(0);
}
}
millis() {
if (this.paused_) {
return this.memory_;
} else {
return Date.now() - this.memory_;
}
}
setState(current_timestamp, paused) {
if ((paused && !this.paused_) || (!paused && this.paused_)) {
this.paused_ = paused;
this.memory_ = Date.now() - this.memory_;
}
this.memory_ = this.paused_ ? current_timestamp : Date.now() - current_timestamp;
this.eventEmitter_.emit("change", { target: this });
}
setMillis(current_timestamp) {
this.memory_ = this.paused_ ? current_timestamp : Date.now() - current_timestamp;
this.eventEmitter_.emit("change", { target: this });
}
pause() {
if (!this.paused_) {
this.paused_ = true;
this.memory_ = Date.now() - this.memory_;
this.eventEmitter_.emit("change", { target: this });
}
}
unpause() {
if (this.paused_) {
this.paused_ = false;
this.memory_ = Date.now() - this.memory_;
this.eventEmitter_.emit("change", { target: this });
}
}
paused() {
return this.paused_;
}
}