@eleven-am/pondsocket 0.1.183 → 0.1.185

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.
@@ -304,8 +304,8 @@ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = n
304
304
  const state = message.state ? JSON.stringify(message.state) : '';
305
305
  yield __classPrivateFieldGet(this, _RedisClient_redisClient, "f").eval(script, 2, cacheKey, key, messageData, message.userId, state);
306
306
  });
307
- }, _RedisClient_emitStateSyncEvent = function _RedisClient_emitStateSyncEvent(endpointId, channelId) {
308
- return __awaiter(this, void 0, void 0, function* () {
307
+ }, _RedisClient_emitStateSyncEvent = function _RedisClient_emitStateSyncEvent(endpointId_1, channelId_1) {
308
+ return __awaiter(this, arguments, void 0, function* (endpointId, channelId, initialFetch = false) {
309
309
  const script = `
310
310
  local active_instances = redis.call('SMEMBERS', 'distributed_instances')
311
311
  local presence_data = {}
@@ -333,6 +333,7 @@ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = n
333
333
  const event = {
334
334
  endpointId,
335
335
  channelId,
336
+ initialFetch,
336
337
  presence: new Map(Object.entries(JSON.parse(presenceData))),
337
338
  assigns: new Map(Object.entries(JSON.parse(assignsData))),
338
339
  };
@@ -366,7 +367,7 @@ _RedisClient_heartbeatInterval = new WeakMap(), _RedisClient_cleanupInterval = n
366
367
  return callback(data);
367
368
  }
368
369
  });
369
- void __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_emitStateSyncEvent).call(this, endpoint, channel);
370
+ void __classPrivateFieldGet(this, _RedisClient_instances, "m", _RedisClient_emitStateSyncEvent).call(this, endpoint, channel, true);
370
371
  return () => {
371
372
  clearInterval(interval);
372
373
  subscription();
@@ -10,13 +10,15 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  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");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _DistributedManager_client, _DistributedManager_subscriptions;
13
+ var _DistributedManager_instances, _DistributedManager_client, _DistributedManager_subscriptions, _DistributedManager_parseAndUpdatePresence;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.DistributedManager = void 0;
16
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
16
17
  const manager_1 = require("./manager");
17
18
  class DistributedManager extends manager_1.Manager {
18
19
  constructor(client) {
19
20
  super(client.channelId);
21
+ _DistributedManager_instances.add(this);
20
22
  _DistributedManager_client.set(this, void 0);
21
23
  _DistributedManager_subscriptions.set(this, null);
22
24
  __classPrivateFieldSet(this, _DistributedManager_client, client, "f");
@@ -84,7 +86,7 @@ class DistributedManager extends manager_1.Manager {
84
86
  });
85
87
  const stateSyncSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToStateSync((state) => {
86
88
  this.assignsCache = new Map(state.assigns);
87
- this.presenceCache = new Map(state.presence);
89
+ __classPrivateFieldGet(this, _DistributedManager_instances, "m", _DistributedManager_parseAndUpdatePresence).call(this, new Map(state.presence), state.initialFetch);
88
90
  });
89
91
  __classPrivateFieldSet(this, _DistributedManager_subscriptions, () => {
90
92
  leaveSubscription();
@@ -101,4 +103,63 @@ class DistributedManager extends manager_1.Manager {
101
103
  }
102
104
  }
103
105
  exports.DistributedManager = DistributedManager;
104
- _DistributedManager_client = new WeakMap(), _DistributedManager_subscriptions = new WeakMap();
106
+ _DistributedManager_client = new WeakMap(), _DistributedManager_subscriptions = new WeakMap(), _DistributedManager_instances = new WeakSet(), _DistributedManager_parseAndUpdatePresence = function _DistributedManager_parseAndUpdatePresence(newCache, initialFetch) {
107
+ if (initialFetch) {
108
+ this.presenceCache = newCache;
109
+ }
110
+ const newUsers = new Set(newCache.keys());
111
+ const oldUsers = new Set(this.presenceCache.keys());
112
+ const usersToAdd = new Set([...newUsers].filter((user) => !oldUsers.has(user)));
113
+ const usersToRemove = new Set([...oldUsers].filter((user) => !newUsers.has(user)));
114
+ const noChange = new Set([...newUsers].filter((user) => oldUsers.has(user)));
115
+ const usersToUpdate = new Set([...noChange]
116
+ .filter((user) => {
117
+ const oldData = this.presenceCache.get(user);
118
+ const newData = newCache.get(user);
119
+ return JSON.stringify(oldData) !== JSON.stringify(newData);
120
+ }));
121
+ const upSertInfo = [
122
+ ...[...usersToAdd]
123
+ .map((user) => ({
124
+ userId: user,
125
+ state: newCache.get(user),
126
+ event: pondsocket_common_1.PresenceEventTypes.JOIN,
127
+ })),
128
+ ...[...usersToUpdate]
129
+ .map((user) => ({
130
+ userId: user,
131
+ state: newCache.get(user),
132
+ event: pondsocket_common_1.PresenceEventTypes.UPDATE,
133
+ })),
134
+ ];
135
+ const upsertMessages = upSertInfo.map(({ userId, state, event }) => {
136
+ this.presenceCache.set(userId, state);
137
+ return {
138
+ event,
139
+ requestId: (0, pondsocket_common_1.uuid)(),
140
+ recipients: Array.from(this.presenceCache.keys()),
141
+ channelName: this.channelId,
142
+ action: pondsocket_common_1.ServerActions.PRESENCE,
143
+ payload: {
144
+ changed: state,
145
+ presence: Array.from(this.presenceCache.values()),
146
+ },
147
+ };
148
+ });
149
+ const removeMessages = [...usersToRemove].map((user) => {
150
+ const current = this.presenceCache.get(user);
151
+ this.presenceCache.delete(user);
152
+ return {
153
+ event: pondsocket_common_1.PresenceEventTypes.LEAVE,
154
+ requestId: (0, pondsocket_common_1.uuid)(),
155
+ recipients: Array.from(this.presenceCache.keys()),
156
+ channelName: this.channelId,
157
+ action: pondsocket_common_1.ServerActions.PRESENCE,
158
+ payload: {
159
+ changed: current,
160
+ presence: Array.from(this.presenceCache.values()),
161
+ },
162
+ };
163
+ });
164
+ [...upsertMessages, ...removeMessages].forEach((message) => this.broadcast(message));
165
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.183",
3
+ "version": "0.1.185",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -34,7 +34,7 @@ class JoinResponse {
34
34
  }
35
35
  accept() {
36
36
  __classPrivateFieldGet(this, _JoinResponse_instances, "m", _JoinResponse_performChecks).call(this);
37
- const onMessage = __classPrivateFieldGet(this, _JoinResponse_instances, "m", _JoinResponse_directMessage).bind(this);
37
+ const onMessage = __classPrivateFieldGet(this, _JoinResponse_engine, "f").parent.parent.sendMessage.bind(__classPrivateFieldGet(this, _JoinResponse_engine, "f").parent.parent, __classPrivateFieldGet(this, _JoinResponse_user, "f").socket);
38
38
  const subscription = __classPrivateFieldGet(this, _JoinResponse_engine, "f").addUser(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, __classPrivateFieldGet(this, _JoinResponse_newAssigns, "f"), onMessage);
39
39
  __classPrivateFieldGet(this, _JoinResponse_user, "f").subscriptions.add(subscription);
40
40
  __classPrivateFieldSet(this, _JoinResponse_accepted, true, "f");