import { Buffer } from 'buffer' ; import SocketBase from './socketBase' ; import { IEndpoint , Msg } from './types' ; export default class Pair extends SocketBase { private endpoint ? : IEndpoint ; private pending : Msg [ ] = [ ] ; protected attachEndpoint ( endpoint ) { if ( this . endpoint ) { endpoint . close ( ) ; return ; } this . endpoint = endpoint ; while ( true ) { const msg = this . pending . shift ( ) ; if ( ! msg ) break ; if ( ! endpoint . send ( msg ) ) break ; } } protected endpointTerminated ( endpoint ) { if ( endpoint === this . endpoint ) this . endpoint = undefined ; } protected xrecv ( endpoint , ... frames ) { if ( endpoint === this . endpoint ) this . emit ( 'message' , ... frames ) ; } protected xsend ( msg ) { if ( this . endpoint ) this . endpoint . send ( msg ) ; else this . pending . push ( msg ) ; } }