-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTlRpc.js
117 lines (104 loc) · 2.72 KB
/
TlRpc.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { loadProtoInstance, modeEnum } = require("./comm/comm");
const TlRpcConfig = require("./config/TlRpcConfig");
/**
* rpc-client, rpc-server通用类
* 负责proto定义的加载和初始化
* @version 1.0.0
* @author iamtsm
*/
class TlRpc {
/**
* -- 私有属性
* api proto实例
*/
static #apiProtoInstance = new Map();
/**
* -- 私有属性
* socket proto实例
*/
static #socketProtoInstance = new Map();
/**
* -- 公共属性
* rpc 配置
*/
static #configInstance = new TlRpcConfig({
protoName : "", protoPackage : "", protoImpl : "", ip : "", port : ""
});
/**
* 初始化
*/
constructor(rpcConfig) {
TlRpc.#configInstance = new TlRpcConfig(rpcConfig);
}
/**
* -- 私有方法
* 获取proto文件对象
* @returns proto instance
*/
#protoInstance() {
return loadProtoInstance(TlRpc.#configInstance.protoPath)
}
/**
* 获取缓存proto instance key
* @returns
*/
#getCacheProtoKey(){
return TlRpc.#configInstance.protoName + "-" + TlRpc.#configInstance.protoPackage;
}
/**
* -- 私有方法
* 从缓存获取proto对象
* @returns proto cache instance
*/
#cacheProtoInstance() {
let instance = null;
const cacheKey = this.#getCacheProtoKey();
if (TlRpc.#configInstance.mode === modeEnum.API) {
instance = TlRpc.#apiProtoInstance.get(cacheKey);
if (!instance) {
instance = this.#protoInstance();
}
TlRpc.#apiProtoInstance.set(cacheKey, instance)
} else if (TlRpc.#configInstance.mode === modeEnum.SOCKET) {
instance = TlRpc.#socketProtoInstance.get(cacheKey);
if (!instance) {
instance = this.#protoInstance();
}
TlRpc.#socketProtoInstance.set(cacheKey, instance)
}
return instance;
}
/**
* -- 私有方法
* 获取package对象
* @returns package instance
*/
#packageInstance(){
const instance = this.#cacheProtoInstance();
if (!instance) {
return null;
}
return instance[TlRpc.#configInstance.protoPackage];
}
/**
* -- 私有方法
* 获取实例化impl对象
* @returns protoImpl instance
*/
implInstance() {
const instance = this.#packageInstance();
if (!instance) {
return null;
}
return instance[TlRpc.#configInstance.protoImpl];
}
/**
* -- 对外提供
* 获取rpc配置
* @returns
*/
getConfig(){
return TlRpc.#configInstance;
}
}
module.exports = TlRpc;