diff --git a/src/loader/impl/lifecycle.ts b/src/loader/impl/lifecycle.ts index c1e672d..09d2550 100644 --- a/src/loader/impl/lifecycle.ts +++ b/src/loader/impl/lifecycle.ts @@ -14,12 +14,24 @@ class LifecycleLoader implements Loader { this.container = container; } + get lifecycleManager(): LifecycleManager { + return this.container.get(ArtusInjectEnum.LifecycleManager); + } + 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; + const origin: Constructable[] = await compatibleRequire(item.path, true); + item._loaderState = Object.assign({ exportNames: ['default'] }, item._loaderState); + const { _loaderState: state } = item as { _loaderState: { exportNames: string[] } }; + + const lifecycleClazzList = []; + + for (const name of state.exportNames) { + const clazz = origin[name]; + this.container.set({ type: clazz }); + this.lifecycleManager.registerHookUnit(clazz); + } + + return lifecycleClazzList; } } diff --git a/test/fixtures/app_with_lifecycle/bootstrap.ts b/test/fixtures/app_with_lifecycle/bootstrap_load.ts similarity index 65% rename from test/fixtures/app_with_lifecycle/bootstrap.ts rename to test/fixtures/app_with_lifecycle/bootstrap_load.ts index 97aad97..7590f3f 100644 --- a/test/fixtures/app_with_lifecycle/bootstrap.ts +++ b/test/fixtures/app_with_lifecycle/bootstrap_load.ts @@ -8,7 +8,7 @@ import LifecycleList from './lifecyclelist'; export const TEST_LIFE_CYCLE_LIST = 'TEST_LIFE_CYCLE_LIST'; @LifecycleHookUnit() -export default class AppLifecycle implements ApplicationLifecycle { +export default class AppLoadLifecycle implements ApplicationLifecycle { @Inject(ArtusInjectEnum.Application) app: ArtusApplication; @@ -36,22 +36,4 @@ export default class AppLifecycle implements ApplicationLifecycle { await new Promise(resolve => setTimeout(resolve, 100)); this.lifecycleList.add('app_didLoad'); } - - @LifecycleHook('willReady') - async willReady() { - await new Promise(resolve => setTimeout(resolve, 100)); - this.lifecycleList.add('app_willReady'); - } - - @LifecycleHook('didReady') - async didReady() { - await new Promise(resolve => setTimeout(resolve, 100)); - this.lifecycleList.add('app_didReady'); - } - - @LifecycleHook() - async beforeClose() { - await new Promise(resolve => setTimeout(resolve, 100)); - this.lifecycleList.add('app_beforeClose'); - } } diff --git a/test/fixtures/app_with_lifecycle/bootstrap_ready.ts b/test/fixtures/app_with_lifecycle/bootstrap_ready.ts new file mode 100644 index 0000000..9f0d7b4 --- /dev/null +++ b/test/fixtures/app_with_lifecycle/bootstrap_ready.ts @@ -0,0 +1,37 @@ +import { + ArtusApplication, + ApplicationLifecycle, LifecycleHookUnit, LifecycleHook, ArtusInjectEnum, +} from '../../../src/index'; + +import { Container, Inject } from '@artus/injection'; +import LifecycleList from './lifecyclelist'; + +@LifecycleHookUnit() +export class AppReadyLifecycle implements ApplicationLifecycle { + @Inject(ArtusInjectEnum.Application) + app: ArtusApplication; + + @Inject() + container: Container; + + @Inject() + lifecycleList: LifecycleList; + + @LifecycleHook('willReady') + async willReady() { + await new Promise(resolve => setTimeout(resolve, 100)); + this.lifecycleList.add('app_willReady'); + } + + @LifecycleHook('didReady') + async didReady() { + await new Promise(resolve => setTimeout(resolve, 100)); + this.lifecycleList.add('app_didReady'); + } + + @LifecycleHook() + async beforeClose() { + await new Promise(resolve => setTimeout(resolve, 100)); + this.lifecycleList.add('app_beforeClose'); + } +}