import { Service } from './service' ; import { Call } from './call' ; import { IGBServerMessage , IGBCreateCall , IGBCreateCallResult , IGBSendCall , IGBEndCall , } from '../proto' ; export class CallStore { private calls : { [ id ] : Call ; } = { } ; public constructor ( private service , private clientId , private send ) { } public initCall ( msg ) { let result = { call_id : msg . call_id , service_id : msg . service_id , result : 0 , } ; if ( typeof msg . call_id !== 'number' || this . calls [ msg . call_id ] ) { result . result = 1 ; result . error_details = 'ID is not set or is already in use.' ; } else { try { let callId = msg . call_id ; let call = new Call ( this . service , msg . call_id , msg . service_id , msg . info , this . send ) ; call . initCall ( ) ; this . calls [ msg . call_id ] = call ; call . disposed . subscribe ( ( ) => { this . releaseLocalCall ( callId ) ; } ) ; } catch ( e ) { result . result = 2 ; result . error_details = e . toString ( ) ; } } this . send ( { call_create : result , } ) ; } public handleCallEnd ( msg ) { let call = this . calls [ msg . call_id ] ; if ( ! call ) { return ; } call . dispose ( ) ; } public handleCallWrite ( msg ) { let call = this . calls [ msg . call_id ] ; if ( ! call ) { return ; } if ( msg . is_end ) { call . sendEnd ( ) ; } else { call . write ( msg . bin_data ) ; } } public releaseLocalCall ( id ) { delete this . calls [ id ] ; } public dispose ( ) { for ( let callId in this . calls ) { if ( ! this . calls . hasOwnProperty ( callId ) ) { continue ; } this . calls [ callId ] . dispose ( ) ; } this . service . clientRelease ( this . clientId ) ; this . calls = { } ; } }