-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.js
57 lines (48 loc) · 1.8 KB
/
singleton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const assert = require('assert');
class Singleton {
constructor(options = {}) {
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.app[options.name], `${options.name} is already exists in app`);
this.clients = new Map();
this.app = options.app;
this.name = options.name;
this.create = options.create;
/* istanbul ignore next */
this.options = options.app.config[this.name] || {};
}
init() {
const options = this.options;
assert(!(options.client && options.clients),
`egg: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) {
const client = this.createInstance(options.client);
this.app[this.name] = client;
assert(!client.createInstance, 'singleton instance should not have createInstance method');
client.createInstance = this.createInstance.bind(this);
return;
}
// multi clent, use app[name].getInstance(id)
if (options.clients) {
for (const id in options.clients) {
this.clients.set(id, this.createInstance(options.clients[id]));
}
this.app[this.name] = this;
return;
}
// no config.clients and config.client
this.app[this.name] = this;
}
get(id) {
return this.clients.get(id);
}
createInstance(config) {
// options.default will be merge in to options.clients[id]
config = Object.assign({}, this.options.default, config);
return this.create(config, this.app);
}
}
module.exports = Singleton;