import { PinLike } from '../pin/pin-like' ; import { Agent } from './agent' ; import { Signature } from './signature' ; import { Composition } from './composition' ; type _ChildType = PinLike | Agent ; type _PinDict = { [ name ] : PinLike ; } ; export type TrackFunc = ( ... children : _ChildType [ ] ) => void ; export type CompositionFactory = ( track : TrackFunc ) => [ _PinDict | PinLike [ ] , _PinDict | PinLike [ ] ] ; class InlineComposition extends Composition { readonly inpins : _PinDict | PinLike [ ] ; readonly outpins : _PinDict | PinLike [ ] ; constructor ( readonly factory , signature ) { super ( signature ) ; [ this . inpins , this . outpins ] = this . factory ( ( ... children ) => children . forEach ( child => this . add ( child ) ) ) ; } init ( ) { } wire ( ) { } build ( ) { } createInput ( label ) { return ( this . inpins as any ) [ label ] ; } createOutput ( label ) { return ( this . outpins as any ) [ label ] ; } createEntries ( ) { return Object . values ( this . inpins ) ; } createExits ( ) { return Object . values ( this . outpins ) ; } } export function composition ( factory ) ; export function composition ( signature , factory ) ; param factoryOrSignature param factory export function composition ( factoryOrSignature , factory ? ) { let signature ; if ( ! factory ) { factory = factoryOrSignature as CompositionFactory ; let tracked = < _ChildType [ ] > [ ] ; let s = factory ( ( ... children ) => { tracked = tracked . concat ( children ) ; } ) ; signature = { inputs : Object . keys ( s [ 0 ] ) , outputs : Object . keys ( s [ 1 ] ) } ; tracked . forEach ( thing => thing . clear ( ) ) ; } else { signature = factoryOrSignature as Signature ; } let func = ( ) => new InlineComposition ( factory as CompositionFactory , signature ) ; ( func as any ) . signature = signature ; return func ; } export default composition ;