Skip to content

Commit

Permalink
fix: should call config hook in idle callback (#218)
Browse files Browse the repository at this point in the history
* fix: should call confighook in idle callback

* feat: add PULL_REQUEST_TEMPLATE.md

* chore: remove pr template

* fix: add didLoad event

* fix: trigger loader with order

* fix: compatitble for custom loader

* fix: add config dir

* chore: remove useless comment
  • Loading branch information
whxaxes authored Dec 5, 2022
1 parent 9f3b77c commit 3b4deac
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/loader/factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import { isInjectable, Container } from '@artus/injection';
import { ArtusInjectEnum, DEFAULT_LOADER, HOOK_FILE_LOADER, LOADER_NAME_META, ScanPolicy } from '../constant';
import { ArtusInjectEnum, DEFAULT_LOADER, HOOK_FILE_LOADER, LOADER_NAME_META, ScanPolicy, DEFAULT_LOADER_LIST_WITH_ORDER } from '../constant';
import {
Manifest,
ManifestItem,
Expand Down Expand Up @@ -65,23 +65,31 @@ export class LoaderFactory {
}

async loadItemList(itemList: ManifestItem[] = [], root?: string): Promise<void> {
let prevLoader = '';
const itemMap = new Map(DEFAULT_LOADER_LIST_WITH_ORDER.map(loaderName => [loaderName, []]));

// group by loader names
for (const item of itemList) {
item.path = root ? path.join(root, item.path) : item.path;
const curLoader = item.loader ?? DEFAULT_LOADER;
if (item.loader !== prevLoader) {
if (prevLoader) {
await this.loaderEmitter.emitAfter(prevLoader);
}
await this.loaderEmitter.emitBefore(curLoader);
prevLoader = curLoader;
item.loader = item.loader ?? DEFAULT_LOADER;
if (!itemMap.has(item.loader)) {
// compatible for custom loader
itemMap.set(item.loader, []);
}
await this.loaderEmitter.emitBeforeEach(curLoader, item);
const result = await this.loadItem(item);
await this.loaderEmitter.emitAfterEach(curLoader, item, result);
itemMap.get(item.loader)!.push(item);
}
if (prevLoader) {
await this.loaderEmitter.emitAfter(prevLoader);

// trigger loader
for (const [ loaderName, itemList ] of itemMap) {
await this.loaderEmitter.emitBefore(loaderName);

for (const item of itemList) {
const curLoader = item.loader;
await this.loaderEmitter.emitBeforeEach(curLoader, item);
const result = await this.loadItem(item);
await this.loaderEmitter.emitAfterEach(curLoader, item, result);
}

await this.loaderEmitter.emitAfter(loaderName);
}
}

Expand Down
43 changes: 43 additions & 0 deletions test/fixtures/app_without_config/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { LifecycleHookUnit, LifecycleHook } from '../../../src/decorator';
import { ApplicationLifecycle } from '../../../src/types';
import { Container, Inject } from '@artus/injection';
import LifecycleList from './lifecyclelist';

@LifecycleHookUnit()
export default class MyLifecycle implements ApplicationLifecycle {
@Inject()
container: Container;

@Inject()
lifecycleList: LifecycleList;

@LifecycleHook()
async configWillLoad() {
this.lifecycleList.add('configWillLoad');
}

@LifecycleHook()
async configDidLoad() {
this.lifecycleList.add('configDidLoad');
}

@LifecycleHook()
async willReady() {
this.lifecycleList.add('willReady');
}

@LifecycleHook()
async didLoad() {
this.lifecycleList.add('didLoad');
}

@LifecycleHook('didReady')
async didReady() {
this.lifecycleList.add('didReady');
}

@LifecycleHook()
async beforeClose() {
this.lifecycleList.add('beforeClose');
}
}
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/app_without_config/lifecyclelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Injectable, ScopeEnum } from '../../../src';

@Injectable({
scope: ScopeEnum.SINGLETON,
})
export default class LifecycleList {
lifecycleList: string[] = [];

async add(name: string) {
this.lifecycleList.push(name);
}
}
4 changes: 4 additions & 0 deletions test/fixtures/app_without_config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "demo-app-repo-ts",
"main": "src/index.ts"
}
8 changes: 8 additions & 0 deletions test/fixtures/app_without_config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"esModuleInterop": true,
"experimentalDecorators": true
},
"rootDir": "./",
"exclude": []
}
17 changes: 17 additions & 0 deletions test/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import assert from 'assert';
import { createApp } from './utils';
import LifecycleList from './fixtures/app_with_lifecycle/lifecyclelist';
import WithoutConfigLifecycleList from './fixtures/app_without_config/lifecyclelist';

describe('test/lifecycle.test.ts', () => {
it('should lifecycle works without error', async () => {
Expand Down Expand Up @@ -88,4 +89,20 @@ describe('test/lifecycle.test.ts', () => {
'app_beforeClose',
]);
});

it('should trigger configDidLoad without config file', async () => {
const appWithoutConfigPath = path.resolve(__dirname, './fixtures/app_without_config');
const app = await createApp(appWithoutConfigPath);
const lifecycleList = app.container.get(WithoutConfigLifecycleList).lifecycleList;
await app.close();

assert.deepStrictEqual(lifecycleList, [
'configWillLoad',
'configDidLoad',
'didLoad',
'willReady',
'didReady',
'beforeClose',
]);
});
});

0 comments on commit 3b4deac

Please sign in to comment.