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

feat: impl EggObjectLifecycle hook in decorator #119

Merged
merged 11 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,62 @@ export class Foo implements EggObjectLifecycle {
}
```

##### 生命周期方法装饰器
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

上面展示的 hook 是通过方法命名约定来实现生命周期 hook,我们还提供了更加可读性更强的装饰器模式。

```ts
import {
EggObjectLifecyclePostConstruct,
EggObjectLifecyclePreInject,
EggObjectLifecyclePostInject,
EggObjectLifecycleInit,
EggObjectLifecyclePreDestroy,
EggObjectLifecycleDestroy,
} from '@eggjs/tegg';
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
name: 'helloInterface',
})
export class HelloService {
@EggObjectLifecyclePostConstruct()
private async _postConstruct() {
console.log('对象构造完成');
}

@EggObjectLifecyclePreInject()
private async _preInject() {
console.log('依赖将要注入');
}

@EggObjectLifecyclePostInject()
private async _postInject() {
console.log('依赖注入完成');
}

@EggObjectLifecycleInit()
private async _init() {
console.log('执行一些异步的初始化过程');
}

@EggObjectLifecyclePreDestroy()
private async _preDestroy() {
console.log('对象将要释放了');
}

@EggObjectLifecycleDestroy()
private async _destroy() {
console.log('执行一些释放资源的操作');
}

async hello(user: User) {
const echoResponse = await this.echoAdapter.echo({ name: user.name });
return `hello, ${echoResponse.name}`;
}
}
```

### 注入

Proto 中可以依赖其他的 Proto,或者 egg 中的对象。
Expand Down
10 changes: 5 additions & 5 deletions benchmark/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"dev": "egg-bin dev"
},
"dependencies": {
"@eggjs/tegg": "^1.6.4",
"@eggjs/tegg-config": "^1.3.3",
"@eggjs/tegg-controller-plugin": "^1.7.5",
"@eggjs/tegg-plugin": "^1.5.5",
"@eggjs/tegg": "^3.7.0",
"@eggjs/tegg-config": "^3.7.0",
"@eggjs/tegg-controller-plugin": "^3.7.0",
"@eggjs/tegg-plugin": "^3.7.0",
"@eggjs/tsconfig": "^1.1.0",
"egg": "^3.9.1"
},
"devDependencies": {
"egg-bin": "^5.1.1"
"egg-bin": "6"
}
}
4 changes: 2 additions & 2 deletions core/aop-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/aop-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
"@eggjs/module-test-util": "^3.7.0",
"@eggjs/tegg-loader": "^3.7.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mm": "^3.2.1",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/background-task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"egg": "^3.9.1",
"mocha": "^10.2.0",
"mz-modules": "^2.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions core/common-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/controller-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"egg": "^3.9.1",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions core/core-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/dynamic-inject-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"@eggjs/module-test-util": "^3.7.0",
"@eggjs/tegg-loader": "^3.7.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/dynamic-inject/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"coffee": "^5.4.0",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/eventbus-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"coffee": "^5.4.0",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions core/eventbus-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@
"@eggjs/tegg-loader": "^3.7.0",
"@eggjs/tegg-metadata": "^3.7.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"coffee": "^5.4.0",
"cross-env": "^7.0.3",
"egg": "^3.9.1",
"mm": "^3.2.0",
"mocha": "^10.2.0",
"mz-modules": "^2.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions core/lifecycle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
"devDependencies": {
"@eggjs/tegg-metadata": "^3.7.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"globby": "^11.1.0",
"is-type-of": "^1.2.1",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions core/orm-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
"devDependencies": {
"@eggjs/tegg-loader": "^3.7.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mm": "^3.2.1",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions core/schedule-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"egg-schedule": "^3.6.6",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions core/standalone-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/tegg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions core/test-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions plugin/aop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
"@eggjs/tegg-config": "^3.7.0",
"@eggjs/tegg-plugin": "^3.7.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"egg": "^3.9.1",
"egg-mock": "^5.5.0",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions plugin/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
4 changes: 2 additions & 2 deletions plugin/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"egg": "^3.9.1",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
Loading