Skip to content

Commit

Permalink
feat:Singleton returns client name when create client (#3905)
Browse files Browse the repository at this point in the history
  • Loading branch information
dark-tone authored and dead-horse committed Oct 20, 2019
1 parent d3f68c3 commit 27dacb7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/core/singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Singleton {

// alias app[name] as client, but still support createInstance method
if (options.client) {
const client = this.createInstance(options.client);
const client = this.createInstance(options.client, options.name);
this.app[this.name] = client;
this._extendDynamicMethods(client);
return;
Expand All @@ -37,7 +37,7 @@ class Singleton {
// multi clent, use app[name].getInstance(id)
if (options.clients) {
Object.keys(options.clients).forEach(id => {
const client = this.createInstance(options.clients[id]);
const client = this.createInstance(options.clients[id], id);
this.clients.set(id, client);
});
this.app[this.name] = this;
Expand All @@ -55,7 +55,7 @@ class Singleton {

// alias app[name] as client, but still support createInstance method
if (options.client) {
const client = await this.createInstanceAsync(options.client);
const client = await this.createInstanceAsync(options.client, options.name);
this.app[this.name] = client;
this._extendDynamicMethods(client);
return;
Expand All @@ -64,7 +64,7 @@ class Singleton {
// multi clent, use app[name].getInstance(id)
if (options.clients) {
await Promise.all(Object.keys(options.clients).map(id => {
return this.createInstanceAsync(options.clients[id])
return this.createInstanceAsync(options.clients[id], id)
.then(client => this.clients.set(id, client));
}));
this.app[this.name] = this;
Expand All @@ -79,19 +79,19 @@ class Singleton {
return this.clients.get(id);
}

createInstance(config) {
createInstance(config, clientName) {
// async creator only support createInstanceAsync
assert(!is.asyncFunction(this.create),
`egg:singleton ${this.name} only support create asynchronous, please use createInstanceAsync`);
// options.default will be merge in to options.clients[id]
config = Object.assign({}, this.options.default, config);
return this.create(config, this.app);
return this.create(config, this.app, clientName);
}

async createInstanceAsync(config) {
async createInstanceAsync(config, clientName) {
// options.default will be merge in to options.clients[id]
config = Object.assign({}, this.options.default, config);
return await this.create(config, this.app);
return await this.create(config, this.app, clientName);
}

_extendDynamicMethods(client) {
Expand Down
56 changes: 56 additions & 0 deletions test/lib/core/singleton.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,34 @@ describe('test/lib/core/singleton.test.js', () => {
assert(warn);
});

it('should return client name when create', async () => {
let success = true;
const name = 'dataService';
const clientName = 'customClient';
function create(config, app, client) {
if (client !== clientName) {
success = false;
}
}
const app = {
config: {
dataService: {
clients: {
customClient: { foo: 'bar1' },
},
},
},
};
const singleton = new Singleton({
name,
app,
create,
});
singleton.init();

assert(success);
});

describe('async create', () => {
it('should init with client', async () => {
const name = 'dataService';
Expand Down Expand Up @@ -353,5 +381,33 @@ describe('test/lib/core/singleton.test.js', () => {
assert(err.message === 'egg:singleton dataService only support create asynchronous, please use createInstanceAsync');
}
});

it('should return client name when create', async () => {
let success = true;
const name = 'dataService';
const clientName = 'customClient';
async function create(config, app, client) {
if (client !== clientName) {
success = false;
}
}
const app = {
config: {
dataService: {
clients: {
customClient: { foo: 'bar1' },
},
},
},
};
const singleton = new Singleton({
name,
app,
create,
});
await singleton.init();

assert(success);
});
});
});

0 comments on commit 27dacb7

Please sign in to comment.