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 7 commits
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
8 changes: 3 additions & 5 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 2 * * *'
jobs:
Runner-ubuntu:
runs-on: ubuntu-latest
Expand All @@ -22,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ 14, 16, 18 ]
node-version: [ 14, 16, 18, 20 ]
steps:
- name: Checkout Git Source
uses: actions/checkout@master
Expand Down Expand Up @@ -53,7 +51,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ 14, 16, 18 ]
node-version: [ 14, 16, 18, 20 ]
steps:
- name: Checkout Git Source
uses: actions/checkout@master
Expand Down Expand Up @@ -88,7 +86,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ 14, 16, 18 ]
node-version: [ 14, 16, 18, 20 ]
steps:
- name: Checkout Git Source
uses: actions/checkout@master
Expand Down
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 {
LifecyclePostConstruct,
LifecyclePreInject,
LifecyclePostInject,
LifecycleInit,
LifecyclePreDestroy,
LifecycleDestroy,
} from '@eggjs/tegg';
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

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

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

@LifecycleDestroy()
protected 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"
}
}
6 changes: 3 additions & 3 deletions core/background-task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
"@eggjs/tegg-runtime": "^3.7.0"
},
"devDependencies": {
"@eggjs/tegg-common-util": "^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",
"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
12 changes: 6 additions & 6 deletions core/background-task/test/BackgroundTaskHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BackgroundTaskHelper } from '../src/BackgroundTaskHelper';
import sleep from 'mz-modules/sleep';
import { TimerUtil } from '@eggjs/tegg-common-util';
import assert from 'assert';

describe('test/BackgroundTaskHelper.test.ts', () => {
Expand All @@ -14,7 +14,7 @@ describe('test/BackgroundTaskHelper.test.ts', () => {
it('should done', async () => {
let run = false;
helper.run(async () => {
await sleep(10);
await TimerUtil.sleep(10);
run = true;
});

Expand All @@ -28,7 +28,7 @@ describe('test/BackgroundTaskHelper.test.ts', () => {
let run = false;
helper.timeout = 10;
helper.run(async () => {
await sleep(100);
await TimerUtil.sleep(100);
run = true;
});

Expand All @@ -40,7 +40,7 @@ describe('test/BackgroundTaskHelper.test.ts', () => {
describe('fn reject', () => {
it('should done', async () => {
helper.run(async () => {
await sleep(10);
await TimerUtil.sleep(10);
throw new Error('mock error');
});

Expand All @@ -62,10 +62,10 @@ describe('test/BackgroundTaskHelper.test.ts', () => {
it('should done', async () => {
let runDone = 0;
helper.run(async () => {
await sleep(10);
await TimerUtil.sleep(10);
runDone++;
helper.run(async () => {
await sleep(10);
await TimerUtil.sleep(10);
runDone++;
});
});
Expand Down
1 change: 1 addition & 0 deletions core/common-util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './src/FSUtil';
export * from './src/StackUtil';
export * from './src/ProxyUtil';
export * from './src/ModuleConfig';
export * from './src/TimerUtil';
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"
}
}
5 changes: 5 additions & 0 deletions core/common-util/src/TimerUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class TimerUtil {
static async sleep(ms: number) {
await new Promise(resolve => setTimeout(resolve, ms));
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions core/common-util/test/TimerUtil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assert from 'assert';
import { TimerUtil } from '..';

describe('test/TimerUtil.test.ts', () => {
it('should sleep work', async () => {
const start = Date.now();
await TimerUtil.sleep(3);
const use = Date.now() - start;
assert(use > 1, `use time ${use}ms`);
});
});
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
2 changes: 1 addition & 1 deletion core/controller-decorator/src/util/MethodInfoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
import { ControllerTypeLike, MiddlewareFunc } from '../model';
import { MapUtil } from '@eggjs/tegg-common-util';
import { ControllerTypeLike, MiddlewareFunc } from '../model';

const METHOD_CONTROLLER_TYPE_MAP = Symbol.for('EggPrototype#controller#mthods');
const METHOD_CONTROLLER_HOST = Symbol.for('EggPrototype#controller#mthods#host');
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
5 changes: 2 additions & 3 deletions core/eventbus-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@
"@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
14 changes: 7 additions & 7 deletions core/eventbus-runtime/test/EventBus.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import path from 'path';
import mm from 'mm';
import assert from 'assert';
import { LoadUnitInstance, LoadUnitInstanceFactory } from '@eggjs/tegg-runtime';
import { EggPrototype, LoadUnitFactory } from '@eggjs/tegg-metadata';
import { TimerUtil } from '@eggjs/tegg-common-util';
import { HelloHandler, HelloProducer } from './fixtures/modules/event/HelloEvent';
import { PrototypeUtil } from '@eggjs/core-decorator';
import { EventContextFactory, EventHandlerFactory, SingletonEventBus } from '..';
import { EventInfoUtil, CORK_ID } from '@eggjs/eventbus-decorator';
import assert from 'assert';
import { Timeout0Handler, Timeout100Handler, TimeoutProducer } from './fixtures/modules/event/MultiEvent';
import sleep from 'mz-modules/sleep';
import { CoreTestHelper, EggTestContext } from '@eggjs/module-test-util';
import { EventContextFactory, EventHandlerFactory, SingletonEventBus } from '..';
import { Timeout0Handler, Timeout100Handler, TimeoutProducer } from './fixtures/modules/event/MultiEvent';

describe('test/EventBus.test.ts', () => {
let modules: Array<LoadUnitInstance>;
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('test/EventBus.test.ts', () => {
eventBus.emitWithContext(ctx, 'hello', [ '01' ]);
const triggerTime = Date.now();

await sleep(100);
await TimerUtil.sleep(100);
eventBus.uncork(corkId);
await helloEvent;
assert(eventTime >= triggerTime + 100);
Expand Down Expand Up @@ -162,9 +162,9 @@ describe('test/EventBus.test.ts', () => {
eventBus.emitWithContext(ctx, 'hello', [ '01' ]);
const triggerTime = Date.now();

await sleep(100);
await TimerUtil.sleep(100);
eventBus.uncork(corkId);
await sleep(100);
await TimerUtil.sleep(100);
eventBus.uncork(corkId);

await helloEvent;
Expand Down
Loading