/
Time.ts
170 lines (149 loc) · 7.93 KB
/
Time.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utils
*/
/** A duration of time. Can be either positive (towards future) or negative (in the past).
* BeDurations are immutable.
* @public
*/
export class BeDuration {
private readonly _milliseconds: number;
private constructor(milliseconds: number = 0) { this._milliseconds = milliseconds; }
/** The duration in milliseconds */
public get milliseconds() { return this._milliseconds; }
public get seconds() { return this._milliseconds / 1000; }
/** Create a BeDuration from seconds.
* @param seconds the number of seconds for this BeDuration
*/
public static fromSeconds(seconds: number) { return new BeDuration(seconds * 1000); }
/** Create a BeDuration from milliseconds.
* @param milliseconds the number of milliseconds for this BeDuration
*/
public static fromMilliseconds(milliseconds: number) { return new BeDuration(milliseconds); }
/** Determine whether this BeDuration is 0 seconds */
public get isZero() { return this._milliseconds === 0; }
/** Determine whether this BeDuration is towards the future */
public get isTowardsFuture(): boolean { return this._milliseconds > 0; }
/** Determine whether this BeDuration is towards the past */
public get isTowardsPast(): boolean { return this._milliseconds < 0; }
/** Subtract a BeDuration from this BeDuration, returning a new BeDuration. */
public minus(other: BeDuration): BeDuration { return new BeDuration(this._milliseconds - other._milliseconds); }
/** Add a BeDuration to this BeDuration, returning a new BeDuration */
public plus(other: BeDuration): BeDuration { return new BeDuration(this._milliseconds + other._milliseconds); }
/** Utility function to just wait for the specified time
* @param ms Duration in milliseconds to wait
* @return Promise that resolves after the specified wait period
*/
public static async wait(ms: number): Promise<void> {
return new Promise<void>((resolve: any) => setTimeout(resolve, ms));
}
/** Utility function to wait for either the specified time or a promise, whichever resolves first
* @param ms Maximum duration in milliseconds to wait
* @param promise A pending promise to wait for
* @return Promise that resolves after the specified wait period or the provided promise resolves, whichever comes first
*/
public static async race<T>(ms: number, promise: PromiseLike<T>): Promise<T | void> {
let timeout: any;
const waitPromise = new Promise<void>((resolve) => {
timeout = setTimeout(resolve, ms);
});
return Promise.race([waitPromise, promise]).finally(() => {
if (timeout)
clearTimeout(timeout);
});
}
/** Utility function to just wait for the specified time
* @return Promise that resolves after the specified wait period
*/
public async wait(): Promise<void> {
return new Promise<void>((resolve: any) => setTimeout(resolve, this._milliseconds));
}
/** Execute a function after delaying by this duration.
* @param fn the function to execute after the delay
* @param scope An optional object scope to serve as the 'this' pointer when `fn` is invoked.
* @param args optional arguments to `fn`
* @return Promise resolved by `fn`
*/
public async executeAfter<T>(fn: (...args: any[]) => T, scope?: any, ...args: any[]): Promise<T> {
return new Promise<T>((resolve: any) => setTimeout(() => resolve(fn.apply(scope, args)), this._milliseconds));
}
}
/** A specific point in time relative to the current time.
* BeTimePoints are used for timing operations. They are created from a BeDuration relative to the "now".
* BeTimePoints are immutable.
* @public
*/
export class BeTimePoint {
private readonly _milliseconds: number;
/** the time in milliseconds, of this BeTimePoint (relative to January 1, 1970 00:00:00 UTC.) */
public get milliseconds() { return this._milliseconds; }
private constructor(milliseconds: number) { this._milliseconds = milliseconds; }
/** Create a BeTimePoint from Date.now() */
public static now() { return new BeTimePoint(Date.now()); }
/** Create a BeTimePoint at a specified duration in the future from now
* @param val the duration from now
*/
public static fromNow(val: BeDuration) { return new BeTimePoint(Date.now() + val.milliseconds); }
/** Create a BeTimePoint at a specified duration in the past before now
* @param val the duration before now
*/
public static beforeNow(val: BeDuration) { return new BeTimePoint(Date.now() - val.milliseconds); }
/** Determine whether this BeTimePoint is a time in the future from the time this method is called (it calls now()!) */
public get isInFuture(): boolean { return Date.now() < this._milliseconds; }
/** Determine whether this BeTimePoint is a time that has already passed before the time this method is called (it calls now()!) */
public get isInPast(): boolean { return Date.now() > this._milliseconds; }
/** Determine whether this BeTimePoint happens before another one.
* @param other the other BeTimePoint.
*/
public before(other: BeTimePoint): boolean { return this._milliseconds < other._milliseconds; }
/** Determine whether this BeTimePoint happens after another one.
* @param other the other BeTimePoint.
*/
public after(other: BeTimePoint): boolean { return this._milliseconds > other._milliseconds; }
/** Subtract a BeDuration from this BeTimePoint, returning a new BeTimePoint. This moves this BeTimePoint backwards in time if BeDuration.isTowardsFuture() === true
* @param duration the duration to subtract.
*/
public minus(duration: BeDuration): BeTimePoint { return new BeTimePoint(this._milliseconds - duration.milliseconds); }
/** Subtract a BeDuration from this BeTimePoint, returning a new BeTimePoint. This moves this BeTimePoint backwards in time if BeDuration.isTowardsFuture() === true
* @param duration the duration to subtract.
*/
public plus(duration: BeDuration) { return new BeTimePoint(this._milliseconds + duration.milliseconds); }
}
/** A StopWatch for timing operations.
* @public
*/
export class StopWatch {
private _start?: BeTimePoint;
private _stop?: BeTimePoint;
/** Get the elapsed time since start() on a running timer. */
public get current(): BeDuration { return BeDuration.fromMilliseconds(BeTimePoint.now().milliseconds - (!!this._start ? this._start.milliseconds : 0)); }
/** Get the elapsed time, in seconds, since start() on a running timer. */
public get currentSeconds(): number { return this.current.seconds; }
/** Get the elapsed time between start() and stop() on this timer in milliseconds. */
public get elapsed(): BeDuration { return BeDuration.fromMilliseconds((!!this._stop ? this._stop.milliseconds : BeTimePoint.now().milliseconds) - (!!this._start ? this._start.milliseconds : 0)); }
/** Get the elapsed time, in seconds, between start() and stop() on this timer. */
public get elapsedSeconds(): number { return this.elapsed.seconds; }
/** ctor for StopWatch
* @param description optional string stored with the StopWatch
* @param startImmediately if true, StopWatch is started when created. Otherwise, call start() explicitly.
*/
constructor(public description?: string, startImmediately = false) {
if (startImmediately)
this.start();
}
/** Start the stopwatch. Any future time measurements will be based on this new value. */
public start(): void {
this.reset();
this._start = BeTimePoint.now();
}
/** Stop the stopwatch so that the duration can be viewed later. */
public stop(): BeDuration {
this._stop = BeTimePoint.now();
return this.elapsed;
}
/** Clear the StopWatch */
public reset(): void { this._start = this._stop = undefined; }
}