Skip to content

Commit 26c95a7

Browse files
authored
fix: use lifecycle instead of beforeStart (#297)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Enhanced the initialization flow by transitioning to a structured lifecycle management approach. - **Bug Fixes** - Clarified error messages related to instance creation, ensuring users are informed that asynchronous creation is supported. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 41fe40f commit 26c95a7

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

src/egg.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ export class EggCore extends KoaApplication {
239239
const singleton = new Singleton(options);
240240
const initPromise = singleton.init();
241241
if (initPromise) {
242-
this.beforeStart(async () => {
242+
this.lifecycle.registerBeforeStart(async () => {
243243
await initPromise;
244-
});
244+
}, `${name}-singleton-init`);
245245
}
246246
}
247247

src/singleton.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class Singleton<T = any> {
108108
createInstance(config: Record<string, any>, clientName: string) {
109109
// async creator only support createInstanceAsync
110110
assert(!isAsyncFunction(this.create),
111-
`[@eggjs/core/singleton] ${this.name} only support synchronous creation, please use createInstanceAsync`);
111+
`[@eggjs/core/singleton] ${this.name} only support asynchronous creation, please use createInstanceAsync`);
112112
// options.default will be merge in to options.clients[id]
113113
config = {
114114
...this.options.default,

test/singleton.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ describe('test/singleton.test.ts', () => {
357357
assert(app.dataService instanceof Singleton);
358358
await assert.rejects(async () => {
359359
await app.dataService.createInstance({ foo1: 'bar1' });
360-
}, /\[@eggjs\/core\/singleton\] dataService only support synchronous creation, please use createInstanceAsync$/);
360+
}, /\[@eggjs\/core\/singleton\] dataService only support asynchronous creation, please use createInstanceAsync$/);
361361
});
362362

363363
it('should return client name when create', async () => {

0 commit comments

Comments
 (0)