import { ContextType } from './types' ; /**
 *
 * When the same key appears in multiple [emissions](https://connective.dev/docs/emission),
 * with conflicting values, when the emissions are merged, the corresponding values are
 * stored within an instance of `MergedEmissionContextVal`. You can read these values using
 * `.values` property:
 *
 * ```typescript
 * myFlow.observable.subscribe(emission => {
 *  if (emission.context.someKey instanceof MergedEmissionContextVal)
 *    emission.context.someKey.values.forEach(value => handle(value));
 *  else
 *    handle(emission.context.someKey);
 * });
 * ```
 *
 */ export class MergedEmissionContextVal { constructor ( readonly values ) { } } const _Unset = { } ; /**
 *
 * Represents emissions passing through reactive flows.
 * mainly has a `.value` property, which is the value that this emission is wrapping,
 * and `.context` property, which is the context of the emission.
 *
 * [read more here](https://connective.dev/docs/emission).
 *
 */ export class Emission { param value param context constructor ( readonly value = undefined , readonly context = { } ) { } param emissions param value public static from ( emissions , value = _Unset ) { return new Emission ( ( value === _Unset ) ? ( emissions . map ( emission => emission . value ) ) : ( value ) , emissions . reduce ( ( ctx , emission ) => { Object . entries ( emission . context ) . forEach ( ( [ key , value ] ) => { if ( key in ctx ) { if ( ctx [ key ] == value ) return ctx ; if ( ctx [ key ] instanceof MergedEmissionContextVal ) { if ( value instanceof MergedEmissionContextVal ) { ctx [ key ] = new MergedEmissionContextVal ( ctx [ key ] . values . concat ( value . values . filter ( v => ! ctx [ key ] . values . includes ( v ) ) ) ) ; } else { if ( ! ctx [ key ] . values . includes ( value ) ) ctx [ key ] . values . push ( value ) ; } } else { if ( value instanceof MergedEmissionContextVal ) { if ( value . values . includes ( ctx [ key ] ) ) ctx [ key ] = value ; else ctx [ key ] = new MergedEmissionContextVal ( [ ctx [ key ] ] . concat ( value . values ) ) ; } else ctx [ key ] = new MergedEmissionContextVal ( [ ctx [ key ] , value ] ) ; } } else ctx [ key ] = value ; } ) ; return ctx ; } , < ContextType > { } ) ) ; } param value public fork ( value ) { return new Emission ( value , this . context ) ; } } param value param context export function emission ( value ? , context ? ) { return new Emission ( value , context ) ; } export default emission ;