import { AsyncIterableX } from './asynciterablex' ; import { AsyncSink } from './asyncsink' ; import { memoize } from './operators/memoize' ; template TSource param { ( handler : ( ... args : any [ ] ) => void ) => void } addHandler param { ( handler : ( ... args : any [ ] ) => void ) => void } removeHandler returns { AsyncIterableX < TSource > } export function fromEventPattern < TSource > ( addHandler , removeHandler ) { const sink = new AsyncSink < TSource > ( ) ; const handler = ( e ) => sink . write ( e ) ; addHandler ( handler ) ; const yielder = async function * ( ) { for ( let next ; ! ( next = await sink . next ( ) ) . done ; ) { yield next . value ; } removeHandler ( handler ) ; sink . end ( ) ; } ; return memoize < TSource > ( ) ( yielder ( ) ) ; }