import { supportsEventListenerPassiveOption , supportsEventListenerOnceOption } from './feature-detect' ; export interface EventListenerArgs { capture ? : boolean ; } export interface AddEventListenerOptions extends EventListenerArgs { passive ? : boolean ; once ? : boolean ; } type AddEventListener = ( type : string , listener : ( event : Event ) => void , options ? : AddEventListenerOptions ) => void ; export function addEventListener ( target , eventName , handler , options = { } ) { const wrappedHandler = ! supportsEventListenerOnceOption ( ) && options . once ? once ( target , eventName , handler ) : handler ; if ( supportsEventListenerPassiveOption ( ) || supportsEventListenerOnceOption ( ) ) { const addListener = ( target . addEventListener as AddEventListener ) ; return addListener . call ( target , eventName , handler , options ) ; } return target . addEventListener ( eventName , wrappedHandler , options . capture ) ; } export function removeEventListener ( target , eventName , handler , capture ? ) { return target . removeEventListener ( eventName , handler , capture ) ; } function once ( target , eventName , handler ) { return function selfRemovingHandler ( event ) { handler . call ( event . currentTarget , event ) ; target . removeEventListener ( eventName , handler ) ; } ; }