import { Injector } from "./injector/injector.ts" ; import { EnlaceServer } from "./server.ts" ; import Log from "../util/log.ts" ; import { getEventsMarkInApplication } from "../decorators/main_application.ts" ; import ApplicationEvents from "../application_events.ts" ; import { AddAdaptorApplicationEventMark } from "../decorators/add_adaptor.ts" ; import { LOGO , PROJECT_NAME , WELCOME_WORDS } from "../constant.ts" ; import { Application } from "./application/application.ts" ; import { Adaptor } from "./adaptor/adaptor.ts" ; export class Environment { public ApplicationKey = Symbol ( "ApplicationKey" ) ; public static shard = new Environment ( ) ; public server : EnlaceServer = this . registerServer ( ) ; private app ! : Application ; private isReady : boolean = false ; private adaptorToObserver : Map < Adaptor , Function > = new Map ( ) ; private initApp ( app ) { if ( ! this . app ) { Log . clear ( ) ; Log . success ( WELCOME_WORDS , PROJECT_NAME ) ; Log . info ( LOGO ) ; this . app = this . registerApplication ( app ) ; this . setEventsInAdaptor ( ) ; this . server . adaptorsToConfigure . observeChange ( updated => { if ( this . isReady ) { this . callAdaptorObserver ( updated . key ) ; } } ) ; } else { } } async scan ( ) { } async run ( app ) { this . initApp ( app ) ; if ( this . app . config . scan ) { await this . scan ( ) ; } this . app . configure ( Injector . shard , this . server ) ; this . server . start ( ) ; this . isReady = true ; for ( const [ key ] of this . server . adaptorsToConfigure ) { this . callAdaptorObserver ( key ) ; } Log . success ( "\nenlace is ready!!\n" ) ; this . app . onStartUp ( ) ; Log . ask ( ) . then ( ) ; } private callAdaptorObserver ( adaptor ) { const observer = this . adaptorToObserver . get ( adaptor ) ; if ( observer ) { observer ( adaptor . router ) ; } } private registerServer ( ) { Injector . shard . register ( EnlaceServer ) ; return Injector . shard . resolve ( EnlaceServer ) ; } private registerApplication ( app ) { Injector . shard . register ( this . ApplicationKey , app ) ; return Injector . shard . resolve ( this . ApplicationKey ) ; } private setEventsInAdaptor ( ) { const eventsMark = getEventsMarkInApplication ( this . app ) ; for ( const mark of eventsMark ) { if ( mark . type === ApplicationEvents . onAddAdaptor ) { const adaptorMark = mark as AddAdaptorApplicationEventMark ; this . adaptorToObserver . set ( adaptorMark . meta , adaptorMark . target ) ; } } } }