// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; export function registerUnhandledRejectionHandler(): void { process.on("unhandledRejection", (error) => { if (error && error.stack) { console.error(error.stack); } else { console.error(error); } }); } export function delay(durationInMilliseconds: number): Promise { const source = new PromiseSource(); setTimeout(() => source.resolve(), durationInMilliseconds); return source.promise; } export class PromiseSource implements Promise { public promise: Promise; private resolver!: (value?: T | PromiseLike) => void; private rejecter!: (reason?: any) => void; constructor() { this.promise = new Promise((resolve, reject) => { this.resolver = resolve; this.rejecter = reject; }); } public resolve(value?: T | PromiseLike) { this.resolver(value); } public reject(reason?: any) { this.rejecter(reason); } // Look like a promise so we can be awaited directly; public then(onfulfilled?: (value: T) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise { return this.promise.then(onfulfilled, onrejected); } public catch(onrejected?: (reason: any) => TResult | PromiseLike): Promise { return this.promise.catch(onrejected); } } export class SyncPoint { private atSyncPoint: PromiseSource; private continueFromSyncPoint: PromiseSource; constructor() { this.atSyncPoint = new PromiseSource(); this.continueFromSyncPoint = new PromiseSource(); } public waitForSyncPoint(): Promise { return this.atSyncPoint.promise; } public continue() { this.continueFromSyncPoint.resolve(); } public waitToContinue(): Promise { this.atSyncPoint.resolve(); return this.continueFromSyncPoint.promise; } }