-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathserviceFactory.ts
102 lines (83 loc) · 2.7 KB
/
serviceFactory.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { extend } from '../util/extend';
import { IServiceFactory } from '../interface';
import { MidwayPriorityManager } from './priorityManager';
import { Inject } from '../decorator';
/**
* 多客户端工厂实现
*/
export abstract class ServiceFactory<T> implements IServiceFactory<T> {
protected clients: Map<string, T> = new Map();
protected clientPriority: Record<string, string>;
protected options = {};
@Inject()
protected priorityManager: MidwayPriorityManager;
protected async initClients(options: any = {}): Promise<void> {
this.options = options;
// merge options.client to options.clients['default']
if (options.client) {
options.clients = options.clients || {};
options.clients['default'] = options.clients['default'] || {};
extend(true, options.clients['default'], options.client);
}
// multi client
if (options.clients) {
for (const id of Object.keys(options.clients)) {
await this.createInstance(options.clients[id], id);
}
}
// set priority
this.clientPriority = options.priority || {};
}
public get<U = T>(id = 'default'): U {
return this.clients.get(id) as unknown as U;
}
public has(id: string): boolean {
return this.clients.has(id);
}
public async createInstance(config, clientName?): Promise<T | undefined> {
// options.default will be merge in to options.clients[id]
config = extend(true, {}, this.options['default'], config);
const client = await this.createClient(config, clientName);
if (client) {
if (clientName) {
this.clients.set(clientName, client);
}
return client;
}
}
public abstract getName(): string;
protected abstract createClient(
config,
clientName
): Promise<T | void> | (T | void);
protected async destroyClient(
client: T,
clientName?: string
): Promise<void> {}
public async stop(): Promise<void> {
for (const [name, value] of this.clients.entries()) {
await this.destroyClient(value, name);
}
}
public getDefaultClientName(): string {
return this.options['defaultClientName'];
}
public getClients() {
return this.clients;
}
public getClientKeys() {
return Array.from(this.clients.keys());
}
public getClientPriority(name: string) {
return this.priorityManager.getPriority(this.clientPriority[name]);
}
public isHighPriority(name: string) {
return this.priorityManager.isHighPriority(this.clientPriority[name]);
}
public isMediumPriority(name: string) {
return this.priorityManager.isMediumPriority(this.clientPriority[name]);
}
public isLowPriority(name: string) {
return this.priorityManager.isLowPriority(this.clientPriority[name]);
}
}