UNPKG

@itwin/core-bentley

Version:

Bentley JavaScript core components

158 lines 7.91 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * 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 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.StopWatch = exports.BeTimePoint = exports.BeDuration = void 0; /** A duration of time. Can be either positive (towards future) or negative (in the past). * BeDurations are immutable. * @public */ class BeDuration { _milliseconds; constructor(milliseconds = 0) { this._milliseconds = milliseconds; } /** The duration in milliseconds */ get milliseconds() { return this._milliseconds; } get seconds() { return this._milliseconds / 1000; } /** Create a BeDuration from seconds. * @param seconds the number of seconds for this BeDuration */ static fromSeconds(seconds) { return new BeDuration(seconds * 1000); } /** Create a BeDuration from milliseconds. * @param milliseconds the number of milliseconds for this BeDuration */ static fromMilliseconds(milliseconds) { return new BeDuration(milliseconds); } /** Determine whether this BeDuration is 0 seconds */ get isZero() { return this._milliseconds === 0; } /** Determine whether this BeDuration is towards the future */ get isTowardsFuture() { return this._milliseconds > 0; } /** Determine whether this BeDuration is towards the past */ get isTowardsPast() { return this._milliseconds < 0; } /** Subtract a BeDuration from this BeDuration, returning a new BeDuration. */ minus(other) { return new BeDuration(this._milliseconds - other._milliseconds); } /** Add a BeDuration to this BeDuration, returning a new BeDuration */ plus(other) { 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 */ static async wait(ms) { return new Promise((resolve) => 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 */ static async race(ms, promise) { let timeout; const waitPromise = new Promise((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 */ async wait() { return new Promise((resolve) => 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` */ async executeAfter(fn, scope, ...args) { return new Promise((resolve) => setTimeout(() => resolve(fn.apply(scope, args)), this._milliseconds)); } } exports.BeDuration = BeDuration; /** 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 */ class BeTimePoint { _milliseconds; /** the time in milliseconds, of this BeTimePoint (relative to January 1, 1970 00:00:00 UTC.) */ get milliseconds() { return this._milliseconds; } constructor(milliseconds) { this._milliseconds = milliseconds; } /** Create a BeTimePoint from Date.now() */ 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 */ static fromNow(val) { 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 */ 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()!) */ get isInFuture() { 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()!) */ get isInPast() { return Date.now() > this._milliseconds; } /** Determine whether this BeTimePoint happens before another one. * @param other the other BeTimePoint. */ before(other) { return this._milliseconds < other._milliseconds; } /** Determine whether this BeTimePoint happens after another one. * @param other the other BeTimePoint. */ after(other) { 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. */ minus(duration) { 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. */ plus(duration) { return new BeTimePoint(this._milliseconds + duration.milliseconds); } } exports.BeTimePoint = BeTimePoint; /** A StopWatch for timing operations. * @public */ class StopWatch { description; _start; _stop; /** Get the elapsed time since start() on a running timer. */ get current() { return BeDuration.fromMilliseconds(BeTimePoint.now().milliseconds - (!!this._start ? this._start.milliseconds : 0)); } /** Get the elapsed time, in seconds, since start() on a running timer. */ get currentSeconds() { return this.current.seconds; } /** Get the elapsed time between start() and stop() on this timer in milliseconds. */ get elapsed() { 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. */ get elapsedSeconds() { 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(description, startImmediately = false) { this.description = description; if (startImmediately) this.start(); } /** Start the stopwatch. Any future time measurements will be based on this new value. */ start() { this.reset(); this._start = BeTimePoint.now(); } /** Stop the stopwatch so that the duration can be viewed later. */ stop() { this._stop = BeTimePoint.now(); return this.elapsed; } /** Clear the StopWatch */ reset() { this._start = this._stop = undefined; } } exports.StopWatch = StopWatch; //# sourceMappingURL=Time.js.map