import { Long } from 'bson' ; import { SessionConfig } from '../config' ; import { ConnectionSession , PacketResData , SessionFactory } from '../network/request-session' ; import { DefaultReq , AsyncCommandResult , DefaultRes } from '../request' ; import { BiStream } from '../stream' ; /**
 * Hook incoming datas
 */ export interface SessionHook { /**
     * Hook incoming data
     */ onData : ( method : string , data : DefaultRes , push : boolean ) => void ; /**
     * Hook command requests
     */ onRequest : ( method : string , data : DefaultReq ) => void ; onClose ( ) : ( ) => void ; } /**
 * Hook created loco session
 */ export class HookedSessionFactory implements SessionFactory { constructor ( private _factory , private _hook = { } ) { } async connect ( userId , config ) { const sessionRes = await this . _factory . connect ( userId , config ) ; if ( ! sessionRes . success ) return sessionRes ; return { status : sessionRes . status , success : true , result : new InspectSession ( sessionRes . result , this . _hook ) } ; } } export class InspectSession implements ConnectionSession { constructor ( private _session , private _hook = { } ) { } get stream ( ) : BiStream { return this . _session . stream ; } request < T = DefaultRes > ( method , data ) { if ( this . _hook . onRequest ) this . _hook . onRequest ( method , data ) ; return this . _session . request ( method , data ) ; } listen ( ) { const iterator = this . _session . listen ( ) ; return { [ Symbol . asyncIterator ] ( ) { return this ; } , next : async ( ) : Promise < IteratorResult < PacketResData > > => { const next = await iterator . next ( ) ; if ( ! next . done && this . _hook . onData ) { this . _hook . onData ( next . value . method , next . value . data , next . value . push ) ; } return next ; } } ; } }