@eleven-am/pondsocket 0.1.178 → 0.1.179

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.
@@ -33,7 +33,7 @@ var __rest = (this && this.__rest) || function (s, e) {
33
33
  var __importDefault = (this && this.__importDefault) || function (mod) {
34
34
  return (mod && mod.__esModule) ? mod : { "default": mod };
35
35
  };
36
- var _RedisClient_instances, _RedisClient_heartbeatInterval, _RedisClient_cleanupInterval, _RedisClient_instanceTtl, _RedisClient_redisClient, _RedisClient_pubClient, _RedisClient_subClient, _RedisClient_instanceId, _RedisClient_assignsPublisher, _RedisClient_presencePublisher, _RedisClient_userLeavesPublisher, _RedisClient_channelMessagePublisher, _RedisClient_heartbeatTimer, _RedisClient_cleanupTimer, _RedisClient_presence_changes_channel_get, _RedisClient_assigns_changes_channel_get, _RedisClient_channel_messages_channel_get, _RedisClient_user_leaves_channel_get, _RedisClient_cleanup, _RedisClient_handleErrors, _RedisClient_getPresenceCacheChannel, _RedisClient_getAssignsCacheChannel, _RedisClient_publishPresenceChange, _RedisClient_publishAssignsChange, _RedisClient_publishChannelMessage, _RedisClient_publishUserLeave, _RedisClient_getPresenceCache, _RedisClient_getAssignsCache, _RedisClient_registerInstance, _RedisClient_unregisterInstance, _RedisClient_unsubscribeFromChannels, _RedisClient_subscribeToChannels, _RedisClient_startHeartbeat, _RedisClient_startPeriodicCleanup, _RedisClient_performConsistencyCheck, _RedisClient_getActiveInstances, _RedisClient_handleRedisMessage, _RedisClient_publishCacheMessage, _RedisClient_readCachedData, _RedisClient_subscribeToCacheChanges, _RedisClient_subscribeToChannelMessages, _RedisClient_subscribeToUserLeaves, _RedisClient_handleExit;
36
+ var _RedisClient_instances, _RedisClient_heartbeatInterval, _RedisClient_cleanupInterval, _RedisClient_instanceTtl, _RedisClient_redisClient, _RedisClient_pubClient, _RedisClient_subClient, _RedisClient_instanceId, _RedisClient_assignsPublisher, _RedisClient_presencePublisher, _RedisClient_userLeavesPublisher, _RedisClient_channelMessagePublisher, _RedisClient_stateSyncPublisher, _RedisClient_heartbeatTimer, _RedisClient_cleanupTimer, _RedisClient_presence_changes_channel_get, _RedisClient_assigns_changes_channel_get, _RedisClient_channel_messages_channel_get, _RedisClient_user_leaves_channel_get, _RedisClient_cleanup, _RedisClient_handleErrors, _RedisClient_getPresenceCacheChannel, _RedisClient_getAssignsCacheChannel, _RedisClient_publishPresenceChange, _RedisClient_publishAssignsChange, _RedisClient_publishChannelMessage, _RedisClient_publishUserLeave, _RedisClient_registerInstance, _RedisClient_unregisterInstance, _RedisClient_unsubscribeFromChannels, _RedisClient_subscribeToChannels, _RedisClient_startHeartbeat, _RedisClient_startPeriodicCleanup, _RedisClient_performConsistencyCheck, _RedisClient_handleRedisMessage, _RedisClient_publishCacheMessage, _RedisClient_emitStateSyncEvent, _RedisClient_subscribeToCacheChanges, _RedisClient_subscribeToChannelMessages, _RedisClient_subscribeToUserLeaves, _RedisClient_subscribeToStateSync, _RedisClient_handleExit;
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
38
  exports.RedisClient = void 0;
39
39
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -54,6 +54,7 @@ class RedisClient {
54
54
  _RedisClient_presencePublisher.set(this, void 0);
55
55
  _RedisClient_userLeavesPublisher.set(this, void 0);
56
56
  _RedisClient_channelMessagePublisher.set(this, void 0);
57
+ _RedisClient_stateSyncPublisher.set(this, void 0);
57
58
  _RedisClient_heartbeatTimer.set(this, void 0);
58
59
  _RedisClient_cleanupTimer.set(this, void 0);
59
60
  __classPrivateFieldSet(this, _RedisClient_instanceTtl, (_a = config.instanceTtl) !== null && _a !== void 0 ? _a : 90, "f");
@@ -66,13 +67,12 @@ class RedisClient {
66
67
  __classPrivateFieldSet(this, _RedisClient_presencePublisher, new pondsocket_common_1.Subject(), "f");
67
68
  __classPrivateFieldSet(this, _RedisClient_userLeavesPublisher, new pondsocket_common_1.Subject(), "f");
68
69
  __classPrivateFieldSet(this, _RedisClient_channelMessagePublisher, new pondsocket_common_1.Subject(), "f");
70
+ __classPrivateFieldSet(this, _RedisClient_stateSyncPublisher, new pondsocket_common_1.Subject(), "f");
69
71
  __classPrivateFieldSet(this, _RedisClient_instanceId, (0, pondsocket_common_1.uuid)(), "f");
70
72
  }
71
73
  buildClient(endpointId) {
72
74
  return (channelId) => ({
73
75
  channelId,
74
- getPresenceCache: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_getPresenceCache).bind(this, endpointId, channelId),
75
- getAssignsCache: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_getAssignsCache).bind(this, endpointId, channelId),
76
76
  publishPresenceChange: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_publishPresenceChange).bind(this, endpointId, channelId),
77
77
  publishAssignsChange: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_publishAssignsChange).bind(this, endpointId, channelId),
78
78
  publishChannelMessage: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_publishChannelMessage).bind(this, endpointId, channelId),
@@ -81,6 +81,7 @@ class RedisClient {
81
81
  subscribeToPresenceChanges: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_subscribeToCacheChanges).bind(this, endpointId, channelId, true),
82
82
  subscribeToAssignsChanges: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_subscribeToCacheChanges).bind(this, endpointId, channelId, false),
83
83
  subscribeToChannelMessages: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_subscribeToChannelMessages).bind(this, endpointId, channelId),
84
+ subscribeToStateSync: __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_subscribeToStateSync).bind(this, endpointId, channelId),
84
85
  });
85
86
  }
86
87
  initialize() {
@@ -105,7 +106,7 @@ class RedisClient {
105
106
  }
106
107
  }
107
108
  exports.RedisClient = RedisClient;
108
- _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = new WeakMap(), _RedisClient_instanceTtl = new WeakMap(), _RedisClient_redisClient = new WeakMap(), _RedisClient_pubClient = new WeakMap(), _RedisClient_subClient = new WeakMap(), _RedisClient_instanceId = new WeakMap(), _RedisClient_assignsPublisher = new WeakMap(), _RedisClient_presencePublisher = new WeakMap(), _RedisClient_userLeavesPublisher = new WeakMap(), _RedisClient_channelMessagePublisher = new WeakMap(), _RedisClient_heartbeatTimer = new WeakMap(), _RedisClient_cleanupTimer = new WeakMap(), _RedisClient_instances = new WeakSet(), _RedisClient_presence_changes_channel_get = function _RedisClient_presence_changes_channel_get() {
109
+ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = new WeakMap(), _RedisClient_instanceTtl = new WeakMap(), _RedisClient_redisClient = new WeakMap(), _RedisClient_pubClient = new WeakMap(), _RedisClient_subClient = new WeakMap(), _RedisClient_instanceId = new WeakMap(), _RedisClient_assignsPublisher = new WeakMap(), _RedisClient_presencePublisher = new WeakMap(), _RedisClient_userLeavesPublisher = new WeakMap(), _RedisClient_channelMessagePublisher = new WeakMap(), _RedisClient_stateSyncPublisher = new WeakMap(), _RedisClient_heartbeatTimer = new WeakMap(), _RedisClient_cleanupTimer = new WeakMap(), _RedisClient_instances = new WeakSet(), _RedisClient_presence_changes_channel_get = function _RedisClient_presence_changes_channel_get() {
109
110
  return 'presence_changes';
110
111
  }, _RedisClient_assigns_changes_channel_get = function _RedisClient_assigns_changes_channel_get() {
111
112
  return 'assigns_changes';
@@ -168,10 +169,6 @@ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = n
168
169
  });
169
170
  yield __classPrivateFieldGet(this, _RedisClient_pubClient, "f").publish(__classPrivateFieldGet(this, _RedisClient_instances, "a", _RedisClient_user_leaves_channel_get), message);
170
171
  });
171
- }, _RedisClient_getPresenceCache = function _RedisClient_getPresenceCache(endpointId, channelId) {
172
- return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_readCachedData).call(this, endpointId, channelId, 'presence_cache:*');
173
- }, _RedisClient_getAssignsCache = function _RedisClient_getAssignsCache(endpointId, channelId) {
174
- return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_readCachedData).call(this, endpointId, channelId, 'assigns_cache:*');
175
172
  }, _RedisClient_registerInstance = function _RedisClient_registerInstance() {
176
173
  return __awaiter(this, void 0, void 0, function* () {
177
174
  const multi = __classPrivateFieldGet(this, _RedisClient_redisClient, "f").multi();
@@ -232,30 +229,43 @@ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = n
232
229
  }), __classPrivateFieldGet(this, _RedisClient_cleanupInterval, "f")), "f");
233
230
  }, _RedisClient_performConsistencyCheck = function _RedisClient_performConsistencyCheck() {
234
231
  return __awaiter(this, void 0, void 0, function* () {
235
- const activeInstances = yield __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_getActiveInstances).call(this);
236
- const allKeys = yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").keys('*_cache:*');
237
- for (const key of allKeys) {
238
- const [_, instanceId] = key.split(':');
239
- if (!activeInstances.has(instanceId)) {
240
- yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").del(key);
241
- }
242
- }
243
- });
244
- }, _RedisClient_getActiveInstances = function _RedisClient_getActiveInstances() {
245
- return __awaiter(this, void 0, void 0, function* () {
246
- const instances = yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").smembers('distributed_instances');
247
- const activeInstances = new Set();
248
- for (const instanceId of instances) {
249
- const lastHeartbeat = yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").get(`heartbeat:${instanceId}`);
250
- if (lastHeartbeat && Date.now() - parseInt(lastHeartbeat, 10) < __classPrivateFieldGet(this, _RedisClient_instanceTtl, "f") * 1000) {
251
- activeInstances.add(instanceId);
252
- }
253
- else {
254
- yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").srem('distributed_instances', instanceId);
255
- yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").del(`heartbeat:${instanceId}`);
256
- }
257
- }
258
- return activeInstances;
232
+ const consistencyCheckScript = `
233
+ local active_instances = redis.call('SMEMBERS', 'distributed_instances')
234
+ local all_keys = redis.call('KEYS', '*_cache:*')
235
+ local inactive_keys = {}
236
+ local unique_pairs = {}
237
+
238
+ for _, key in ipairs(all_keys) do
239
+ local parts = {}
240
+ for part in string.gmatch(key, '[^:]+') do
241
+ table.insert(parts, part)
242
+ end
243
+ local instance_id, endpoint_id, channel_id = parts[2], parts[3], parts[4]
244
+
245
+ if not (active_instances[instance_id]) then
246
+ table.insert(inactive_keys, key)
247
+ local pair = endpoint_id .. ':' .. channel_id
248
+ unique_pairs[pair] = true
249
+ end
250
+ end
251
+
252
+ if #inactive_keys > 0 then
253
+ redis.call('DEL', unpack(inactive_keys))
254
+ end
255
+
256
+ local unique_pairs_list = {}
257
+ for pair in pairs(unique_pairs) do
258
+ table.insert(unique_pairs_list, pair)
259
+ end
260
+
261
+ return unique_pairs_list
262
+ `;
263
+ const [uniquePairs] = yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").eval(consistencyCheckScript, 0);
264
+ const promises = uniquePairs.map((pair) => {
265
+ const [endpointId, channelId] = pair.split(':');
266
+ return __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_emitStateSyncEvent).call(this, endpointId, channelId);
267
+ });
268
+ yield Promise.all(promises);
259
269
  });
260
270
  }, _RedisClient_handleRedisMessage = function _RedisClient_handleRedisMessage(channel, message) {
261
271
  const data = JSON.parse(message);
@@ -277,56 +287,58 @@ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = n
277
287
  }
278
288
  }, _RedisClient_publishCacheMessage = function _RedisClient_publishCacheMessage(key, cacheKey, message) {
279
289
  return __awaiter(this, void 0, void 0, function* () {
290
+ const script = `
291
+ local messageData = ARGV[1]
292
+ local userId = ARGV[2]
293
+ local state = ARGV[3]
294
+
295
+ if state ~= '' then
296
+ redis.call('HSET', KEYS[1], userId, state)
297
+ else
298
+ redis.call('HDEL', KEYS[1], userId)
299
+ end
300
+
301
+ redis.call('PUBLISH', KEYS[2], messageData)
302
+
303
+ return 1
304
+ `;
280
305
  const messageData = JSON.stringify(message);
281
- if (message.state) {
282
- yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").hset(cacheKey, message.userId, JSON.stringify(message.state));
283
- }
284
- else {
285
- yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").hdel(cacheKey, message.userId);
286
- }
287
- yield __classPrivateFieldGet(this, _RedisClient_pubClient, "f").publish(key, messageData);
306
+ const state = message.state ? JSON.stringify(message.state) : '';
307
+ yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").eval(script, 2, cacheKey, key, messageData, message.userId, state);
288
308
  });
289
- }, _RedisClient_readCachedData = function _RedisClient_readCachedData(endpointId, channelId, cacheKey) {
309
+ }, _RedisClient_emitStateSyncEvent = function _RedisClient_emitStateSyncEvent(endpointId, channelId) {
290
310
  return __awaiter(this, void 0, void 0, function* () {
291
- const response = new Map();
292
- const activeInstances = new Set(yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").smembers('distributed_instances'));
293
- let cursor = '0';
294
- do {
295
- const [nextCursor, keys] = yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").scan(cursor, 'MATCH', cacheKey, 'COUNT', '100');
296
- cursor = nextCursor;
297
- const pipeline = __classPrivateFieldGet(this, _RedisClient_redisClient, "f").pipeline();
298
- keys.forEach((key) => {
299
- const [_, instanceId, keyEndpointId, keyChannelId] = key.split(':');
300
- if (keyEndpointId === endpointId && keyChannelId === channelId) {
301
- if (activeInstances.has(instanceId)) {
302
- pipeline.hgetall(key);
303
- }
304
- else {
305
- pipeline.del(key);
306
- }
307
- }
308
- });
309
- const results = yield pipeline.exec();
310
- if (results) {
311
- results.forEach(([error, data]) => {
312
- if (!error && data && typeof data === 'object') {
313
- Object.entries(data)
314
- .forEach(([userId, value]) => {
315
- try {
316
- const parsedData = JSON.parse(value);
317
- if (typeof parsedData === 'object' && parsedData !== null) {
318
- response.set(userId, parsedData);
319
- }
320
- }
321
- catch (e) {
322
- throw new redisError_1.RedisError('Error parsing cached data');
323
- }
324
- });
325
- }
326
- });
327
- }
328
- } while (cursor !== '0');
329
- return response;
311
+ const script = `
312
+ local active_instances = redis.call('SMEMBERS', 'distributed_instances')
313
+ local presence_data = {}
314
+ local assigns_data = {}
315
+
316
+ for _, instance in ipairs(active_instances) do
317
+ local presence_key = 'presence_cache:' .. instance .. ':' .. ARGV[1] .. ':' .. ARGV[2]
318
+ local assigns_key = 'assigns_cache:' .. instance .. ':' .. ARGV[1] .. ':' .. ARGV[2]
319
+
320
+ local presence = redis.call('HGETALL', presence_key)
321
+ local assigns = redis.call('HGETALL', assigns_key)
322
+
323
+ for i = 1, #presence, 2 do
324
+ presence_data[presence[i]] = presence[i+1]
325
+ end
326
+
327
+ for i = 1, #assigns, 2 do
328
+ assigns_data[assigns[i]] = assigns[i+1]
329
+ end
330
+ end
331
+
332
+ return {cjson.encode(presence_data), cjson.encode(assigns_data)}
333
+ `;
334
+ const [presenceData, assignsData] = yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").eval(script, 0, endpointId, channelId);
335
+ const event = {
336
+ endpointId,
337
+ channelId,
338
+ presence: new Map(Object.entries(JSON.parse(presenceData))),
339
+ assigns: new Map(Object.entries(JSON.parse(assignsData))),
340
+ };
341
+ __classPrivateFieldGet(this, _RedisClient_stateSyncPublisher, "f").publish(event);
330
342
  });
331
343
  }, _RedisClient_subscribeToCacheChanges = function _RedisClient_subscribeToCacheChanges(endpoint, channel, presence, callback) {
332
344
  const subject = presence ? __classPrivateFieldGet(this, _RedisClient_presencePublisher, "f") : __classPrivateFieldGet(this, _RedisClient_assignsPublisher, "f");
@@ -348,6 +360,19 @@ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = n
348
360
  return callback(userId);
349
361
  }
350
362
  });
363
+ }, _RedisClient_subscribeToStateSync = function _RedisClient_subscribeToStateSync(endpoint, channel, callback) {
364
+ const interval = setInterval(() => __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_emitStateSyncEvent).call(this, endpoint, channel), __classPrivateFieldGet(this, _RedisClient_heartbeatInterval, "f") * 10);
365
+ const subscription = __classPrivateFieldGet(this, _RedisClient_stateSyncPublisher, "f").subscribe((_a) => {
366
+ var { endpointId, channelId } = _a, data = __rest(_a, ["endpointId", "channelId"]);
367
+ if (endpointId === endpoint && channelId === channel) {
368
+ return callback(data);
369
+ }
370
+ });
371
+ void __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_emitStateSyncEvent).call(this, endpoint, channel);
372
+ return () => {
373
+ clearInterval(interval);
374
+ subscription();
375
+ };
351
376
  }, _RedisClient_handleExit = function _RedisClient_handleExit() {
352
377
  return __awaiter(this, void 0, void 0, function* () {
353
378
  try {
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
3
  if (kind === "m") throw new TypeError("Private method is not writable");
13
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -45,7 +36,7 @@ class EndpointEngine {
45
36
  }
46
37
  createChannel(path, handler) {
47
38
  const pondChannel = new lobbyEngine_1.LobbyEngine(this);
48
- __classPrivateFieldGet(this, _EndpointEngine_middleware, "f").use((user, joinParams, next) => __awaiter(this, void 0, void 0, function* () {
39
+ __classPrivateFieldGet(this, _EndpointEngine_middleware, "f").use((user, joinParams, next) => {
49
40
  const event = (0, matcher_1.parseAddress)(path, user.channelName);
50
41
  if (event) {
51
42
  const options = {
@@ -54,13 +45,13 @@ class EndpointEngine {
54
45
  params: event,
55
46
  joinParams,
56
47
  };
57
- const channel = yield pondChannel.getOrCreateChannel(user.channelName);
48
+ const channel = pondChannel.getOrCreateChannel(user.channelName);
58
49
  const request = new joinRequest_1.JoinRequest(options, channel);
59
50
  const response = new joinResponse_1.JoinResponse(user, channel);
60
51
  return handler(request, response, next);
61
52
  }
62
53
  next();
63
- }));
54
+ });
64
55
  __classPrivateFieldGet(this, _EndpointEngine_lobbyEngines, "f").set(path, pondChannel);
65
56
  return new pondChannel_1.PondChannel(pondChannel);
66
57
  }
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
3
  if (kind === "m") throw new TypeError("Private method is not writable");
13
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -69,11 +60,9 @@ exports.LobbyEngine = LobbyEngine;
69
60
  _LobbyEngine_channels = new WeakMap(), _LobbyEngine_instances = new WeakSet(), _LobbyEngine_getChannel = function _LobbyEngine_getChannel(channelName) {
70
61
  return __classPrivateFieldGet(this, _LobbyEngine_channels, "f").get(channelName) || null;
71
62
  }, _LobbyEngine_createChannel = function _LobbyEngine_createChannel(channelName) {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- const onManagerClose = __classPrivateFieldGet(this, _LobbyEngine_channels, "f").delete.bind(__classPrivateFieldGet(this, _LobbyEngine_channels, "f"), channelName);
74
- const manager = yield this.parent.createManager(channelName, onManagerClose);
75
- const channel = new channelEngine_1.ChannelEngine(this, channelName, manager);
76
- __classPrivateFieldGet(this, _LobbyEngine_channels, "f").set(channelName, channel);
77
- return channel;
78
- });
63
+ const onManagerClose = __classPrivateFieldGet(this, _LobbyEngine_channels, "f").delete.bind(__classPrivateFieldGet(this, _LobbyEngine_channels, "f"), channelName);
64
+ const manager = this.parent.createManager(channelName, onManagerClose);
65
+ const channel = new channelEngine_1.ChannelEngine(this, channelName, manager);
66
+ __classPrivateFieldGet(this, _LobbyEngine_channels, "f").set(channelName, channel);
67
+ return channel;
79
68
  };
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
3
  if (kind === "m") throw new TypeError("Private method is not writable");
13
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -66,44 +57,42 @@ class DistributedManager extends manager_1.Manager {
66
57
  __classPrivateFieldGet(this, _DistributedManager_client, "f").publishChannelMessage(message);
67
58
  }
68
59
  initialize(unsubscribe) {
69
- const _super = Object.create(null, {
70
- initialize: { get: () => super.initialize }
60
+ super.initialize(unsubscribe);
61
+ const leaveSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToUserLeaves((userId) => {
62
+ var _a;
63
+ (_a = this.userSubscriptions.get(userId)) === null || _a === void 0 ? void 0 : _a();
64
+ this.userSubscriptions.delete(userId);
65
+ });
66
+ const presenceSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToPresenceChanges(({ userId, state }) => {
67
+ if (state) {
68
+ this.presenceCache.set(userId, state);
69
+ }
70
+ else {
71
+ this.presenceCache.delete(userId);
72
+ }
73
+ });
74
+ const assignSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToAssignsChanges(({ userId, state }) => {
75
+ if (state) {
76
+ this.assignsCache.set(userId, state);
77
+ }
78
+ else {
79
+ this.assignsCache.delete(userId);
80
+ }
81
+ });
82
+ const messageSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToChannelMessages((message) => {
83
+ this.publisher.publish(message);
71
84
  });
72
- return __awaiter(this, void 0, void 0, function* () {
73
- yield _super.initialize.call(this, unsubscribe);
74
- this.presenceCache = yield __classPrivateFieldGet(this, _DistributedManager_client, "f").getPresenceCache();
75
- this.assignsCache = yield __classPrivateFieldGet(this, _DistributedManager_client, "f").getAssignsCache();
76
- const leaveSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToUserLeaves((userId) => {
77
- var _a;
78
- (_a = this.userSubscriptions.get(userId)) === null || _a === void 0 ? void 0 : _a();
79
- this.userSubscriptions.delete(userId);
80
- });
81
- const presenceSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToPresenceChanges(({ userId, state }) => {
82
- if (state) {
83
- this.presenceCache.set(userId, state);
84
- }
85
- else {
86
- this.presenceCache.delete(userId);
87
- }
88
- });
89
- const assignSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToAssignsChanges(({ userId, state }) => {
90
- if (state) {
91
- this.assignsCache.set(userId, state);
92
- }
93
- else {
94
- this.assignsCache.delete(userId);
95
- }
96
- });
97
- const messageSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToChannelMessages((message) => {
98
- this.publisher.publish(message);
99
- });
100
- __classPrivateFieldSet(this, _DistributedManager_subscriptions, () => {
101
- leaveSubscription();
102
- presenceSubscription();
103
- assignSubscription();
104
- messageSubscription();
105
- }, "f");
85
+ const stateSyncSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToStateSync((state) => {
86
+ this.assignsCache = new Map(state.assigns);
87
+ this.presenceCache = new Map(state.presence);
106
88
  });
89
+ __classPrivateFieldSet(this, _DistributedManager_subscriptions, () => {
90
+ leaveSubscription();
91
+ presenceSubscription();
92
+ assignSubscription();
93
+ messageSubscription();
94
+ stateSyncSubscription();
95
+ }, "f");
107
96
  }
108
97
  close() {
109
98
  var _a;
@@ -52,7 +52,6 @@ class Manager {
52
52
  }
53
53
  initialize(unsubscribe) {
54
54
  __classPrivateFieldSet(this, _Manager_onClose, unsubscribe, "f");
55
- return Promise.resolve();
56
55
  }
57
56
  getPresence(userId) {
58
57
  return this.presenceCache.get(userId) || null;
@@ -1,31 +1,20 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.ManagerFactory = void 0;
13
4
  const distributedManager_1 = require("./distributedManager");
14
5
  const localManager_1 = require("./localManager");
15
6
  class ManagerFactory {
16
7
  static create(channelId, clientFactory, onClose) {
17
- return __awaiter(this, void 0, void 0, function* () {
18
- let manager;
19
- if (clientFactory) {
20
- const client = clientFactory(channelId);
21
- manager = new distributedManager_1.DistributedManager(client);
22
- }
23
- else {
24
- manager = new localManager_1.LocalManager(channelId);
25
- }
26
- yield manager.initialize(onClose);
27
- return manager;
28
- });
8
+ let manager;
9
+ if (clientFactory) {
10
+ const client = clientFactory(channelId);
11
+ manager = new distributedManager_1.DistributedManager(client);
12
+ }
13
+ else {
14
+ manager = new localManager_1.LocalManager(channelId);
15
+ }
16
+ manager.initialize(onClose);
17
+ return manager;
29
18
  }
30
19
  }
31
20
  exports.ManagerFactory = ManagerFactory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.178",
3
+ "version": "0.1.179",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -49,7 +49,7 @@
49
49
  "eslint-config-prettier": "^9.1.0",
50
50
  "eslint-import-resolver-node": "^0.3.9",
51
51
  "eslint-plugin-file-progress": "^1.5.0",
52
- "eslint-plugin-import": "^2.30.0",
52
+ "eslint-plugin-import": "^2.31.0",
53
53
  "eslint-plugin-prettier": "^5.2.1",
54
54
  "jest": "^29.7.0",
55
55
  "nodemon": "^3.1.7",