import { telemetry } from '../Telemetry'; /** */
/** @internal NOTE: Internal APIs. Subject to change. Use of these APIs in production applications is not supported. */
export function measure(op, a) {
    const start = performance.now();
    const ret = a();
    const end = performance.now();
    telemetry.trackTrace({ message: `${op} ${(end - start).toFixed(4)} ms` });
    return ret;
}
export function measureAsync(op, a) {
    const start = performance.now();
    const ret = a();
    ret.finally(() => {
        const end = performance.now();
        telemetry.trackTrace({ message: `${op} ${(end - start).toFixed(4)} ms` });
    });
    return ret;
}
export class StopWatch {
    _totalElapsed = 0;
    _start = 0;
    constructor(start = false) {
        if (start)
            this.resetAndStart();
    }
    get isRunning() {
        return this._start > 0;
    }
    resetAndStart() {
        this._totalElapsed = 0;
        this._start = performance.now();
    }
    resetAndStop() {
        this._start = 0;
        this._totalElapsed = 0;
    }
    resume() {
        if (!this.isRunning) {
            this._start = performance.now();
        }
    }
    get elapsed() {
        return this.isRunning ? performance.now() - this._start : 0;
    }
    get totalElapsed() {
        return this._totalElapsed + this.elapsed;
    }
    stop() {
        if (!this.isRunning) {
            return this._totalElapsed;
        }
        const e = performance.now() - this._start;
        this._totalElapsed += e;
        this._start = 0;
        return e;
    }
}
const resolvedPromise = Promise.resolve();
export function timeoutAsync(ms) {
    if (!ms) {
        // Becomes a microtask. so code afterwards will execute first.
        return resolvedPromise;
    }
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(undefined);
        }, ms);
    });
}
export function yieldThreadFor(ms) {
    return timeoutAsync(ms);
}
//# sourceMappingURL=stopwatch.js.map