@nsshunt/stsmessaging 1.0.37 → 1.0.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/stsmessaging.mjs +604 -664
- package/dist/stsmessaging.mjs.map +1 -1
- package/dist/stsmessaging.umd.js +607 -667
- package/dist/stsmessaging.umd.js.map +1 -1
- package/package.json +11 -12
package/dist/stsmessaging.umd.js
CHANGED
|
@@ -1,371 +1,226 @@
|
|
|
1
1
|
(function(global, factory) {
|
|
2
2
|
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("uuid"), require("tiny-emitter"), require("ioredis"), require("chalk"), require("@nsshunt/stsutils")) : typeof define === "function" && define.amd ? define(["exports", "uuid", "tiny-emitter", "ioredis", "chalk", "@nsshunt/stsutils"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@nsshunt/stsmessaging"] = {}, global.uuid, global["tiny-emitter"], global.ioredis, global.chalk, global.stsutils));
|
|
3
|
-
})(this, function(exports2, uuid, tinyEmitter, ioredis, chalk, stsutils) {
|
|
4
|
-
"use strict";
|
|
5
|
-
var __typeError = (msg) => {
|
|
6
|
-
throw TypeError(msg);
|
|
7
|
-
};
|
|
8
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
10
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
11
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
12
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
14
|
-
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
15
|
-
|
|
16
|
-
var _id, _options, _inflightMessages, _messageHeader, _SendMessageNoResponse, _SendMessage, _ProcessMessage, _messagingManager, _options2, _events, _requestChannel, _responseChannel, _redisSubscriber, _redisPublisher, _clients, _pingTimeout, _RedisMessageHandler_instances, LogInfo_fn, LogError_fn, _processRawMessage, _messageSender, _ProcessResponseMessage, _processPayload, _messagingManager2, _options3, _clients2, _events2, _startWorkerOptions, _ProcessWorkerMessageRaw, _processPayload2, _messagingManager3, _options4, _worker, _events3, _startWorkerOptions2, _startPrimaryWorker, _ProcessPrimaryMessageRaw, _ProcessWorkerMessageRaw2, _processPayload3;
|
|
3
|
+
})(this, (function(exports2, uuid, tinyEmitter, ioredis, chalk, stsutils) {
|
|
4
|
+
"use strict";
|
|
17
5
|
class MessagingManager {
|
|
6
|
+
#id;
|
|
7
|
+
#options;
|
|
8
|
+
#inflightMessages = {};
|
|
9
|
+
#messageHeader;
|
|
18
10
|
constructor(options) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
11
|
+
this.#id = uuid.v4();
|
|
12
|
+
this.#options = options;
|
|
13
|
+
this.#messageHeader = `__STS__${this.#options.namespace}__${uuid.v4()}`;
|
|
14
|
+
}
|
|
15
|
+
get id() {
|
|
16
|
+
return this.#id;
|
|
17
|
+
}
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
+
ReceivedMessageFromMaster(msg) {
|
|
20
|
+
}
|
|
21
|
+
SendMessageNoResponse = (payload, options) => {
|
|
22
|
+
this.#SendMessageNoResponse(payload, options);
|
|
23
|
+
};
|
|
24
|
+
SendMessage = (payload, options) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
this.#SendMessage(
|
|
27
|
+
payload,
|
|
28
|
+
options,
|
|
29
|
+
(payload2) => {
|
|
29
30
|
resolve(payload2.responsePayload);
|
|
30
|
-
},
|
|
31
|
+
},
|
|
32
|
+
(payload2) => {
|
|
31
33
|
reject(payload2.requestPayload);
|
|
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
|
-
clearTimeout(inFlightMessageRecord.timeout);
|
|
95
|
-
inFlightMessageRecord.callBack({
|
|
96
|
-
responsePayload: Object.values(inFlightMessageRecord.responses).map((r) => r.responsePayload)
|
|
97
|
-
}, options);
|
|
98
|
-
delete __privateGet(this, _inflightMessages)[message.messageId];
|
|
99
|
-
}
|
|
100
|
-
} else if (completed) {
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
#SendMessageNoResponse = (payload, options) => {
|
|
39
|
+
const messageId = uuid.v4();
|
|
40
|
+
const requestPayload = {
|
|
41
|
+
header: this.#messageHeader,
|
|
42
|
+
messageId,
|
|
43
|
+
senderId: this.#id,
|
|
44
|
+
senderRole: this.#options.role,
|
|
45
|
+
requestPayload: payload,
|
|
46
|
+
responsePayload: {},
|
|
47
|
+
pid: process.pid.toString(),
|
|
48
|
+
messageType: "REQUEST_NO_RESPONSE"
|
|
49
|
+
};
|
|
50
|
+
this.#options.messageSender(requestPayload, options);
|
|
51
|
+
};
|
|
52
|
+
#SendMessage = (payload, options, callBack, errorCallBack) => {
|
|
53
|
+
const messageId = uuid.v4();
|
|
54
|
+
const requestPayload = {
|
|
55
|
+
header: this.#messageHeader,
|
|
56
|
+
messageId,
|
|
57
|
+
senderId: this.#id,
|
|
58
|
+
senderRole: this.#options.role,
|
|
59
|
+
requestPayload: payload,
|
|
60
|
+
responsePayload: {},
|
|
61
|
+
pid: process.pid.toString(),
|
|
62
|
+
messageType: "REQUEST"
|
|
63
|
+
};
|
|
64
|
+
const messageRecord = {
|
|
65
|
+
messageId,
|
|
66
|
+
senderId: this.#id,
|
|
67
|
+
senderRole: this.#options.role,
|
|
68
|
+
requestPayload,
|
|
69
|
+
responses: {},
|
|
70
|
+
// record
|
|
71
|
+
startTime: performance.now(),
|
|
72
|
+
endTime: 0,
|
|
73
|
+
timeout: setTimeout(() => {
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
delete this.#inflightMessages[messageRecord.messageId];
|
|
76
|
+
}, 0).unref();
|
|
77
|
+
errorCallBack(requestPayload);
|
|
78
|
+
}, this.#options.requestResponseMessageTimeout).unref(),
|
|
79
|
+
// max message timeout allowed
|
|
80
|
+
callBack,
|
|
81
|
+
errorCallBack
|
|
82
|
+
};
|
|
83
|
+
this.#inflightMessages[messageRecord.messageId] = messageRecord;
|
|
84
|
+
this.#options.messageSender(requestPayload, options);
|
|
85
|
+
};
|
|
86
|
+
#ProcessMessage = async (msg, options) => {
|
|
87
|
+
if (msg.header && msg.header.localeCompare(this.#messageHeader) === 0) {
|
|
88
|
+
const message = msg;
|
|
89
|
+
if (this.#inflightMessages[message.messageId]) {
|
|
90
|
+
const inFlightMessageRecord = this.#inflightMessages[message.messageId];
|
|
91
|
+
inFlightMessageRecord.responses[message.senderId] = { ...message };
|
|
92
|
+
let completed = true;
|
|
93
|
+
if (this.#options.ProcessResponseMessage) {
|
|
94
|
+
completed = await this.#options.ProcessResponseMessage(inFlightMessageRecord.responses, options);
|
|
95
|
+
if (completed) {
|
|
101
96
|
inFlightMessageRecord.endTime = performance.now();
|
|
102
97
|
clearTimeout(inFlightMessageRecord.timeout);
|
|
103
|
-
inFlightMessageRecord.callBack(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
98
|
+
inFlightMessageRecord.callBack({
|
|
99
|
+
responsePayload: Object.values(inFlightMessageRecord.responses).map((r) => r.responsePayload)
|
|
100
|
+
}, options);
|
|
101
|
+
delete this.#inflightMessages[message.messageId];
|
|
102
|
+
}
|
|
103
|
+
} else if (completed) {
|
|
104
|
+
inFlightMessageRecord.endTime = performance.now();
|
|
105
|
+
clearTimeout(inFlightMessageRecord.timeout);
|
|
106
|
+
inFlightMessageRecord.callBack(message, options);
|
|
107
|
+
delete this.#inflightMessages[message.messageId];
|
|
108
|
+
} else ;
|
|
107
109
|
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
Start = (options) => {
|
|
113
|
+
this.#messageHeader = `__STS__${this.#options.namespace}__${uuid.v4()}`;
|
|
114
|
+
this.#options.messageReceiverStart(options);
|
|
115
|
+
};
|
|
116
|
+
Stop = (options) => {
|
|
117
|
+
this.#options.messageReceiverStop(options);
|
|
118
|
+
for (const [, iPCMessageProcessorWorkerRecord] of Object.entries(this.#inflightMessages)) {
|
|
119
|
+
if (iPCMessageProcessorWorkerRecord.timeout) {
|
|
120
|
+
clearTimeout(iPCMessageProcessorWorkerRecord.timeout);
|
|
119
121
|
}
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
122
|
+
}
|
|
123
|
+
this.#inflightMessages = {};
|
|
124
|
+
};
|
|
125
|
+
// Process a message recieved from a worker
|
|
126
|
+
ProcessMessage = async (msg, options) => {
|
|
127
|
+
if (msg.header) {
|
|
128
|
+
const checkName = `__STS__${this.#options.namespace}__`;
|
|
129
|
+
if (msg.header.includes(checkName)) {
|
|
130
|
+
const message = msg;
|
|
131
|
+
if (msg.messageType.localeCompare("REQUEST") === 0 || msg.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
132
|
+
let processMessage = true;
|
|
133
|
+
if (message.requestPayload.args && message.requestPayload.args.length > 0 && message.requestPayload.args[0].group) {
|
|
134
|
+
const group = message.requestPayload.args[0].group;
|
|
135
|
+
processMessage = this.#options.groups.indexOf(group) === -1 ? false : true;
|
|
136
|
+
}
|
|
137
|
+
if (processMessage) {
|
|
138
|
+
if (msg.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
139
|
+
this.#options.ProcessRequestMessage(message, options);
|
|
140
|
+
} else {
|
|
141
|
+
message.responsePayload = await this.#options.ProcessRequestMessage(message, options);
|
|
142
|
+
message.senderId = this.#id;
|
|
143
|
+
message.messageType = "RESPONSE";
|
|
144
|
+
this.#options.messageSender(message, options);
|
|
143
145
|
}
|
|
144
|
-
} else {
|
|
145
|
-
__privateGet(this, _ProcessMessage).call(this, msg, options);
|
|
146
146
|
}
|
|
147
|
+
} else {
|
|
148
|
+
this.#ProcessMessage(msg, options);
|
|
147
149
|
}
|
|
148
150
|
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
__privateSet(this, _options, options);
|
|
152
|
-
__privateSet(this, _messageHeader, `__STS__${__privateGet(this, _options).namespace}__${uuid.v4()}`);
|
|
153
|
-
}
|
|
154
|
-
get id() {
|
|
155
|
-
return __privateGet(this, _id);
|
|
156
|
-
}
|
|
157
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
158
|
-
ReceivedMessageFromMaster(msg) {
|
|
159
|
-
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
160
153
|
}
|
|
161
|
-
_id = new WeakMap();
|
|
162
|
-
_options = new WeakMap();
|
|
163
|
-
_inflightMessages = new WeakMap();
|
|
164
|
-
_messageHeader = new WeakMap();
|
|
165
|
-
_SendMessageNoResponse = new WeakMap();
|
|
166
|
-
_SendMessage = new WeakMap();
|
|
167
|
-
_ProcessMessage = new WeakMap();
|
|
168
154
|
const REQUEST_CHANNEL = "__STS__SVC_stsappframework_request";
|
|
169
155
|
const RESPONSE_CHANNEL = "__STS__SVC_stsappframework_response";
|
|
170
156
|
class RedisMessageHandler extends tinyEmitter.TinyEmitter {
|
|
157
|
+
#messagingManager = null;
|
|
158
|
+
#options;
|
|
159
|
+
#events = {};
|
|
160
|
+
#requestChannel;
|
|
161
|
+
#responseChannel;
|
|
162
|
+
#redisSubscriber;
|
|
163
|
+
#redisPublisher;
|
|
164
|
+
#clients = {};
|
|
165
|
+
#pingTimeout = null;
|
|
171
166
|
constructor(options) {
|
|
172
167
|
super();
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
__privateAdd(this, _events, {});
|
|
177
|
-
__privateAdd(this, _requestChannel);
|
|
178
|
-
__privateAdd(this, _responseChannel);
|
|
179
|
-
__privateAdd(this, _redisSubscriber);
|
|
180
|
-
__privateAdd(this, _redisPublisher);
|
|
181
|
-
__privateAdd(this, _clients, {});
|
|
182
|
-
__privateAdd(this, _pingTimeout, null);
|
|
183
|
-
__privateAdd(this, _processRawMessage, (channel, rawmessage) => {
|
|
184
|
-
var _a;
|
|
185
|
-
const message = JSON.parse(rawmessage);
|
|
186
|
-
(_a = __privateGet(this, _messagingManager)) == null ? void 0 : _a.ProcessMessage(message, { channel });
|
|
187
|
-
});
|
|
188
|
-
__publicField(this, "AddGroup", (group) => {
|
|
189
|
-
const index = __privateGet(this, _options2).groups.indexOf(group);
|
|
190
|
-
if (index === -1) {
|
|
191
|
-
__privateGet(this, _options2).groups.push(group);
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
__publicField(this, "RemoveGroup", (group) => {
|
|
195
|
-
const removeIndex = __privateGet(this, _options2).groups.indexOf(group);
|
|
196
|
-
if (removeIndex !== -1) {
|
|
197
|
-
__privateGet(this, _options2).groups.splice(removeIndex, 1);
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
__publicField(this, "SetupPrimary", () => {
|
|
201
|
-
const ipcMessageManagerOptions = {
|
|
202
|
-
logger: __privateGet(this, _options2).logger,
|
|
203
|
-
requestResponseMessageTimeout: 5e3,
|
|
204
|
-
namespace: __privateGet(this, _options2).namespace,
|
|
205
|
-
role: __privateGet(this, _options2).role,
|
|
206
|
-
groups: __privateGet(this, _options2).groups,
|
|
207
|
-
messageSender: __privateGet(this, _messageSender),
|
|
208
|
-
// This method is used to calculate if all responses have been received from multiple clients (broadcast)
|
|
209
|
-
// returns true/false.
|
|
210
|
-
ProcessResponseMessage: __privateGet(this, _ProcessResponseMessage),
|
|
211
|
-
// This gets called when an event is received from a message receiver (when ProcessMessage is invoked from the receiver event handler)
|
|
212
|
-
ProcessRequestMessage: __privateGet(this, _processPayload),
|
|
213
|
-
messageReceiverStart: (options) => {
|
|
214
|
-
__privateGet(this, _redisSubscriber).on("message", __privateGet(this, _processRawMessage));
|
|
215
|
-
},
|
|
216
|
-
messageReceiverStop: (options) => {
|
|
217
|
-
__privateGet(this, _redisSubscriber).off("message", __privateGet(this, _processRawMessage));
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
__privateSet(this, _messagingManager, new MessagingManager(ipcMessageManagerOptions));
|
|
221
|
-
});
|
|
222
|
-
__privateAdd(this, _messageSender, (payload, options) => {
|
|
223
|
-
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
224
|
-
__privateGet(this, _redisPublisher).publish(__privateGet(this, _requestChannel), JSON.stringify(payload));
|
|
225
|
-
} else if (payload.messageType.localeCompare("RESPONSE") === 0) {
|
|
226
|
-
__privateGet(this, _redisPublisher).publish(__privateGet(this, _responseChannel), JSON.stringify(payload));
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
__privateAdd(this, _ProcessResponseMessage, async (responses, options) => {
|
|
230
|
-
let found = true;
|
|
231
|
-
let requestGroup = null;
|
|
232
|
-
for (const [responseId, response] of Object.entries(responses)) {
|
|
233
|
-
if (response.requestPayload.args.length > 0 && response.requestPayload.args[0].group) {
|
|
234
|
-
requestGroup = response.requestPayload.args[0].group;
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (requestGroup) {
|
|
239
|
-
const clientsInGroup = Object.values(__privateGet(this, _clients)).filter((c) => {
|
|
240
|
-
if (c.groups.indexOf(requestGroup) === -1) {
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
return true;
|
|
244
|
-
});
|
|
245
|
-
found = true;
|
|
246
|
-
clientsInGroup.forEach((c) => {
|
|
247
|
-
if (!responses[c.id]) {
|
|
248
|
-
found = false;
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
} else {
|
|
252
|
-
const clientsInGroup = Object.values(__privateGet(this, _clients));
|
|
253
|
-
found = true;
|
|
254
|
-
clientsInGroup.forEach((c) => {
|
|
255
|
-
if (!responses[c.id]) {
|
|
256
|
-
found = false;
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
return found;
|
|
261
|
-
});
|
|
262
|
-
__privateAdd(this, _processPayload, (payload, options) => {
|
|
263
|
-
return new Promise((resolve, reject) => {
|
|
264
|
-
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
265
|
-
if (payload.requestPayload["__eventName"]) {
|
|
266
|
-
const eventName = payload.requestPayload["__eventName"];
|
|
267
|
-
if (__privateGet(this, _events)[eventName]) {
|
|
268
|
-
try {
|
|
269
|
-
if (payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
270
|
-
__privateGet(this, _events)[eventName].callback(...payload.requestPayload.args);
|
|
271
|
-
resolve({});
|
|
272
|
-
} else {
|
|
273
|
-
__privateGet(this, _events)[eventName].callback(...payload.requestPayload.args, (responseMessage) => {
|
|
274
|
-
resolve(responseMessage);
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
} catch (error) {
|
|
278
|
-
reject(error);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
__publicField(this, "Start", () => {
|
|
286
|
-
var _a;
|
|
287
|
-
(_a = __privateGet(this, _messagingManager)) == null ? void 0 : _a.Start();
|
|
288
|
-
});
|
|
289
|
-
__publicField(this, "Stop", () => {
|
|
290
|
-
var _a;
|
|
291
|
-
if (__privateGet(this, _pingTimeout)) {
|
|
292
|
-
clearTimeout(__privateGet(this, _pingTimeout));
|
|
293
|
-
__privateSet(this, _pingTimeout, null);
|
|
294
|
-
}
|
|
295
|
-
(_a = __privateGet(this, _messagingManager)) == null ? void 0 : _a.Stop();
|
|
296
|
-
__privateGet(this, _redisSubscriber).quit();
|
|
297
|
-
__privateGet(this, _redisSubscriber).disconnect();
|
|
298
|
-
__privateGet(this, _redisPublisher).quit();
|
|
299
|
-
__privateGet(this, _redisPublisher).disconnect();
|
|
300
|
-
});
|
|
301
|
-
__publicField(this, "emitex", async (event, ...args) => {
|
|
302
|
-
return __privateGet(this, _messagingManager).SendMessage({
|
|
303
|
-
__eventName: event,
|
|
304
|
-
args
|
|
305
|
-
});
|
|
306
|
-
});
|
|
307
|
-
__publicField(this, "emitNoResponse", async (event, ...args) => {
|
|
308
|
-
__privateGet(this, _messagingManager).SendMessageNoResponse({
|
|
309
|
-
__eventName: event,
|
|
310
|
-
args
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
__privateSet(this, _options2, options);
|
|
314
|
-
__privateSet(this, _requestChannel, REQUEST_CHANNEL);
|
|
315
|
-
__privateSet(this, _responseChannel, RESPONSE_CHANNEL);
|
|
168
|
+
this.#options = options;
|
|
169
|
+
this.#requestChannel = REQUEST_CHANNEL;
|
|
170
|
+
this.#responseChannel = RESPONSE_CHANNEL;
|
|
316
171
|
const redisOptions = {
|
|
317
172
|
showFriendlyErrorStack: true,
|
|
318
173
|
maxRetriesPerRequest: 20
|
|
319
174
|
};
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
175
|
+
this.#redisSubscriber = new ioredis.Redis(this.#options.redisUrl, redisOptions);
|
|
176
|
+
this.#redisPublisher = new ioredis.Redis(this.#options.redisUrl, redisOptions);
|
|
177
|
+
this.#redisSubscriber.on("error", (error) => {
|
|
178
|
+
this.#LogError(chalk.red(`RedisMessageHandler:constructor(): PID: [${process.pid}] Role: [${this.#options.role}] Error (redisSubscriber on error): [${error}]`));
|
|
324
179
|
});
|
|
325
|
-
|
|
326
|
-
|
|
180
|
+
this.#redisPublisher.on("error", (error) => {
|
|
181
|
+
this.#LogError(chalk.red(`RedisMessageHandler:constructor(): PID: [${process.pid}] Role: [${this.#options.role}] Error (redisPublisher on error): [${error}]`));
|
|
327
182
|
});
|
|
328
|
-
|
|
183
|
+
this.#redisSubscriber.subscribe(this.#requestChannel, this.#responseChannel, (error, count) => {
|
|
329
184
|
if (error) {
|
|
330
|
-
|
|
185
|
+
this.#LogError(chalk.red(`RedisMessageHandler:constructor(): PID: [${process.pid}] Role: [${this.#options.role}] Error - Failed to subscribe: [${error}]`));
|
|
331
186
|
} else {
|
|
332
|
-
|
|
187
|
+
this.#LogInfo(chalk.white(`RedisMessageHandler:constructor(): PID: [${process.pid}] Role: [${this.#options.role}] Subscribed successfully! This client is currently subscribed to ${count} channels.`));
|
|
333
188
|
}
|
|
334
189
|
});
|
|
335
190
|
this.SetupPrimary();
|
|
336
|
-
if (
|
|
191
|
+
if (this.#options.role.localeCompare("CLIENT") === 0) {
|
|
337
192
|
const ping = () => {
|
|
338
|
-
|
|
193
|
+
this.#pingTimeout = setTimeout(() => {
|
|
339
194
|
const pingData = {
|
|
340
|
-
id:
|
|
341
|
-
groups:
|
|
195
|
+
id: this.#messagingManager.id,
|
|
196
|
+
groups: this.#options.groups
|
|
342
197
|
};
|
|
343
|
-
if (
|
|
344
|
-
pingData.extraData =
|
|
198
|
+
if (this.#options.extraData) {
|
|
199
|
+
pingData.extraData = this.#options.extraData;
|
|
345
200
|
}
|
|
346
201
|
this.emitNoResponse("ping", pingData);
|
|
347
202
|
ping();
|
|
348
|
-
}, 1e3).unref()
|
|
203
|
+
}, 1e3).unref();
|
|
349
204
|
};
|
|
350
205
|
ping();
|
|
351
206
|
} else {
|
|
352
207
|
this.on("ping", (pingData, callback) => {
|
|
353
208
|
const { id, groups, extraData } = pingData;
|
|
354
|
-
if (
|
|
355
|
-
clearTimeout(
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
delete
|
|
209
|
+
if (this.#clients[id]) {
|
|
210
|
+
clearTimeout(this.#clients[id].timeout);
|
|
211
|
+
this.#clients[id].pingCount++;
|
|
212
|
+
this.#clients[id].timeout = setTimeout(() => {
|
|
213
|
+
delete this.#clients[id];
|
|
359
214
|
}, 2e3);
|
|
360
|
-
|
|
361
|
-
|
|
215
|
+
this.#clients[id].groups = groups;
|
|
216
|
+
this.#clients[id].extraData = extraData;
|
|
362
217
|
} else {
|
|
363
|
-
|
|
218
|
+
this.#clients[id] = {
|
|
364
219
|
id,
|
|
365
220
|
clientConnected: /* @__PURE__ */ new Date(),
|
|
366
221
|
pingCount: 0,
|
|
367
222
|
timeout: setTimeout(() => {
|
|
368
|
-
delete
|
|
223
|
+
delete this.#clients[id];
|
|
369
224
|
}, 2e3),
|
|
370
225
|
groups,
|
|
371
226
|
extraData
|
|
@@ -374,63 +229,182 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
|
|
|
374
229
|
});
|
|
375
230
|
}
|
|
376
231
|
}
|
|
232
|
+
#LogInfo(message) {
|
|
233
|
+
this.#options.logger.info(message);
|
|
234
|
+
}
|
|
235
|
+
#LogError(message) {
|
|
236
|
+
this.#options.logger.error(message);
|
|
237
|
+
}
|
|
238
|
+
#processRawMessage = (channel, rawmessage) => {
|
|
239
|
+
const message = JSON.parse(rawmessage);
|
|
240
|
+
this.#messagingManager?.ProcessMessage(message, { channel });
|
|
241
|
+
};
|
|
377
242
|
get clients() {
|
|
378
|
-
return
|
|
243
|
+
return this.#clients;
|
|
379
244
|
}
|
|
380
245
|
get groups() {
|
|
381
|
-
return
|
|
246
|
+
return this.#options.groups;
|
|
382
247
|
}
|
|
248
|
+
AddGroup = (group) => {
|
|
249
|
+
const index = this.#options.groups.indexOf(group);
|
|
250
|
+
if (index === -1) {
|
|
251
|
+
this.#options.groups.push(group);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
RemoveGroup = (group) => {
|
|
255
|
+
const removeIndex = this.#options.groups.indexOf(group);
|
|
256
|
+
if (removeIndex !== -1) {
|
|
257
|
+
this.#options.groups.splice(removeIndex, 1);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
SetupPrimary = () => {
|
|
261
|
+
const ipcMessageManagerOptions = {
|
|
262
|
+
logger: this.#options.logger,
|
|
263
|
+
requestResponseMessageTimeout: 5e3,
|
|
264
|
+
namespace: this.#options.namespace,
|
|
265
|
+
role: this.#options.role,
|
|
266
|
+
groups: this.#options.groups,
|
|
267
|
+
messageSender: this.#messageSender,
|
|
268
|
+
// This method is used to calculate if all responses have been received from multiple clients (broadcast)
|
|
269
|
+
// returns true/false.
|
|
270
|
+
ProcessResponseMessage: this.#ProcessResponseMessage,
|
|
271
|
+
// This gets called when an event is received from a message receiver (when ProcessMessage is invoked from the receiver event handler)
|
|
272
|
+
ProcessRequestMessage: this.#processPayload,
|
|
273
|
+
messageReceiverStart: (options) => {
|
|
274
|
+
this.#redisSubscriber.on("message", this.#processRawMessage);
|
|
275
|
+
},
|
|
276
|
+
messageReceiverStop: (options) => {
|
|
277
|
+
this.#redisSubscriber.off("message", this.#processRawMessage);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
this.#messagingManager = new MessagingManager(ipcMessageManagerOptions);
|
|
281
|
+
};
|
|
282
|
+
#messageSender = (payload, options) => {
|
|
283
|
+
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
284
|
+
this.#redisPublisher.publish(this.#requestChannel, JSON.stringify(payload));
|
|
285
|
+
} else if (payload.messageType.localeCompare("RESPONSE") === 0) {
|
|
286
|
+
this.#redisPublisher.publish(this.#responseChannel, JSON.stringify(payload));
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
#ProcessResponseMessage = async (responses, options) => {
|
|
290
|
+
let found = true;
|
|
291
|
+
let requestGroup = null;
|
|
292
|
+
for (const [responseId, response] of Object.entries(responses)) {
|
|
293
|
+
if (response.requestPayload.args.length > 0 && response.requestPayload.args[0].group) {
|
|
294
|
+
requestGroup = response.requestPayload.args[0].group;
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (requestGroup) {
|
|
299
|
+
const clientsInGroup = Object.values(this.#clients).filter((c) => {
|
|
300
|
+
if (c.groups.indexOf(requestGroup) === -1) {
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
return true;
|
|
304
|
+
});
|
|
305
|
+
found = true;
|
|
306
|
+
clientsInGroup.forEach((c) => {
|
|
307
|
+
if (!responses[c.id]) {
|
|
308
|
+
found = false;
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
} else {
|
|
312
|
+
const clientsInGroup = Object.values(this.#clients);
|
|
313
|
+
found = true;
|
|
314
|
+
clientsInGroup.forEach((c) => {
|
|
315
|
+
if (!responses[c.id]) {
|
|
316
|
+
found = false;
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return found;
|
|
321
|
+
};
|
|
322
|
+
#processPayload = (payload, options) => {
|
|
323
|
+
return new Promise((resolve, reject) => {
|
|
324
|
+
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
325
|
+
if (payload.requestPayload["__eventName"]) {
|
|
326
|
+
const eventName = payload.requestPayload["__eventName"];
|
|
327
|
+
if (this.#events[eventName]) {
|
|
328
|
+
try {
|
|
329
|
+
if (payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
330
|
+
this.#events[eventName].callback(...payload.requestPayload.args);
|
|
331
|
+
resolve({});
|
|
332
|
+
} else {
|
|
333
|
+
this.#events[eventName].callback(...payload.requestPayload.args, (responseMessage) => {
|
|
334
|
+
resolve(responseMessage);
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
} catch (error) {
|
|
338
|
+
reject(error);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
};
|
|
383
345
|
// p.on('fromworker', (arg1: number, arg2: string, callback: any) => {
|
|
384
346
|
on(event, callback, ctx) {
|
|
385
|
-
if (
|
|
386
|
-
delete
|
|
347
|
+
if (this.#events[event]) {
|
|
348
|
+
delete this.#events[event];
|
|
387
349
|
}
|
|
388
350
|
const eventObject = {
|
|
389
351
|
event,
|
|
390
352
|
callback,
|
|
391
353
|
ctx
|
|
392
354
|
};
|
|
393
|
-
|
|
355
|
+
this.#events[eventObject.event] = eventObject;
|
|
394
356
|
return this;
|
|
395
357
|
}
|
|
396
358
|
off(event, callback) {
|
|
397
|
-
if (
|
|
398
|
-
delete
|
|
359
|
+
if (this.#events[event]) {
|
|
360
|
+
delete this.#events[event];
|
|
399
361
|
}
|
|
400
362
|
return this;
|
|
401
363
|
}
|
|
364
|
+
Start = () => {
|
|
365
|
+
this.#messagingManager?.Start();
|
|
366
|
+
};
|
|
367
|
+
Stop = () => {
|
|
368
|
+
if (this.#pingTimeout) {
|
|
369
|
+
clearTimeout(this.#pingTimeout);
|
|
370
|
+
this.#pingTimeout = null;
|
|
371
|
+
}
|
|
372
|
+
this.#messagingManager?.Stop();
|
|
373
|
+
this.#redisSubscriber.quit();
|
|
374
|
+
this.#redisSubscriber.disconnect();
|
|
375
|
+
this.#redisPublisher.quit();
|
|
376
|
+
this.#redisPublisher.disconnect();
|
|
377
|
+
};
|
|
402
378
|
emit(event, ...args) {
|
|
403
379
|
(async () => {
|
|
404
|
-
var _a;
|
|
405
380
|
try {
|
|
406
|
-
const retVal = await
|
|
381
|
+
const retVal = await this.#messagingManager?.SendMessage({
|
|
407
382
|
__eventName: event,
|
|
408
383
|
args: args.slice(0, args.length - 1)
|
|
409
|
-
})
|
|
384
|
+
});
|
|
410
385
|
args[args.length - 1](retVal);
|
|
411
386
|
} catch (error) {
|
|
412
|
-
if (
|
|
413
|
-
if (
|
|
387
|
+
if (this.#options.ignoreEvents) {
|
|
388
|
+
if (this.#options.ignoreEvents.indexOf(error.__eventName) !== -1) {
|
|
414
389
|
return;
|
|
415
390
|
}
|
|
416
391
|
}
|
|
417
|
-
|
|
392
|
+
this.#options.logger.error(chalk.red(`RedisMessageHandler:emit(): Error: [${JSON.stringify(error)}]`));
|
|
418
393
|
}
|
|
419
394
|
})();
|
|
420
395
|
return this;
|
|
421
396
|
}
|
|
422
397
|
emitWithError(event, args, responseCb, errorCb) {
|
|
423
398
|
(async () => {
|
|
424
|
-
var _a;
|
|
425
399
|
try {
|
|
426
|
-
const retVal = await
|
|
400
|
+
const retVal = await this.#messagingManager?.SendMessage({
|
|
427
401
|
__eventName: event,
|
|
428
402
|
args: [args]
|
|
429
|
-
})
|
|
403
|
+
});
|
|
430
404
|
responseCb(retVal);
|
|
431
405
|
} catch (error) {
|
|
432
|
-
if (
|
|
433
|
-
if (
|
|
406
|
+
if (this.#options.ignoreEvents) {
|
|
407
|
+
if (this.#options.ignoreEvents.indexOf(error.__eventName) !== -1) {
|
|
434
408
|
return;
|
|
435
409
|
}
|
|
436
410
|
}
|
|
@@ -439,176 +413,28 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
|
|
|
439
413
|
})();
|
|
440
414
|
return this;
|
|
441
415
|
}
|
|
416
|
+
emitex = async (event, ...args) => {
|
|
417
|
+
return this.#messagingManager.SendMessage({
|
|
418
|
+
__eventName: event,
|
|
419
|
+
args
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
emitNoResponse = async (event, ...args) => {
|
|
423
|
+
this.#messagingManager.SendMessageNoResponse({
|
|
424
|
+
__eventName: event,
|
|
425
|
+
args
|
|
426
|
+
});
|
|
427
|
+
};
|
|
442
428
|
}
|
|
443
|
-
_messagingManager = new WeakMap();
|
|
444
|
-
_options2 = new WeakMap();
|
|
445
|
-
_events = new WeakMap();
|
|
446
|
-
_requestChannel = new WeakMap();
|
|
447
|
-
_responseChannel = new WeakMap();
|
|
448
|
-
_redisSubscriber = new WeakMap();
|
|
449
|
-
_redisPublisher = new WeakMap();
|
|
450
|
-
_clients = new WeakMap();
|
|
451
|
-
_pingTimeout = new WeakMap();
|
|
452
|
-
_RedisMessageHandler_instances = new WeakSet();
|
|
453
|
-
LogInfo_fn = function(message) {
|
|
454
|
-
__privateGet(this, _options2).logger.info(message);
|
|
455
|
-
};
|
|
456
|
-
LogError_fn = function(message) {
|
|
457
|
-
__privateGet(this, _options2).logger.error(message);
|
|
458
|
-
};
|
|
459
|
-
_processRawMessage = new WeakMap();
|
|
460
|
-
_messageSender = new WeakMap();
|
|
461
|
-
_ProcessResponseMessage = new WeakMap();
|
|
462
|
-
_processPayload = new WeakMap();
|
|
463
429
|
class IPCMessageHandler extends tinyEmitter.TinyEmitter {
|
|
430
|
+
#messagingManager = null;
|
|
431
|
+
#options;
|
|
432
|
+
#clients = {};
|
|
433
|
+
#events = {};
|
|
434
|
+
#startWorkerOptions;
|
|
464
435
|
constructor(options) {
|
|
465
436
|
super();
|
|
466
|
-
|
|
467
|
-
__privateAdd(this, _options3);
|
|
468
|
-
__privateAdd(this, _clients2, {});
|
|
469
|
-
__privateAdd(this, _events2, {});
|
|
470
|
-
__privateAdd(this, _startWorkerOptions);
|
|
471
|
-
__publicField(this, "AddClient", (client) => {
|
|
472
|
-
const id = uuid.v4();
|
|
473
|
-
const processMessage = (payload) => {
|
|
474
|
-
var _a;
|
|
475
|
-
(_a = __privateGet(this, _messagingManager2)) == null ? void 0 : _a.ProcessMessage(payload, { client });
|
|
476
|
-
};
|
|
477
|
-
client.on("message", processMessage);
|
|
478
|
-
__privateGet(this, _clients2)[id] = {
|
|
479
|
-
client,
|
|
480
|
-
processMessage
|
|
481
|
-
};
|
|
482
|
-
return id;
|
|
483
|
-
});
|
|
484
|
-
__publicField(this, "RemoveClient", (id) => {
|
|
485
|
-
const clientRecord = __privateGet(this, _clients2)[id];
|
|
486
|
-
if (clientRecord) {
|
|
487
|
-
clientRecord.client.off("message", clientRecord.processMessage);
|
|
488
|
-
delete __privateGet(this, _clients2)[id];
|
|
489
|
-
}
|
|
490
|
-
});
|
|
491
|
-
__publicField(this, "SetupPrimary", () => {
|
|
492
|
-
const ipcMessageManagerOptions = {
|
|
493
|
-
logger: stsutils.defaultLogger,
|
|
494
|
-
requestResponseMessageTimeout: 5e3,
|
|
495
|
-
namespace: __privateGet(this, _options3).namespace,
|
|
496
|
-
role: "SERVER",
|
|
497
|
-
messageSender: (payload, options) => {
|
|
498
|
-
options.client.send(payload);
|
|
499
|
-
},
|
|
500
|
-
ProcessRequestMessage: async (payload, options) => {
|
|
501
|
-
return __privateGet(this, _processPayload2).call(this, payload, options);
|
|
502
|
-
},
|
|
503
|
-
//@@ also need way to add/remove a worker/child to the collection
|
|
504
|
-
//@@ the ping should also auto remove (such as a failed worker)
|
|
505
|
-
messageReceiverStart: (options) => {
|
|
506
|
-
},
|
|
507
|
-
messageReceiverStop: (options) => {
|
|
508
|
-
},
|
|
509
|
-
groups: []
|
|
510
|
-
};
|
|
511
|
-
__privateSet(this, _messagingManager2, new MessagingManager(ipcMessageManagerOptions));
|
|
512
|
-
});
|
|
513
|
-
__privateAdd(this, _ProcessWorkerMessageRaw, (payload) => {
|
|
514
|
-
var _a;
|
|
515
|
-
(_a = __privateGet(this, _messagingManager2)) == null ? void 0 : _a.ProcessMessage(payload, __privateGet(this, _startWorkerOptions));
|
|
516
|
-
});
|
|
517
|
-
__publicField(this, "SetupWorker", () => {
|
|
518
|
-
const ipcMessageManagerOptions = {
|
|
519
|
-
logger: stsutils.defaultLogger,
|
|
520
|
-
requestResponseMessageTimeout: __privateGet(this, _options3).requestResponseMessageTimeout,
|
|
521
|
-
namespace: __privateGet(this, _options3).namespace,
|
|
522
|
-
role: "CLIENT",
|
|
523
|
-
messageSender: (payload, options) => {
|
|
524
|
-
process.send(payload);
|
|
525
|
-
},
|
|
526
|
-
ProcessRequestMessage: async (payload, options) => {
|
|
527
|
-
return __privateGet(this, _processPayload2).call(this, payload, options);
|
|
528
|
-
},
|
|
529
|
-
messageReceiverStart: (options) => {
|
|
530
|
-
__privateSet(this, _startWorkerOptions, { ...options });
|
|
531
|
-
process.on("message", __privateGet(this, _ProcessWorkerMessageRaw));
|
|
532
|
-
},
|
|
533
|
-
messageReceiverStop: (options) => {
|
|
534
|
-
process.off("message", __privateGet(this, _ProcessWorkerMessageRaw));
|
|
535
|
-
},
|
|
536
|
-
groups: []
|
|
537
|
-
};
|
|
538
|
-
__privateSet(this, _messagingManager2, new MessagingManager(ipcMessageManagerOptions));
|
|
539
|
-
});
|
|
540
|
-
__publicField(this, "SendMessage", async (payload) => {
|
|
541
|
-
var _a, _b;
|
|
542
|
-
if (__privateGet(this, _messagingManager2)) {
|
|
543
|
-
if (__privateGet(this, _options3).role.localeCompare("CLIENT") === 0) {
|
|
544
|
-
return (_a = __privateGet(this, _messagingManager2)) == null ? void 0 : _a.SendMessage(payload, {});
|
|
545
|
-
} else {
|
|
546
|
-
const promArray = [];
|
|
547
|
-
for (const [clientId, clientRecord] of Object.entries(__privateGet(this, _clients2))) {
|
|
548
|
-
promArray.push((_b = __privateGet(this, _messagingManager2)) == null ? void 0 : _b.SendMessage(payload, { client: clientRecord.client }));
|
|
549
|
-
}
|
|
550
|
-
try {
|
|
551
|
-
const retVal = await Promise.all(promArray);
|
|
552
|
-
return {
|
|
553
|
-
result: retVal
|
|
554
|
-
};
|
|
555
|
-
} catch (error) {
|
|
556
|
-
return {};
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
} else {
|
|
560
|
-
return {};
|
|
561
|
-
}
|
|
562
|
-
});
|
|
563
|
-
__privateAdd(this, _processPayload2, (payload, options) => {
|
|
564
|
-
return new Promise((resolve, reject) => {
|
|
565
|
-
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
566
|
-
if (payload.requestPayload["__eventName"]) {
|
|
567
|
-
const eventName = payload.requestPayload["__eventName"];
|
|
568
|
-
if (__privateGet(this, _events2)[eventName]) {
|
|
569
|
-
try {
|
|
570
|
-
if (payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
571
|
-
__privateGet(this, _events2)[eventName].callback(...payload.requestPayload.args);
|
|
572
|
-
resolve({});
|
|
573
|
-
} else {
|
|
574
|
-
__privateGet(this, _events2)[eventName].callback(...payload.requestPayload.args, (responseMessage) => {
|
|
575
|
-
resolve(responseMessage);
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
} catch (error) {
|
|
579
|
-
reject(error);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
});
|
|
585
|
-
});
|
|
586
|
-
// Supply complete collection of workers
|
|
587
|
-
__publicField(this, "Start", () => {
|
|
588
|
-
var _a;
|
|
589
|
-
(_a = __privateGet(this, _messagingManager2)) == null ? void 0 : _a.Start({});
|
|
590
|
-
});
|
|
591
|
-
// Supply complete collection of workers
|
|
592
|
-
__publicField(this, "Stop", () => {
|
|
593
|
-
var _a;
|
|
594
|
-
(_a = __privateGet(this, _messagingManager2)) == null ? void 0 : _a.Stop({});
|
|
595
|
-
});
|
|
596
|
-
__publicField(this, "emitNoResponse", async (event, ...args) => {
|
|
597
|
-
if (__privateGet(this, _options3).role.localeCompare("CLIENT") === 0) {
|
|
598
|
-
__privateGet(this, _messagingManager2).SendMessageNoResponse({
|
|
599
|
-
__eventName: event,
|
|
600
|
-
args
|
|
601
|
-
});
|
|
602
|
-
} else {
|
|
603
|
-
for (const [clientId, clientRecord] of Object.entries(__privateGet(this, _clients2))) {
|
|
604
|
-
__privateGet(this, _messagingManager2).SendMessageNoResponse({
|
|
605
|
-
__eventName: event,
|
|
606
|
-
args
|
|
607
|
-
}, { client: clientRecord.client });
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
});
|
|
611
|
-
__privateSet(this, _options3, options);
|
|
437
|
+
this.#options = options;
|
|
612
438
|
if (options.role.localeCompare("CLIENT") === 0) {
|
|
613
439
|
this.SetupWorker();
|
|
614
440
|
} else {
|
|
@@ -616,30 +442,150 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
|
|
|
616
442
|
}
|
|
617
443
|
}
|
|
618
444
|
get __events() {
|
|
619
|
-
return
|
|
445
|
+
return this.#events;
|
|
620
446
|
}
|
|
447
|
+
AddClient = (client) => {
|
|
448
|
+
const id = uuid.v4();
|
|
449
|
+
const processMessage = (payload) => {
|
|
450
|
+
this.#messagingManager?.ProcessMessage(payload, { client });
|
|
451
|
+
};
|
|
452
|
+
client.on("message", processMessage);
|
|
453
|
+
this.#clients[id] = {
|
|
454
|
+
client,
|
|
455
|
+
processMessage
|
|
456
|
+
};
|
|
457
|
+
return id;
|
|
458
|
+
};
|
|
459
|
+
RemoveClient = (id) => {
|
|
460
|
+
const clientRecord = this.#clients[id];
|
|
461
|
+
if (clientRecord) {
|
|
462
|
+
clientRecord.client.off("message", clientRecord.processMessage);
|
|
463
|
+
delete this.#clients[id];
|
|
464
|
+
}
|
|
465
|
+
};
|
|
621
466
|
get clients() {
|
|
622
|
-
return
|
|
467
|
+
return this.#clients;
|
|
623
468
|
}
|
|
469
|
+
SetupPrimary = () => {
|
|
470
|
+
const ipcMessageManagerOptions = {
|
|
471
|
+
logger: stsutils.defaultLogger,
|
|
472
|
+
requestResponseMessageTimeout: 5e3,
|
|
473
|
+
namespace: this.#options.namespace,
|
|
474
|
+
role: "SERVER",
|
|
475
|
+
messageSender: (payload, options) => {
|
|
476
|
+
options.client.send(payload);
|
|
477
|
+
},
|
|
478
|
+
ProcessRequestMessage: async (payload, options) => {
|
|
479
|
+
return this.#processPayload(payload, options);
|
|
480
|
+
},
|
|
481
|
+
//@@ also need way to add/remove a worker/child to the collection
|
|
482
|
+
//@@ the ping should also auto remove (such as a failed worker)
|
|
483
|
+
messageReceiverStart: (options) => {
|
|
484
|
+
},
|
|
485
|
+
messageReceiverStop: (options) => {
|
|
486
|
+
},
|
|
487
|
+
groups: []
|
|
488
|
+
};
|
|
489
|
+
this.#messagingManager = new MessagingManager(ipcMessageManagerOptions);
|
|
490
|
+
};
|
|
491
|
+
#ProcessWorkerMessageRaw = (payload) => {
|
|
492
|
+
this.#messagingManager?.ProcessMessage(payload, this.#startWorkerOptions);
|
|
493
|
+
};
|
|
494
|
+
SetupWorker = () => {
|
|
495
|
+
const ipcMessageManagerOptions = {
|
|
496
|
+
logger: stsutils.defaultLogger,
|
|
497
|
+
requestResponseMessageTimeout: this.#options.requestResponseMessageTimeout,
|
|
498
|
+
namespace: this.#options.namespace,
|
|
499
|
+
role: "CLIENT",
|
|
500
|
+
messageSender: (payload, options) => {
|
|
501
|
+
process.send(payload);
|
|
502
|
+
},
|
|
503
|
+
ProcessRequestMessage: async (payload, options) => {
|
|
504
|
+
return this.#processPayload(payload, options);
|
|
505
|
+
},
|
|
506
|
+
messageReceiverStart: (options) => {
|
|
507
|
+
this.#startWorkerOptions = { ...options };
|
|
508
|
+
process.on("message", this.#ProcessWorkerMessageRaw);
|
|
509
|
+
},
|
|
510
|
+
messageReceiverStop: (options) => {
|
|
511
|
+
process.off("message", this.#ProcessWorkerMessageRaw);
|
|
512
|
+
},
|
|
513
|
+
groups: []
|
|
514
|
+
};
|
|
515
|
+
this.#messagingManager = new MessagingManager(ipcMessageManagerOptions);
|
|
516
|
+
};
|
|
517
|
+
SendMessage = async (payload) => {
|
|
518
|
+
if (this.#messagingManager) {
|
|
519
|
+
if (this.#options.role.localeCompare("CLIENT") === 0) {
|
|
520
|
+
return this.#messagingManager?.SendMessage(payload, {});
|
|
521
|
+
} else {
|
|
522
|
+
const promArray = [];
|
|
523
|
+
for (const [clientId, clientRecord] of Object.entries(this.#clients)) {
|
|
524
|
+
promArray.push(this.#messagingManager?.SendMessage(payload, { client: clientRecord.client }));
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
const retVal = await Promise.all(promArray);
|
|
528
|
+
return {
|
|
529
|
+
result: retVal
|
|
530
|
+
};
|
|
531
|
+
} catch (error) {
|
|
532
|
+
return {};
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
} else {
|
|
536
|
+
return {};
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
#processPayload = (payload, options) => {
|
|
540
|
+
return new Promise((resolve, reject) => {
|
|
541
|
+
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
542
|
+
if (payload.requestPayload["__eventName"]) {
|
|
543
|
+
const eventName = payload.requestPayload["__eventName"];
|
|
544
|
+
if (this.#events[eventName]) {
|
|
545
|
+
try {
|
|
546
|
+
if (payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
547
|
+
this.#events[eventName].callback(...payload.requestPayload.args);
|
|
548
|
+
resolve({});
|
|
549
|
+
} else {
|
|
550
|
+
this.#events[eventName].callback(...payload.requestPayload.args, (responseMessage) => {
|
|
551
|
+
resolve(responseMessage);
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
} catch (error) {
|
|
555
|
+
reject(error);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
};
|
|
624
562
|
// p.on('fromworker', (arg1: number, arg2: string, callback: any) => {
|
|
625
563
|
on(event, callback, ctx) {
|
|
626
|
-
if (
|
|
627
|
-
delete
|
|
564
|
+
if (this.#events[event]) {
|
|
565
|
+
delete this.#events[event];
|
|
628
566
|
}
|
|
629
567
|
const eventObject = {
|
|
630
568
|
event,
|
|
631
569
|
callback,
|
|
632
570
|
ctx
|
|
633
571
|
};
|
|
634
|
-
|
|
572
|
+
this.#events[eventObject.event] = eventObject;
|
|
635
573
|
return this;
|
|
636
574
|
}
|
|
637
575
|
off(event, callback) {
|
|
638
|
-
if (
|
|
639
|
-
delete
|
|
576
|
+
if (this.#events[event]) {
|
|
577
|
+
delete this.#events[event];
|
|
640
578
|
}
|
|
641
579
|
return this;
|
|
642
580
|
}
|
|
581
|
+
// Supply complete collection of workers
|
|
582
|
+
Start = () => {
|
|
583
|
+
this.#messagingManager?.Start({});
|
|
584
|
+
};
|
|
585
|
+
// Supply complete collection of workers
|
|
586
|
+
Stop = () => {
|
|
587
|
+
this.#messagingManager?.Stop({});
|
|
588
|
+
};
|
|
643
589
|
/*
|
|
644
590
|
override emit(event: string, ...args: any[]): this {
|
|
645
591
|
const sendMessage = async () => {
|
|
@@ -664,154 +610,43 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
|
|
|
664
610
|
});
|
|
665
611
|
args[args.length - 1](retVal);
|
|
666
612
|
} catch (error) {
|
|
667
|
-
if (
|
|
668
|
-
if (
|
|
613
|
+
if (this.#options.ignoreEvents) {
|
|
614
|
+
if (this.#options.ignoreEvents.indexOf(error.__eventName) !== -1) {
|
|
669
615
|
return;
|
|
670
616
|
}
|
|
671
617
|
}
|
|
672
618
|
console.log(error);
|
|
673
|
-
|
|
619
|
+
this.#options.logger.error(chalk.red(`IPCMessageHandler:emit(): Error: [${JSON.stringify(error)}]`));
|
|
674
620
|
}
|
|
675
621
|
})();
|
|
676
622
|
return this;
|
|
677
623
|
}
|
|
624
|
+
emitNoResponse = async (event, ...args) => {
|
|
625
|
+
if (this.#options.role.localeCompare("CLIENT") === 0) {
|
|
626
|
+
this.#messagingManager.SendMessageNoResponse({
|
|
627
|
+
__eventName: event,
|
|
628
|
+
args
|
|
629
|
+
});
|
|
630
|
+
} else {
|
|
631
|
+
for (const [clientId, clientRecord] of Object.entries(this.#clients)) {
|
|
632
|
+
this.#messagingManager.SendMessageNoResponse({
|
|
633
|
+
__eventName: event,
|
|
634
|
+
args
|
|
635
|
+
}, { client: clientRecord.client });
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
};
|
|
678
639
|
}
|
|
679
|
-
_messagingManager2 = new WeakMap();
|
|
680
|
-
_options3 = new WeakMap();
|
|
681
|
-
_clients2 = new WeakMap();
|
|
682
|
-
_events2 = new WeakMap();
|
|
683
|
-
_startWorkerOptions = new WeakMap();
|
|
684
|
-
_ProcessWorkerMessageRaw = new WeakMap();
|
|
685
|
-
_processPayload2 = new WeakMap();
|
|
686
640
|
class IPCMessageHandlerPair extends tinyEmitter.TinyEmitter {
|
|
641
|
+
#messagingManager = null;
|
|
642
|
+
#options;
|
|
643
|
+
#worker = null;
|
|
644
|
+
#events = {};
|
|
645
|
+
#startWorkerOptions;
|
|
646
|
+
#startPrimaryWorker = null;
|
|
687
647
|
constructor(options) {
|
|
688
648
|
super();
|
|
689
|
-
|
|
690
|
-
__privateAdd(this, _options4);
|
|
691
|
-
__privateAdd(this, _worker, null);
|
|
692
|
-
__privateAdd(this, _events3, {});
|
|
693
|
-
__privateAdd(this, _startWorkerOptions2);
|
|
694
|
-
__privateAdd(this, _startPrimaryWorker, null);
|
|
695
|
-
__privateAdd(this, _ProcessPrimaryMessageRaw, (payload) => {
|
|
696
|
-
var _a;
|
|
697
|
-
(_a = __privateGet(this, _messagingManager3)) == null ? void 0 : _a.ProcessMessage(payload, { worker: __privateGet(this, _startPrimaryWorker) });
|
|
698
|
-
});
|
|
699
|
-
__publicField(this, "SetupPrimary", () => {
|
|
700
|
-
const ipcMessageManagerOptions = {
|
|
701
|
-
logger: stsutils.defaultLogger,
|
|
702
|
-
requestResponseMessageTimeout: 5e3,
|
|
703
|
-
namespace: __privateGet(this, _options4).namespace,
|
|
704
|
-
role: "SERVER",
|
|
705
|
-
messageSender: (payload, options) => {
|
|
706
|
-
options.worker.send(payload);
|
|
707
|
-
},
|
|
708
|
-
ProcessRequestMessage: async (payload, options) => {
|
|
709
|
-
return __privateGet(this, _processPayload3).call(this, payload, options);
|
|
710
|
-
},
|
|
711
|
-
//@@ also need way to add/remove a worker/child to the collection
|
|
712
|
-
//@@ the ping should also auto remove (such as a failed worker)
|
|
713
|
-
messageReceiverStart: (options) => {
|
|
714
|
-
const worker = options.worker;
|
|
715
|
-
__privateSet(this, _startPrimaryWorker, worker);
|
|
716
|
-
worker.on("message", __privateGet(this, _ProcessPrimaryMessageRaw));
|
|
717
|
-
},
|
|
718
|
-
messageReceiverStop: (options) => {
|
|
719
|
-
const worker = options.worker;
|
|
720
|
-
worker.off("message", __privateGet(this, _ProcessPrimaryMessageRaw));
|
|
721
|
-
},
|
|
722
|
-
groups: []
|
|
723
|
-
};
|
|
724
|
-
__privateSet(this, _messagingManager3, new MessagingManager(ipcMessageManagerOptions));
|
|
725
|
-
});
|
|
726
|
-
__privateAdd(this, _ProcessWorkerMessageRaw2, (payload) => {
|
|
727
|
-
var _a;
|
|
728
|
-
(_a = __privateGet(this, _messagingManager3)) == null ? void 0 : _a.ProcessMessage(payload, __privateGet(this, _startWorkerOptions2));
|
|
729
|
-
});
|
|
730
|
-
__publicField(this, "SetupWorker", () => {
|
|
731
|
-
const ipcMessageManagerOptions = {
|
|
732
|
-
logger: stsutils.defaultLogger,
|
|
733
|
-
requestResponseMessageTimeout: __privateGet(this, _options4).requestResponseMessageTimeout,
|
|
734
|
-
namespace: __privateGet(this, _options4).namespace,
|
|
735
|
-
role: "CLIENT",
|
|
736
|
-
messageSender: (payload, options) => {
|
|
737
|
-
process.send(payload);
|
|
738
|
-
},
|
|
739
|
-
ProcessRequestMessage: async (payload, options) => {
|
|
740
|
-
return __privateGet(this, _processPayload3).call(this, payload, options);
|
|
741
|
-
},
|
|
742
|
-
messageReceiverStart: (options) => {
|
|
743
|
-
__privateSet(this, _startWorkerOptions2, { ...options });
|
|
744
|
-
process.on("message", __privateGet(this, _ProcessWorkerMessageRaw2));
|
|
745
|
-
},
|
|
746
|
-
messageReceiverStop: (options) => {
|
|
747
|
-
process.off("message", __privateGet(this, _ProcessWorkerMessageRaw2));
|
|
748
|
-
},
|
|
749
|
-
groups: []
|
|
750
|
-
};
|
|
751
|
-
__privateSet(this, _messagingManager3, new MessagingManager(ipcMessageManagerOptions));
|
|
752
|
-
});
|
|
753
|
-
__publicField(this, "SendMessage", async (payload) => {
|
|
754
|
-
var _a, _b;
|
|
755
|
-
if (__privateGet(this, _messagingManager3)) {
|
|
756
|
-
if (__privateGet(this, _worker)) {
|
|
757
|
-
return (_a = __privateGet(this, _messagingManager3)) == null ? void 0 : _a.SendMessage(payload, { worker: __privateGet(this, _worker) });
|
|
758
|
-
} else {
|
|
759
|
-
return (_b = __privateGet(this, _messagingManager3)) == null ? void 0 : _b.SendMessage(payload, {});
|
|
760
|
-
}
|
|
761
|
-
} else {
|
|
762
|
-
return {};
|
|
763
|
-
}
|
|
764
|
-
});
|
|
765
|
-
__privateAdd(this, _processPayload3, (payload, options) => {
|
|
766
|
-
return new Promise((resolve, reject) => {
|
|
767
|
-
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
768
|
-
if (payload.requestPayload["__eventName"]) {
|
|
769
|
-
const eventName = payload.requestPayload["__eventName"];
|
|
770
|
-
if (__privateGet(this, _events3)[eventName]) {
|
|
771
|
-
try {
|
|
772
|
-
if (payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
773
|
-
__privateGet(this, _events3)[eventName].callback(...payload.requestPayload.args);
|
|
774
|
-
resolve({});
|
|
775
|
-
} else {
|
|
776
|
-
__privateGet(this, _events3)[eventName].callback(...payload.requestPayload.args, (responseMessage) => {
|
|
777
|
-
resolve(responseMessage);
|
|
778
|
-
});
|
|
779
|
-
}
|
|
780
|
-
} catch (error) {
|
|
781
|
-
reject(error);
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
});
|
|
787
|
-
});
|
|
788
|
-
// Supply complete collection of workers
|
|
789
|
-
__publicField(this, "Start", (worker) => {
|
|
790
|
-
var _a, _b;
|
|
791
|
-
if (worker) {
|
|
792
|
-
(_a = __privateGet(this, _messagingManager3)) == null ? void 0 : _a.Start({ worker });
|
|
793
|
-
__privateSet(this, _worker, worker);
|
|
794
|
-
} else {
|
|
795
|
-
(_b = __privateGet(this, _messagingManager3)) == null ? void 0 : _b.Start({});
|
|
796
|
-
}
|
|
797
|
-
});
|
|
798
|
-
// Supply complete collection of workers
|
|
799
|
-
__publicField(this, "Stop", () => {
|
|
800
|
-
var _a, _b;
|
|
801
|
-
if (__privateGet(this, _worker)) {
|
|
802
|
-
(_a = __privateGet(this, _messagingManager3)) == null ? void 0 : _a.Stop({ worker: __privateGet(this, _worker) });
|
|
803
|
-
__privateSet(this, _worker, null);
|
|
804
|
-
} else {
|
|
805
|
-
(_b = __privateGet(this, _messagingManager3)) == null ? void 0 : _b.Stop({});
|
|
806
|
-
}
|
|
807
|
-
});
|
|
808
|
-
__publicField(this, "emitNoResponse", async (event, ...args) => {
|
|
809
|
-
__privateGet(this, _messagingManager3).SendMessageNoResponse({
|
|
810
|
-
__eventName: event,
|
|
811
|
-
args
|
|
812
|
-
});
|
|
813
|
-
});
|
|
814
|
-
__privateSet(this, _options4, options);
|
|
649
|
+
this.#options = options;
|
|
815
650
|
if (options.role.localeCompare("CLIENT") === 0) {
|
|
816
651
|
this.SetupWorker();
|
|
817
652
|
} else {
|
|
@@ -819,29 +654,137 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
|
|
|
819
654
|
}
|
|
820
655
|
}
|
|
821
656
|
get __events() {
|
|
822
|
-
return
|
|
657
|
+
return this.#events;
|
|
823
658
|
}
|
|
659
|
+
#ProcessPrimaryMessageRaw = (payload) => {
|
|
660
|
+
this.#messagingManager?.ProcessMessage(payload, { worker: this.#startPrimaryWorker });
|
|
661
|
+
};
|
|
662
|
+
SetupPrimary = () => {
|
|
663
|
+
const ipcMessageManagerOptions = {
|
|
664
|
+
logger: stsutils.defaultLogger,
|
|
665
|
+
requestResponseMessageTimeout: 5e3,
|
|
666
|
+
namespace: this.#options.namespace,
|
|
667
|
+
role: "SERVER",
|
|
668
|
+
messageSender: (payload, options) => {
|
|
669
|
+
options.worker.send(payload);
|
|
670
|
+
},
|
|
671
|
+
ProcessRequestMessage: async (payload, options) => {
|
|
672
|
+
return this.#processPayload(payload, options);
|
|
673
|
+
},
|
|
674
|
+
//@@ also need way to add/remove a worker/child to the collection
|
|
675
|
+
//@@ the ping should also auto remove (such as a failed worker)
|
|
676
|
+
messageReceiverStart: (options) => {
|
|
677
|
+
const worker = options.worker;
|
|
678
|
+
this.#startPrimaryWorker = worker;
|
|
679
|
+
worker.on("message", this.#ProcessPrimaryMessageRaw);
|
|
680
|
+
},
|
|
681
|
+
messageReceiverStop: (options) => {
|
|
682
|
+
const worker = options.worker;
|
|
683
|
+
worker.off("message", this.#ProcessPrimaryMessageRaw);
|
|
684
|
+
},
|
|
685
|
+
groups: []
|
|
686
|
+
};
|
|
687
|
+
this.#messagingManager = new MessagingManager(ipcMessageManagerOptions);
|
|
688
|
+
};
|
|
689
|
+
#ProcessWorkerMessageRaw = (payload) => {
|
|
690
|
+
this.#messagingManager?.ProcessMessage(payload, this.#startWorkerOptions);
|
|
691
|
+
};
|
|
692
|
+
SetupWorker = () => {
|
|
693
|
+
const ipcMessageManagerOptions = {
|
|
694
|
+
logger: stsutils.defaultLogger,
|
|
695
|
+
requestResponseMessageTimeout: this.#options.requestResponseMessageTimeout,
|
|
696
|
+
namespace: this.#options.namespace,
|
|
697
|
+
role: "CLIENT",
|
|
698
|
+
messageSender: (payload, options) => {
|
|
699
|
+
process.send(payload);
|
|
700
|
+
},
|
|
701
|
+
ProcessRequestMessage: async (payload, options) => {
|
|
702
|
+
return this.#processPayload(payload, options);
|
|
703
|
+
},
|
|
704
|
+
messageReceiverStart: (options) => {
|
|
705
|
+
this.#startWorkerOptions = { ...options };
|
|
706
|
+
process.on("message", this.#ProcessWorkerMessageRaw);
|
|
707
|
+
},
|
|
708
|
+
messageReceiverStop: (options) => {
|
|
709
|
+
process.off("message", this.#ProcessWorkerMessageRaw);
|
|
710
|
+
},
|
|
711
|
+
groups: []
|
|
712
|
+
};
|
|
713
|
+
this.#messagingManager = new MessagingManager(ipcMessageManagerOptions);
|
|
714
|
+
};
|
|
715
|
+
SendMessage = async (payload) => {
|
|
716
|
+
if (this.#messagingManager) {
|
|
717
|
+
if (this.#worker) {
|
|
718
|
+
return this.#messagingManager?.SendMessage(payload, { worker: this.#worker });
|
|
719
|
+
} else {
|
|
720
|
+
return this.#messagingManager?.SendMessage(payload, {});
|
|
721
|
+
}
|
|
722
|
+
} else {
|
|
723
|
+
return {};
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
#processPayload = (payload, options) => {
|
|
727
|
+
return new Promise((resolve, reject) => {
|
|
728
|
+
if (payload.messageType.localeCompare("REQUEST") === 0 || payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
729
|
+
if (payload.requestPayload["__eventName"]) {
|
|
730
|
+
const eventName = payload.requestPayload["__eventName"];
|
|
731
|
+
if (this.#events[eventName]) {
|
|
732
|
+
try {
|
|
733
|
+
if (payload.messageType.localeCompare("REQUEST_NO_RESPONSE") === 0) {
|
|
734
|
+
this.#events[eventName].callback(...payload.requestPayload.args);
|
|
735
|
+
resolve({});
|
|
736
|
+
} else {
|
|
737
|
+
this.#events[eventName].callback(...payload.requestPayload.args, (responseMessage) => {
|
|
738
|
+
resolve(responseMessage);
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
} catch (error) {
|
|
742
|
+
reject(error);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
};
|
|
824
749
|
// p.on('fromworker', (arg1: number, arg2: string, callback: any) => {
|
|
825
750
|
on(event, callback, ctx) {
|
|
826
|
-
if (
|
|
827
|
-
delete
|
|
751
|
+
if (this.#events[event]) {
|
|
752
|
+
delete this.#events[event];
|
|
828
753
|
}
|
|
829
754
|
const eventObject = {
|
|
830
755
|
event,
|
|
831
756
|
callback,
|
|
832
757
|
ctx
|
|
833
758
|
};
|
|
834
|
-
|
|
759
|
+
this.#events[eventObject.event] = eventObject;
|
|
835
760
|
return this;
|
|
836
761
|
}
|
|
837
762
|
off(event, callback) {
|
|
838
|
-
if (
|
|
839
|
-
delete
|
|
763
|
+
if (this.#events[event]) {
|
|
764
|
+
delete this.#events[event];
|
|
840
765
|
}
|
|
841
766
|
return this;
|
|
842
767
|
}
|
|
768
|
+
// Supply complete collection of workers
|
|
769
|
+
Start = (worker) => {
|
|
770
|
+
if (worker) {
|
|
771
|
+
this.#messagingManager?.Start({ worker });
|
|
772
|
+
this.#worker = worker;
|
|
773
|
+
} else {
|
|
774
|
+
this.#messagingManager?.Start({});
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
// Supply complete collection of workers
|
|
778
|
+
Stop = () => {
|
|
779
|
+
if (this.#worker) {
|
|
780
|
+
this.#messagingManager?.Stop({ worker: this.#worker });
|
|
781
|
+
this.#worker = null;
|
|
782
|
+
} else {
|
|
783
|
+
this.#messagingManager?.Stop({});
|
|
784
|
+
}
|
|
785
|
+
};
|
|
843
786
|
get worker() {
|
|
844
|
-
return
|
|
787
|
+
return this.#worker;
|
|
845
788
|
}
|
|
846
789
|
/*
|
|
847
790
|
override emit(event: string, ...args: any[]): this {
|
|
@@ -867,31 +810,28 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
|
|
|
867
810
|
});
|
|
868
811
|
args[args.length - 1](retVal);
|
|
869
812
|
} catch (error) {
|
|
870
|
-
if (
|
|
871
|
-
if (
|
|
813
|
+
if (this.#options.ignoreEvents) {
|
|
814
|
+
if (this.#options.ignoreEvents.indexOf(error.__eventName) !== -1) {
|
|
872
815
|
return;
|
|
873
816
|
}
|
|
874
817
|
}
|
|
875
818
|
console.log(error);
|
|
876
|
-
|
|
819
|
+
this.#options.logger.error(chalk.red(`IPCMessageHandler:emit(): Error: [${JSON.stringify(error)}]`));
|
|
877
820
|
}
|
|
878
821
|
})();
|
|
879
822
|
return this;
|
|
880
823
|
}
|
|
824
|
+
emitNoResponse = async (event, ...args) => {
|
|
825
|
+
this.#messagingManager.SendMessageNoResponse({
|
|
826
|
+
__eventName: event,
|
|
827
|
+
args
|
|
828
|
+
});
|
|
829
|
+
};
|
|
881
830
|
}
|
|
882
|
-
_messagingManager3 = new WeakMap();
|
|
883
|
-
_options4 = new WeakMap();
|
|
884
|
-
_worker = new WeakMap();
|
|
885
|
-
_events3 = new WeakMap();
|
|
886
|
-
_startWorkerOptions2 = new WeakMap();
|
|
887
|
-
_startPrimaryWorker = new WeakMap();
|
|
888
|
-
_ProcessPrimaryMessageRaw = new WeakMap();
|
|
889
|
-
_ProcessWorkerMessageRaw2 = new WeakMap();
|
|
890
|
-
_processPayload3 = new WeakMap();
|
|
891
831
|
exports2.IPCMessageHandler = IPCMessageHandler;
|
|
892
832
|
exports2.IPCMessageHandlerPair = IPCMessageHandlerPair;
|
|
893
833
|
exports2.MessagingManager = MessagingManager;
|
|
894
834
|
exports2.RedisMessageHandler = RedisMessageHandler;
|
|
895
835
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
896
|
-
});
|
|
836
|
+
}));
|
|
897
837
|
//# sourceMappingURL=stsmessaging.umd.js.map
|