forked from teambit/bit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.ts
More file actions
34 lines (30 loc) · 794 Bytes
/
timer.ts
File metadata and controls
34 lines (30 loc) · 794 Bytes
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
import { TimerAlreadyRunning, TimerNotStarted } from './exceptions';
import { TimerResponse } from './response';
export class Timer {
private startTime: number | null = null;
/**
* start the timer.
*/
start(): Timer {
if (this.startTime) throw new TimerAlreadyRunning();
this.startTime = Date.now();
return this;
}
/**
* stop the timer and return timer results.
*/
stop(): TimerResponse {
if (!this.startTime) throw new TimerNotStarted();
const endTime = Date.now();
return new TimerResponse(this.calculateElapsed(this.startTime, endTime));
}
private calculateElapsed(startTime: number, endTime: number) {
return endTime - startTime;
}
/**
* create a new timer instance.
*/
static create() {
return new Timer();
}
}