From 6f79b40a00d379c48719776fdf1b5eab053ae7a2 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Mon, 1 Aug 2022 20:40:03 +0800 Subject: [PATCH] feat: support inject in lifecycle (#145) * feat: support inject in lifecycle * remove unused WithXXX api * chore: update framework test * make lifecycle props lazy inject --- package.json | 2 +- src/constant.ts | 10 ++---- src/decorator.ts | 31 ++----------------- src/index.ts | 7 +---- src/lifecycle/index.ts | 14 ++------- src/loader/impl/lifecycle.ts | 1 + test/fixtures/app_koa_with_ts/src/app.ts | 13 +++----- .../src/controller/conifg.ts | 3 +- .../artus_application/src/controller/hello.ts | 7 ++--- .../src/controller/plugin.ts | 3 +- .../plugins/artus_plugin_mysql_ob/src/app.ts | 9 ++---- .../plugins/artus_plugin_mysql_rds/src/app.ts | 9 ++---- .../src/plugins/artus_plugin_redis/src/app.ts | 9 ++---- test/fixtures/custom_instance/bootstrap.ts | 12 +++---- test/fixtures/frameworks/bar/src/http.ts | 9 ++---- test/fixtures/frameworks/bar/src/lifecycle.ts | 9 ++---- .../layer/foo/foo1/src/lifecycle.ts | 9 ++---- .../layer/foo/foo2/src/lifecycle.ts | 9 ++---- test/fixtures/trigger/event/app.ts | 9 ++---- test/fixtures/trigger/http/app.ts | 9 ++---- test/fixtures/trigger/timer/app.ts | 9 ++---- test/framework.test.ts | 1 - 22 files changed, 54 insertions(+), 140 deletions(-) diff --git a/package.json b/package.json index 70da73a..c5680da 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "typescript": "^4.7.2" }, "dependencies": { - "@artus/injection": "^0.4.0", + "@artus/injection": "^0.4.1", "@artus/pipeline": "^0.2.2", "deepmerge": "^4.2.2", "minimatch": "^5.0.1" diff --git a/src/constant.ts b/src/constant.ts index cd84884..51be2b3 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -25,10 +25,6 @@ export enum ARTUS_DEFAULT_CONFIG_ENV { } export const HOOK_NAME_META_PREFIX = 'hookName:'; -export const CONSTRUCTOR_PARAMS = 'constructor:params'; -export const CONSTRUCTOR_PARAMS_APP = 'constructor:params:app'; -export const CONSTRUCTOR_PARAMS_CONTAINER = 'constructor:params:container'; -export const CONSTRUCTOR_PARAMS_CONTEXT = 'constructor:params:context'; export const HOOK_FILE_LOADER = 'appHook:fileLoader'; export const HOOK_CONFIG_HANDLE = 'appHook:configHandle::'; @@ -54,12 +50,12 @@ export const EXCEPTION_FILENAME = 'exception.json'; export const DEFAULT_LOADER_LIST_WITH_ORDER = [ 'exception', 'plugin-config', - 'lifecycle-hook-unit', - 'config', 'plugin-meta', - 'module', 'framework-config', 'package-json', + 'module', + 'lifecycle-hook-unit', + 'config', ]; export const DEFAULT_CONFIG_DIR = 'src/config'; diff --git a/src/decorator.ts b/src/decorator.ts index 33bed41..a8484a0 100644 --- a/src/decorator.ts +++ b/src/decorator.ts @@ -1,16 +1,13 @@ +import { Injectable } from '@artus/injection'; import { - CONSTRUCTOR_PARAMS, - CONSTRUCTOR_PARAMS_APP, - CONSTRUCTOR_PARAMS_CONTAINER, HOOK_NAME_META_PREFIX, - CONSTRUCTOR_PARAMS_CONTEXT, HOOK_FILE_LOADER, } from './constant'; export function LifecycleHookUnit(): ClassDecorator { return (target: any) => { - // Ready to remove? Reflect.defineMetadata(HOOK_FILE_LOADER, { loader: 'lifecycle-hook-unit' }, target); + Injectable({ lazy: true })(target); }; } @@ -23,29 +20,5 @@ export function LifecycleHook(hookName?: string): PropertyDecorator { }; } -const WithConstructorParams = (tag: string): ParameterDecorator => { - return (target: any, _propertyKey: string | symbol, parameterIndex: number) => { - const paramsMd = Reflect.getOwnMetadata(CONSTRUCTOR_PARAMS, target) ?? []; - paramsMd[parameterIndex] = tag; - if (_propertyKey) { - Reflect.defineMetadata(CONSTRUCTOR_PARAMS, paramsMd, target[_propertyKey]); // for proto - } else { - Reflect.defineMetadata(CONSTRUCTOR_PARAMS, paramsMd, target); // for constructor - } - }; -}; - -export function WithApplication(): ParameterDecorator { - return WithConstructorParams(CONSTRUCTOR_PARAMS_APP); -} - -export function WithContainer(): ParameterDecorator { - return WithConstructorParams(CONSTRUCTOR_PARAMS_CONTAINER); -} - -export function WithContext(): ParameterDecorator { - return WithConstructorParams(CONSTRUCTOR_PARAMS_CONTEXT); -} - export * from './loader/decorator'; export * from './trigger/decorator'; diff --git a/src/index.ts b/src/index.ts index f79718f..efebf7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,4 @@ - - -export { - Inject, - Injectable, -} from '@artus/injection'; +export * from '@artus/injection'; export * from './loader'; export * from './logger'; diff --git a/src/lifecycle/index.ts b/src/lifecycle/index.ts index c3b7e48..e86680d 100644 --- a/src/lifecycle/index.ts +++ b/src/lifecycle/index.ts @@ -1,17 +1,14 @@ import { Constructable, Container } from '@artus/injection'; import { Application } from '../types'; import { - CONSTRUCTOR_PARAMS, - CONSTRUCTOR_PARAMS_APP, - CONSTRUCTOR_PARAMS_CONTAINER, HOOK_NAME_META_PREFIX, } from '../constant'; -export type HookFunction = (hookProps : { +export type HookFunction = (hookProps: { app: Application, lifecycleManager: LifecycleManager, payload?: T -}) => void|Promise; +}) => void | Promise; export class LifecycleManager { hookList: string[] = [ @@ -52,12 +49,7 @@ export class LifecycleManager { registerHookUnit(extClazz: Constructable) { const fnMetaKeys = Reflect.getMetadataKeys(extClazz); - const constructorParams = Reflect.getMetadata(CONSTRUCTOR_PARAMS, extClazz) ?? []; - const paramsMap = { - [CONSTRUCTOR_PARAMS_APP]: this.app, - [CONSTRUCTOR_PARAMS_CONTAINER]: this.container, - }; - const extClazzInstance = new extClazz(...constructorParams.map(param => paramsMap[param])); + const extClazzInstance = this.container.get(extClazz); for (const fnMetaKey of fnMetaKeys) { if (typeof fnMetaKey !== 'string' || !fnMetaKey.startsWith(HOOK_NAME_META_PREFIX)) { continue; diff --git a/src/loader/impl/lifecycle.ts b/src/loader/impl/lifecycle.ts index a02ec97..c1e672d 100644 --- a/src/loader/impl/lifecycle.ts +++ b/src/loader/impl/lifecycle.ts @@ -17,6 +17,7 @@ class LifecycleLoader implements Loader { async load(item: ManifestItem) { const extClazz: Constructable = await compatibleRequire(item.path); const lifecycleManager: LifecycleManager = this.container.get(ArtusInjectEnum.LifecycleManager); + this.container.set({ type: extClazz }); lifecycleManager.registerHookUnit(extClazz); return extClazz; } diff --git a/test/fixtures/app_koa_with_ts/src/app.ts b/test/fixtures/app_koa_with_ts/src/app.ts index f9bcba6..ff819c6 100644 --- a/test/fixtures/app_koa_with_ts/src/app.ts +++ b/test/fixtures/app_koa_with_ts/src/app.ts @@ -1,10 +1,10 @@ import { DefaultContext } from 'koa'; import { Server } from 'http'; -import { Container } from '@artus/injection'; +import { Container, Inject } from '@artus/injection'; import { Context, Input } from '@artus/pipeline'; -import { ArtusApplication } from '../../../../src'; -import { LifecycleHookUnit, LifecycleHook, WithApplication, WithContainer } from '../../../../src/decorator'; +import { ArtusApplication, ArtusInjectEnum } from '../../../../src'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../src/types'; import KoaApplication from './koa_app'; @@ -14,14 +14,11 @@ export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; + @Inject() container: Container; - constructor(@WithApplication() app: ArtusApplication, @WithContainer() container: Container) { - this.app = app; - this.container = container; - } - get koaApp(): KoaApplication { return this.container.get(KoaApplication); } diff --git a/test/fixtures/artus_application/src/controller/conifg.ts b/test/fixtures/artus_application/src/controller/conifg.ts index fe51331..be9cca0 100644 --- a/test/fixtures/artus_application/src/controller/conifg.ts +++ b/test/fixtures/artus_application/src/controller/conifg.ts @@ -1,6 +1,5 @@ import { Context } from '@artus/pipeline'; import { HttpController, HttpMethod, HTTPMethodEnum } from '../../../frameworks/bar/src'; -import { WithContext } from '../../../../../src/decorator'; @HttpController() export default class Hello { @@ -8,7 +7,7 @@ export default class Hello { method: HTTPMethodEnum.GET, path: '/config', }) - async index(@WithContext() ctx: Context) { + async index(ctx: Context) { const { params: { config } } = ctx.input; return { message: `get conifg succeed`, diff --git a/test/fixtures/artus_application/src/controller/hello.ts b/test/fixtures/artus_application/src/controller/hello.ts index 71393e8..9b95419 100644 --- a/test/fixtures/artus_application/src/controller/hello.ts +++ b/test/fixtures/artus_application/src/controller/hello.ts @@ -1,6 +1,5 @@ import { Context } from '@artus/pipeline'; import { HttpController, HttpMethod, HTTPMethodEnum } from '../../../frameworks/bar/src'; -import { WithContext } from '../../../../../src/decorator'; @HttpController() export default class Hello { @@ -8,7 +7,7 @@ export default class Hello { method: HTTPMethodEnum.GET, path: '/home', }) - async index(@WithContext() ctx: Context) { + async index(ctx: Context) { const { params: { config } } = ctx.input; return { title: `Hello Artus ${config.name}` }; } @@ -17,7 +16,7 @@ export default class Hello { method: HTTPMethodEnum.GET, path: '/get_name2', }) - async name2(@WithContext() ctx: Context) { + async name2(ctx: Context) { const { params: { config } } = ctx.input; return { title: `Hello Artus ${config.name2}` }; } @@ -26,7 +25,7 @@ export default class Hello { method: HTTPMethodEnum.GET, path: '/get_name3', }) - async name3(@WithContext() ctx: Context) { + async name3(ctx: Context) { const { params: { config } } = ctx.input; return { title: `Hello Artus ${config.name3}` }; } diff --git a/test/fixtures/artus_application/src/controller/plugin.ts b/test/fixtures/artus_application/src/controller/plugin.ts index 5735895..5b0bacf 100644 --- a/test/fixtures/artus_application/src/controller/plugin.ts +++ b/test/fixtures/artus_application/src/controller/plugin.ts @@ -1,6 +1,5 @@ import { HttpController, HttpMethod, HTTPMethodEnum } from '../../../frameworks/bar/src'; import { Inject } from '@artus/injection'; -import { WithContext } from '../../../../../src/decorator'; import { ArtusApplication } from '../../../../../src'; import { Context } from '@artus/pipeline'; @@ -23,7 +22,7 @@ export default class Hello { method: HTTPMethodEnum.GET, path: '/plugin-redis', }) - async getRedisClient(@WithContext() ctx: Context) { + async getRedisClient(ctx: Context) { const app: ArtusApplication = ctx.input.params.app; let client; try { diff --git a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts index be128f0..32c6ae0 100644 --- a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts +++ b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_ob/src/app.ts @@ -1,19 +1,16 @@ import { Server } from 'http'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../../../../src/decorator'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../../../../src/types'; -import { ArtusApplication } from '../../../../../../../src'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../../../../src'; import Client, { MysqlConfig } from './client'; export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() async willReady() { const mysql = this.app.container.get('ARTUS_MYSQL') as Client; diff --git a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts index be128f0..32c6ae0 100644 --- a/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts +++ b/test/fixtures/artus_application/src/plugins/artus_plugin_mysql_rds/src/app.ts @@ -1,19 +1,16 @@ import { Server } from 'http'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../../../../src/decorator'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../../../../src/types'; -import { ArtusApplication } from '../../../../../../../src'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../../../../src'; import Client, { MysqlConfig } from './client'; export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() async willReady() { const mysql = this.app.container.get('ARTUS_MYSQL') as Client; diff --git a/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts b/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts index 72f1dba..0dd866e 100644 --- a/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts +++ b/test/fixtures/artus_application/src/plugins/artus_plugin_redis/src/app.ts @@ -1,19 +1,16 @@ import { Server } from 'http'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../../../../src/decorator'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../../../../src/types'; -import { ArtusApplication } from '../../../../../../../src'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../../../../src'; import Client, { RedisConfig } from './client'; export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() async willReady() { const redis = this.app.container.get('ARTUS_REDIS') as Client; diff --git a/test/fixtures/custom_instance/bootstrap.ts b/test/fixtures/custom_instance/bootstrap.ts index c766784..83fdcb4 100644 --- a/test/fixtures/custom_instance/bootstrap.ts +++ b/test/fixtures/custom_instance/bootstrap.ts @@ -1,22 +1,18 @@ import { ArtusApplication, - ApplicationLifecycle, LifecycleHookUnit, LifecycleHook, - WithApplication, WithContainer, + ApplicationLifecycle, LifecycleHookUnit, LifecycleHook, ArtusInjectEnum, } from '../../../src/index'; -import { Container } from '@artus/injection'; +import { Container, Inject } from '@artus/injection'; import Custom from './custom'; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; + @Inject() container: Container; - constructor(@WithApplication() app: ArtusApplication, @WithContainer() container) { - this.app = app; - this.container = container; - } - @LifecycleHook() configDidLoad() { this.container.set({ id: Custom, value: new Custom('foo') }); diff --git a/test/fixtures/frameworks/bar/src/http.ts b/test/fixtures/frameworks/bar/src/http.ts index 5f8ebf2..dd39f96 100644 --- a/test/fixtures/frameworks/bar/src/http.ts +++ b/test/fixtures/frameworks/bar/src/http.ts @@ -1,7 +1,6 @@ import 'reflect-metadata'; import { Context, Next } from '@artus/pipeline'; -import { Constructable , Injectable, ScopeEnum } from '@artus/injection'; -import { CONSTRUCTOR_PARAMS, CONSTRUCTOR_PARAMS_CONTEXT } from '../../../../../src/constant'; +import { Constructable, Injectable, ScopeEnum } from '@artus/injection'; import { HttpTrigger } from '../../abstract/foo'; export const enum HTTPMethodEnum { @@ -72,11 +71,7 @@ export function registerController(trigger: HttpTrigger) { if (req.url === `${prefix}${path}` && req.method === method) { const instance: any = ctx.container.get(clazz); const target = instance[key]; - const params: any = Reflect.getMetadata(CONSTRUCTOR_PARAMS, target) ?? []; - const paramsMap = { - [CONSTRUCTOR_PARAMS_CONTEXT]: ctx, - }; - ctx.output.data.content = await target.call(instance, ...params.map(param => paramsMap[param])); + ctx.output.data.content = await target.call(instance, ctx); } await next(); }); diff --git a/test/fixtures/frameworks/bar/src/lifecycle.ts b/test/fixtures/frameworks/bar/src/lifecycle.ts index 024da8a..064cd19 100644 --- a/test/fixtures/frameworks/bar/src/lifecycle.ts +++ b/test/fixtures/frameworks/bar/src/lifecycle.ts @@ -1,7 +1,7 @@ import { Server } from 'http'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../../src/decorator'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../../src/types'; -import { ArtusApplication } from '../../../../../src'; +import { ArtusInjectEnum, ArtusApplication, Inject } from '../../../../../src'; import { registerController } from './/http'; import { HttpTrigger } from '../../abstract/foo'; @@ -9,12 +9,9 @@ export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() async didLoad() { // register controller diff --git a/test/fixtures/frameworks/layer/foo/foo1/src/lifecycle.ts b/test/fixtures/frameworks/layer/foo/foo1/src/lifecycle.ts index 4a2bc17..7cde8b6 100644 --- a/test/fixtures/frameworks/layer/foo/foo1/src/lifecycle.ts +++ b/test/fixtures/frameworks/layer/foo/foo1/src/lifecycle.ts @@ -1,19 +1,16 @@ import http, { Server } from 'http'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../../../../src/decorator'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../../../../src/types'; import { Input } from '@artus/pipeline'; -import { ArtusApplication } from '../../../../../../../src'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../../../../src'; export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() willReady() { const config = this.app.config ?? {}; diff --git a/test/fixtures/frameworks/layer/foo/foo2/src/lifecycle.ts b/test/fixtures/frameworks/layer/foo/foo2/src/lifecycle.ts index 4a2bc17..7cde8b6 100644 --- a/test/fixtures/frameworks/layer/foo/foo2/src/lifecycle.ts +++ b/test/fixtures/frameworks/layer/foo/foo2/src/lifecycle.ts @@ -1,19 +1,16 @@ import http, { Server } from 'http'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../../../../src/decorator'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../../../../src/types'; import { Input } from '@artus/pipeline'; -import { ArtusApplication } from '../../../../../../../src'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../../../../src'; export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() willReady() { const config = this.app.config ?? {}; diff --git a/test/fixtures/trigger/event/app.ts b/test/fixtures/trigger/event/app.ts index a1f599c..d76b1ff 100644 --- a/test/fixtures/trigger/event/app.ts +++ b/test/fixtures/trigger/event/app.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; -import { ArtusApplication } from '../../../../src'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../src/decorator'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../src'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../src/decorator'; import { Context, Input, Next } from '@artus/pipeline'; import { ApplicationLifecycle } from '../../../../src/types'; @@ -8,12 +8,9 @@ export const event = new EventEmitter(); @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() async didLoad() { this.app.trigger.use(async (ctx: Context, next: Next) => { diff --git a/test/fixtures/trigger/http/app.ts b/test/fixtures/trigger/http/app.ts index a8fd02b..88a2311 100644 --- a/test/fixtures/trigger/http/app.ts +++ b/test/fixtures/trigger/http/app.ts @@ -1,19 +1,16 @@ import http, { Server } from 'http'; import { Context, Input } from '@artus/pipeline'; -import { ArtusApplication } from '../../../../src'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../src/decorator'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../src'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../src/decorator'; import { ApplicationLifecycle } from '../../../../src/types'; export let server: Server; @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() async didLoad() { this.app.trigger.use(async (ctx: Context) => { diff --git a/test/fixtures/trigger/timer/app.ts b/test/fixtures/trigger/timer/app.ts index 33482dc..769e9ad 100644 --- a/test/fixtures/trigger/timer/app.ts +++ b/test/fixtures/trigger/timer/app.ts @@ -1,5 +1,5 @@ -import { ArtusApplication } from '../../../../src'; -import { LifecycleHookUnit, LifecycleHook, WithApplication } from '../../../../src/decorator'; +import { ArtusApplication, Inject, ArtusInjectEnum } from '../../../../src'; +import { LifecycleHookUnit, LifecycleHook } from '../../../../src/decorator'; import { Context, Input } from '@artus/pipeline'; import { ApplicationLifecycle } from '../../../../src/types'; @@ -17,12 +17,9 @@ export const execution = { @LifecycleHookUnit() export default class MyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) app: ArtusApplication; - constructor(@WithApplication() app: ArtusApplication) { - this.app = app; - } - @LifecycleHook() async didLoad() { this.app.trigger.use(async (ctx: Context) => { diff --git a/test/framework.test.ts b/test/framework.test.ts index 26531a9..7f556d0 100644 --- a/test/framework.test.ts +++ b/test/framework.test.ts @@ -54,6 +54,5 @@ describe('test/framework.test.ts', () => { await app.artus.close(); assert(!app.isListening()); - }); });