import { Long } from 'bson' ; import { EventContext , TypedEmitter } from '../../event' ; import { OpenLinkService , InformedOpenLink } from '../../openlink' ; import { InformedOpenLinkStruct , structToOpenLink , structToOpenLinkInfo } from '../../packet/struct' ; import { DefaultRes } from '../../request' ; import { OpenLinkEvent } from '../event' ; import { Managed } from '../managed' ; export interface OpenLinkUpdater { addClientLink ( link ) : void ; deleteClientLink ( linkId ) : boolean ; } export class TalkOpenLinkHandler implements Managed < OpenLinkEvent > { constructor ( private _service , private _emitter , private _updater ) { } private _callEvent < U extends keyof OpenLinkEvent > ( parentCtx , event , ... args ) { this . _emitter . emit ( event , ... args ) ; parentCtx . emit ( event , ... args ) ; } async pushReceived ( method , data , parentCtx ) { switch ( method ) { case 'SYNCLINKCR' : { const linkStruct = data [ 'ol' ] as InformedOpenLinkStruct ; const informed = { openLink : structToOpenLink ( linkStruct ) , info : structToOpenLinkInfo ( linkStruct ) , } ; this . _updater . addClientLink ( informed ) ; this . _callEvent ( parentCtx , 'link_created' , informed ) ; break ; } case 'SYNCLINKDL' : { const linkId = data [ 'li' ] as Long ; const clientLink = this . _service . getClientLink ( linkId ) ; if ( ! clientLink ) return ; this . _updater . deleteClientLink ( linkId ) ; this . _callEvent ( parentCtx , 'link_deleted' , clientLink ) ; break ; } default : break ; } } }