Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 20, 2025
1 parent 4dec832 commit a4c7901
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class Singleton<T = any> {
readonly options: Record<string, any>;

constructor(options: SingletonOptions) {
assert(options.name, '[egg:singleton] Singleton#constructor options.name is required');
assert(options.app, '[egg:singleton] Singleton#constructor options.app is required');
assert(options.create, '[egg:singleton] Singleton#constructor options.create is required');
assert(!(options.name in options.app), `[egg:singleton] ${options.name} is already exists in app`);
assert(options.name, '[@eggjs/core/singleton] Singleton#constructor options.name is required');
assert(options.app, '[@eggjs/core/singleton] Singleton#constructor options.app is required');
assert(options.create, '[@eggjs/core/singleton] Singleton#constructor options.create is required');
assert(!(options.name in options.app), `[@eggjs/core/singleton] ${options.name} is already exists in app`);
this.app = options.app;
this.name = options.name;
this.create = options.create;
Expand All @@ -36,7 +36,7 @@ export class Singleton<T = any> {
initSync() {
const options = this.options;
assert(!(options.client && options.clients),
`[egg:singleton] ${this.name} can not set options.client and options.clients both`);
`[@eggjs/core/singleton] ${this.name} can not set options.client and options.clients both`);

// alias app[name] as client, but still support createInstance method
if (options.client) {
Expand All @@ -63,7 +63,7 @@ export class Singleton<T = any> {
async initAsync() {
const options = this.options;
assert(!(options.client && options.clients),
`[egg:singleton] ${this.name} can not set options.client and options.clients both`);
`[@eggjs/core/singleton] ${this.name} can not set options.client and options.clients both`);

// alias app[name] as client, but still support createInstance method
if (options.client) {
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Singleton<T = any> {
createInstance(config: Record<string, any>, clientName: string) {
// async creator only support createInstanceAsync
assert(!isAsyncFunction(this.create),
`egg:singleton ${this.name} only support create asynchronous, please use createInstanceAsync`);
`[@eggjs/core/singleton] ${this.name} only support synchronous creation, please use createInstanceAsync`);
// options.default will be merge in to options.clients[id]
config = {
...this.options.default,
Expand All @@ -127,8 +127,8 @@ export class Singleton<T = any> {
}

#extendDynamicMethods(client: any) {
assert(!client.createInstance, 'singleton instance should not have createInstance method');
assert(!client.createInstanceAsync, 'singleton instance should not have createInstanceAsync method');
assert(!client.createInstance, '[@eggjs/core/singleton] singleton instance should not have createInstance method');
assert(!client.createInstanceAsync, '[@eggjs/core/singleton] singleton instance should not have createInstanceAsync method');

try {
let extendable = client;
Expand All @@ -141,7 +141,7 @@ export class Singleton<T = any> {
extendable.createInstanceAsync = this.createInstanceAsync.bind(this);
} catch (err) {
this.app.coreLogger.warn(
'[egg:singleton] %s dynamic create is disabled because of client is un-extendable',
'[@eggjs/core/singleton] %s dynamic create is disabled because of client is un-extendable',
this.name);
this.app.coreLogger.warn(err);
}
Expand Down
12 changes: 4 additions & 8 deletions test/singleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,11 @@ describe('test/singleton.test.ts', () => {
create: asyncCreate,
});
await singleton.init();
assert(app.dataService === singleton);
assert.equal(app.dataService, singleton);
assert(app.dataService instanceof Singleton);
try {
app.dataService = await app.dataService.createInstance({ foo1: 'bar1' });
throw new Error('should not execute');
} catch (err: any) {
assert.equal(err.message,
'egg:singleton dataService only support create asynchronous, please use createInstanceAsync');
}
await assert.rejects(async () => {
await app.dataService.createInstance({ foo1: 'bar1' });
}, /\[@eggjs\/core\/singleton\] dataService only support synchronous creation, please use createInstanceAsync$/);
});

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

0 comments on commit a4c7901

Please sign in to comment.