Skip to content

Commit

Permalink
component/filter可以接收appConfig参数初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrover committed Jul 30, 2019
1 parent ccb434a commit fcbc419
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@rockerjs/mvc",
"version": "1.0.11",
"version": "1.0.12",
"description": "A MVC framework based on Rockerjs/core used by node.js",
"author": {
"name": "yangli",
Expand Down
12 changes: 7 additions & 5 deletions src/engine/index.ts
Expand Up @@ -96,7 +96,7 @@ export async function bootstrap(rootPath: string) {
// 1st step: parse configuration
// Logger.info(separatorGenerator("step 1. parse configuration"));
await parseConfigFile(rootPath);

const appConfig = CONFIG_TABLE[CURRENT_ENV] && CONFIG_TABLE[CURRENT_ENV][APP_TAG];
// 2nd step: scan files and load them, but can't find the starter configured in app.config
// Logger.info(separatorGenerator("step 2. files scanning & loading"));
await scan(rootPath).then((result) => {
Expand Down Expand Up @@ -139,7 +139,7 @@ export async function bootstrap(rootPath: string) {
const curretEnvConfig = CONFIG_TABLE[CURRENT_ENV] && CONFIG_TABLE[CURRENT_ENV][componentName];
const object = Container.getObject<IComponentCanon>(componentName);
curretEnvConfig && (curretEnvConfig[CONFIG_FILE_ENV] = CURRENT_ENV);
await object.start(curretEnvConfig);
await object.start(curretEnvConfig, appConfig);
break;
}
}
Expand All @@ -153,7 +153,8 @@ export async function bootstrap(rootPath: string) {
const object = Container.getObject<IComponentCanon>(componentName);
curretEnvConfig && (curretEnvConfig[CONFIG_FILE_ENV] = CURRENT_ENV);
componentNames.push(componentName);
componentsInitialArray.push(object.start(curretEnvConfig));
// component的初始化函数传递两个参数:componentConfig和appConfig
componentsInitialArray.push(object.start(curretEnvConfig, appConfig));
});

let initialOutcome = await Promise.all(componentsInitialArray);
Expand All @@ -179,7 +180,7 @@ export async function bootstrap(rootPath: string) {
if (object.status !== "on") {
const curretEnvConfig = CONFIG_TABLE[CURRENT_ENV] && CONFIG_TABLE[CURRENT_ENV][componentName];
curretEnvConfig && (curretEnvConfig[CONFIG_FILE_ENV] = CURRENT_ENV);
componentsRestartArray.push(object.start(curretEnvConfig));
componentsRestartArray.push(object.start(curretEnvConfig, appConfig));
}
});
// restart components
Expand Down Expand Up @@ -208,7 +209,8 @@ export async function bootstrap(rootPath: string) {
Object.keys(filtersConfig).forEach((filterName) => {
try {
const filter = Container.getObject<AbstractFilter>(filterName);
filter.init(filtersConfig[filterName]);
// filter的初始化函数传递两个参数
filter.init(filtersConfig[filterName], appConfig);
filters.push(filter);
} catch (e) {
throw new ApplicationException(`Filter error duaring its lifetime, ${e.message}`, e.stack);
Expand Down
8 changes: 4 additions & 4 deletions src/engine/main.ts
Expand Up @@ -21,13 +21,13 @@ export abstract class AbstractComponent {
// return this._moduleObject;
// }

public abstract start(config: any): Promise<any> | any;
public abstract start(config: any, appConfig?: any): Promise<any> | any;
}

export interface IComponentCanon {
name: string;
status: "on" | "pending" | "off";
start: (...args: any[]) => Promise<any>;
start: (args: any, appConfig?: any) => Promise<any>;
export: () => object;
}

Expand Down Expand Up @@ -68,10 +68,10 @@ export function Component(...args: any[]): ClassDecorator | any {
}

// args should be injected from config file
public start(config: any): Promise<any> {
public start(config: any, appConfig: any): Promise<any> {
if (this._export && typeof this._export.start === "function") {
try {
const sret = this._export.start(config);
const sret = this._export.start(config, appConfig);
return Util.isPromise(sret) ? sret.then((result) => {
this.status = "on";
return result ? result : this;
Expand Down
4 changes: 2 additions & 2 deletions src/web/annotation.ts
Expand Up @@ -32,7 +32,7 @@ export function Filter(...args: any[]): ClassDecorator | any {
}

export abstract class AbstractFilter {
public abstract init(args: any): void;
public abstract init(args: any, appConfig?: any): void;
public abstract async doFilter(context: Application.Context, next): Promise<void>;
public abstract destroy(): void;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ export function Param(_cfg: _Types.RouterParamType): Function {
return v;
}
const tfn = dt[index];
if (tfn.name.toUpperCase() === "OBJECT") {
if (tfn.name.toUpperCase() === "OBJECT" || tfn.name.toUpperCase() === "BOOLEAN") {
return typeof (v) === "string" ? (new Function("", `return ${v}`))() : v; // Support ill-formed json object
} else {
return tfn(v);
Expand Down

0 comments on commit fcbc419

Please sign in to comment.