@eleven-am/pondsocket 0.1.204 → 0.1.207
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/abstracts/distributor.js +8 -4
- package/engines/channelEngine.js +3 -25
- package/package.json +1 -1
package/abstracts/distributor.js
CHANGED
|
@@ -19,7 +19,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
19
19
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
20
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
21
|
};
|
|
22
|
-
var _RedisDistributedBackend_instances, _RedisDistributedBackend_publishClient, _RedisDistributedBackend_subscribeClient, _RedisDistributedBackend_keyPrefix, _RedisDistributedBackend_subject, _RedisDistributedBackend_isConnected, _RedisDistributedBackend_initialize, _RedisDistributedBackend_handleRedisMessage, _RedisDistributedBackend_isValidMessage, _RedisDistributedBackend_buildKey;
|
|
22
|
+
var _RedisDistributedBackend_instances, _RedisDistributedBackend_publishClient, _RedisDistributedBackend_subscribeClient, _RedisDistributedBackend_keyPrefix, _RedisDistributedBackend_subject, _RedisDistributedBackend_nodeId, _RedisDistributedBackend_isConnected, _RedisDistributedBackend_initialize, _RedisDistributedBackend_handleRedisMessage, _RedisDistributedBackend_isValidMessage, _RedisDistributedBackend_buildKey;
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.RedisDistributedBackend = void 0;
|
|
25
25
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
@@ -32,9 +32,12 @@ class RedisDistributedBackend {
|
|
|
32
32
|
_RedisDistributedBackend_subscribeClient.set(this, void 0);
|
|
33
33
|
_RedisDistributedBackend_keyPrefix.set(this, void 0);
|
|
34
34
|
_RedisDistributedBackend_subject.set(this, void 0);
|
|
35
|
+
_RedisDistributedBackend_nodeId.set(this, void 0);
|
|
35
36
|
_RedisDistributedBackend_isConnected.set(this, false);
|
|
36
37
|
const { host = 'localhost', port = 6379, password, database = 0, url, keyPrefix = 'pondsocket', } = options;
|
|
37
38
|
__classPrivateFieldSet(this, _RedisDistributedBackend_keyPrefix, keyPrefix, "f");
|
|
39
|
+
__classPrivateFieldSet(this, _RedisDistributedBackend_nodeId, Math.random().toString(36)
|
|
40
|
+
.substring(2, 15), "f");
|
|
38
41
|
__classPrivateFieldSet(this, _RedisDistributedBackend_subject, new pondsocket_common_1.Subject(), "f");
|
|
39
42
|
// Create Redis clients
|
|
40
43
|
const clientConfig = url
|
|
@@ -58,7 +61,7 @@ class RedisDistributedBackend {
|
|
|
58
61
|
*/
|
|
59
62
|
subscribe(endpointName, channelName, handler) {
|
|
60
63
|
return this.subject.subscribe((message) => {
|
|
61
|
-
if (message.endpointName === endpointName && message.channelName === channelName) {
|
|
64
|
+
if (message.endpointName === endpointName && message.channelName === channelName && message.nodeId !== __classPrivateFieldGet(this, _RedisDistributedBackend_nodeId, "f")) {
|
|
62
65
|
handler(message);
|
|
63
66
|
}
|
|
64
67
|
});
|
|
@@ -72,7 +75,8 @@ class RedisDistributedBackend {
|
|
|
72
75
|
throw new Error('Redis backend is not connected');
|
|
73
76
|
}
|
|
74
77
|
const key = __classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_buildKey).call(this, endpointName, channelName);
|
|
75
|
-
const
|
|
78
|
+
const distributedMessage = Object.assign(Object.assign({}, message), { nodeId: __classPrivateFieldGet(this, _RedisDistributedBackend_nodeId, "f") });
|
|
79
|
+
const serializedMessage = JSON.stringify(distributedMessage);
|
|
76
80
|
yield __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").publish(key, serializedMessage);
|
|
77
81
|
});
|
|
78
82
|
}
|
|
@@ -93,7 +97,7 @@ class RedisDistributedBackend {
|
|
|
93
97
|
}
|
|
94
98
|
}
|
|
95
99
|
exports.RedisDistributedBackend = RedisDistributedBackend;
|
|
96
|
-
_RedisDistributedBackend_publishClient = new WeakMap(), _RedisDistributedBackend_subscribeClient = new WeakMap(), _RedisDistributedBackend_keyPrefix = new WeakMap(), _RedisDistributedBackend_subject = new WeakMap(), _RedisDistributedBackend_isConnected = new WeakMap(), _RedisDistributedBackend_instances = new WeakSet(), _RedisDistributedBackend_initialize = function _RedisDistributedBackend_initialize() {
|
|
100
|
+
_RedisDistributedBackend_publishClient = new WeakMap(), _RedisDistributedBackend_subscribeClient = new WeakMap(), _RedisDistributedBackend_keyPrefix = new WeakMap(), _RedisDistributedBackend_subject = new WeakMap(), _RedisDistributedBackend_nodeId = new WeakMap(), _RedisDistributedBackend_isConnected = new WeakMap(), _RedisDistributedBackend_instances = new WeakSet(), _RedisDistributedBackend_initialize = function _RedisDistributedBackend_initialize() {
|
|
97
101
|
return __awaiter(this, void 0, void 0, function* () {
|
|
98
102
|
yield Promise.all([
|
|
99
103
|
__classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").connect(),
|
package/engines/channelEngine.js
CHANGED
|
@@ -112,10 +112,8 @@ class ChannelEngine {
|
|
|
112
112
|
};
|
|
113
113
|
const recipients = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getUsersFromRecipients).call(this, recipient, sender);
|
|
114
114
|
const internalEvent = Object.assign(Object.assign({}, channelEvent), { recipients });
|
|
115
|
-
// Publish locally
|
|
116
115
|
__classPrivateFieldGet(this, _ChannelEngine_publisher, "f").publish(internalEvent);
|
|
117
|
-
|
|
118
|
-
if (__classPrivateFieldGet(this, _ChannelEngine_backend, "f") && action === pondsocket_common_1.ServerActions.BROADCAST && sender !== pondsocket_common_1.SystemSender.CHANNEL) {
|
|
116
|
+
if (__classPrivateFieldGet(this, _ChannelEngine_backend, "f")) {
|
|
119
117
|
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_broadcastToNodes).call(this, {
|
|
120
118
|
type: types_1.DistributedMessageType.USER_MESSAGE,
|
|
121
119
|
endpointName: __classPrivateFieldGet(this, _ChannelEngine_endpointId, "f"),
|
|
@@ -154,7 +152,6 @@ class ChannelEngine {
|
|
|
154
152
|
trackPresence(userId, presence) {
|
|
155
153
|
const presenceEngine = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getOrCreatePresenceEngine).call(this);
|
|
156
154
|
presenceEngine.trackPresence(userId, presence);
|
|
157
|
-
// Broadcast presence update to other nodes
|
|
158
155
|
if (__classPrivateFieldGet(this, _ChannelEngine_backend, "f")) {
|
|
159
156
|
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_broadcastToNodes).call(this, {
|
|
160
157
|
type: types_1.DistributedMessageType.PRESENCE_UPDATE,
|
|
@@ -173,7 +170,6 @@ class ChannelEngine {
|
|
|
173
170
|
updatePresence(userId, presence) {
|
|
174
171
|
const presenceEngine = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getOrCreatePresenceEngine).call(this);
|
|
175
172
|
presenceEngine.updatePresence(userId, presence);
|
|
176
|
-
// Broadcast presence update to other nodes
|
|
177
173
|
if (__classPrivateFieldGet(this, _ChannelEngine_backend, "f")) {
|
|
178
174
|
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_broadcastToNodes).call(this, {
|
|
179
175
|
type: types_1.DistributedMessageType.PRESENCE_UPDATE,
|
|
@@ -430,6 +426,7 @@ _ChannelEngine_endpointId = new WeakMap(), _ChannelEngine_backend = new WeakMap(
|
|
|
430
426
|
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_handleDistributedMessage).call(this, message);
|
|
431
427
|
}), "f");
|
|
432
428
|
}, _ChannelEngine_handleDistributedMessage = function _ChannelEngine_handleDistributedMessage(message) {
|
|
429
|
+
console.log(message);
|
|
433
430
|
switch (message.type) {
|
|
434
431
|
case types_1.DistributedMessageType.STATE_REQUEST:
|
|
435
432
|
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_handleStateRequest).call(this, message);
|
|
@@ -520,46 +517,27 @@ _ChannelEngine_endpointId = new WeakMap(), _ChannelEngine_backend = new WeakMap(
|
|
|
520
517
|
__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").removePresence(message.userId);
|
|
521
518
|
}
|
|
522
519
|
}, _ChannelEngine_handleRemoteMessage = function _ChannelEngine_handleRemoteMessage(message) {
|
|
523
|
-
const localRecipients = message.recipients.filter((userId) => this.users.has(userId));
|
|
524
|
-
if (localRecipients.length === 0) {
|
|
525
|
-
return;
|
|
526
|
-
}
|
|
527
520
|
const internalEvent = {
|
|
528
521
|
channelName: __classPrivateFieldGet(this, _ChannelEngine_name, "f"),
|
|
529
522
|
requestId: message.requestId,
|
|
530
523
|
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
531
524
|
event: message.event,
|
|
532
525
|
payload: message.payload,
|
|
533
|
-
recipients:
|
|
526
|
+
recipients: message.recipients,
|
|
534
527
|
};
|
|
535
528
|
__classPrivateFieldGet(this, _ChannelEngine_publisher, "f").publish(internalEvent);
|
|
536
529
|
}, _ChannelEngine_handleRemotePresenceUpdate = function _ChannelEngine_handleRemotePresenceUpdate(message) {
|
|
537
|
-
if (!this.users.has(message.userId)) {
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
530
|
const presenceEngine = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getOrCreatePresenceEngine).call(this);
|
|
541
531
|
presenceEngine.upsertPresence(message.userId, message.presence);
|
|
542
532
|
}, _ChannelEngine_handleRemotePresenceRemoved = function _ChannelEngine_handleRemotePresenceRemoved(message) {
|
|
543
|
-
if (!this.users.has(message.userId)) {
|
|
544
|
-
return;
|
|
545
|
-
}
|
|
546
533
|
if (__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) {
|
|
547
534
|
__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").removePresence(message.userId);
|
|
548
535
|
}
|
|
549
536
|
}, _ChannelEngine_handleRemoteAssignsUpdate = function _ChannelEngine_handleRemoteAssignsUpdate(message) {
|
|
550
|
-
if (!this.users.has(message.userId)) {
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
537
|
__classPrivateFieldGet(this, _ChannelEngine_assignsCache, "f").set(message.userId, message.assigns);
|
|
554
538
|
}, _ChannelEngine_handleRemoteAssignsRemoved = function _ChannelEngine_handleRemoteAssignsRemoved(message) {
|
|
555
|
-
if (!this.users.has(message.userId)) {
|
|
556
|
-
return;
|
|
557
|
-
}
|
|
558
539
|
__classPrivateFieldGet(this, _ChannelEngine_assignsCache, "f").set(message.userId, {});
|
|
559
540
|
}, _ChannelEngine_handleRemoteEvictUser = function _ChannelEngine_handleRemoteEvictUser(message) {
|
|
560
|
-
if (!this.users.has(message.userId)) {
|
|
561
|
-
return;
|
|
562
|
-
}
|
|
563
541
|
__classPrivateFieldGet(this, _ChannelEngine_assignsCache, "f").delete(message.userId);
|
|
564
542
|
if (__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) {
|
|
565
543
|
__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").removePresence(message.userId);
|