|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | export class Clock { |
18 | | - private _ended: boolean; |
19 | | - private _startTime: Date; |
20 | | - private _hrtime: [number, number]; |
21 | | - private diff: [number,number]; |
| 18 | + private endedLocal: boolean; |
| 19 | + private startTimeLocal: Date; |
| 20 | + private hrtimeLocal: [number, number]; |
| 21 | + private diff: [number, number]; |
22 | 22 |
|
23 | | - constructor() { |
24 | | - this._ended = false |
25 | | - this._startTime = new Date() |
26 | | - this._hrtime = process.hrtime() |
27 | | - this.diff = null |
28 | | - } |
| 23 | + constructor() { |
| 24 | + this.endedLocal = false; |
| 25 | + this.startTimeLocal = new Date(); |
| 26 | + this.hrtimeLocal = process.hrtime(); |
| 27 | + this.diff = null; |
| 28 | + } |
29 | 29 |
|
30 | | - public end(): void { |
31 | | - if (this._ended){ |
32 | | - return |
33 | | - } |
34 | | - this.diff = process.hrtime(this._hrtime) |
35 | | - this._ended = true |
36 | | - } |
37 | | - |
38 | | - public get duration(): number { |
39 | | - if (!this._ended){ |
40 | | - return null |
41 | | - } |
42 | | - var ns = this.diff[0] * 1e9 + this.diff[1] |
43 | | - return ns / 1e6 |
44 | | - } |
45 | | - |
46 | | - public offset(timer: Clock): number { |
47 | | - var a = timer.hrtime |
48 | | - var b = this.hrtime |
49 | | - var ns = (b[0] - a[0]) * 1e9 + (b[1] - a[1]) |
50 | | - return ns / 1e6 |
| 30 | + end(): void { |
| 31 | + if (this.endedLocal) { |
| 32 | + return; |
51 | 33 | } |
| 34 | + this.diff = process.hrtime(this.hrtimeLocal); |
| 35 | + this.endedLocal = true; |
| 36 | + } |
52 | 37 |
|
53 | | - public get hrtime() : [number, number] { |
54 | | - return this._hrtime; |
55 | | - } |
56 | | - |
57 | | - public get startTime(): Date { |
58 | | - return this._startTime; |
| 38 | + get duration(): number { |
| 39 | + if (!this.endedLocal) { |
| 40 | + return null; |
59 | 41 | } |
60 | | - |
61 | | - public get endTime(): Date { |
62 | | - let result: Date = null; |
63 | | - if(this.ended) { |
64 | | - result= new Date(this.startTime.getTime() + this.duration); |
65 | | - } |
66 | | - return result; |
67 | | - } |
68 | | - |
69 | | - public get ended(): boolean { |
70 | | - return this._ended; |
71 | | - } |
72 | | - |
73 | | -} |
74 | | - |
75 | | - |
76 | | - |
| 42 | + const ns = this.diff[0] * 1e9 + this.diff[1]; |
| 43 | + return ns / 1e6; |
| 44 | + } |
77 | 45 |
|
| 46 | + offset(timer: Clock): number { |
| 47 | + const a = timer.hrtime; |
| 48 | + const b = this.hrtime; |
| 49 | + const ns = (b[0] - a[0]) * 1e9 + (b[1] - a[1]); |
| 50 | + return ns / 1e6; |
| 51 | + } |
78 | 52 |
|
| 53 | + get hrtime(): [number, number] { |
| 54 | + return this.hrtimeLocal; |
| 55 | + } |
79 | 56 |
|
| 57 | + get startTime(): Date { |
| 58 | + return this.startTimeLocal; |
| 59 | + } |
80 | 60 |
|
| 61 | + get endTime(): Date { |
| 62 | + let result: Date = null; |
| 63 | + if (this.ended) { |
| 64 | + result = new Date(this.startTime.getTime() + this.duration); |
| 65 | + } |
| 66 | + return result; |
| 67 | + } |
81 | 68 |
|
| 69 | + get ended(): boolean { |
| 70 | + return this.endedLocal; |
| 71 | + } |
| 72 | +} |
0 commit comments