import { Buffer } from 'buffer' ; import SocketBase from './socketBase' ; import LoadBalancer from './utils/loadBalancer' ; import { IEndpoint , Msg } from './types' ; export default class Dealer extends SocketBase { private loadBalancer = new LoadBalancer ( ) ; private pending : Msg [ ] = [ ] ; protected attachEndpoint ( endpoint ) { this . loadBalancer . attach ( endpoint ) ; while ( true ) { const msg = this . pending . shift ( ) ; if ( ! msg ) break ; if ( ! this . loadBalancer . send ( msg ) ) break ; } } protected endpointTerminated ( endpoint ) { this . loadBalancer . terminated ( endpoint ) ; } protected xrecv ( endpoint , ... frames ) { this . emit ( 'message' , ... frames ) ; } protected xsend ( msg ) { if ( ! this . loadBalancer . send ( msg ) ) this . pending . push ( msg ) ; } }