license import { WarningCarryingException } from 'polymer-analyzer' ; import { Disposable , IConnection } from 'vscode-languageserver' ; export class EventStream < T > { static create < T > ( ) { const stream = new EventStream < T > ( ) ; const fire = stream . fire . bind ( stream ) ; return { fire , stream } ; } private handlers = new Set < ( value : T ) => void > ( ) ; private fire ( value ) { for ( const handler of this . handlers ) { handler ( value ) ; } } listen ( handler ) { this . handlers . add ( handler ) ; return { dispose : ( ) => { this . handlers . delete ( handler ) ; } } ; } /**
     * Return a fresh Promise that resolves with the next value.
     *
     * Mostly useful in tests.
     */ get next ( ) : Promise < T > { return new Promise ( ( resolve ) => { const disposable = this . listen ( ( value ) => { disposable . dispose ( ) ; resolve ( value ) ; } ) ; } ) ; } } export class AutoDisposable implements Disposable { dispose ( ) { for ( const disposable of this . disposables ) { disposable . dispose ( ) ; } } protected readonly disposables : Disposable [ ] = [ ] ; } /**
 * Declares a method that ensures that we don't throw uncaught errors, as
 * that can result in intrusive stack trace popups for users.
 */ export abstract class Handler extends AutoDisposable { protected abstract connection : IConnection ; /**
     * If the given promise rejects, unobtrusively log the error and return the
     * fallback value instead.
     */ protected async handleErrors < Result , Fallback > ( promise , fallbackValue ) { try { return await promise ; } catch ( err ) { if ( ! ( err instanceof WarningCarryingException ) ) { this . connection . console . warn ( err . stack || err . message || err ) ; } return fallbackValue ; } } } export interface Change < T > { newer : T ; older : T ; }