import { AsyncIterableX } from './asynciterablex' ; import { fromEventPattern } from './fromeventpattern' ; export interface NodeEventEmitter { addListener ( event , listener ) : this ; removeListener ( event , listener ) : this ; } export type EventListenerOptions = { capture ? : boolean ; passive ? : boolean ; once ? : boolean ; } | boolean ; export type EventedTarget = EventTarget | NodeEventEmitter ; function isNodeEventEmitter ( obj ) { return ! ! obj && typeof obj . addListener === 'function' && typeof obj . removeListener === 'function' ; } function isEventTarget ( obj ) { return ( ! ! obj && typeof obj . addEventListener === 'function' && typeof obj . removeEventListener === 'function' ) ; } template TSource param { EventedTarget } obj param { string } type param { EventListenerOptions } options returns { AsyncIterableX < TSource > } export function fromEvent < TSource > ( obj , type , options ? ) { if ( isEventTarget ( obj ) ) { const target = < EventTarget > obj ; return fromEventPattern < TSource > ( ( h ) => target . addEventListener ( type , < EventListener > h , options ) , ( h ) => target . removeEventListener ( type , < EventListener > h , options ) ) ; } else if ( isNodeEventEmitter ( obj ) ) { const target = < NodeEventEmitter > obj ; return fromEventPattern < TSource > ( ( h ) => target . addListener ( type , h ) , ( h ) => target . removeListener ( type , h ) ) ; } else { throw new TypeError ( 'Unsupported event target' ) ; } }