import { IAgent } from './defs' ; import EventEmitter from './EventEmitter' ; import Molecule from './Molecule' ; export type AgentConfig = { molecule : Molecule ; [ key ] : any ; } ; class Agent implements IAgent { config : any = { } ; store ? : any ; emitter : EventEmitter ; preventInit : boolean = false ; constructor ( config ? ) { Object . assign ( this . config , config ) ; let configWithoutMolecule = Object . assign ( { } , config ) ; delete configWithoutMolecule . molecule ; this . validate ( configWithoutMolecule ) ; this . emitter = new EventEmitter ( { context : config . name , debug : this . isDebug ( ) , } ) ; } get molecule ( ) { return this . config . molecule ; } public on ( event , handler ) { this . emitter . on ( event , handler ) ; } public once ( event , handler ) { this . emitter . once ( event , handler ) ; } public off ( event , handler ) { this . emitter . off ( event , handler ) ; } public emit ( event , ... args ) { this . emitter . emit ( event , ... args ) ; } return param name public getAgent ( name ) { return this . molecule . getAgent ( name ) ; } prepare ( ) { } init ( ) { } clean ( ) { } validate ( config ) { } isDebug ( ) { return this . config . debug || this . molecule . debug ; } static factory ( config ? ) { const def = this ; return function ( molecule ) { return new def ( { ... config , molecule } ) ; } ; } } export default Agent ;