packageDocumentation module public export class BeDuration { private readonly _milliseconds : number ; private constructor ( milliseconds = 0 ) { this . _milliseconds = milliseconds ; } /** The duration in milliseconds */ public get milliseconds ( ) { return this . _milliseconds ; } public get seconds ( ) { return this . _milliseconds / 1000 ; } param seconds public static fromSeconds ( seconds ) { return new BeDuration ( seconds * 1000 ) ; } param milliseconds public static fromMilliseconds ( milliseconds ) { 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 ) { return new BeDuration ( this . _milliseconds - other . _milliseconds ) ; } /** Add a BeDuration to this BeDuration, returning a new BeDuration */ public plus ( other ) { return new BeDuration ( this . _milliseconds + other . _milliseconds ) ; } param ms return public static async wait ( ms ) { return new Promise < void > ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } param ms param promise return public static async race < T > ( ms , promise ) { let timeout ; const waitPromise = new Promise < void > ( ( resolve ) => { timeout = setTimeout ( resolve , ms ) ; } ) ; return Promise . race ( [ waitPromise , promise ] ) . finally ( ( ) => { if ( timeout ) clearTimeout ( timeout ) ; } ) ; } return public async wait ( ) { return new Promise < void > ( ( resolve ) => setTimeout ( resolve , this . _milliseconds ) ) ; } param fn param scope param args return public async executeAfter < T > ( fn , scope ? , ... args ) { return new Promise < T > ( ( resolve ) => setTimeout ( ( ) => resolve ( fn . apply ( scope , args ) ) , this . _milliseconds ) ) ; } } 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 ) { this . _milliseconds = milliseconds ; } /** Create a BeTimePoint from Date.now() */ public static now ( ) { return new BeTimePoint ( Date . now ( ) ) ; } param val public static fromNow ( val ) { return new BeTimePoint ( Date . now ( ) + val . milliseconds ) ; } param val public static beforeNow ( val ) { 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 ; } param other public before ( other ) { return this . _milliseconds < other . _milliseconds ; } param other public after ( other ) { return this . _milliseconds > other . _milliseconds ; } param duration public minus ( duration ) { return new BeTimePoint ( this . _milliseconds - duration . milliseconds ) ; } param duration public plus ( duration ) { return new BeTimePoint ( this . _milliseconds + duration . milliseconds ) ; } } 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. */ 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 ; } param description param startImmediately constructor ( public description ? , startImmediately = false ) { if ( startImmediately ) this . start ( ) ; } /** Start the stopwatch. Any future time measurements will be based on this new value. */ public start ( ) { this . reset ( ) ; this . _start = BeTimePoint . now ( ) ; } /** Stop the stopwatch so that the duration can be viewed later. */ public stop ( ) { this . _stop = BeTimePoint . now ( ) ; return this . elapsed ; } /** Clear the StopWatch */ public reset ( ) { this . _start = this . _stop = undefined ; } }