import { IInternalEvent , IJsonRpcResponseSuccess , IJsonRpcResponseError , IJsonRpcRequest , IEventEmitter , } from "@walletconnect/legacy-types" ; import { isJsonRpcRequest , isJsonRpcResponseSuccess , isJsonRpcResponseError , isInternalEvent , isReservedEvent , } from "@walletconnect/legacy-utils" ; class EventManager { private _eventEmitters : IEventEmitter [ ] ; constructor ( ) { this . _eventEmitters = [ ] ; } public subscribe ( eventEmitter ) { this . _eventEmitters . push ( eventEmitter ) ; } public unsubscribe ( event ) { this . _eventEmitters = this . _eventEmitters . filter ( x => x . event !== event ) ; } public trigger ( payload ) { let eventEmitters = [ ] ; let event ; if ( isJsonRpcRequest ( payload ) ) { event = payload . method ; } else if ( isJsonRpcResponseSuccess ( payload ) || isJsonRpcResponseError ( payload ) ) { event = `response:${ payload . id }` ; } else if ( isInternalEvent ( payload ) ) { event = payload . event ; } else { event = "" ; } if ( event ) { eventEmitters = this . _eventEmitters . filter ( ( eventEmitter ) => eventEmitter . event === event ) ; } if ( ( ! eventEmitters || ! eventEmitters . length ) && ! isReservedEvent ( event ) && ! isInternalEvent ( event ) ) { eventEmitters = this . _eventEmitters . filter ( ( eventEmitter ) => eventEmitter . event === "call_request" ) ; } eventEmitters . forEach ( ( eventEmitter ) => { if ( isJsonRpcResponseError ( payload ) ) { const error = new Error ( payload . error . message ) ; eventEmitter . callback ( error , null ) ; } else { eventEmitter . callback ( null , payload ) ; } } ) ; } } export default EventManager ;