-
Notifications
You must be signed in to change notification settings - Fork 5
/
ezy-clients.js
74 lines (66 loc) · 1.77 KB
/
ezy-clients.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import EzyClient from './ezy-client';
/**
* Singleton object to manage all clients of a server.
* - Each server has many zones
* - Each zone has its own client
*/
class EzyClients {
constructor() {
this.clients = {};
this.defaultClientName = '';
}
/**
* Singleton implementation
* @returns {EzyClients} Singleton object
*/
static getInstance() {
if (!EzyClients.instance) {
EzyClients.instance = new EzyClients();
}
return EzyClients.instance;
}
/**
* Create and save a new client
* @param {EzyClientConfig} config
* @returns {EzyClient} The newly created client
*/
newClient(config) {
var client = new EzyClient(config);
this.addClient(client);
if (this.defaultClientName === '') this.defaultClientName = client.name;
return client;
}
/**
* Create a new client and set it as default one
* @param {EzyClientConfig} config
* @returns {EzyClient} The newly created client
*/
newDefaultClient(config) {
var client = this.newClient(config);
this.defaultClientName = client.name;
return client;
}
/**
* Add a client to this singleton object
* @param {EzyClient} client Client to be added
*/
addClient(client) {
this.clients[client.name] = client;
}
/**
* Get a client by name
* @param {string} clientName Name of client
* @returns {EzyClient} The queried client
*/
getClient(clientName) {
return this.clients[clientName];
}
/**
* Get default client
* @returns {EzyClient} The default client
*/
getDefaultClient() {
return this.clients[this.defaultClientName];
}
}
export default EzyClients;