Skip to content

Commit

Permalink
feat: support getCurrentApplicationContext API (#981)
Browse files Browse the repository at this point in the history
* refactor: support getCurrent API

* fix: lint
  • Loading branch information
czy88840616 authored Apr 13, 2021
1 parent 099e76c commit dd6ce11
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/bootstrap/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class BootstrapStarter {
applicationContext,
});

global['MIDWAY_MAIN_FRAMEWORK'] = this.getMainFramework();
mainApp = await this.getFirstActions('getApplication');

// 初始化其余的副框架
Expand Down Expand Up @@ -148,6 +149,10 @@ export class BootstrapStarter {
return [];
}

protected getMainFramework() {
return this.bootstrapItems[0];
}

protected refreshBootstrapItems() {
this.bootstrapItems = this.bootstrapItems.map(bootstrapItem => {
if (typeof bootstrapItem === 'function') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Configuration, Config, ALL, Inject } from '@midwayjs/decorator';
import { ALL, App, Config, Configuration, Inject, MidwayFrameworkType } from '@midwayjs/decorator';
import { join } from 'path';
import * as assert from 'assert';
import { RemoteConfigService } from './service/remoteConfigService';
import { getCurrentApplicationContext, getCurrentMainFramework, IMidwayApplication } from '@midwayjs/core';

@Configuration({
importConfigs: [
Expand All @@ -15,9 +16,14 @@ export class AutoConfiguration {
@Inject()
configService: RemoteConfigService;

@App()
app: IMidwayApplication;

async onReady() {
console.log('ready');
assert(this.prepareConfig['prepare'] === 'remote data');
assert(this.prepareConfig['id'] === this.configService.innerData);
assert(getCurrentApplicationContext() === this.app.getApplicationContext());
assert(getCurrentMainFramework().getFrameworkType() === MidwayFrameworkType.WEB);
}
}
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ export * from './util/webRouterCollector';
export * from './util/triggerCollector';
export * from './util/emptyFramework';
export { plainToClass, classToPlain } from 'class-transformer';
export * from './logger';
export { createConfiguration } from './functional/configuration';
export { MidwayConfigService } from './service/configService';
export { MidwayEnvironmentService } from './service/environmentService';
export * from './logger';
export * from './util/contextUtil';
/**
* @deprecated please import from @midwayjs/logger
*/
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/util/contextUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
IConfigurationOptions,
IMidwayApplication,
IMidwayContainer,
IMidwayFramework,
} from '../interface';

export const getCurrentApplicationContext = (): IMidwayContainer => {
return getCurrentMainFramework().getApplicationContext();
};

export const getCurrentMainFramework = <
APP extends IMidwayApplication,
T extends IConfigurationOptions
>(): IMidwayFramework<APP, T> => {
return global['MIDWAY_MAIN_FRAMEWORK'] as IMidwayFramework<APP, T>;
};

export const getCurrentMainApp = <APP extends IMidwayApplication>(): APP => {
return getCurrentMainFramework().getApplication() as APP;
};
19 changes: 19 additions & 0 deletions packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import * as mm from 'mm';
import { LifeCycleTest, LifeCycleTest1, TestBinding } from './fixtures/lifecycle';
import sinon = require('sinon');
import { getCurrentApplicationContext, getCurrentMainApp, getCurrentMainFramework } from '../src/util/contextUtil';

@Provide()
class TestModule {
Expand Down Expand Up @@ -709,4 +710,22 @@ describe('/test/baseFramework.test.ts', () => {
}
]);
});


it('should test global framework', async () => {
const framework = new LightFramework();
await framework.initialize({
baseDir: path.join(__dirname, './fixtures/base-app/src'),
});

mm(global, 'MIDWAY_MAIN_FRAMEWORK', framework);

const appCtx = framework.getApplicationContext();
expect(getCurrentMainFramework()).toEqual(framework);
expect(getCurrentApplicationContext()).toEqual(appCtx);
expect(getCurrentMainApp()).toEqual(framework.getApplication());

mm.restore();
});

});

0 comments on commit dd6ce11

Please sign in to comment.