File tree Expand file tree Collapse file tree 4 files changed +32
-6
lines changed
Expand file tree Collapse file tree 4 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { EggModuleLoader } from './EggModuleLoader';
55import { EggProtoImplClass , PrototypeUtil } from '@eggjs/tegg' ;
66import { StandaloneUtil , MainRunner } from '@eggjs/tegg/standalone' ;
77import { StandaloneLoadUnit , StandaloneLoadUnitType } from './StandaloneLoadUnit' ;
8+ import { StandaloneContext } from './StandaloneContext' ;
89
910export interface ModuleConfigHolder {
1011 name : string ;
@@ -80,7 +81,11 @@ export class Runner {
8081 if ( ! proto ) {
8182 throw new Error ( `can not get proto for clazz ${ runnerClass . name } ` ) ;
8283 }
83- const eggObject = await EggContainerFactory . getOrCreateEggObject ( proto as EggPrototype ) ;
84+ const lifecycle = { } ;
85+ const ctx = new StandaloneContext ( ) ;
86+ await ctx . init ( lifecycle ) ;
87+ const eggObject = await EggContainerFactory . getOrCreateEggObject ( proto as EggPrototype , undefined , ctx ) ;
88+ await ctx . destroy ( lifecycle ) ;
8489 const runner = eggObject . obj as MainRunner < T > ;
8590 return await runner . main ( ) ;
8691 }
Original file line number Diff line number Diff line change 1+ import { AbstractEggContext } from '@eggjs/tegg-runtime' ;
2+ import { IdenticalUtil } from '@eggjs/tegg-lifecycle' ;
3+
4+ export class StandaloneContext extends AbstractEggContext {
5+ id : string ;
6+
7+ constructor ( ) {
8+ super ( ) ;
9+ this . id = IdenticalUtil . createContextId ( ) ;
10+ }
11+ }
Original file line number Diff line number Diff line change 1- import { Inject , SingletonProto } from '@eggjs/tegg' ;
1+ import { ContextProto , Inject , SingletonProto } from '@eggjs/tegg' ;
22import { Runner , MainRunner } from '@eggjs/tegg/standalone' ;
33
44@SingletonProto ( )
55export class Hello {
66 hello ( ) {
7- return 'hello' ;
7+ return 'hello! ' ;
88 }
99}
1010
11- @SingletonProto ( )
11+ @ContextProto ( )
12+ export class HelloContext {
13+ hello ( ) {
14+ return 'hello from ctx' ;
15+ }
16+ }
17+
18+ @ContextProto ( )
1219@Runner ( )
1320export class Foo implements MainRunner < string > {
1421 @Inject ( )
1522 hello : Hello ;
1623
24+ @Inject ( )
25+ helloContext : HelloContext ;
26+
1727 async main ( ) : Promise < string > {
18- return this . hello . hello ( ) ;
28+ return this . hello . hello ( ) + this . helloContext . hello ( ) ;
1929 }
2030}
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ describe('test/index.test.ts', () => {
66 describe ( 'simple runner' , ( ) => {
77 it ( 'should work' , async ( ) => {
88 const msg : string = await main ( path . join ( __dirname , './fixtures/simple' ) ) ;
9- assert ( msg === 'hello' ) ;
9+ assert ( msg === 'hello!hello from ctx ' ) ;
1010 } ) ;
1111 } ) ;
1212
You can’t perform that action at this time.
0 commit comments