Skip to content

Commit

Permalink
chore: upgrade injection
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Mar 1, 2019
1 parent d7a3a83 commit 82d4eda
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/midway-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"extend2": "^1.0.0",
"globby": "^9.0.0",
"inflection": "^1.12.0",
"injection": "^1.3.1",
"injection": "^1.3.2",
"is-type-of": "^1.2.1",
"reflect-metadata": "^0.1.13"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/midway-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"autod": "midway-bin autod"
},
"dependencies": {
"injection": "^1.3.1"
"injection": "^1.3.2"
},
"devDependencies": {
"midway-bin": "^1.4.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/midway-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"license": "MIT",
"devDependencies": {
"injection": "^1.3.1",
"injection": "^1.3.2",
"midway-bin": "^1.4.1",
"midway-core": "^1.4.1"
},
Expand Down
13 changes: 8 additions & 5 deletions packages/midway-schedule/agent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ScheduleOpts, SCHEDULE_KEY } from '@midwayjs/decorator';
import { getClassMetaData, listModule, TagClsMetadata, TAGGED_CLS } from 'injection';
import 'reflect-metadata';
import { SCHEDULE_KEY, ScheduleOpts } from '@midwayjs/decorator';
import { getClassMetaData, getProviderId, listModule } from 'injection';

export = (agent) => {

if (!agent.schedule) {
return;
}

// ugly!! just support all and worker strategy
class AllStrategy extends agent['TimerScheduleStrategy'] {
handler() {
Expand All @@ -24,13 +27,13 @@ export = (agent) => {
agent.messenger.once('egg-ready', () => {
const schedules: any[] = listModule(SCHEDULE_KEY);
for (const scheduleModule of schedules) {
const metaData = Reflect.getMetadata(TAGGED_CLS, scheduleModule) as TagClsMetadata;
const provideId = getProviderId(scheduleModule);
const opts: ScheduleOpts = getClassMetaData(SCHEDULE_KEY, scheduleModule);
const type = opts.type;
if (opts.disable) {
continue;
}
const key = metaData.id + '#' + scheduleModule.name;
const key = provideId + '#' + scheduleModule.name;
const Strategy = strategyMap.get(type);
if (!Strategy) {
const err = new Error(`schedule type [${type}] is not defined`);
Expand Down
15 changes: 10 additions & 5 deletions packages/midway-schedule/app.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { ScheduleOpts, SCHEDULE_KEY } from '@midwayjs/decorator';
import { getClassMetaData, listModule, TagClsMetadata, TAGGED_CLS } from 'injection';
import { getClassMetaData, listModule, getProviderId } from 'injection';
import * as is from 'is-type-of';
import 'reflect-metadata';

export = (app) => {

// egg-schedule 的 app 里没有 schedule
if (!app.runSchedule) {
return;
}

const schedules: any[] = listModule(SCHEDULE_KEY);
for (const scheduleModule of schedules) {
const metaData = Reflect.getMetadata(TAGGED_CLS, scheduleModule) as TagClsMetadata;
if (metaData) {
const key = metaData.id + '#' + scheduleModule.name;
const providerId = getProviderId(scheduleModule);
if (providerId) {
const key = providerId + '#' + scheduleModule.name;
const opts: ScheduleOpts = getClassMetaData(SCHEDULE_KEY, scheduleModule);
const task = async (ctx, data) => {
const ins = await ctx.requestContext.getAsync(scheduleModule);
Expand Down
7 changes: 3 additions & 4 deletions packages/midway-schedule/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"eggPlugin": {
"name": "schedulePlus",
"dep": [
"optionalDependencies": [
"schedule"
]
},
Expand All @@ -26,8 +26,7 @@
},
"dependencies": {
"@midwayjs/decorator": "^1.4.2",
"injection": "^1.3.1",
"is-type-of": "^1.2.1",
"reflect-metadata": "^0.1.13"
"injection": "^1.3.2",
"is-type-of": "^1.2.1"
}
}
6 changes: 2 additions & 4 deletions packages/midway-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@
"@midwayjs/decorator": "^1.4.2",
"debug": "^4.1.1",
"egg": "^2.17.0",
"egg-core": "^4.14.1",
"extend2": "^1.0.0",
"globby": "^9.0.0",
"inflection": "^1.12.0",
"injection": "^1.3.1",
"injection": "^1.3.2",
"is-type-of": "^1.2.1",
"midway-core": "^1.4.1",
"midway-schedule": "^1.4.2",
"mkdirp": "^0.5.1",
"reflect-metadata": "^0.1.13"
"mkdirp": "^0.5.1"
},
"author": "Harry Chen <czy88840616@gmail.com>",
"repository": {
Expand Down
13 changes: 0 additions & 13 deletions packages/midway-web/src/interface.ts

This file was deleted.

15 changes: 7 additions & 8 deletions packages/midway-web/src/loader/webLoader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { EggRouter as Router } from '@eggjs/router';
import { CONTROLLER_KEY, RouterOption, PRIORITY_KEY, WEB_ROUTER_KEY, ControllerOption, WebMiddleware } from '@midwayjs/decorator';
import { getClassMetaData, listModule, TagClsMetadata, TAGGED_CLS } from 'injection';
import { getClassMetaData, listModule, getProviderId } from 'injection';
import { MidwayLoader } from 'midway-core';
import 'reflect-metadata';

export class MidwayWebLoader extends MidwayLoader {
private controllerIds: string[] = [];
Expand All @@ -16,13 +15,13 @@ export class MidwayWebLoader extends MidwayLoader {

// implement @controller
for (const module of controllerModules) {
const metaData = Reflect.getMetadata(TAGGED_CLS, module) as TagClsMetadata;
if (metaData && metaData.id) {
if (this.controllerIds.indexOf(metaData.id) > -1) {
throw new Error(`controller identifier [${metaData.id}] is exists!`);
const providerId = getProviderId(module);
if (providerId) {
if (this.controllerIds.indexOf(providerId) > -1) {
throw new Error(`controller identifier [${providerId}] is exists!`);
}
this.controllerIds.push(metaData.id);
await this.preRegisterRouter(module, metaData.id);
this.controllerIds.push(providerId);
await this.preRegisterRouter(module, providerId);
}
}

Expand Down
13 changes: 0 additions & 13 deletions packages/midway-web/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
import 'reflect-metadata';

export function attachMetaDataOnClass(clz, key, value) {
// save method name on class
let classMetaValue = Reflect.getMetadata(key, clz);
if (classMetaValue) {
classMetaValue = classMetaValue.concat(value);
} else {
classMetaValue = [value];
}
Reflect.defineMetadata(key, classMetaValue, clz);
}

const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
const ARGUMENT_NAMES = /([^\s,]+)/g;

Expand Down
2 changes: 1 addition & 1 deletion packages/midway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"egg": "^2.17.0",
"egg-cluster": "^1.22.2",
"egg-core": "^4.14.1",
"injection": "^1.3.1",
"injection": "^1.3.2",
"midway-core": "^1.4.1",
"midway-web": "^1.4.2",
"ts-node": "^8.0.2"
Expand Down

0 comments on commit 82d4eda

Please sign in to comment.