Skip to content

Commit

Permalink
fix(loader): lifecycle loader support named export
Browse files Browse the repository at this point in the history
  • Loading branch information
noahziheng committed Aug 25, 2022
1 parent fed9fba commit 75ce365
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
22 changes: 17 additions & 5 deletions src/loader/impl/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationLifecycle> = 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<ApplicationLifecycle>[] = 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
}
37 changes: 37 additions & 0 deletions test/fixtures/app_with_lifecycle/bootstrap_ready.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}

0 comments on commit 75ce365

Please sign in to comment.