@eleven-am/pondsocket 0.1.197 → 0.1.198

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.
@@ -1,165 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _DistributedManager_instances, _DistributedManager_client, _DistributedManager_subscriptions, _DistributedManager_parseAndUpdatePresence;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.DistributedManager = void 0;
16
- const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
- const manager_1 = require("./manager");
18
- class DistributedManager extends manager_1.Manager {
19
- constructor(client) {
20
- super(client.channelId);
21
- _DistributedManager_instances.add(this);
22
- _DistributedManager_client.set(this, void 0);
23
- _DistributedManager_subscriptions.set(this, null);
24
- __classPrivateFieldSet(this, _DistributedManager_client, client, "f");
25
- }
26
- trackPresence(userId, data) {
27
- const message = this.processPresenceData(manager_1.ActionTypes.CREATE, userId, data);
28
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishPresenceChange(userId, data);
29
- return this.broadcast(message);
30
- }
31
- updatePresence(userId, data) {
32
- const message = this.processPresenceData(manager_1.ActionTypes.UPDATE, userId, data);
33
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishPresenceChange(userId, data);
34
- return this.broadcast(message);
35
- }
36
- removePresence(userId) {
37
- const message = this.processPresenceData(manager_1.ActionTypes.DELETE, userId, null);
38
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishPresenceChange(userId, null);
39
- return this.broadcast(message);
40
- }
41
- setAssigns(userId, data) {
42
- const assigns = this.processAssignsData(manager_1.ActionTypes.CREATE, userId, data);
43
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishAssignsChange(userId, assigns);
44
- }
45
- updateAssigns(userId, data) {
46
- const assigns = this.processAssignsData(manager_1.ActionTypes.UPDATE, userId, data);
47
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishAssignsChange(userId, assigns);
48
- }
49
- removeAssigns(userId) {
50
- this.processAssignsData(manager_1.ActionTypes.DELETE, userId, null);
51
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishAssignsChange(userId, null);
52
- }
53
- removeUser(userId) {
54
- const userData = super.removeUser(userId);
55
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishUserLeave(userId);
56
- return userData;
57
- }
58
- broadcast(message) {
59
- __classPrivateFieldGet(this, _DistributedManager_client, "f").publishChannelMessage(message);
60
- }
61
- initialize(unsubscribe) {
62
- super.initialize(unsubscribe);
63
- const leaveSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToUserLeaves((userId) => {
64
- var _a;
65
- (_a = this.userSubscriptions.get(userId)) === null || _a === void 0 ? void 0 : _a();
66
- this.userSubscriptions.delete(userId);
67
- });
68
- const presenceSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToPresenceChanges(({ userId, state }) => {
69
- if (state) {
70
- this.presenceCache.set(userId, state);
71
- }
72
- else {
73
- this.presenceCache.delete(userId);
74
- }
75
- });
76
- const assignSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToAssignsChanges(({ userId, state }) => {
77
- if (state) {
78
- this.assignsCache.set(userId, state);
79
- }
80
- else {
81
- this.assignsCache.delete(userId);
82
- }
83
- });
84
- const messageSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToChannelMessages((message) => {
85
- this.publisher.publish(message);
86
- });
87
- const stateSyncSubscription = __classPrivateFieldGet(this, _DistributedManager_client, "f").subscribeToStateSync((state) => {
88
- this.assignsCache = new Map(state.assigns);
89
- __classPrivateFieldGet(this, _DistributedManager_instances, "m", _DistributedManager_parseAndUpdatePresence).call(this, new Map(state.presence), state.initialFetch);
90
- });
91
- __classPrivateFieldSet(this, _DistributedManager_subscriptions, () => {
92
- leaveSubscription();
93
- presenceSubscription();
94
- assignSubscription();
95
- messageSubscription();
96
- stateSyncSubscription();
97
- }, "f");
98
- }
99
- close() {
100
- var _a;
101
- (_a = __classPrivateFieldGet(this, _DistributedManager_subscriptions, "f")) === null || _a === void 0 ? void 0 : _a.call(this);
102
- return super.close();
103
- }
104
- }
105
- exports.DistributedManager = DistributedManager;
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
- };
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LocalManager = void 0;
4
- const manager_1 = require("./manager");
5
- class LocalManager extends manager_1.Manager {
6
- constructor(channelId) {
7
- super(channelId);
8
- }
9
- trackPresence(userId, data) {
10
- const message = this.processPresenceData(manager_1.ActionTypes.CREATE, userId, data);
11
- return this.broadcast(message);
12
- }
13
- updatePresence(userId, data) {
14
- const message = this.processPresenceData(manager_1.ActionTypes.UPDATE, userId, data);
15
- return this.broadcast(message);
16
- }
17
- removePresence(userId) {
18
- const message = this.processPresenceData(manager_1.ActionTypes.DELETE, userId, null);
19
- return this.broadcast(message);
20
- }
21
- setAssigns(userId, data) {
22
- return this.processAssignsData(manager_1.ActionTypes.CREATE, userId, data);
23
- }
24
- updateAssigns(userId, data) {
25
- return this.processAssignsData(manager_1.ActionTypes.UPDATE, userId, data);
26
- }
27
- removeAssigns(userId) {
28
- return this.processAssignsData(manager_1.ActionTypes.DELETE, userId, null);
29
- }
30
- removeUser(userId) {
31
- var _a;
32
- const userData = super.removeUser(userId);
33
- (_a = this.userSubscriptions.get(userId)) === null || _a === void 0 ? void 0 : _a();
34
- this.userSubscriptions.delete(userId);
35
- return userData;
36
- }
37
- broadcast(message) {
38
- this.publisher.publish(message);
39
- }
40
- }
41
- exports.LocalManager = LocalManager;
@@ -1,172 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var __rest = (this && this.__rest) || function (s, e) {
14
- var t = {};
15
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
- t[p] = s[p];
17
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
- t[p[i]] = s[p[i]];
21
- }
22
- return t;
23
- };
24
- var _Manager_instances, _Manager_onClose, _Manager_processData;
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.Manager = exports.ActionTypes = void 0;
27
- const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
28
- const httpError_1 = require("../errors/httpError");
29
- var DataTypes;
30
- (function (DataTypes) {
31
- DataTypes["PRESENCE"] = "presence";
32
- DataTypes["ASSIGNS"] = "assigns";
33
- })(DataTypes || (DataTypes = {}));
34
- var ActionTypes;
35
- (function (ActionTypes) {
36
- ActionTypes["CREATE"] = "create";
37
- ActionTypes["UPDATE"] = "update";
38
- ActionTypes["DELETE"] = "delete";
39
- })(ActionTypes || (exports.ActionTypes = ActionTypes = {}));
40
- class Manager {
41
- constructor(channelId) {
42
- _Manager_instances.add(this);
43
- this.channelId = channelId;
44
- this.userSubscriptions = new Map();
45
- this.publisher = new pondsocket_common_1.Subject();
46
- this.presenceCache = new Map();
47
- this.assignsCache = new Map();
48
- _Manager_onClose.set(this, null);
49
- }
50
- get userIds() {
51
- return new Set(this.assignsCache.keys());
52
- }
53
- initialize(unsubscribe) {
54
- __classPrivateFieldSet(this, _Manager_onClose, unsubscribe, "f");
55
- }
56
- getPresence(userId) {
57
- return this.presenceCache.get(userId) || null;
58
- }
59
- getAllPresence() {
60
- return new Map(this.presenceCache);
61
- }
62
- upsertPresence(userId, data) {
63
- if (this.presenceCache.has(userId)) {
64
- this.updatePresence(userId, data);
65
- }
66
- else {
67
- this.trackPresence(userId, data);
68
- }
69
- }
70
- getAssigns(userId) {
71
- return this.assignsCache.get(userId) || null;
72
- }
73
- getAllAssigns() {
74
- return new Map(this.assignsCache);
75
- }
76
- close() {
77
- var _a;
78
- this.publisher.close();
79
- this.presenceCache.clear();
80
- this.assignsCache.clear();
81
- this.userSubscriptions.forEach((unsubscribe) => unsubscribe());
82
- this.userSubscriptions.clear();
83
- (_a = __classPrivateFieldGet(this, _Manager_onClose, "f")) === null || _a === void 0 ? void 0 : _a.call(this);
84
- }
85
- addUser(userId, assigns, onMessage) {
86
- this.setAssigns(userId, assigns);
87
- const subscription = this.publisher.subscribe((_a) => {
88
- var { recipients } = _a, event = __rest(_a, ["recipients"]);
89
- if (recipients.includes(userId)) {
90
- onMessage(event);
91
- }
92
- });
93
- this.userSubscriptions.set(userId, subscription);
94
- }
95
- removeUser(userId) {
96
- var _a;
97
- const userData = this.getUserData(userId);
98
- try {
99
- this.removePresence(userId);
100
- this.removeAssigns(userId);
101
- (_a = this.userSubscriptions.get(userId)) === null || _a === void 0 ? void 0 : _a();
102
- this.userSubscriptions.delete(userId);
103
- if (this.assignsCache.size === 0) {
104
- this.close();
105
- }
106
- }
107
- catch (error) {
108
- // no-op
109
- }
110
- return userData;
111
- }
112
- getUserData(userId) {
113
- const presence = this.getPresence(userId);
114
- const assigns = this.getAssigns(userId);
115
- if (!presence && !assigns) {
116
- const message = `User with id ${userId} does not exist in the channel ${this.channelId}`;
117
- const code = 404;
118
- throw new httpError_1.HttpError(code, message);
119
- }
120
- return {
121
- assigns: assigns || {},
122
- presence: presence || {},
123
- id: userId,
124
- };
125
- }
126
- processPresenceData(action, userId, data) {
127
- const current = __classPrivateFieldGet(this, _Manager_instances, "m", _Manager_processData).call(this, this.presenceCache, DataTypes.PRESENCE, action, userId, data);
128
- const total = Array.from(this.presenceCache.values());
129
- const userIds = Array.from(this.presenceCache.keys());
130
- const event = action === ActionTypes.CREATE ? pondsocket_common_1.PresenceEventTypes.JOIN : action === ActionTypes.UPDATE ? pondsocket_common_1.PresenceEventTypes.UPDATE : pondsocket_common_1.PresenceEventTypes.LEAVE;
131
- return {
132
- event,
133
- requestId: (0, pondsocket_common_1.uuid)(),
134
- recipients: userIds,
135
- channelName: this.channelId,
136
- action: pondsocket_common_1.ServerActions.PRESENCE,
137
- payload: {
138
- changed: current,
139
- presence: total,
140
- },
141
- };
142
- }
143
- processAssignsData(action, userId, data) {
144
- return __classPrivateFieldGet(this, _Manager_instances, "m", _Manager_processData).call(this, this.assignsCache, DataTypes.ASSIGNS, action, userId, data);
145
- }
146
- }
147
- exports.Manager = Manager;
148
- _Manager_onClose = new WeakMap(), _Manager_instances = new WeakSet(), _Manager_processData = function _Manager_processData(cache, dataType, action, userId, data) {
149
- if (action === ActionTypes.CREATE && cache.has(userId)) {
150
- const message = `User with id ${userId} already exists in the ${dataType} cache`;
151
- const code = 409;
152
- throw new httpError_1.HttpError(code, message);
153
- }
154
- else if ((action === ActionTypes.UPDATE || action === ActionTypes.DELETE) && !cache.has(userId)) {
155
- const message = `User with id ${userId} does not exist in the ${dataType} cache`;
156
- const code = 404;
157
- throw new httpError_1.HttpError(code, message);
158
- }
159
- if (action !== ActionTypes.DELETE && !data) {
160
- const message = `Data is required for ${action} action`;
161
- const code = 400;
162
- throw new httpError_1.HttpError(code, message);
163
- }
164
- const current = cache.get(userId);
165
- if (data) {
166
- const updated = Object.assign(Object.assign({}, current), data);
167
- cache.set(userId, updated);
168
- return updated;
169
- }
170
- cache.delete(userId);
171
- return current || {};
172
- };
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ManagerFactory = void 0;
4
- const distributedManager_1 = require("./distributedManager");
5
- const localManager_1 = require("./localManager");
6
- class ManagerFactory {
7
- static create(channelId, clientFactory, onClose) {
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;
18
- }
19
- }
20
- exports.ManagerFactory = ManagerFactory;