Skip to content

Commit

Permalink
fix: functional configuration load async code (#1300)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Sep 25, 2021
1 parent b97ba50 commit 32bcf03
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
20 changes: 18 additions & 2 deletions packages/core/src/functional/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import { InjectionConfigurationOptions } from '@midwayjs/decorator';
export class FunctionalConfiguration {
private readyHandler;
private stopHandler;
private configLoadHandler;
private options: InjectionConfigurationOptions;

constructor(options: InjectionConfigurationOptions) {
this.options = options;
this.readyHandler = () => {};
this.stopHandler = () => {};
this.configLoadHandler = () => {};
}

onConfigLoad(
configLoadHandler:
| ((container: IMidwayContainer, app: IMidwayApplication) => any)
| IMidwayContainer,
app?: IMidwayApplication
) {
if (typeof configLoadHandler === 'function') {
this.configLoadHandler = configLoadHandler;
} else {
return this.configLoadHandler(configLoadHandler, app);
}
return this;
}

onReady(
Expand All @@ -21,7 +37,7 @@ export class FunctionalConfiguration {
if (typeof readyHandler === 'function') {
this.readyHandler = readyHandler;
} else {
this.readyHandler(readyHandler, app);
return this.readyHandler(readyHandler, app);
}
return this;
}
Expand All @@ -35,7 +51,7 @@ export class FunctionalConfiguration {
if (typeof stopHandler === 'function') {
this.stopHandler = stopHandler;
} else {
this.stopHandler(stopHandler, app);
return this.stopHandler(stopHandler, app);
}
return this;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,8 @@ describe('/test/baseFramework.test.ts', () => {
),
});

expect(framework.getConfiguration('a')).toEqual(1);

await framework.stop();

// const appCtx = framework.getApplicationContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createConfiguration } from '../../../../src'
import { createHooks } from './components/hooks';
import { sleep } from '@midwayjs/decorator';

export default createConfiguration({
imports: [
Expand All @@ -14,4 +15,9 @@ export default createConfiguration({
console.log('on ready', app);
}).onStop(async (container, app) => {
console.log('on stop', app);
});
}).onConfigLoad(async () => {
await sleep(50);
return {
a: 1
}
})

0 comments on commit 32bcf03

Please sign in to comment.