import { IEventEmitter } from "../../interface" ; export class BrowserEventEmitter implements IEventEmitter { private target : HTMLElement ; constructor ( target ? ) { this . target = target ?? document . createElement ( "div" ) ; } dispatch < D > ( eventName , data ) { let customEvent ; if ( typeof window [ "CustomEvent" ] === "function" ) { customEvent = new CustomEvent ( eventName , { detail : data } ) ; } else { customEvent = document . createEvent ( 'CustomEvent' ) ; customEvent . initCustomEvent ( eventName , false , false , data ) ; } this . target . dispatchEvent ( customEvent ) ; } addListener ( eventName , callback ) { const wrappedFunction = ( event ) => callback ( event . detail ) ; this . target . addEventListener ( eventName , wrappedFunction ) ; return wrappedFunction ; } removeListener ( eventName , callback ) { this . target . removeEventListener ( eventName , callback ) ; } }