Skip to content

Commit

Permalink
fix: get singleton from shared context
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Mar 2, 2021
1 parent 44abb6c commit 3ebcf13
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/context/applicationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ export class BaseApplicationContext
}

const definition = this.registry.getDefinition(identifier);
if (definition && definition.isSingletonScope() && this.parent) {
/**
* 1、请求作用域不允许有多层 applicationContext,这里要保证请求作用域容器对应的 applicationContext 只有一层
* 2、如果有 parent,且单例在当前容器没有,那么则要做同步
*/
const instance = this.parent.getAsync(identifier, args);
this.registry.registerObject(identifier, instance);
return instance;
}

if (!definition && this.parent) {
return this.parent.getAsync(identifier, args);
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/context/midwayContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ export class MidwayContainer
if (typeof identifier !== 'string') {
identifier = this.getIdentifier(identifier);
}

const ins: any = await super.getAsync<T>(identifier, args);
return this.wrapperAspectToInstance(ins);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ describe('/test/baseFramework.test.ts', () => {
// const appCtx = framework.getApplicationContext();
});

it.skip('should run multi framework in one process and use cache', async () => {
it('should run multi framework in one process and use cache', async () => {
const framework1 = new EmptyFramework();
framework1.configure({});
await framework1.initialize({
Expand All @@ -649,7 +649,7 @@ describe('/test/baseFramework.test.ts', () => {
const userService1 = await framework1.getApplicationContext().getAsync('userService');
const userService2 = await framework2.getApplicationContext().getAsync('userService');
// 相同实例
expect(userService1 == userService2).toBeTruthy();
expect(userService1['id']).toEqual(userService2['id']);
});

it('should run multi framework in one process and container independent', async () => {
Expand Down Expand Up @@ -684,7 +684,7 @@ describe('/test/baseFramework.test.ts', () => {
// share application context data
const userService1 = await framework1.getApplicationContext().getAsync('userService');
const userService2 = await framework2.getApplicationContext().getAsync('userService');
// 不同引用
expect(userService1).not.toBe(userService2);
// 不同实例
expect(userService1['id']).not.toEqual(userService2['id']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
@Provide()
@Scope(ScopeEnum.Singleton)
export class UserService {

id = Math.random();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
@Provide()
@Scope(ScopeEnum.Singleton)
export class UserService {

id = Math.random();
}

0 comments on commit 3ebcf13

Please sign in to comment.