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