diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 69739e74..fe6a556c 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -4,8 +4,6 @@ on: branches: [ master ] pull_request: branches: [ master ] - schedule: - - cron: '0 2 * * *' jobs: Runner-ubuntu: runs-on: ubuntu-latest @@ -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 @@ -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 @@ -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 diff --git a/README.md b/README.md index 8fe73d94..09b517f0 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,62 @@ export class Foo implements EggObjectLifecycle { } ``` +##### 生命周期方法装饰器 + +上面展示的 hook 是通过方法命名约定来实现生命周期 hook,我们还提供了更加可读性更强的装饰器模式。 + +```ts +import { + LifecyclePostConstruct, + LifecyclePreInject, + LifecyclePostInject, + LifecycleInit, + LifecyclePreDestroy, + LifecycleDestroy, +} from '@eggjs/tegg'; + +@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 中的对象。 diff --git a/benchmark/http/package.json b/benchmark/http/package.json index 6666de99..ea685b4e 100644 --- a/benchmark/http/package.json +++ b/benchmark/http/package.json @@ -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" } } diff --git a/core/aop-decorator/package.json b/core/aop-decorator/package.json index 1a70e2c9..0fd88dec 100644 --- a/core/aop-decorator/package.json +++ b/core/aop-decorator/package.json @@ -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" } } diff --git a/core/aop-runtime/package.json b/core/aop-runtime/package.json index d007960c..b0d5bdb9 100644 --- a/core/aop-runtime/package.json +++ b/core/aop-runtime/package.json @@ -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" } } diff --git a/core/background-task/package.json b/core/background-task/package.json index 00efa563..5efbb7aa 100644 --- a/core/background-task/package.json +++ b/core/background-task/package.json @@ -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" diff --git a/core/background-task/test/BackgroundTaskHelper.test.ts b/core/background-task/test/BackgroundTaskHelper.test.ts index 430a4d3d..906f748c 100644 --- a/core/background-task/test/BackgroundTaskHelper.test.ts +++ b/core/background-task/test/BackgroundTaskHelper.test.ts @@ -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', () => { @@ -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; }); @@ -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; }); @@ -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'); }); @@ -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++; }); }); diff --git a/core/common-util/index.ts b/core/common-util/index.ts index 12f53367..f1025197 100644 --- a/core/common-util/index.ts +++ b/core/common-util/index.ts @@ -6,3 +6,4 @@ export * from './src/FSUtil'; export * from './src/StackUtil'; export * from './src/ProxyUtil'; export * from './src/ModuleConfig'; +export * from './src/TimerUtil'; diff --git a/core/common-util/package.json b/core/common-util/package.json index 2575fd14..4fb3392d 100644 --- a/core/common-util/package.json +++ b/core/common-util/package.json @@ -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" } } diff --git a/core/common-util/src/TimerUtil.ts b/core/common-util/src/TimerUtil.ts new file mode 100644 index 00000000..65f68690 --- /dev/null +++ b/core/common-util/src/TimerUtil.ts @@ -0,0 +1,5 @@ +export class TimerUtil { + static async sleep(ms: number) { + await new Promise(resolve => setTimeout(resolve, ms)); + } +} diff --git a/core/common-util/test/TimerUtil.test.ts b/core/common-util/test/TimerUtil.test.ts new file mode 100644 index 00000000..f512657d --- /dev/null +++ b/core/common-util/test/TimerUtil.test.ts @@ -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`); + }); +}); diff --git a/core/controller-decorator/package.json b/core/controller-decorator/package.json index 1e13fe03..1da03f12 100644 --- a/core/controller-decorator/package.json +++ b/core/controller-decorator/package.json @@ -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" diff --git a/core/controller-decorator/src/util/MethodInfoUtil.ts b/core/controller-decorator/src/util/MethodInfoUtil.ts index f18a5c1e..9fd67e55 100644 --- a/core/controller-decorator/src/util/MethodInfoUtil.ts +++ b/core/controller-decorator/src/util/MethodInfoUtil.ts @@ -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'); diff --git a/core/core-decorator/package.json b/core/core-decorator/package.json index 142c0257..cde49c69 100644 --- a/core/core-decorator/package.json +++ b/core/core-decorator/package.json @@ -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" } } diff --git a/core/dynamic-inject-runtime/package.json b/core/dynamic-inject-runtime/package.json index 5a26aca1..e9c1e2e4 100644 --- a/core/dynamic-inject-runtime/package.json +++ b/core/dynamic-inject-runtime/package.json @@ -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" } } diff --git a/core/dynamic-inject/package.json b/core/dynamic-inject/package.json index 25831b32..1514096f 100644 --- a/core/dynamic-inject/package.json +++ b/core/dynamic-inject/package.json @@ -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" } } diff --git a/core/eventbus-decorator/package.json b/core/eventbus-decorator/package.json index 15be7de8..1a8e3c6c 100644 --- a/core/eventbus-decorator/package.json +++ b/core/eventbus-decorator/package.json @@ -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" diff --git a/core/eventbus-runtime/package.json b/core/eventbus-runtime/package.json index 25f600f0..4dd21df3 100644 --- a/core/eventbus-runtime/package.json +++ b/core/eventbus-runtime/package.json @@ -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" diff --git a/core/eventbus-runtime/test/EventBus.test.ts b/core/eventbus-runtime/test/EventBus.test.ts index e49355fb..abcd467d 100644 --- a/core/eventbus-runtime/test/EventBus.test.ts +++ b/core/eventbus-runtime/test/EventBus.test.ts @@ -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; @@ -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); @@ -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; diff --git a/core/eventbus-runtime/test/fixtures/modules/event/MultiEvent.ts b/core/eventbus-runtime/test/fixtures/modules/event/MultiEvent.ts index 520811e8..8beb98ae 100644 --- a/core/eventbus-runtime/test/fixtures/modules/event/MultiEvent.ts +++ b/core/eventbus-runtime/test/fixtures/modules/event/MultiEvent.ts @@ -1,6 +1,6 @@ import { Event, EventBus } from '@eggjs/eventbus-decorator'; import { AccessLevel, Inject, SingletonProto } from '@eggjs/core-decorator'; -import sleep from 'mz-modules/sleep'; +import { TimerUtil } from '@eggjs/tegg-common-util'; import type { EggLogger } from 'egg'; declare module '@eggjs/eventbus-decorator' { @@ -36,7 +36,7 @@ export class Timeout100Handler { private readonly logger: EggLogger; async handle() { - await sleep(100); + await TimerUtil.sleep(100); // access logger, ensure context still alive this.logger.info('timeout 100'); Timeout100Handler.called = true; diff --git a/core/lifecycle/index.ts b/core/lifecycle/index.ts index dc8d4a1e..5ac490bf 100644 --- a/core/lifecycle/index.ts +++ b/core/lifecycle/index.ts @@ -2,3 +2,4 @@ export * from './src/LifecycleHook'; export * from './src/EggObjectLifecycle'; export * from './src/LifycycleUtil'; export * from './src/IdenticalObject'; +export * from './src/decorator'; diff --git a/core/lifecycle/package.json b/core/lifecycle/package.json index 24af0368..8f1004be 100644 --- a/core/lifecycle/package.json +++ b/core/lifecycle/package.json @@ -38,12 +38,16 @@ "publishConfig": { "access": "public" }, + "dependencies": { + "@eggjs/core-decorator": "^3.7.0", + "@eggjs/tegg-metadata": "^3.7.0" + }, "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" } } diff --git a/core/lifecycle/src/LifycycleUtil.ts b/core/lifecycle/src/LifycycleUtil.ts index 3b69fdd3..9c6ed90e 100644 --- a/core/lifecycle/src/LifycycleUtil.ts +++ b/core/lifecycle/src/LifycycleUtil.ts @@ -1,4 +1,9 @@ +import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator'; +import { EggPrototype } from '@eggjs/tegg-metadata'; import { LifecycleContext, LifecycleHook, LifecycleObject } from './LifecycleHook'; +import { EggObjectLifecycle } from './EggObjectLifecycle'; + +export type LifecycleHookName = keyof EggObjectLifecycle; export class LifecycleUtil> { @@ -80,4 +85,14 @@ export class LifecycleUtil(LIFECYCLE_HOOK); + } } diff --git a/core/lifecycle/src/decorator/index.ts b/core/lifecycle/src/decorator/index.ts new file mode 100644 index 00000000..0923c63b --- /dev/null +++ b/core/lifecycle/src/decorator/index.ts @@ -0,0 +1,18 @@ +import { EggProtoImplClass } from '@eggjs/core-decorator'; +import { LifecycleUtil, LifecycleHookName } from '../LifycycleUtil'; + +function createLifecycle(hookName: LifecycleHookName) { + return () => { + return function(target: object, methodName: string) { + const clazz = target.constructor as EggProtoImplClass; + LifecycleUtil.setLifecycleHook(methodName, hookName, clazz); + }; + }; +} + +export const LifecyclePostConstruct = createLifecycle('postConstruct'); +export const LifecyclePreInject = createLifecycle('preInject'); +export const LifecyclePostInject = createLifecycle('postInject'); +export const LifecycleInit = createLifecycle('init'); +export const LifecyclePreDestroy = createLifecycle('preDestroy'); +export const LifecycleDestroy = createLifecycle('destroy'); diff --git a/core/loader/package.json b/core/loader/package.json index e48139a4..445508cc 100644 --- a/core/loader/package.json +++ b/core/loader/package.json @@ -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" } } diff --git a/core/metadata/package.json b/core/metadata/package.json index 17a07f60..36c75c04 100644 --- a/core/metadata/package.json +++ b/core/metadata/package.json @@ -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" diff --git a/core/orm-decorator/package.json b/core/orm-decorator/package.json index d21032af..e4947b37 100644 --- a/core/orm-decorator/package.json +++ b/core/orm-decorator/package.json @@ -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" } } diff --git a/core/runtime/package.json b/core/runtime/package.json index 8130c45a..daa78700 100644 --- a/core/runtime/package.json +++ b/core/runtime/package.json @@ -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" diff --git a/core/runtime/src/impl/EggObjectImpl.ts b/core/runtime/src/impl/EggObjectImpl.ts index b02b60ff..466b0860 100644 --- a/core/runtime/src/impl/EggObjectImpl.ts +++ b/core/runtime/src/impl/EggObjectImpl.ts @@ -34,12 +34,14 @@ export default class EggObjectImpl implements EggObject { // global hook await EggObjectLifecycleUtil.objectPreCreate(ctx, this); // self hook - if (objLifecycleHook.postConstruct) { - await objLifecycleHook.postConstruct(); + const postConstructMethod = EggObjectLifecycleUtil.getLifecycleHook('postConstruct', this.proto) ?? 'postConstruct'; + if (objLifecycleHook[postConstructMethod]) { + await objLifecycleHook[postConstructMethod](); } - if (objLifecycleHook.preInject) { - await objLifecycleHook.preInject(); + const preInjectMethod = EggObjectLifecycleUtil.getLifecycleHook('preInject', this.proto) ?? 'preInject'; + if (objLifecycleHook[preInjectMethod]) { + await objLifecycleHook[preInjectMethod](); } await Promise.all(this.proto.injectObjects.map(async injectObject => { const proto = injectObject.proto; @@ -59,12 +61,14 @@ export default class EggObjectImpl implements EggObject { await EggObjectLifecycleUtil.objectPostCreate(ctx, this); // self hook - if (objLifecycleHook.postInject) { - await objLifecycleHook.postInject(); + const postInjectMethod = EggObjectLifecycleUtil.getLifecycleHook('postInject', this.proto) ?? 'postInject'; + if (objLifecycleHook[postInjectMethod]) { + await objLifecycleHook[postInjectMethod](); } - if (objLifecycleHook.init) { - await objLifecycleHook.init(); + const initMethod = EggObjectLifecycleUtil.getLifecycleHook('init', this.proto) ?? 'init'; + if (objLifecycleHook[initMethod]) { + await objLifecycleHook[initMethod](); } this.status = EggObjectStatus.READY; @@ -82,12 +86,14 @@ export default class EggObjectImpl implements EggObject { // self hook const objLifecycleHook = this._obj as EggObjectLifecycle; - if (objLifecycleHook.preDestroy) { - await objLifecycleHook.preDestroy(); + const preDestroyMethod = EggObjectLifecycleUtil.getLifecycleHook('preDestroy', this.proto) ?? 'preDestroy'; + if (objLifecycleHook[preDestroyMethod]) { + await objLifecycleHook[preDestroyMethod](); } - if (objLifecycleHook.destroy) { - await objLifecycleHook.destroy(); + const destroyMethod = EggObjectLifecycleUtil.getLifecycleHook('destroy', this.proto) ?? 'destroy'; + if (objLifecycleHook[destroyMethod]) { + await objLifecycleHook[destroyMethod](); } this.status = EggObjectStatus.DESTROYED; diff --git a/core/runtime/test/fixtures/modules/lifecycle-hook/object.ts b/core/runtime/test/fixtures/modules/lifecycle-hook/object.ts index 12930176..4dde9f2b 100644 --- a/core/runtime/test/fixtures/modules/lifecycle-hook/object.ts +++ b/core/runtime/test/fixtures/modules/lifecycle-hook/object.ts @@ -1,5 +1,13 @@ import { AccessLevel, ContextProto, SingletonProto } from '@eggjs/core-decorator'; -import { EggObjectLifecycle } from '@eggjs/tegg-lifecycle'; +import { + EggObjectLifecycle, + LifecyclePostConstruct, + LifecyclePreInject, + LifecyclePostInject, + LifecycleInit, + LifecyclePreDestroy, + LifecycleDestroy, +} from '@eggjs/tegg-lifecycle'; @ContextProto({ accessLevel: AccessLevel.PUBLIC, @@ -43,7 +51,7 @@ export class Foo implements EggObjectLifecycle { @SingletonProto({ accessLevel: AccessLevel.PUBLIC, }) -export class Bar implements EggObjectLifecycle { +export class Bar { private called: string[] = []; getLifecycleCalled() { @@ -54,27 +62,37 @@ export class Bar implements EggObjectLifecycle { this.called.push('construct'); } - async postConstruct(): Promise { + @LifecyclePostConstruct() + protected async _postConstruct() { this.called.push('postConstruct'); } - async preInject(): Promise { + @LifecyclePreInject() + protected async _preInject() { this.called.push('preInject'); } - async postInject(): Promise { + @LifecyclePostInject() + protected async _postInject() { this.called.push('postInject'); } - async init(): Promise { + protected async init() { + this.called.push('init should not called'); + } + + @LifecycleInit() + protected async _init() { this.called.push('init'); } - async preDestroy(): Promise { + @LifecyclePreDestroy() + protected async _preDestroy() { this.called.push('preDestroy'); } - async destroy(): Promise { + @LifecycleDestroy() + protected async _destroy() { this.called.push('destroy'); } } diff --git a/core/schedule-decorator/package.json b/core/schedule-decorator/package.json index 87d5e1bb..9dab6d50 100644 --- a/core/schedule-decorator/package.json +++ b/core/schedule-decorator/package.json @@ -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" diff --git a/core/standalone-decorator/package.json b/core/standalone-decorator/package.json index 12c556c3..8ffed480 100644 --- a/core/standalone-decorator/package.json +++ b/core/standalone-decorator/package.json @@ -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" } } diff --git a/core/tegg/package.json b/core/tegg/package.json index 6bbcafee..98b62eec 100644 --- a/core/tegg/package.json +++ b/core/tegg/package.json @@ -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" } } diff --git a/core/test-util/package.json b/core/test-util/package.json index 35eba095..d0ad2c68 100644 --- a/core/test-util/package.json +++ b/core/test-util/package.json @@ -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" } } diff --git a/plugin/aop/package.json b/plugin/aop/package.json index 8b4b5974..4d57d163 100644 --- a/plugin/aop/package.json +++ b/plugin/aop/package.json @@ -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" diff --git a/plugin/common/package.json b/plugin/common/package.json index 5609b5d4..e00ca333 100644 --- a/plugin/common/package.json +++ b/plugin/common/package.json @@ -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" } } diff --git a/plugin/config/package.json b/plugin/config/package.json index 1289bbb3..2a52b130 100644 --- a/plugin/config/package.json +++ b/plugin/config/package.json @@ -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" diff --git a/plugin/controller/package.json b/plugin/controller/package.json index 5f49ab3b..779aaed4 100644 --- a/plugin/controller/package.json +++ b/plugin/controller/package.json @@ -64,7 +64,7 @@ "@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", @@ -72,7 +72,7 @@ "globby": "^11.1.0", "mocha": "^10.2.0", "ts-node": "^10.9.1", - "typescript": "^4.9.4" + "typescript": "^5.0.4" }, "publishConfig": { "access": "public" diff --git a/plugin/eventbus/package.json b/plugin/eventbus/package.json index db6e3259..57dc9b8a 100644 --- a/plugin/eventbus/package.json +++ b/plugin/eventbus/package.json @@ -55,19 +55,19 @@ "@eggjs/tegg-runtime": "^3.7.0" }, "devDependencies": { + "@eggjs/tegg-common-util": "^3.7.0", "@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", "await-event": "^2.1.0", "cross-env": "^7.0.3", "egg": "^3.9.1", "egg-mock": "^5.5.0", "egg-tracer": "^2.0.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" diff --git a/plugin/eventbus/test/eventbus.test.ts b/plugin/eventbus/test/eventbus.test.ts index aa01a1e5..71cf9ffe 100644 --- a/plugin/eventbus/test/eventbus.test.ts +++ b/plugin/eventbus/test/eventbus.test.ts @@ -1,7 +1,7 @@ import assert from 'assert'; import path from 'path'; import mm from 'egg-mock'; -import sleep from 'mz-modules/sleep'; +import { TimerUtil } from '@eggjs/tegg-common-util'; import { HelloService } from './fixtures/apps/event-app/app/event-module/HelloService'; import { HelloLogger } from './fixtures/apps/event-app/app/event-module/HelloLogger'; @@ -57,7 +57,7 @@ describe('test/eventbus.test.ts', () => { const triggerTime = Date.now(); helloService.hello(); - await sleep(100); + await TimerUtil.sleep(100); helloService.uncork(); const eventWaiter = await app.getEventWaiter(); @@ -123,7 +123,7 @@ describe('test/eventbus.test.ts', () => { const eventWaiter = await app.getEventWaiter(); helloService.cork(); helloService.hello(); - await sleep(100); + await TimerUtil.sleep(100); helloService.uncork(); await eventWaiter.await('helloEgg'); }), @@ -132,7 +132,7 @@ describe('test/eventbus.test.ts', () => { const eventWaiter = await app.getEventWaiter(); helloService.cork(); helloService.hello(); - await sleep(100); + await TimerUtil.sleep(100); helloService.uncork(); await eventWaiter.await('helloEgg'); }), diff --git a/plugin/orm/package.json b/plugin/orm/package.json index ad7280ae..c0400f7b 100644 --- a/plugin/orm/package.json +++ b/plugin/orm/package.json @@ -67,7 +67,7 @@ "@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", @@ -76,7 +76,7 @@ "mocha": "^10.2.0", "mysql": "^2.18.1", "ts-node": "^10.9.1", - "typescript": "^4.9.4" + "typescript": "^5.0.4" }, "publishConfig": { "access": "public" diff --git a/plugin/schedule/package.json b/plugin/schedule/package.json index 558047e1..0a2dceb7 100644 --- a/plugin/schedule/package.json +++ b/plugin/schedule/package.json @@ -63,15 +63,14 @@ "@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", "egg-schedule": "^4.0.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" diff --git a/plugin/schedule/test/schedule.test.ts b/plugin/schedule/test/schedule.test.ts index 496bb9bb..4cbe7577 100644 --- a/plugin/schedule/test/schedule.test.ts +++ b/plugin/schedule/test/schedule.test.ts @@ -1,8 +1,8 @@ import path from 'path'; -import mm from 'egg-mock'; import fs from 'fs/promises'; import assert from 'assert'; -import sleep from 'mz-modules/sleep'; +import mm from 'egg-mock'; +import { TimerUtil } from '@eggjs/tegg-common-util'; describe('test/schedule.test.ts', () => { let app; @@ -33,7 +33,7 @@ describe('test/schedule.test.ts', () => { }); it('schedule should work', async () => { - await sleep(1000); + await TimerUtil.sleep(1000); const scheduleLog = await getScheduleLogContent('schedule-app'); assert(/schedule called/.test(scheduleLog)); }); diff --git a/plugin/tegg/package.json b/plugin/tegg/package.json index e227ff08..6925eba5 100644 --- a/plugin/tegg/package.json +++ b/plugin/tegg/package.json @@ -58,7 +58,7 @@ "devDependencies": { "@eggjs/tegg-config": "^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-logger": "^3.0.1", @@ -66,9 +66,8 @@ "egg-schedule": "^4.0.0", "egg-tracer": "^2.0.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" diff --git a/plugin/tegg/test/BackgroundTask.test.ts b/plugin/tegg/test/BackgroundTask.test.ts index 8a22cd27..6ac6d48c 100644 --- a/plugin/tegg/test/BackgroundTask.test.ts +++ b/plugin/tegg/test/BackgroundTask.test.ts @@ -1,12 +1,12 @@ -import mm from 'egg-mock'; import assert from 'assert'; import path from 'path'; -import { CountService } from './fixtures/apps/background-app/modules/multi-module-background/CountService'; -import sleep from 'mz-modules/sleep'; import fs from 'fs'; +import mm from 'egg-mock'; +import { TimerUtil } from '@eggjs/tegg-common-util'; import { TEGG_CONTEXT } from '@eggjs/egg-module-common'; import { BackgroundTaskHelper } from '@eggjs/tegg'; import { EggContext, EggContextLifecycleUtil } from '@eggjs/tegg-runtime'; +import { CountService } from './fixtures/apps/background-app/modules/multi-module-background/CountService'; describe('test/BackgroundTask.test.ts', () => { const appDir = path.join(__dirname, 'fixtures/apps/background-app'); @@ -40,7 +40,7 @@ describe('test/BackgroundTask.test.ts', () => { const countService = await app.getEggObject(CountService); assert(countService.count === 0); - await sleep(1000); + await TimerUtil.sleep(1000); assert(countService.count === 1); }); @@ -50,7 +50,7 @@ describe('test/BackgroundTask.test.ts', () => { .get('/backgroudTimeout') .expect(200); - await sleep(7000); + await TimerUtil.sleep(7000); const errorLog = fs.readFileSync(path.resolve(appDir, 'logs/egg-app/common-error.log'), 'utf-8'); assert(errorLog.includes('Can not read property `testObj` because egg ctx has been destroyed [')); }); diff --git a/plugin/tegg/test/app/extend/context.test.ts b/plugin/tegg/test/app/extend/context.test.ts index 8faa5bfd..6d7f680a 100644 --- a/plugin/tegg/test/app/extend/context.test.ts +++ b/plugin/tegg/test/app/extend/context.test.ts @@ -2,7 +2,7 @@ import assert from 'assert'; import path from 'path'; import mm from 'egg-mock'; import { Application } from 'egg'; -import sleep from 'mz-modules/sleep'; +import { TimerUtil } from '@eggjs/tegg-common-util'; import AppService from '../../fixtures/apps/egg-app/modules/multi-module-service/AppService'; import PersistenceService from '../../fixtures/apps/egg-app/modules/multi-module-repo/PersistenceService'; import { LONG_STACK_DELIMITER } from '../../../lib/run_in_background'; @@ -58,7 +58,7 @@ describe('test/app/extend/context.test.ts', () => { let backgroundIsDone = false; await app.mockModuleContextScope(async ctx => { ctx.runInBackground(async () => { - await sleep(100); + await TimerUtil.sleep(100); backgroundIsDone = true; }); }); @@ -69,9 +69,9 @@ describe('test/app/extend/context.test.ts', () => { let backgroundIsDone = false; await app.mockModuleContextScope(async ctx => { ctx.runInBackground(async () => { - await sleep(100); + await TimerUtil.sleep(100); ctx.runInBackground(async () => { - await sleep(100); + await TimerUtil.sleep(100); backgroundIsDone = true; }); }); @@ -88,7 +88,7 @@ describe('test/app/extend/context.test.ts', () => { ctx.runInBackground(async () => { throw new Error('background'); }); - await sleep(1000); + await TimerUtil.sleep(1000); }); const stack: string = backgroundError.stack; // background diff --git a/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts b/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts index 642cf283..fa85e4de 100644 --- a/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts +++ b/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts @@ -1,8 +1,8 @@ +import assert from 'assert'; import { AccessLevel, SingletonProto, Inject, ContextProto } from '@eggjs/tegg'; import { BackgroundTaskHelper } from '@eggjs/tegg-background-task'; +import { TimerUtil } from '@eggjs/tegg-common-util'; import { CountService } from './CountService'; -import sleep from 'mz-modules/sleep'; -import assert from 'assert'; @ContextProto() export class TestObj { @@ -25,7 +25,7 @@ export default class BackgroundService { async backgroundAdd(delay = 1000) { this.backgroundTaskHelper.timeout = 5000; this.backgroundTaskHelper.run(async () => { - await sleep(delay); + await TimerUtil.sleep(delay); assert(this.testObj.ok); this.countService.count += 1; }); diff --git a/standalone/standalone/package.json b/standalone/standalone/package.json index 63e3a3c0..f9b85af2 100644 --- a/standalone/standalone/package.json +++ b/standalone/standalone/package.json @@ -53,10 +53,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" } } diff --git a/tsconfig.json b/tsconfig.json index afc7f859..0f9ad714 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,6 @@ "noImplicitAny": false, "esModuleInterop": true, "useUnknownInCatchVariables": false, - "charset": "utf8", "allowJs": false, "pretty": true, "noEmitOnError": false,