Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: configuration inject plugin and more in production environment #680

Merged
merged 35 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f12cd10
refactor: egg load and midway bootstrap
czy88840616 Oct 20, 2020
92c4cb6
feat: add container cache
czy88840616 Oct 21, 2020
692c32c
fix: web refactor case error
czy88840616 Oct 21, 2020
789cfa3
chore: clean code
czy88840616 Oct 21, 2020
1fb3ee5
chore: clean code
czy88840616 Oct 21, 2020
c344f42
fix: app.generateController
czy88840616 Oct 21, 2020
15754f8
test: complete case and enable old suit
czy88840616 Oct 21, 2020
7ca58aa
chore: add @deprecated jsdoc
czy88840616 Oct 22, 2020
1dd84bf
v2.3.17-beta.1
czy88840616 Oct 22, 2020
561ad17
Merge branch '2.x' into fix_egg_loader
czy88840616 Oct 22, 2020
297d419
refactor: use new method to resolve registry
czy88840616 Oct 23, 2020
600e1e5
chore: revert cache
czy88840616 Oct 23, 2020
626d825
v2.3.18-beta.1
czy88840616 Oct 23, 2020
f9daf1e
chore: try add definition meteadata
czy88840616 Oct 23, 2020
2754389
refactor: add cache for definition
czy88840616 Oct 24, 2020
f412a7b
chore: lint
czy88840616 Oct 24, 2020
b9e40e7
chore: update
czy88840616 Oct 24, 2020
35bf565
fix: move cache to midwayContainer
czy88840616 Oct 24, 2020
d5f8030
v2.3.18-beta.2
czy88840616 Oct 24, 2020
44896a5
fix: configuration emit twice in app
czy88840616 Oct 24, 2020
10d06b4
refactor: merge container and midwayContainer
czy88840616 Oct 25, 2020
a17f2c2
fix: lint
czy88840616 Oct 25, 2020
4814910
fix: lint
czy88840616 Oct 25, 2020
6f46753
fix: lint
czy88840616 Oct 25, 2020
bfe1926
refactor: merge loader and base framework
czy88840616 Oct 26, 2020
f7ad592
test: fix case
czy88840616 Oct 26, 2020
e2335ff
test: change lifecycle and pass case
czy88840616 Oct 26, 2020
1afb6db
fix: lifecycle trigger
czy88840616 Oct 26, 2020
595f272
v2.3.18-beta.3
czy88840616 Oct 26, 2020
df60453
fix: configuration trigger
czy88840616 Oct 26, 2020
a0948a7
v2.3.18-beta.4
czy88840616 Oct 26, 2020
92713d8
chore: update tsc
czy88840616 Oct 27, 2020
a603d23
v2.3.18-beta.5
czy88840616 Oct 27, 2020
d9de275
chore: update
czy88840616 Oct 27, 2020
8356e59
v2.3.18-beta.6
czy88840616 Oct 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: lint
  • Loading branch information
czy88840616 committed Oct 25, 2020
commit a17f2c2e327cf2eee7e09b9e5e0ec9c2fdbe1475
3 changes: 2 additions & 1 deletion packages/core/src/context/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
saveProviderId,
isClass,
isFunction,
IComponentInfo, listModule,
IComponentInfo,
listModule,
} from '@midwayjs/decorator';

import { dirname, isAbsolute, join } from 'path';
Expand Down
52 changes: 31 additions & 21 deletions packages/core/src/context/midwayContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
ALL,
isAsyncFunction,
isClass,
isFunction, getConstructorInject, TAGGED_PROP, getObjectDefProps,
isFunction,
getConstructorInject,
TAGGED_PROP,
getObjectDefProps,
} from '@midwayjs/decorator';
import { ContainerConfiguration } from './configuration';
import { FUNCTION_INJECT_KEY } from '../common/constants';
Expand All @@ -27,7 +30,8 @@ import {
IContainerConfiguration,
IEnvironmentService,
ILifeCycle,
IMidwayContainer, IObjectDefinitionMetadata,
IMidwayContainer,
IObjectDefinitionMetadata,
REQUEST_CTX_KEY,
} from '../interface';
import { MidwayConfigService } from '../service/configService';
Expand Down Expand Up @@ -55,31 +59,35 @@ const DEFAULT_IGNORE_PATTERN = [
];

const globalDebugLogger = util.debuglog('midway:container');

export class MidwayContainer extends BaseApplicationContext implements IMidwayContainer {
id = Math.random().toString(10).slice(-5);
debugLogger = globalDebugLogger;
definitionMetadataList = [];
resolverHandler: ResolverHandler;
let containerIdx = 0;

export class MidwayContainer
extends BaseApplicationContext
implements IMidwayContainer {
protected id: number;
private debugLogger = globalDebugLogger;
private definitionMetadataList = [];
protected resolverHandler: ResolverHandler;
// 仅仅用于兼容requestContainer的ctx
ctx = {};
readyBindModules: Map<string, Set<any>> = new Map();
configurationMap: Map<string, IContainerConfiguration> = new Map();
private ctx = {};
private configurationMap: Map<string, IContainerConfiguration> = new Map();
// 特殊处理,按照 main 加载
likeMainConfiguration: IContainerConfiguration[] = [];
configService: IConfigService;
environmentService: IEnvironmentService;
private likeMainConfiguration: IContainerConfiguration[] = [];
public configService: IConfigService;
public environmentService: IEnvironmentService;

/**
* 单个进程中上一次的 applicationContext 的 registry
*/
static parentDefinitionMetadata: IObjectDefinitionMetadata[];

constructor(
baseDir: string = process.cwd(),
parent?: IApplicationContext
) {
constructor(baseDir: string = process.cwd(), parent?: IApplicationContext) {
super(baseDir, parent);
this.id = this.createContainerIdx();
}

protected createContainerIdx() {
return containerIdx++;
}

init(): void {
Expand Down Expand Up @@ -122,9 +130,7 @@ export class MidwayContainer extends BaseApplicationContext implements IMidwayCo

// auto load cache next time when loadDirectory invoked
if (MidwayContainer.parentDefinitionMetadata) {
this.restoreDefinitions(
MidwayContainer.parentDefinitionMetadata
);
this.restoreDefinitions(MidwayContainer.parentDefinitionMetadata);
} else {
this.loadDirectory(opts);
// 保存元信息最新的上下文中,供其他容器复用,减少重复扫描
Expand Down Expand Up @@ -745,4 +751,8 @@ export class MidwayContainer extends BaseApplicationContext implements IMidwayCo
}
}
}

public getResolverHandler() {
return this.resolverHandler;
}
}
16 changes: 10 additions & 6 deletions packages/core/src/context/requestContainer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { MidwayContainer } from './midwayContainer';
import { REQUEST_CTX_KEY } from '../interface';
import { REQUEST_CTX_KEY, IMidwayContainer } from '../interface';
import { parsePrefix } from '../util/';
import { PIPELINE_IDENTIFIER } from '@midwayjs/decorator';

export class MidwayRequestContainer extends MidwayContainer {
private applicationContext: MidwayContainer;
private applicationContext: IMidwayContainer;

constructor(ctx, applicationContext) {
constructor(ctx, applicationContext: IMidwayContainer) {
super(null, applicationContext);
this.applicationContext = applicationContext;
// register ctx
Expand All @@ -17,14 +17,18 @@ export class MidwayRequestContainer extends MidwayContainer {
this.registerObject('logger', ctx.logger);
}

const resolverHandler = this.applicationContext.resolverHandler;
const resolverHandler = this.applicationContext.getResolverHandler();
this.beforeEachCreated(
resolverHandler.beforeEachCreated.bind(resolverHandler)
);
this.afterEachCreated(
resolverHandler.afterEachCreated.bind(resolverHandler)
);
}
protected createContainerIdx() {
// requestContainer id = -1;
return -1;
}

init() {
// do nothing
Expand Down Expand Up @@ -96,10 +100,10 @@ export class MidwayRequestContainer extends MidwayContainer {
}

get configService() {
return this.applicationContext.configService;
return this.applicationContext.getConfigService();
}

get environmentService() {
return this.applicationContext.environmentService;
return this.applicationContext.getEnvironmentService();
}
}
5 changes: 2 additions & 3 deletions packages/core/src/context/resolverHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CLASS_KEY_CONSTRUCTOR, getClassMetadata } from '@midwayjs/decorator';
import { ManagedResolverFactory } from './managedResolverFactory';
import { MidwayContainer } from './midwayContainer';
import * as util from 'util';
import { HandlerFunction, IResolverHandler } from '../interface';

interface FrameworkDecoratorMetadata {
key: string;
Expand All @@ -10,9 +11,7 @@ interface FrameworkDecoratorMetadata {

const debug = util.debuglog('midway:container');

export type HandlerFunction = (handlerKey: string, instance?: any) => any;

export class ResolverHandler {
export class ResolverHandler implements IResolverHandler {
private handlerMap: Map<string, HandlerFunction>;
private resolverFactory: ManagedResolverFactory;

Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ export interface IContainerConfiguration {
bindConfigurationClass(clzz: any, filePath?: string);
}


export type HandlerFunction = (handlerKey: string, instance?: any) => any;

export interface IResolverHandler {
beforeEachCreated(target, constructorArgs: any[], context);
afterEachCreated(instance, context, definition);
registerHandler(key: string, fn: HandlerFunction);
getHandler(key: string);
}

export interface IMidwayContainer extends IApplicationContext {
bind<T>(target: T, options?: ObjectDefinitionOptions): void;
bind<T>(
Expand All @@ -238,6 +248,7 @@ export interface IMidwayContainer extends IApplicationContext {
getConfigService(): IConfigService;
getEnvironmentService(): IEnvironmentService;
getCurrentEnv(): string;
getResolverHandler(): IResolverHandler;
addAspect(
aspectIns: IMethodAspect,
aspectData: AspectMetadata
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/fixtures/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IValveHandler, IPipelineContext, IPipelineHandler } from '../../src/features/pipeline';
import { Provide, Inject, Pipeline } from '@midwayjs/decorator';
import { IContainer } from '../../src/interface';
import { IMidwayContainer } from '../../src/interface';

class VideoDto {
videoId: string;
Expand Down Expand Up @@ -235,7 +235,7 @@ export class DataMainTest {
}
}

export default (container: IContainer) => {
export default (container: IMidwayContainer) => {
container.bind(TestService);
container.bind(VideoFeeds);
container.bind(AccountMap);
Expand Down
18 changes: 12 additions & 6 deletions packages/decorator/src/common/decoratorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import {
ObjectIdentifier,
ReflectResult,
TagClsMetadata,
TagPropsMetadata
TagPropsMetadata,
} from '../interface';
import {
CLASS_KEY_CONSTRUCTOR, INJECT_TAG,
CLASS_KEY_CONSTRUCTOR,
INJECT_TAG,
MAIN_MODULE_KEY,
OBJ_DEF_CLS,
PRIVATE_META_DATA_KEY, TAGGED,
TAGGED_CLS, TAGGED_PROP,
PRIVATE_META_DATA_KEY,
TAGGED,
TAGGED_CLS,
TAGGED_PROP,
} from './constant';

import { DUPLICATED_INJECTABLE_DECORATOR, DUPLICATED_METADATA, INVALID_DECORATOR_OPERATION } from './errMsg';
import {
DUPLICATED_INJECTABLE_DECORATOR,
DUPLICATED_METADATA,
INVALID_DECORATOR_OPERATION,
} from './errMsg';
import { Metadata } from './metadata';
import { getParamNames, classNamed } from '../util';

Expand Down Expand Up @@ -792,7 +799,6 @@ export function getMethodReturnTypes(target, propertyKey: string | symbol) {
return Reflect.getMetadata('design:returntype', target, propertyKey);
}


function _tagParameterOrProperty(
metadataKey: string,
annotationTarget: any,
Expand Down