import { Observable , PartialObserver , Subscription } from 'rxjs' ; import { isBindable , Bindable } from '../shared/bindable' ; import { Emission } from '../shared/emission' ; import { ResolveCallback , ErrorCallback , NotifyCallback } from '../shared/types' ; import { GroupObservableError } from './errors/group-subscription' ; import { PinLike } from './pin-like' ; import { PartialFlow } from './partial-flow' ; /**
 *
 * Represents [groups of pins](https://connective.dev/docs/group).
 *
 */ export class Group implements PinLike , Bindable { /**
     *
     * The array of all pins within the group.
     *
     */ readonly pins : PinLike [ ] ; constructor ( pins ) { this . pins = pins ; } warning get observable ( ) : Observable < Emission > { throw new GroupObservableError ( ) ; } param pins returns from ( ... pins ) { pins . forEach ( pin => this . pins . forEach ( p => p . from ( pin ) ) ) ; return traverseFrom ( ... pins ) ; } param pins returns to ( ... pins ) { pins . forEach ( pin => this . pins . forEach ( p => p . to ( pin ) ) ) ; return traverseTo ( ... pins ) ; } param pins returns serialFrom ( ... pins ) { ( < PartialFlow [ ] > pins . filter ( pin => pin instanceof PartialFlow ) ) . forEach ( flow => { for ( let i = 0 ; i < Math . min ( this . pins . length , flow . exits . pins . length ) ; i ++ ) this . pins [ i ] . from ( flow . exits . pins [ i ] ) ; } ) ; let purePins = pins . filter ( p => ! ( p instanceof PartialFlow ) ) ; for ( let i = 0 ; i < Math . min ( this . pins . length , purePins . length ) ; i ++ ) this . pins [ i ] . from ( purePins [ i ] ) ; return traverseFrom ( ... pins ) ; } param pins returns serialTo ( ... pins ) { ( < PartialFlow [ ] > pins . filter ( pin => pin instanceof PartialFlow ) ) . forEach ( flow => { for ( let i = 0 ; i < Math . min ( this . pins . length , flow . entries . pins . length ) ; i ++ ) this . pins [ i ] . to ( flow . entries . pins [ i ] ) ; } ) ; let purePins = pins . filter ( p => ! ( p instanceof PartialFlow ) ) ; for ( let i = 0 ; i < Math . min ( this . pins . length , purePins . length ) ; i ++ ) this . pins [ i ] . to ( purePins [ i ] ) ; return traverseTo ( ... pins ) ; } /**
     *
     * Calls `.clear()` on all pins of the group
     *
     */ clear ( ) { this . pins . forEach ( pin => pin . clear ( ) ) ; return this ; } /**
     *
     * Calls `.bind()` on all pins of the group
     *
     */ bind ( ) { this . pins . forEach ( pin => { if ( isBindable ( pin ) ) pin . bind ( ) ; } ) ; return this ; } subscribe ( observer ? ) ; subscribe ( next ? , error ? , complete ? ) ; returns subscribe ( _ ? , __ ? , ___ ? ) { return this . pins . reduce ( ( sub , pin ) => { sub . add ( pin . subscribe ( _ as any , __ , ___ ) ) ; return sub ; } , new Subscription ( ) ) ; } } param pins export function group ( ... pins ) { return new Group ( pins ) ; } param pins export function traverseTo ( ... pins ) { if ( pins . length == 1 ) { let pin = pins [ 0 ] ; if ( pin instanceof PartialFlow ) return pin . exits ; else return pin ; } else return group ( ... pins . reduce ( ( all , pin ) => { if ( pin instanceof PartialFlow ) return all . concat ( pin . exits . pins ) ; else return all . concat ( [ pin ] ) ; } , < PinLike [ ] > [ ] ) ) ; } param pins export function traverseFrom ( ... pins ) { if ( pins . length == 1 ) { let pin = pins [ 0 ] ; if ( pin instanceof PartialFlow ) return pin . entries ; else return pin ; } else return group ( ... pins . reduce ( ( all , pin ) => { if ( pin instanceof PartialFlow ) return all . concat ( pin . entries . pins ) ; else return all . concat ( [ pin ] ) ; } , < PinLike [ ] > [ ] ) ) ; } export default group ;