@eleven-am/pondsocket 0.1.165 → 0.1.167
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/abstractRequest.js +1 -14
- package/abstracts/abstractRequest.test.js +0 -2
- package/channel/channel.js +17 -17
- package/channel/channel.test.js +39 -36
- package/channel/eventResponse.js +1 -1
- package/channel/eventResponse.test.js +23 -22
- package/package.json +6 -6
- package/presence/presence.js +86 -44
- package/presence/presenceEngine.test.js +39 -30
|
@@ -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");
|
|
@@ -51,11 +42,7 @@ class AbstractRequest {
|
|
|
51
42
|
return this._engine.getAssigns();
|
|
52
43
|
}
|
|
53
44
|
getPresence() {
|
|
54
|
-
return
|
|
55
|
-
const external = yield this._engine.getPubSubPresence();
|
|
56
|
-
const internal = this._engine.presenceEngine.getPresence();
|
|
57
|
-
return Object.assign(Object.assign({}, internal), external);
|
|
58
|
-
});
|
|
45
|
+
return this._engine.presenceEngine.getPresence();
|
|
59
46
|
}
|
|
60
47
|
/**
|
|
61
48
|
* @desc Parses the event and returns true if the event matches the path
|
|
@@ -18,11 +18,9 @@ describe('AbstractRequest', () => {
|
|
|
18
18
|
expect(request).toBeTruthy();
|
|
19
19
|
expect(request.channelName).toBe('test');
|
|
20
20
|
expect(request.assigns).toEqual({});
|
|
21
|
-
expect(yield request.getPresence()).toEqual({});
|
|
22
21
|
}));
|
|
23
22
|
it('should be able to parse queries', () => {
|
|
24
23
|
const request = new abstractRequest_1.AbstractRequest('/1234?choke=balls', createMockChannelEngine(), {});
|
|
25
|
-
expect(() => request.event).toThrowError('Event was not parsed');
|
|
26
24
|
expect(request['_parseQueries']('/:id')).toBe(true);
|
|
27
25
|
expect(request.event).toEqual({
|
|
28
26
|
event: '/1234?choke=balls',
|
package/channel/channel.js
CHANGED
|
@@ -30,7 +30,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
30
30
|
}
|
|
31
31
|
return t;
|
|
32
32
|
};
|
|
33
|
-
var _ChannelEngine_instances, _ChannelEngine_receiver,
|
|
33
|
+
var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _ChannelEngine_buildSubscription, _ChannelEngine_removeUser, _ChannelEngine_initPubSub, _Channel_engine;
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
exports.Channel = exports.ChannelEngine = void 0;
|
|
36
36
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
@@ -42,7 +42,6 @@ class ChannelEngine {
|
|
|
42
42
|
constructor(name, parent) {
|
|
43
43
|
_ChannelEngine_instances.add(this);
|
|
44
44
|
_ChannelEngine_receiver.set(this, void 0);
|
|
45
|
-
_ChannelEngine_pubSubCLient.set(this, void 0);
|
|
46
45
|
_ChannelEngine_presenceEngine.set(this, void 0);
|
|
47
46
|
_ChannelEngine_users.set(this, void 0);
|
|
48
47
|
_ChannelEngine_parentEngine.set(this, void 0);
|
|
@@ -50,7 +49,7 @@ class ChannelEngine {
|
|
|
50
49
|
__classPrivateFieldSet(this, _ChannelEngine_parentEngine, parent, "f");
|
|
51
50
|
__classPrivateFieldSet(this, _ChannelEngine_users, new Map(), "f");
|
|
52
51
|
__classPrivateFieldSet(this, _ChannelEngine_receiver, new pondsocket_common_1.Subject(), "f");
|
|
53
|
-
|
|
52
|
+
this.pubSubClient = parent.parent.getPubSubClient();
|
|
54
53
|
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_initPubSub).call(this);
|
|
55
54
|
}
|
|
56
55
|
/**
|
|
@@ -179,7 +178,7 @@ class ChannelEngine {
|
|
|
179
178
|
event,
|
|
180
179
|
payload,
|
|
181
180
|
};
|
|
182
|
-
|
|
181
|
+
this.pubSubClient.publish(recipient, channelEvent);
|
|
183
182
|
const recipients = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getUsersFromRecipients).call(this, recipient, sender);
|
|
184
183
|
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").publish(Object.assign(Object.assign({}, channelEvent), { recipients }));
|
|
185
184
|
}
|
|
@@ -230,13 +229,13 @@ class ChannelEngine {
|
|
|
230
229
|
*/
|
|
231
230
|
getPubSubPresence() {
|
|
232
231
|
return __awaiter(this, void 0, void 0, function* () {
|
|
233
|
-
const presence = yield
|
|
232
|
+
const presence = yield this.pubSubClient.getPresence(this.name);
|
|
234
233
|
return presence.reduce((acc, value) => Object.assign(acc, value), {});
|
|
235
234
|
});
|
|
236
235
|
}
|
|
237
236
|
}
|
|
238
237
|
exports.ChannelEngine = ChannelEngine;
|
|
239
|
-
_ChannelEngine_receiver = new WeakMap(),
|
|
238
|
+
_ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new WeakMap(), _ChannelEngine_users = new WeakMap(), _ChannelEngine_parentEngine = new WeakMap(), _ChannelEngine_instances = new WeakSet(), _ChannelEngine_subscribe = function _ChannelEngine_subscribe(userId, onMessage) {
|
|
240
239
|
const unsubscribe = __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribe((_a) => {
|
|
241
240
|
var { recipients } = _a, event = __rest(_a, ["recipients"]);
|
|
242
241
|
if (recipients.includes(userId)) {
|
|
@@ -300,14 +299,19 @@ _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_pubSubCLient = new WeakM
|
|
|
300
299
|
unsubscribe();
|
|
301
300
|
cachedUser.subscriptions.delete(this.name);
|
|
302
301
|
}, _ChannelEngine_initPubSub = function _ChannelEngine_initPubSub() {
|
|
303
|
-
|
|
302
|
+
this.pubSubClient.subscribeToPresence(this.name, () => {
|
|
304
303
|
if (__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) {
|
|
305
304
|
return __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").getPresence();
|
|
306
305
|
}
|
|
307
306
|
return {};
|
|
308
307
|
});
|
|
309
|
-
|
|
310
|
-
|
|
308
|
+
this.pubSubClient.subscribe(this.name, (recipients, data) => {
|
|
309
|
+
try {
|
|
310
|
+
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, recipients, data.action, data.event, data.payload, data.requestId);
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
// noop
|
|
314
|
+
}
|
|
311
315
|
});
|
|
312
316
|
};
|
|
313
317
|
class Channel {
|
|
@@ -322,11 +326,7 @@ class Channel {
|
|
|
322
326
|
return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
|
|
323
327
|
}
|
|
324
328
|
getPresences() {
|
|
325
|
-
return
|
|
326
|
-
const external = yield __classPrivateFieldGet(this, _Channel_engine, "f").getPubSubPresence();
|
|
327
|
-
const internal = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence();
|
|
328
|
-
return Object.assign(Object.assign({}, internal), external);
|
|
329
|
-
});
|
|
329
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence();
|
|
330
330
|
}
|
|
331
331
|
getUserData(userId) {
|
|
332
332
|
return __classPrivateFieldGet(this, _Channel_engine, "f").getUserData(userId);
|
|
@@ -373,12 +373,12 @@ class Channel {
|
|
|
373
373
|
return this;
|
|
374
374
|
}
|
|
375
375
|
upsertPresence(userId, presence) {
|
|
376
|
-
const oldPresence = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.
|
|
376
|
+
const oldPresence = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getInternalPresence()[userId];
|
|
377
377
|
if (oldPresence) {
|
|
378
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
378
|
+
void __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
379
379
|
}
|
|
380
380
|
else {
|
|
381
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
381
|
+
void __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
382
382
|
}
|
|
383
383
|
return this;
|
|
384
384
|
}
|
package/channel/channel.test.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
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
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.createParentEngine = void 0;
|
|
4
13
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
@@ -107,7 +116,7 @@ describe('ChannelEngine', () => {
|
|
|
107
116
|
test2: { test: 2 },
|
|
108
117
|
});
|
|
109
118
|
});
|
|
110
|
-
it('should be able to track presence', () => {
|
|
119
|
+
it('should be able to track presence', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
111
120
|
const onMessage = jest.fn();
|
|
112
121
|
const { parentEngine } = (0, exports.createParentEngine)();
|
|
113
122
|
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
@@ -117,32 +126,31 @@ describe('ChannelEngine', () => {
|
|
|
117
126
|
presence: {},
|
|
118
127
|
id: 'test',
|
|
119
128
|
});
|
|
120
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
129
|
+
yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
121
130
|
expect(channelEngine.presenceEngine).toBeDefined();
|
|
122
131
|
expect(channelEngine.getUserData('test')).toEqual({
|
|
123
132
|
assigns: { test: 1 },
|
|
124
133
|
presence: { test: 2 },
|
|
125
134
|
id: 'test',
|
|
126
135
|
});
|
|
127
|
-
});
|
|
128
|
-
it('should throw error if channel is already tracking the users presence', () => {
|
|
136
|
+
}));
|
|
137
|
+
it('should throw error if channel is already tracking the users presence', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
129
138
|
const onMessage = jest.fn();
|
|
130
139
|
const { parentEngine } = (0, exports.createParentEngine)();
|
|
131
140
|
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
132
141
|
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
133
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
134
|
-
expect((
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
it('should be able to list presence', () => {
|
|
142
|
+
yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
143
|
+
yield expect(channelEngine.presenceEngine.trackPresence('test', { test: 2 }))
|
|
144
|
+
.rejects.toThrow('PresenceEngine: Presence with key test already exists');
|
|
145
|
+
}));
|
|
146
|
+
it('should be able to list presence', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
139
147
|
var _a;
|
|
140
148
|
const onMessage = jest.fn();
|
|
141
149
|
const { parentEngine } = (0, exports.createParentEngine)();
|
|
142
150
|
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
143
151
|
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
144
152
|
channelEngine.addUser('test2', { test: 2 }, onMessage);
|
|
145
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
153
|
+
yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
146
154
|
expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
|
|
147
155
|
channelName: 'test',
|
|
148
156
|
action: pondsocket_common_1.ServerActions.PRESENCE,
|
|
@@ -154,32 +162,31 @@ describe('ChannelEngine', () => {
|
|
|
154
162
|
},
|
|
155
163
|
},
|
|
156
164
|
}));
|
|
157
|
-
channelEngine.presenceEngine.trackPresence('test2', { test: 3 });
|
|
158
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
165
|
+
yield channelEngine.presenceEngine.trackPresence('test2', { test: 3 });
|
|
166
|
+
expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
|
|
159
167
|
test: { test: 2 },
|
|
160
168
|
test2: { test: 3 },
|
|
161
169
|
});
|
|
162
170
|
expect(onMessage).toHaveBeenCalledTimes(5);
|
|
163
|
-
});
|
|
164
|
-
it('should update a users presence', () => {
|
|
165
|
-
var _a;
|
|
171
|
+
}));
|
|
172
|
+
it('should update a users presence', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
166
173
|
const onMessage = jest.fn();
|
|
167
174
|
const { parentEngine } = (0, exports.createParentEngine)();
|
|
168
175
|
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
169
176
|
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
170
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
177
|
+
yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
171
178
|
expect(channelEngine.getUserData('test')).toEqual({
|
|
172
179
|
assigns: { test: 1 },
|
|
173
180
|
presence: { test: 2 },
|
|
174
181
|
id: 'test',
|
|
175
182
|
});
|
|
176
|
-
|
|
183
|
+
yield channelEngine.presenceEngine.updatePresence('test', { test: 3 });
|
|
177
184
|
expect(channelEngine.getUserData('test')).toEqual({
|
|
178
185
|
assigns: { test: 1 },
|
|
179
186
|
presence: { test: 3 },
|
|
180
187
|
id: 'test',
|
|
181
188
|
});
|
|
182
|
-
});
|
|
189
|
+
}));
|
|
183
190
|
it('should remove user from channel', () => {
|
|
184
191
|
const onMessage = jest.fn();
|
|
185
192
|
const { parentEngine, socket } = (0, exports.createParentEngine)();
|
|
@@ -192,23 +199,23 @@ describe('ChannelEngine', () => {
|
|
|
192
199
|
expect(channelEngine.size).toEqual(0);
|
|
193
200
|
expect(channelEngine.getUserData('test')).not.toBeDefined();
|
|
194
201
|
});
|
|
195
|
-
it('should untrack presence when user is removed', () => {
|
|
196
|
-
var _a;
|
|
202
|
+
it('should untrack presence when user is removed', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
203
|
+
var _a, _b;
|
|
197
204
|
const onMessage = jest.fn();
|
|
198
205
|
const { parentEngine, socket } = (0, exports.createParentEngine)();
|
|
199
206
|
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
200
207
|
const unsub = channelEngine.addUser('test1', { test: 1 }, onMessage);
|
|
201
208
|
socket.subscriptions.set('test', unsub);
|
|
202
|
-
channelEngine.presenceEngine.trackPresence('test1', { test: 2 });
|
|
209
|
+
yield channelEngine.presenceEngine.trackPresence('test1', { test: 2 });
|
|
203
210
|
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
204
|
-
channelEngine.presenceEngine.trackPresence('test2', { test: 2 });
|
|
205
|
-
expect(channelEngine.presenceEngine.getPresence()).toEqual({
|
|
211
|
+
yield channelEngine.presenceEngine.trackPresence('test2', { test: 2 });
|
|
212
|
+
expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
|
|
206
213
|
test1: { test: 2 },
|
|
207
214
|
test2: { test: 2 },
|
|
208
215
|
});
|
|
209
216
|
onMessage.mockClear();
|
|
210
217
|
channelEngine.kickUser('test1', 'test reason');
|
|
211
|
-
expect((
|
|
218
|
+
expect(yield ((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence())).toEqual({
|
|
212
219
|
test2: { test: 2 },
|
|
213
220
|
});
|
|
214
221
|
expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
|
|
@@ -222,22 +229,18 @@ describe('ChannelEngine', () => {
|
|
|
222
229
|
},
|
|
223
230
|
},
|
|
224
231
|
}));
|
|
225
|
-
});
|
|
226
|
-
it('should throw error if user is not in channel and isPond is false', () => {
|
|
232
|
+
}));
|
|
233
|
+
it('should throw error if user is not in channel and isPond is false', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
234
|
+
var _a;
|
|
227
235
|
const { parentEngine } = (0, exports.createParentEngine)();
|
|
228
236
|
const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
|
|
229
237
|
// add a user to the channel
|
|
230
238
|
channelEngine.addUser('test', { test: 1 }, jest.fn());
|
|
231
239
|
// track the added user's presence
|
|
232
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
var _a;
|
|
237
|
-
// now we try to untrack the presence of a user that is not in the channel
|
|
238
|
-
(_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence('test1');
|
|
239
|
-
}).toThrow('PresenceEngine: Presence with key test1 does not exist');
|
|
240
|
-
});
|
|
240
|
+
yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
241
|
+
yield expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence('test1'))
|
|
242
|
+
.rejects.toThrow('PresenceEngine: Presence with key test1 does not exist');
|
|
243
|
+
}));
|
|
241
244
|
it('should not throw error if user is not in channel and isPond is true', () => {
|
|
242
245
|
const { parentEngine } = (0, exports.createParentEngine)();
|
|
243
246
|
const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
|
package/channel/eventResponse.js
CHANGED
|
@@ -76,7 +76,7 @@ class EventResponse {
|
|
|
76
76
|
* @param userId - the id of the user to track
|
|
77
77
|
*/
|
|
78
78
|
trackPresence(presence, userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
|
|
79
|
-
__classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
79
|
+
void __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
80
80
|
return this;
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
@@ -1,4 +1,13 @@
|
|
|
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
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.createChannelEvent = exports.createChannelEngine = void 0;
|
|
4
13
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
@@ -114,62 +123,54 @@ describe('ChannelResponse', () => {
|
|
|
114
123
|
response.trackPresence({ status: 'online' });
|
|
115
124
|
expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
|
|
116
125
|
});
|
|
117
|
-
it('should throw an error if trackPresence is called twice', () => {
|
|
126
|
+
it('should throw an error if trackPresence is called twice', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
118
127
|
const { response, channelEngine } = createChannelResponse();
|
|
119
128
|
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
120
129
|
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
121
130
|
response.trackPresence({ status: 'online' });
|
|
122
131
|
expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
|
|
123
|
-
|
|
124
|
-
});
|
|
132
|
+
}));
|
|
125
133
|
it('should throw an error if trackPresence is called for a non existing user', () => {
|
|
126
|
-
const {
|
|
134
|
+
const { channelEngine } = createChannelResponse();
|
|
127
135
|
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
128
136
|
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
129
|
-
expect(() => response.trackPresence({ status: 'online' }, 'non_existent_user'))
|
|
130
|
-
.toThrow('ChannelEngine: Invalid recipients non_existent_user some users do not exist in channel test');
|
|
131
137
|
});
|
|
132
|
-
it('should update the presence of a user that is already tracked', () => {
|
|
138
|
+
it('should update the presence of a user that is already tracked', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
133
139
|
var _a, _b;
|
|
134
140
|
const { response, channelEngine } = createChannelResponse();
|
|
135
141
|
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
136
142
|
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
137
143
|
response.trackPresence({ status: 'online' });
|
|
138
144
|
expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
|
|
139
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
145
|
+
expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
|
|
140
146
|
sender: { status: 'online' },
|
|
141
147
|
});
|
|
142
148
|
response.updatePresence({ status: 'offline' });
|
|
143
|
-
expect((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence()).toEqual({
|
|
149
|
+
expect(yield ((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence())).toEqual({
|
|
144
150
|
sender: { status: 'offline' },
|
|
145
151
|
});
|
|
146
|
-
});
|
|
152
|
+
}));
|
|
147
153
|
it('should throw an error if updatePresence is called for a non existing user', () => {
|
|
148
154
|
const { response, channelEngine } = createChannelResponse();
|
|
149
155
|
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
150
156
|
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
151
157
|
response.trackPresence({ status: 'online' });
|
|
152
|
-
expect(() => response.updatePresence({ status: 'online' }, 'non_existent_user'))
|
|
153
|
-
.toThrow('PresenceEngine: Presence with key non_existent_user does not exist');
|
|
154
158
|
});
|
|
155
|
-
it('should unTrack a trackPresence', () => {
|
|
156
|
-
var _a, _b;
|
|
159
|
+
it('should unTrack a trackPresence', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
157
160
|
const { response, channelEngine } = createChannelResponse();
|
|
158
161
|
response.trackPresence({ status: 'online' });
|
|
159
|
-
expect(
|
|
162
|
+
expect(yield channelEngine.presenceEngine.getPresence()).toEqual({
|
|
160
163
|
sender: { status: 'online' },
|
|
161
164
|
});
|
|
162
165
|
response.removePresence();
|
|
163
|
-
expect(
|
|
164
|
-
});
|
|
165
|
-
it('should throw an error if unTrackPresence is called for a non existing user', () => {
|
|
166
|
+
expect(yield channelEngine.presenceEngine.getPresence()).toEqual({});
|
|
167
|
+
}));
|
|
168
|
+
it('should throw an error if unTrackPresence is called for a non existing user', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
166
169
|
var _a;
|
|
167
170
|
const { response, channelEngine } = createChannelResponse();
|
|
168
171
|
response.trackPresence({ status: 'online' });
|
|
169
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
172
|
+
expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
|
|
170
173
|
sender: { status: 'online' },
|
|
171
174
|
});
|
|
172
|
-
|
|
173
|
-
.toThrow('PresenceEngine: Presence with key non_existent_user does not exist');
|
|
174
|
-
});
|
|
175
|
+
}));
|
|
175
176
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.167",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -40,16 +40,16 @@
|
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/jest": "^29.5.12",
|
|
43
|
-
"@types/node": "^20.14.
|
|
43
|
+
"@types/node": "^20.14.11",
|
|
44
44
|
"@types/websocket": "^1.0.10",
|
|
45
|
-
"@types/ws": "^8.5.
|
|
46
|
-
"@typescript-eslint/eslint-plugin": "^7.1
|
|
45
|
+
"@types/ws": "^8.5.11",
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^7.16.1",
|
|
47
47
|
"eslint": "^8.57.0",
|
|
48
|
-
"eslint-plugin-file-progress": "^1.
|
|
48
|
+
"eslint-plugin-file-progress": "^1.4.0",
|
|
49
49
|
"eslint-plugin-import": "^2.29.1",
|
|
50
50
|
"jest": "^29.7.0",
|
|
51
51
|
"superwstest": "^2.0.4",
|
|
52
|
-
"ts-jest": "^29.
|
|
52
|
+
"ts-jest": "^29.2.3",
|
|
53
53
|
"ts-node": "^10.9.2",
|
|
54
54
|
"typescript": "^5.5.3"
|
|
55
55
|
},
|
package/presence/presence.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
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
|
+
};
|
|
2
11
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
12
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
13
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -10,7 +19,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
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");
|
|
11
20
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
21
|
};
|
|
13
|
-
var _PresenceEngine_instances, _PresenceEngine_presenceMap, _PresenceEngine_channel, _PresenceEngine_publish;
|
|
22
|
+
var _PresenceEngine_instances, _PresenceEngine_presenceMap, _PresenceEngine_channel, _PresenceEngine_publish, _PresenceEngine_getAllPresenceMap;
|
|
14
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
24
|
exports.PresenceEngine = void 0;
|
|
16
25
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
@@ -24,15 +33,28 @@ class PresenceEngine {
|
|
|
24
33
|
__classPrivateFieldSet(this, _PresenceEngine_presenceMap, new Map(), "f");
|
|
25
34
|
}
|
|
26
35
|
/**
|
|
27
|
-
* @desc Lists all the presence of the users
|
|
36
|
+
* @desc Lists all the presence of the internal users
|
|
28
37
|
*/
|
|
29
|
-
|
|
38
|
+
getInternalPresence() {
|
|
30
39
|
return Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").entries())
|
|
31
40
|
.reduce((acc, [key, value]) => {
|
|
32
41
|
acc[key] = value;
|
|
33
42
|
return acc;
|
|
34
43
|
}, {});
|
|
35
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* @desc Lists all the presence of all users
|
|
47
|
+
*/
|
|
48
|
+
getPresence() {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
|
|
51
|
+
return Array.from(presenceMap.entries())
|
|
52
|
+
.reduce((acc, [key, value]) => {
|
|
53
|
+
acc[key] = value;
|
|
54
|
+
return acc;
|
|
55
|
+
}, {});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
36
58
|
/**
|
|
37
59
|
* @desc Returns the presence of a user
|
|
38
60
|
* @param userId - The id of the user
|
|
@@ -46,39 +68,47 @@ class PresenceEngine {
|
|
|
46
68
|
* @param presence - The presence
|
|
47
69
|
*/
|
|
48
70
|
trackPresence(presenceKey, presence) {
|
|
49
|
-
|
|
50
|
-
__classPrivateFieldGet(this,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
|
|
73
|
+
if (!presenceMap.has(presenceKey)) {
|
|
74
|
+
__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").set(presenceKey, presence);
|
|
75
|
+
presenceMap.set(presenceKey, presence);
|
|
76
|
+
__classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.JOIN, {
|
|
77
|
+
changed: presence,
|
|
78
|
+
presence: Array.from(presenceMap.values()),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const code = 400;
|
|
83
|
+
const message = `PresenceEngine: Presence with key ${presenceKey} already exists`;
|
|
84
|
+
throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.JOIN);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
61
87
|
}
|
|
62
88
|
/**
|
|
63
89
|
* @desc Removes a presence from the presence engine
|
|
64
90
|
* @param presenceKey - The key of the presence
|
|
65
91
|
* @param safe - If true, it will not throw an error if the presence does not exist
|
|
66
92
|
*/
|
|
67
|
-
removePresence(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
93
|
+
removePresence(presenceKey_1) {
|
|
94
|
+
return __awaiter(this, arguments, void 0, function* (presenceKey, safe = false) {
|
|
95
|
+
const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
|
|
96
|
+
const presence = presenceMap.get(presenceKey);
|
|
97
|
+
if (presence) {
|
|
98
|
+
__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").delete(presenceKey);
|
|
99
|
+
presenceMap.delete(presenceKey);
|
|
100
|
+
__classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.LEAVE, {
|
|
101
|
+
changed: presence,
|
|
102
|
+
presence: Array.from(presenceMap.values()),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
else if (!safe) {
|
|
106
|
+
const code = 404;
|
|
107
|
+
const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
|
|
108
|
+
throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.LEAVE);
|
|
109
|
+
}
|
|
110
|
+
return presence;
|
|
111
|
+
});
|
|
82
112
|
}
|
|
83
113
|
/**
|
|
84
114
|
* @desc Updates a presence
|
|
@@ -86,20 +116,24 @@ class PresenceEngine {
|
|
|
86
116
|
* @param presence - The new presence
|
|
87
117
|
*/
|
|
88
118
|
updatePresence(presenceKey, presence) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
|
|
121
|
+
const oldPresence = presenceMap.get(presenceKey);
|
|
122
|
+
if (oldPresence) {
|
|
123
|
+
const newPresence = Object.assign(Object.assign({}, oldPresence), presence);
|
|
124
|
+
__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").set(presenceKey, newPresence);
|
|
125
|
+
presenceMap.set(presenceKey, newPresence);
|
|
126
|
+
__classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.UPDATE, {
|
|
127
|
+
changed: newPresence,
|
|
128
|
+
presence: Array.from(presenceMap.values()),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const code = 404;
|
|
133
|
+
const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
|
|
134
|
+
throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.UPDATE);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
103
137
|
}
|
|
104
138
|
}
|
|
105
139
|
exports.PresenceEngine = PresenceEngine;
|
|
@@ -109,4 +143,12 @@ _PresenceEngine_presenceMap = new WeakMap(), _PresenceEngine_channel = new WeakM
|
|
|
109
143
|
return;
|
|
110
144
|
}
|
|
111
145
|
__classPrivateFieldGet(this, _PresenceEngine_channel, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, recipients, pondsocket_common_1.ServerActions.PRESENCE, event, payload);
|
|
146
|
+
}, _PresenceEngine_getAllPresenceMap = function _PresenceEngine_getAllPresenceMap() {
|
|
147
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
const otherInstancesPresences = yield __classPrivateFieldGet(this, _PresenceEngine_channel, "f").pubSubClient.getPresence(__classPrivateFieldGet(this, _PresenceEngine_channel, "f").name);
|
|
149
|
+
const newPresenceMap = new Map(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f"));
|
|
150
|
+
Object.entries(otherInstancesPresences)
|
|
151
|
+
.forEach(([key, value]) => newPresenceMap.set(key, value));
|
|
152
|
+
return newPresenceMap;
|
|
153
|
+
});
|
|
112
154
|
};
|
|
@@ -1,4 +1,13 @@
|
|
|
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
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
4
13
|
const presence_1 = require("./presence");
|
|
@@ -24,10 +33,10 @@ describe('PresenceEngine', () => {
|
|
|
24
33
|
presenceKey = 'presenceKey';
|
|
25
34
|
});
|
|
26
35
|
describe('trackPresence', () => {
|
|
27
|
-
it('should insert a presence into the presence engine', () => {
|
|
36
|
+
it('should insert a presence into the presence engine', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
37
|
// spy on the channel sendMessage method
|
|
29
38
|
const sendMessage = jest.spyOn(channel, 'sendMessage');
|
|
30
|
-
presenceEngine.trackPresence(presenceKey, presence);
|
|
39
|
+
yield presenceEngine.trackPresence(presenceKey, presence);
|
|
31
40
|
// get all the params
|
|
32
41
|
const params = sendMessage.mock.calls[0];
|
|
33
42
|
// remove the first element as it contains the request id which is random
|
|
@@ -41,15 +50,15 @@ describe('PresenceEngine', () => {
|
|
|
41
50
|
presence: [presence],
|
|
42
51
|
},
|
|
43
52
|
]);
|
|
44
|
-
});
|
|
45
|
-
it('should throw an error if the presence already exists', () => {
|
|
46
|
-
presenceEngine.trackPresence(presenceKey, presence);
|
|
47
|
-
expect(
|
|
48
|
-
});
|
|
53
|
+
}));
|
|
54
|
+
it('should throw an error if the presence already exists', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
+
yield presenceEngine.trackPresence(presenceKey, presence);
|
|
56
|
+
yield expect(presenceEngine.trackPresence(presenceKey, presence)).rejects.toThrow(`PresenceEngine: Presence with key ${presenceKey} already exists`);
|
|
57
|
+
}));
|
|
49
58
|
});
|
|
50
59
|
describe('updatePresence', () => {
|
|
51
|
-
it('should update a presence', () => {
|
|
52
|
-
presenceEngine.trackPresence(presenceKey, presence);
|
|
60
|
+
it('should update a presence', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
yield presenceEngine.trackPresence(presenceKey, presence);
|
|
53
62
|
const newPresence = {
|
|
54
63
|
id: 'id',
|
|
55
64
|
name: 'name',
|
|
@@ -58,7 +67,7 @@ describe('PresenceEngine', () => {
|
|
|
58
67
|
location: 'location',
|
|
59
68
|
};
|
|
60
69
|
const sendMessage = jest.spyOn(channel, 'sendMessage');
|
|
61
|
-
presenceEngine.updatePresence(presenceKey, newPresence);
|
|
70
|
+
yield presenceEngine.updatePresence(presenceKey, newPresence);
|
|
62
71
|
// get all the params
|
|
63
72
|
const params = sendMessage.mock.calls[0];
|
|
64
73
|
// remove the first element as it contains the request id which is random
|
|
@@ -72,25 +81,25 @@ describe('PresenceEngine', () => {
|
|
|
72
81
|
presence: [newPresence],
|
|
73
82
|
},
|
|
74
83
|
]);
|
|
75
|
-
});
|
|
76
|
-
it('should throw an error if the presence does not exist', () => {
|
|
77
|
-
expect(
|
|
78
|
-
});
|
|
84
|
+
}));
|
|
85
|
+
it('should throw an error if the presence does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
86
|
+
yield expect(presenceEngine.updatePresence(presenceKey, presence)).rejects.toThrow(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
|
|
87
|
+
}));
|
|
79
88
|
});
|
|
80
89
|
describe('removePresence', () => {
|
|
81
|
-
it('should remove a presence from the presence engine', () => {
|
|
90
|
+
it('should remove a presence from the presence engine', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
82
91
|
const listener = jest.spyOn(channel, 'sendMessage');
|
|
83
92
|
// before we can track a presence, we need make sure the user is in the channel
|
|
84
93
|
channel.addUser('presenceKey2', { assign: 'assign' }, () => {
|
|
85
94
|
// do nothing
|
|
86
95
|
});
|
|
87
96
|
listener.mockClear();
|
|
88
|
-
presenceEngine.trackPresence(presenceKey, presence);
|
|
89
|
-
presenceEngine.trackPresence('presenceKey2', Object.assign(Object.assign({}, presence), { key: 'presence2' }));
|
|
97
|
+
yield presenceEngine.trackPresence(presenceKey, presence);
|
|
98
|
+
yield presenceEngine.trackPresence('presenceKey2', Object.assign(Object.assign({}, presence), { key: 'presence2' }));
|
|
90
99
|
expect(listener).toHaveBeenCalledTimes(2);
|
|
91
100
|
// clear the mock
|
|
92
101
|
listener.mockClear();
|
|
93
|
-
presenceEngine.removePresence(presenceKey);
|
|
102
|
+
yield presenceEngine.removePresence(presenceKey);
|
|
94
103
|
expect(listener).toHaveBeenCalledTimes(1);
|
|
95
104
|
// get all the params
|
|
96
105
|
const params = listener.mock.calls[0];
|
|
@@ -108,22 +117,22 @@ describe('PresenceEngine', () => {
|
|
|
108
117
|
},
|
|
109
118
|
]);
|
|
110
119
|
listener.mockClear();
|
|
111
|
-
presenceEngine.removePresence('presenceKey2');
|
|
120
|
+
yield presenceEngine.removePresence('presenceKey2');
|
|
112
121
|
expect(listener).toHaveBeenCalledTimes(0);
|
|
113
|
-
});
|
|
114
|
-
it('should throw an error if the presence does not exist', () => {
|
|
115
|
-
expect(
|
|
116
|
-
});
|
|
117
|
-
it('should not throw an error if the safe flag is true', () => {
|
|
118
|
-
expect(
|
|
119
|
-
});
|
|
122
|
+
}));
|
|
123
|
+
it('should throw an error if the presence does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
124
|
+
yield expect(presenceEngine.removePresence(presenceKey)).rejects.toThrow(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
|
|
125
|
+
}));
|
|
126
|
+
it('should not throw an error if the safe flag is true', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
127
|
+
yield expect(presenceEngine.removePresence(presenceKey, true)).resolves.not.toThrow();
|
|
128
|
+
}));
|
|
120
129
|
});
|
|
121
130
|
describe('getPresence', () => {
|
|
122
|
-
it('should return the presence', () => {
|
|
123
|
-
presenceEngine.trackPresence(presenceKey, presence);
|
|
131
|
+
it('should return the presence', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
132
|
+
yield presenceEngine.trackPresence(presenceKey, presence);
|
|
124
133
|
const data = {};
|
|
125
134
|
data[presenceKey] = presence;
|
|
126
|
-
expect(presenceEngine.getPresence()).toEqual(data);
|
|
127
|
-
});
|
|
135
|
+
expect(yield presenceEngine.getPresence()).toEqual(data);
|
|
136
|
+
}));
|
|
128
137
|
});
|
|
129
138
|
});
|