Skip to content

Commit b35dc2d

Browse files
authored
feat: standalone support context (#65)
1 parent 65dd5fa commit b35dc2d

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

standalone/standalone/src/Runner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EggModuleLoader } from './EggModuleLoader';
55
import { EggProtoImplClass, PrototypeUtil } from '@eggjs/tegg';
66
import { StandaloneUtil, MainRunner } from '@eggjs/tegg/standalone';
77
import { StandaloneLoadUnit, StandaloneLoadUnitType } from './StandaloneLoadUnit';
8+
import { StandaloneContext } from './StandaloneContext';
89

910
export 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
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
import { Inject, SingletonProto } from '@eggjs/tegg';
1+
import { ContextProto, Inject, SingletonProto } from '@eggjs/tegg';
22
import { Runner, MainRunner } from '@eggjs/tegg/standalone';
33

44
@SingletonProto()
55
export 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()
1320
export 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
}

standalone/standalone/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)