-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockService.js
50 lines (36 loc) · 987 Bytes
/
ClockService.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
// (c) 2018-present, The Awesome Engineering Company, https://awesomeneg.com
"use strict";
import {ZephService} from "./Zeph.js";
const $FREQUENCY = Symbol("frequency");
const $NEXT = Symbol("next");
const $TIME = Symbol("time");
class ClockService extends ZephService {
constructor() {
super();
this[$NEXT] = null;
this[$TIME] = Date.now();
this.frequency = 60000;
}
get frequency() {
return this[$FREQUENCY];
}
set frequency(n=60000) {
n = parseInt(n,10);
if (n && !isNaN(parseInt(n))) this[$FREQUENCY] = n;
this.update();
}
get time() {
return this[$TIME];
}
update() {
if (this[$NEXT]) clearTimeout(this[$NEXT]);
this[$NEXT] = null;
let time = Date.now();
this[$TIME] = ((time/this.frequency)|0)*this.frequency;
let next = this[$TIME]+this.frequency-Date.now();
this[$NEXT] = setTimeout(this.update.bind(this),next);
this.fire("updated",this[$TIME]);
}
}
const instance = new ClockService();
export {instance as ClockService};