import { withLabels } from '@effection/core' ; import { createStream , Stream } from '@effection/stream' ; import { EventSource , addListener , removeListener } from './event-source' ; param source param name param streamName typeParam export function on < T = unknown > ( source , name , streamName = `on('${ name }')` ) { return createStream ( ( publish ) => withLabels ( function * ( ) { addListener ( source , name , publish ) ; try { yield ; } finally { removeListener ( source , name , publish ) ; } } , { name : 'listen' , eventName : name , source : source . toString ( ) } ) , streamName ) ; } /**
 * Exactly like  on except each value produced by the
 * stream is an array of all the arguments passed when the event was
 * dispatched.
 *
 * This should rarely be needed, and never with `EventTarget`
 * which always invoke their event listeners with a single argument.
 *
 * ### Example
 *
 * stream an odd-ball event emitter that passes multiple arguments to
 * its handers. Most event emitters do not act this way:
 *
 * ```javascript
 * let emitter = new EventEmitter();
 *
 * yield spawn(onEmit(emitter, 'multiplication').forEach(([left, right]) => {
 *   console.log(`${left} times ${right} = ${left * right}!`);
 * }));
 *
 * yield sleep(10);
 *
 * emitter.emit('multiplication', 7, 6);
 * //=> 7 times 6 = 42!
 * ```
 *
 *  param source param name param streamName typeParam export function onEmit < TArgs extends Array < unknown > = unknown [ ] > ( source , name , streamName = `onEmit('${ name }')` ) { return createStream ( ( publish ) => withLabels ( function * ( ) { let listener = ( ... args ) => publish ( args ) ; addListener ( source , name , listener ) ; try { yield ; } finally { removeListener ( source , name , listener ) ; } } , { name : 'listen' , eventName : name , source : source . toString ( ) } ) , streamName ) ; }