1- import { ModuleConfig , ModuleConfigUtil , ModuleReference } from '@eggjs/tegg-common-util' ;
2- import { EggPrototype , LoadUnit , LoadUnitFactory } from '@eggjs/tegg-metadata' ;
1+ import { ModuleConfigUtil , ModuleReference } from '@eggjs/tegg-common-util' ;
2+ import {
3+ EggPrototype ,
4+ LoadUnit ,
5+ LoadUnitFactory ,
6+ LoadUnitLifecycleUtil ,
7+ } from '@eggjs/tegg-metadata' ;
38import {
49 ContextHandler ,
510 EggContainerFactory , EggContext ,
@@ -10,18 +15,21 @@ import {
1015import { EggProtoImplClass , PrototypeUtil } from '@eggjs/tegg' ;
1116import { StandaloneUtil , MainRunner } from '@eggjs/tegg/standalone' ;
1217import { EggModuleLoader } from './EggModuleLoader' ;
13- import { StandaloneLoadUnit , StandaloneLoadUnitType } from './StandaloneLoadUnit' ;
18+ import { InnerObject , StandaloneLoadUnit , StandaloneLoadUnitType } from './StandaloneLoadUnit' ;
1419import { StandaloneContext } from './StandaloneContext' ;
1520import { StandaloneContextHandler } from './StandaloneContextHandler' ;
16-
17- export interface ModuleConfigHolder {
18- name : string ;
19- config : ModuleConfig ;
20- reference : ModuleReference ;
21- }
21+ import { ModuleConfigHolder , ModuleConfigs } from './ModuleConfigs' ;
22+ import { ConfigSourceQualifierAttribute } from './ConfigSource' ;
23+ import { ConfigSourceLoadUnitHook } from './ConfigSourceLoadUnitHook' ;
2224
2325export interface RunnerOptions {
24- innerObjects : Record < string , object > ;
26+ /**
27+ * @deprecated
28+ * use inner object handlers instead
29+ */
30+ innerObjects ?: Record < string , object > ;
31+
32+ innerObjectHandlers ?: Record < string , InnerObject [ ] > ;
2533}
2634
2735export class Runner {
@@ -30,16 +38,23 @@ export class Runner {
3038 readonly moduleConfigs : Record < string , ModuleConfigHolder > ;
3139 private loadUnitLoader : EggModuleLoader ;
3240 private runnerProto : EggPrototype ;
41+ private configSourceEggPrototypeHook : ConfigSourceLoadUnitHook ;
3342
3443 loadUnits : LoadUnit [ ] = [ ] ;
3544 loadUnitInstances : LoadUnitInstance [ ] = [ ] ;
36- innerObjects : Record < string , object > | undefined ;
45+ innerObjects : Record < string , InnerObject [ ] > ;
3746
3847 constructor ( cwd : string , options ?: RunnerOptions ) {
3948 this . cwd = cwd ;
40- this . innerObjects = options ?. innerObjects ;
4149 this . moduleReferences = ModuleConfigUtil . readModuleReference ( this . cwd ) ;
4250 this . moduleConfigs = { } ;
51+ this . innerObjects = {
52+ moduleConfigs : [ {
53+ obj : new ModuleConfigs ( this . moduleConfigs ) ,
54+ } ] ,
55+ moduleConfig : [ ] ,
56+ } ;
57+
4358 for ( const reference of this . moduleReferences ) {
4459 const absoluteRef = {
4560 path : ModuleConfigUtil . resolveModuleDir ( reference . path , this . cwd ) ,
@@ -52,26 +67,42 @@ export class Runner {
5267 config : ModuleConfigUtil . loadModuleConfigSync ( absoluteRef . path ) || { } ,
5368 } ;
5469 }
70+ for ( const moduleConfig of Object . values ( this . moduleConfigs ) ) {
71+ this . innerObjects . moduleConfig . push ( {
72+ obj : moduleConfig . config ,
73+ qualifiers : [ {
74+ attribute : ConfigSourceQualifierAttribute ,
75+ value : moduleConfig . name ,
76+ } ] ,
77+ } ) ;
78+ }
79+ if ( options ?. innerObjects ) {
80+ for ( const [ name , obj ] of Object . entries ( options . innerObjects ) ) {
81+ this . innerObjects [ name ] = [ {
82+ obj,
83+ } ] ;
84+ }
85+ } else if ( options ?. innerObjectHandlers ) {
86+ Object . assign ( this . innerObjects , options . innerObjectHandlers ) ;
87+ }
5588 this . loadUnitLoader = new EggModuleLoader ( this . moduleReferences ) ;
89+ const configSourceEggPrototypeHook = new ConfigSourceLoadUnitHook ( ) ;
90+ LoadUnitLifecycleUtil . registerLifecycle ( configSourceEggPrototypeHook ) ;
5691 }
5792
5893 async init ( ) {
5994 StandaloneContextHandler . register ( ) ;
60- this . loadUnits = [ ] ;
61- if ( this . innerObjects ) {
62- LoadUnitFactory . registerLoadUnitCreator ( StandaloneLoadUnitType , ( ) => {
63- return new StandaloneLoadUnit ( this . innerObjects ! ) ;
64- } ) ;
65- LoadUnitInstanceFactory . registerLoadUnitInstanceClass ( StandaloneLoadUnitType , ModuleLoadUnitInstance . createModuleLoadUnitInstance ) ;
66- const standaloneLoadUnit = await LoadUnitFactory . createLoadUnit ( 'MockStandaloneLoadUnitPath' , StandaloneLoadUnitType , {
67- load ( ) : EggProtoImplClass [ ] {
68- return [ ] ;
69- } ,
70- } ) ;
71- this . loadUnits . push ( standaloneLoadUnit ) ;
72- }
95+ LoadUnitFactory . registerLoadUnitCreator ( StandaloneLoadUnitType , ( ) => {
96+ return new StandaloneLoadUnit ( this . innerObjects ) ;
97+ } ) ;
98+ LoadUnitInstanceFactory . registerLoadUnitInstanceClass ( StandaloneLoadUnitType , ModuleLoadUnitInstance . createModuleLoadUnitInstance ) ;
99+ const standaloneLoadUnit = await LoadUnitFactory . createLoadUnit ( 'MockStandaloneLoadUnitPath' , StandaloneLoadUnitType , {
100+ load ( ) : EggProtoImplClass [ ] {
101+ return [ ] ;
102+ } ,
103+ } ) ;
73104 const loadUnits = await this . loadUnitLoader . load ( ) ;
74- this . loadUnits = this . loadUnits . concat ( loadUnits ) ;
105+ this . loadUnits = [ standaloneLoadUnit , ... loadUnits ] ;
75106
76107 const instances : LoadUnitInstance [ ] = [ ] ;
77108 for ( const loadUnit of this . loadUnits ) {
@@ -123,5 +154,8 @@ export class Runner {
123154 await LoadUnitFactory . destroyLoadUnit ( loadUnit ) ;
124155 }
125156 }
157+ if ( this . configSourceEggPrototypeHook ) {
158+ LoadUnitLifecycleUtil . deleteLifecycle ( this . configSourceEggPrototypeHook ) ;
159+ }
126160 }
127161}
0 commit comments