import { ErrorCallback , ContextType } from '../shared/types' ; import { Node , NodeInputs , NodeOutput } from './node' ; export type ExprNoArgFunc = ( error : ErrorCallback , context : ContextType ) => any ; export type ExprWithArgFunc = ( ... args : any [ ] ) => any ; export type ExprFunc = ExprNoArgFunc | ExprWithArgFunc ; /**
 *
 * Represents [expression](https://connective.dev/docs/expr) agents.
 *
 */ export class Expr extends Node { /**
     *
     * The expression function
     *
     */ readonly func : any ; constructor ( func ) ; constructor ( inputs , func ) ; param inputsOrFunc param func constructor ( inputsOrFunc ? , func ? ) { super ( { inputs : ( typeof inputsOrFunc === 'function' ) ? [ ] : inputsOrFunc , required : ( typeof inputsOrFunc === 'function' ) ? [ ] : inputsOrFunc , outputs : [ 'result' ] } ) ; this . func = func ? func : inputsOrFunc ; } protected run ( inputs , output , error , context ) { let _ilist = this . signature . inputs ? this . signature . inputs . map ( i => inputs [ i ] ) : [ ] ; try { let val = this . func . apply ( undefined , _ilist . concat ( context ) ) ; if ( typeof val === 'function' ) val . apply ( undefined , [ ( out ) => output ( 'result' , out ) , error ] ) ; else output ( 'result' , val ) ; } catch ( err ) { error ( err ) ; } } /**
     *
     * Shortcut for `.out('result')`. The result of the evaluation of the
     * expression will be emitted via this output.
     *
     */ public get result ( ) { return this . out ( 'result' ) ; } } export function expr ( func ) ; export function expr ( inputs , func ) ; param inputsOrFunc param func export function expr ( inputsOrFunc ? , func ? ) { if ( func ) return new Expr ( inputsOrFunc as string [ ] , func ) ; else { let func = inputsOrFunc as ExprFunc ; return new Expr ( Array . apply ( 0 , { length : func . length } ) . map ( ( _ , i ) => i . toString ( ) ) , func ) ; } } export default expr ;