import { ExecutionResult } from 'graphql' ; import { EventEmitter } from 'events' ; import { Client } from '../../client' ; import { RequestParams } from '../../common' ; interface TSubscribe < T > { waitForNext : ( test ? : ( value : ExecutionResult < T , unknown > ) => void , expire ? : number ) => Promise < void > ; waitForError : ( test ? : ( error : unknown ) => void , expire ? : number ) => Promise < void > ; waitForComplete : ( test ? : ( ) => void , expire ? : number ) => Promise < void > ; dispose : ( ) => void ; } export function tsubscribe < T = unknown > ( client , payload ) { const emitter = new EventEmitter ( ) ; const results = [ ] ; let error , completed = false ; const dispose = client . subscribe < T > ( payload , { next : ( value ) => { console . log ( { value } ) ; results . push ( value ) ; emitter . emit ( 'next' ) ; } , error : ( err ) => { error = err ; emitter . emit ( 'err' ) ; emitter . removeAllListeners ( ) ; } , complete : ( ) => { completed = true ; emitter . emit ( 'complete' ) ; emitter . removeAllListeners ( ) ; } , } ) ; return { waitForNext : ( test , expire ) => { return new Promise ( ( resolve ) => { function done ( ) { const result = results . shift ( ) ! ; test ?. ( result ) ; resolve ( ) ; } if ( results . length > 0 ) return done ( ) ; emitter . once ( 'next' , done ) ; if ( expire ) setTimeout ( ( ) => { emitter . off ( 'next' , done ) ; resolve ( ) ; } , expire ) ; } ) ; } , waitForError : ( test , expire ) => { return new Promise ( ( resolve ) => { function done ( ) { test ?. ( error ) ; resolve ( ) ; } if ( error ) return done ( ) ; emitter . once ( 'err' , done ) ; if ( expire ) setTimeout ( ( ) => { emitter . off ( 'err' , done ) ; resolve ( ) ; } , expire ) ; } ) ; } , waitForComplete : ( test , expire ) => { return new Promise ( ( resolve ) => { function done ( ) { test ?. ( ) ; resolve ( ) ; } if ( completed ) return done ( ) ; emitter . once ( 'complete' , done ) ; if ( expire ) setTimeout ( ( ) => { emitter . off ( 'complete' , done ) ; resolve ( ) ; } , expire ) ; } ) ; } , dispose , } ; }