export interface CancelAction { ( message ? ) : void ; } export interface CancelExecutor { ( cancel ) : void ; } export interface CancelTokenSource { token : CancelToken ; cancel : CancelAction ; } export interface CancelTokenConstructor { new ( executor ) : CancelToken ; source ( ) : CancelTokenSource ; } export class Cancel { public message ? : string ; public constructor ( message ? ) { this . message = message ; } public toString ( ) { const message = this . message ? `: ${ this . message }` : '' ; return `Cancel${ message }` ; } } export function isCancel ( value ) { return value instanceof Cancel ; } export class CancelToken { private reason ? : Cancel ; public listener : Promise < Cancel > ; public constructor ( executor ) { let action ! ; this . listener = new Promise < Cancel > ( ( resolve ) => { action = ( message ) => { if ( this . reason ) { return ; } this . reason = new Cancel ( message ) ; resolve ( this . reason ) ; } ; } ) ; executor ( action ) ; } public static source ( ) { let cancel ! ; const token = new CancelToken ( ( action ) => { cancel = action ; } ) ; return { token , cancel , } ; } public throwIfRequested ( ) { if ( this . reason ) { throw this . reason ; } } } export function isCancelToken ( value ) { return value instanceof CancelToken ; }