/**
 * Promise-based object stream with seamless support for back-pressure and error
 * handling, written in Typescript.
 *
 * Copyright (C) 2015 Martin Poelstra
 * License: MIT
 */ /**
 * Do nothing.
 */ export function noop ( ... args ) { } /**
 * Swallow any error that may result from this promise.
 * Prevents PossiblyUnhandledExceptionErrors.
 */ export function swallowErrors ( promise ) { promise . then ( undefined , noop ) ; } /**
 * Combination of a promise and its resolve/reject functions.
 * Created using defer().
 *
 * It is generally better (and slightly faster) to use the Promise
 * constructor to create a promise, as that will also catch any exception
 * thrown while running the resolver.
 *
 * A Deferred can be useful in some scenarios though, e.g. when working with
 * timers, protocol request/response pairs, etc.
 */ export interface Deferred < T > { /**
     * Initially unresolved promise, resolved by the resolve or reject
     * function on this object.
     */ promise : Promise < T > ; /**
     * Reject corresponding promise.
     * The first call to either resolve or reject resolves the promise, any
     * other calls are ignored.
     * This function is a free function (i.e. not a 'method' on this object).
     */ reject : ( reason : Error ) => void ; /**
     * Resolve corresponding promise.
     * The first call to either resolve or reject resolves the promise, any
     * other calls are ignored.
     * This function is a free function (i.e. not a 'method' on this object).
     * Note: resolving with a rejected PromiseLike leads to a rejected promise.
     */ resolve : ( value : T | PromiseLike < T > ) => void ; } /**
 * Convenience version of Deferred that allows calling resolve() without an
 * argument.
 *
 * Deferred is a combination of a promise and its resolve/reject functions.
 * Created using defer().
 *
 * It is generally better (and slightly faster) to use the Promise
 * constructor to create a promise, as that will also catch any exception
 * thrown while running the resolver.
 *
 * A Deferred can be useful in some scenarios though, e.g. when working with
 * timers, protocol request/response pairs, etc.
 */ export interface VoidDeferred extends Deferred < void > { /**
     * Resolve corresponding promise.
     * The first call to either resolve or reject resolves the promise, any
     * other calls are ignored.
     * This function is a free function (i.e. not a 'method' on this object).
     * Note: resolving with a rejected PromiseLike leads to a rejected promise.
     */ resolve : ( value ? : void | PromiseLike < void > ) => void ; } /**
 * Returns a deferred promise, i.e. one that you can still resolve or reject using the returned functions
 */ export function defer ( ) ; export function defer < T > ( ) ; export function defer < T > ( ) { let resolve ; let reject ; const promise = new Promise ( ( a , b ) : void => { resolve = a ; reject = b ; } ) ; return { promise , reject : reject ! , resolve : resolve ! } ; } /**
 * Used to track the status of a promise
 */ export interface TrackedPromise < T > { /**
     * Promise not fulfilled/rejected yet
     */ isPending : boolean ; /**
     * Promise is rejected; error is in 'reason' member
     */ isRejected : boolean ; /**
     * Promise is fulfilled, value is in 'value' member
     */ isFulfilled : boolean ; /**
     * The original promise
     */ promise : PromiseLike < T > ; /**
     * The error for a rejection
     */ reason ? : Error ; /**
     * The value when fulfilled
     */ value ? : T ; } export interface TrackedVoidPromise extends TrackedPromise < void > { value ? : void ; } param p export function track ( p ) ; export function track < T > ( p ) ; export function track < T > ( p ) { const tracked = { isFulfilled : false , isPending : true , isRejected : false , promise : undefined as any , } ; const trackedPromise = p . then ( ( value ) : T => { tracked . isPending = false ; tracked . isFulfilled = true ; tracked . value = value ; return value ; } , ( error ) : T => { tracked . isPending = false ; tracked . isRejected = true ; tracked . reason = error ; throw error ; } ) ; tracked . promise = trackedPromise ; return tracked ; } /**
 * Resolves a promise after a number of milliseconds
 */ export function delay ( duration ) ; export function delay < T > ( duration , t ) ; export function delay ( duration , t ? ) { return new Promise ( ( resolve ) : void => { setTimeout ( ( ) => resolve ( t ) , duration ) ; } ) ; }