@eleven-am/pondsocket 0.1.141 → 0.1.142

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.
@@ -5,18 +5,43 @@ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
5
5
  const channel_1 = require("./channel");
6
6
  const eventRequest_1 = require("./eventRequest");
7
7
  const eventResponse_1 = require("./eventResponse");
8
+ const endpoint_test_1 = require("../endpoint/endpoint.test");
8
9
  const createParentEngine = () => {
9
- const parentEngine = {
10
- destroyChannel: jest.fn(),
10
+ const socket = {
11
+ clientId: 'test2',
12
+ assigns: { assign: 'assign' },
13
+ channelName: 'channel',
14
+ requestId: 'requestId',
15
+ subscriptions: new Map(),
16
+ pendingSubscriptions: new Set(),
17
+ socket: {
18
+ send: jest.fn(),
19
+ },
20
+ };
21
+ const lobbyEngineMock = {
22
+ onEvent: jest.fn(),
23
+ onLeave: jest.fn(),
24
+ broadcast: jest.fn(),
11
25
  execute: jest.fn(),
26
+ getChannel: jest.fn(),
27
+ destroyChannel: jest.fn(),
28
+ listChannels: jest.fn(),
29
+ createChannel: jest.fn(),
30
+ parent: (0, endpoint_test_1.createEndpointEngine)(socket),
31
+ middleware: {
32
+ run: jest.fn(),
33
+ },
34
+ };
35
+ return {
36
+ parentEngine: lobbyEngineMock,
37
+ socket,
12
38
  };
13
- return parentEngine;
14
39
  };
15
40
  exports.createParentEngine = createParentEngine;
16
41
  describe('ChannelEngine', () => {
17
42
  it('should add user to channel', () => {
18
43
  const onMessage = jest.fn();
19
- const parentEngine = (0, exports.createParentEngine)();
44
+ const { parentEngine } = (0, exports.createParentEngine)();
20
45
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
21
46
  expect(channelEngine.size).toEqual(0);
22
47
  expect(channelEngine.getUserData('test')).not.toBeDefined();
@@ -30,7 +55,7 @@ describe('ChannelEngine', () => {
30
55
  });
31
56
  it('should throw error if user is already in channel', () => {
32
57
  const onMessage = jest.fn();
33
- const parentEngine = (0, exports.createParentEngine)();
58
+ const { parentEngine } = (0, exports.createParentEngine)();
34
59
  const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
35
60
  channelEngine.addUser('test', { test: 1 }, onMessage);
36
61
  expect(() => {
@@ -38,22 +63,22 @@ describe('ChannelEngine', () => {
38
63
  }).toThrow('ChannelEngine: User with id test already exists in channel testChannel');
39
64
  });
40
65
  it('should throw error if user is not in channel: updateAssigns', () => {
41
- const parentEngine = (0, exports.createParentEngine)();
66
+ const { parentEngine } = (0, exports.createParentEngine)();
42
67
  const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
43
68
  expect(() => {
44
69
  channelEngine.updateAssigns('test', { test: 2 });
45
70
  }).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
46
71
  });
47
72
  it('should throw error if user is not in channel: removeUser', () => {
48
- const parentEngine = (0, exports.createParentEngine)();
73
+ const { parentEngine } = (0, exports.createParentEngine)();
49
74
  const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
50
75
  expect(() => {
51
- channelEngine.removeUser('test', false);
52
- }).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
76
+ channelEngine.kickUser('test', 'test reason');
77
+ }).toThrow('ChannelEngine: Invalid recipients test some users do not exist in channel testChannel');
53
78
  });
54
79
  it('should update a users assigns', () => {
55
80
  const onMessage = jest.fn();
56
- const parentEngine = (0, exports.createParentEngine)();
81
+ const { parentEngine, socket } = (0, exports.createParentEngine)();
57
82
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
58
83
  channelEngine.addUser('test', { test: 1 }, onMessage);
59
84
  expect(channelEngine.getUserData('test')).toEqual({
@@ -70,7 +95,7 @@ describe('ChannelEngine', () => {
70
95
  });
71
96
  it('should be able to get users assigns in the channel', () => {
72
97
  const onMessage = jest.fn();
73
- const parentEngine = (0, exports.createParentEngine)();
98
+ const { parentEngine } = (0, exports.createParentEngine)();
74
99
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
75
100
  expect(channelEngine.getAssigns()).toEqual({});
76
101
  channelEngine.addUser('test', { test: 1 }, onMessage);
@@ -85,7 +110,7 @@ describe('ChannelEngine', () => {
85
110
  });
86
111
  it('should be able to track presence', () => {
87
112
  const onMessage = jest.fn();
88
- const parentEngine = (0, exports.createParentEngine)();
113
+ const { parentEngine } = (0, exports.createParentEngine)();
89
114
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
90
115
  channelEngine.addUser('test', { test: 1 }, onMessage);
91
116
  expect(channelEngine.getUserData('test')).toEqual({
@@ -103,7 +128,7 @@ describe('ChannelEngine', () => {
103
128
  });
104
129
  it('should throw error if channel is already tracking the users presence', () => {
105
130
  const onMessage = jest.fn();
106
- const parentEngine = (0, exports.createParentEngine)();
131
+ const { parentEngine } = (0, exports.createParentEngine)();
107
132
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
108
133
  channelEngine.addUser('test', { test: 1 }, onMessage);
109
134
  channelEngine.presenceEngine.trackPresence('test', { test: 2 });
@@ -114,7 +139,7 @@ describe('ChannelEngine', () => {
114
139
  it('should be able to list presence', () => {
115
140
  var _a;
116
141
  const onMessage = jest.fn();
117
- const parentEngine = (0, exports.createParentEngine)();
142
+ const { parentEngine } = (0, exports.createParentEngine)();
118
143
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
119
144
  channelEngine.addUser('test', { test: 1 }, onMessage);
120
145
  channelEngine.addUser('test2', { test: 2 }, onMessage);
@@ -140,7 +165,7 @@ describe('ChannelEngine', () => {
140
165
  it('should update a users presence', () => {
141
166
  var _a;
142
167
  const onMessage = jest.fn();
143
- const parentEngine = (0, exports.createParentEngine)();
168
+ const { parentEngine } = (0, exports.createParentEngine)();
144
169
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
145
170
  channelEngine.addUser('test', { test: 1 }, onMessage);
146
171
  channelEngine.presenceEngine.trackPresence('test', { test: 2 });
@@ -158,31 +183,33 @@ describe('ChannelEngine', () => {
158
183
  });
159
184
  it('should remove user from channel', () => {
160
185
  const onMessage = jest.fn();
161
- const parentEngine = (0, exports.createParentEngine)();
186
+ const { parentEngine, socket } = (0, exports.createParentEngine)();
162
187
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
163
- channelEngine.addUser('test', { test: 1 }, onMessage);
188
+ const unsub = channelEngine.addUser('test1', { test: 1 }, onMessage);
189
+ socket.subscriptions.set('test', unsub);
164
190
  expect(channelEngine.size).toEqual(1);
165
- expect(channelEngine.getUserData('test')).toBeDefined();
166
- channelEngine.removeUser('test', true);
191
+ expect(channelEngine.getUserData('test1')).toBeDefined();
192
+ channelEngine.kickUser('test1', 'test reason');
167
193
  expect(channelEngine.size).toEqual(0);
168
194
  expect(channelEngine.getUserData('test')).not.toBeDefined();
169
195
  });
170
196
  it('should untrack presence when user is removed', () => {
171
- var _a, _b;
197
+ var _a;
172
198
  const onMessage = jest.fn();
173
- const parentEngine = (0, exports.createParentEngine)();
199
+ const { parentEngine, socket } = (0, exports.createParentEngine)();
174
200
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
175
- channelEngine.addUser('test', { test: 1 }, onMessage);
176
- channelEngine.presenceEngine.trackPresence('test', { test: 2 });
201
+ const unsub = channelEngine.addUser('test1', { test: 1 }, onMessage);
202
+ socket.subscriptions.set('test', unsub);
203
+ channelEngine.presenceEngine.trackPresence('test1', { test: 2 });
177
204
  channelEngine.addUser('test2', { test: 1 }, onMessage);
178
205
  channelEngine.presenceEngine.trackPresence('test2', { test: 2 });
179
- expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
180
- test: { test: 2 },
206
+ expect(channelEngine.presenceEngine.getPresence()).toEqual({
207
+ test1: { test: 2 },
181
208
  test2: { test: 2 },
182
209
  });
183
210
  onMessage.mockClear();
184
- channelEngine.removeUser('test', true);
185
- expect((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence()).toEqual({
211
+ channelEngine.kickUser('test1', 'test reason');
212
+ expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
186
213
  test2: { test: 2 },
187
214
  });
188
215
  expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
@@ -198,7 +225,7 @@ describe('ChannelEngine', () => {
198
225
  }));
199
226
  });
200
227
  it('should throw error if user is not in channel and isPond is false', () => {
201
- const parentEngine = (0, exports.createParentEngine)();
228
+ const { parentEngine } = (0, exports.createParentEngine)();
202
229
  const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
203
230
  // add a user to the channel
204
231
  channelEngine.addUser('test', { test: 1 }, jest.fn());
@@ -213,7 +240,7 @@ describe('ChannelEngine', () => {
213
240
  }).toThrow('PresenceEngine: Presence with key test1 does not exist');
214
241
  });
215
242
  it('should not throw error if user is not in channel and isPond is true', () => {
216
- const parentEngine = (0, exports.createParentEngine)();
243
+ const { parentEngine } = (0, exports.createParentEngine)();
217
244
  const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
218
245
  // add a user to the channel
219
246
  channelEngine.addUser('test', { test: 1 }, jest.fn());
@@ -229,11 +256,12 @@ describe('ChannelEngine', () => {
229
256
  });
230
257
  it('should be able to kick a user from the channel', () => {
231
258
  const onMessage = jest.fn();
232
- const parentEngine = (0, exports.createParentEngine)();
259
+ const { parentEngine, socket } = (0, exports.createParentEngine)();
233
260
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
234
- channelEngine.addUser('test', { test: 1 }, onMessage);
235
- channelEngine.addUser('test2', { test: 1 }, onMessage);
236
- channelEngine.kickUser('test2', 'test reason');
261
+ channelEngine.addUser('test1', { test: 1 }, onMessage);
262
+ const unsub = channelEngine.addUser('test2', { test: 1 }, onMessage);
263
+ socket.subscriptions.set('test', unsub);
264
+ channelEngine.kickUser('test1', 'test reason');
237
265
  expect(channelEngine.size).toEqual(1);
238
266
  expect(channelEngine.getUserData('test2')).not.toBeDefined();
239
267
  expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
@@ -251,27 +279,27 @@ describe('ChannelEngine', () => {
251
279
  event: 'kicked',
252
280
  payload: {
253
281
  reason: 'test reason',
254
- userId: 'test2',
282
+ userId: 'test1',
255
283
  },
256
284
  }));
257
285
  expect(onMessage).toHaveBeenCalledTimes(2);
258
286
  });
259
287
  it('should call destroy on parent engine', () => {
260
- const parentEngine = (0, exports.createParentEngine)();
288
+ const { parentEngine } = (0, exports.createParentEngine)();
261
289
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
262
290
  channelEngine.destroy('test');
263
291
  expect(parentEngine.destroyChannel).toHaveBeenCalled();
264
292
  });
265
293
  it('should broadcast a message to all users', () => {
266
294
  const onMessage = jest.fn();
267
- const parentEngine = (0, exports.createParentEngine)();
268
- parentEngine.execute = (_, res) => {
269
- res.accept();
295
+ const { parentEngine } = (0, exports.createParentEngine)();
296
+ parentEngine.middleware.run = (_, res) => {
297
+ // res.accept();
270
298
  };
271
299
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
272
300
  channelEngine.addUser('test', { test: 1 }, onMessage);
273
301
  channelEngine.addUser('test2', { test: 1 }, onMessage);
274
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
302
+ channelEngine.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
275
303
  expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
276
304
  action: pondsocket_common_1.ServerActions.BROADCAST,
277
305
  channelName: 'test',
@@ -282,24 +310,24 @@ describe('ChannelEngine', () => {
282
310
  }));
283
311
  expect(onMessage).toHaveBeenCalledTimes(2);
284
312
  onMessage.mockClear();
285
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), 'test2', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
313
+ channelEngine.sendMessage('test2', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
286
314
  expect(onMessage).toHaveBeenCalledTimes(1);
287
315
  onMessage.mockClear();
288
316
  // when user is not in channel it throws an error
289
317
  expect(() => {
290
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), 'test3', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
318
+ channelEngine.sendMessage('test3', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
291
319
  }).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
292
320
  });
293
321
  it('should broadcast a message to all users except sender', () => {
294
322
  const onMessage = jest.fn();
295
- const parentEngine = (0, exports.createParentEngine)();
296
- parentEngine.execute = (_, res) => {
297
- res.accept();
323
+ const { parentEngine } = (0, exports.createParentEngine)();
324
+ parentEngine.middleware.run = (_, res) => {
325
+ // res.accept();
298
326
  };
299
327
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
300
328
  channelEngine.addUser('test', { test: 1 }, onMessage);
301
329
  channelEngine.addUser('test2', { test: 1 }, onMessage);
302
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), 'test2', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
330
+ channelEngine.sendMessage('test2', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
303
331
  expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
304
332
  action: pondsocket_common_1.ServerActions.BROADCAST,
305
333
  channelName: 'test',
@@ -311,24 +339,24 @@ describe('ChannelEngine', () => {
311
339
  expect(onMessage).toHaveBeenCalledTimes(1);
312
340
  // when user is not in channel it throws an error
313
341
  expect(() => {
314
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), 'test3', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
342
+ channelEngine.sendMessage('test3', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
315
343
  }).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
316
344
  // when sender is channel itself it throws an error
317
345
  expect(() => {
318
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
346
+ channelEngine.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
319
347
  }).toThrow(`ChannelEngine: Cannot use ${pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER} with ${pondsocket_common_1.SystemSender.CHANNEL}`);
320
348
  });
321
349
  it('should broadcast a message to specific users', () => {
322
350
  const onMessage = jest.fn();
323
- const parentEngine = (0, exports.createParentEngine)();
324
- parentEngine.execute = (_, res) => {
325
- res.accept();
351
+ const { parentEngine } = (0, exports.createParentEngine)();
352
+ parentEngine.middleware.run = (_, res) => {
353
+ // res.accept();
326
354
  };
327
355
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
328
356
  channelEngine.addUser('test', { test: 1 }, onMessage);
329
357
  channelEngine.addUser('test2', { test: 1 }, onMessage);
330
358
  channelEngine.addUser('test3', { test: 1 }, onMessage);
331
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), 'test2', ['test', 'test3'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
359
+ channelEngine.sendMessage('test2', ['test', 'test3'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
332
360
  expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
333
361
  action: pondsocket_common_1.ServerActions.BROADCAST,
334
362
  channelName: 'test',
@@ -348,21 +376,20 @@ describe('ChannelEngine', () => {
348
376
  expect(onMessage).toHaveBeenCalledTimes(2);
349
377
  // when recipient is not in channel it throws an error
350
378
  expect(() => {
351
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), 'test3', ['test', 'test3', 'test4'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
379
+ channelEngine.sendMessage('test3', ['test', 'test3', 'test4'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
352
380
  }).toThrow('ChannelEngine: Invalid recipients test,test3,test4 some users do not exist in channel test');
353
381
  // when sender is not in channel it throws an error
354
382
  expect(() => {
355
- channelEngine.sendMessage((0, pondsocket_common_1.uuid)(), 'test4', ['test', 'test3'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
383
+ channelEngine.sendMessage('test4', ['test', 'test3'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
356
384
  }).toThrow('ChannelEngine: User with id test4 does not exist in channel test');
357
385
  });
358
386
  it('should broadcast messages while also triggering the onMessage callback', () => {
359
387
  const onMessage = jest.fn();
360
- const parentEngine = (0, exports.createParentEngine)();
388
+ const { parentEngine } = (0, exports.createParentEngine)();
361
389
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
362
- parentEngine.execute = (req, res) => {
390
+ parentEngine.middleware.run = (req, res) => {
363
391
  expect(req).toBeInstanceOf(eventRequest_1.EventRequest);
364
392
  expect(res).toBeInstanceOf(eventResponse_1.EventResponse);
365
- res.accept();
366
393
  };
367
394
  expect(() => channelEngine.broadcastMessage('test2', {
368
395
  action: pondsocket_common_1.ClientActions.BROADCAST,
@@ -370,7 +397,6 @@ describe('ChannelEngine', () => {
370
397
  event: 'test',
371
398
  payload: { test: 1 },
372
399
  requestId: (0, pondsocket_common_1.uuid)(),
373
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
374
400
  })).toThrow('ChannelEngine: User with id test2 does not exist in channel test');
375
401
  channelEngine.addUser('test2', { test: 1 }, onMessage);
376
402
  channelEngine.broadcastMessage('test2', {
@@ -379,12 +405,12 @@ describe('ChannelEngine', () => {
379
405
  event: 'test',
380
406
  payload: { test: 1 },
381
407
  requestId: (0, pondsocket_common_1.uuid)(),
382
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
383
408
  });
384
- expect(onMessage).toHaveBeenCalledTimes(1);
409
+ // This is because the message didn't perform any sort of broadcast
410
+ expect(onMessage).toHaveBeenCalledTimes(0);
385
411
  let count = 0;
386
412
  onMessage.mockClear();
387
- parentEngine.execute = (req, res, next) => {
413
+ parentEngine.middleware.run = (req, res, next) => {
388
414
  expect(req).toBeInstanceOf(eventRequest_1.EventRequest);
389
415
  expect(res).toBeInstanceOf(eventResponse_1.EventResponse);
390
416
  count++;
@@ -396,7 +422,6 @@ describe('ChannelEngine', () => {
396
422
  event: 'test',
397
423
  payload: { test: 1 },
398
424
  requestId: (0, pondsocket_common_1.uuid)(),
399
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
400
425
  });
401
426
  // proves that the message was received by the parent engine
402
427
  expect(count).toBe(1);
@@ -4,20 +4,20 @@ const eventRequest_1 = require("./eventRequest");
4
4
  const eventResponse_test_1 = require("./eventResponse.test");
5
5
  describe('ChannelRequest', () => {
6
6
  it('should create a new ChannelRequest', () => {
7
- const channelEngine = (0, eventResponse_test_1.createChannelEngine)();
7
+ const { channelEngine } = (0, eventResponse_test_1.createChannelEngine)();
8
8
  const event = (0, eventResponse_test_1.createChannelEvent)(channelEngine.name);
9
9
  const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
10
10
  expect(channelRequest).toBeDefined();
11
11
  });
12
12
  it('should return the payload', () => {
13
- const channelEngine = (0, eventResponse_test_1.createChannelEngine)();
13
+ const { channelEngine } = (0, eventResponse_test_1.createChannelEngine)();
14
14
  const event = (0, eventResponse_test_1.createChannelEvent)(channelEngine.name);
15
15
  const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
16
16
  channelRequest._parseQueries('event');
17
17
  expect(channelRequest.event.payload).toEqual(event.payload);
18
18
  });
19
19
  it('should return the user', () => {
20
- const channelEngine = (0, eventResponse_test_1.createChannelEngine)();
20
+ const { channelEngine } = (0, eventResponse_test_1.createChannelEngine)();
21
21
  const event = (0, eventResponse_test_1.createChannelEvent)(channelEngine.name);
22
22
  const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
23
23
  // because the user in the event does not exist in the channel, this should throw an error
@@ -10,72 +10,36 @@ 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 _EventResponse_instances, _EventResponse_event, _EventResponse_engine, _EventResponse_executed, _EventResponse_manageAssigns, _EventResponse_performChecks;
13
+ var _EventResponse_instances, _EventResponse_event, _EventResponse_engine, _EventResponse_requestId, _EventResponse_manageAssigns, _EventResponse_sendMessage;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.EventResponse = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
- const abstractResponse_1 = require("../abstracts/abstractResponse");
18
- const pondError_1 = require("../errors/pondError");
19
- class EventResponse extends abstractResponse_1.PondResponse {
17
+ class EventResponse {
20
18
  constructor(event, engine) {
21
- super(event.requestId);
22
19
  _EventResponse_instances.add(this);
23
20
  _EventResponse_event.set(this, void 0);
24
21
  _EventResponse_engine.set(this, void 0);
25
- _EventResponse_executed.set(this, void 0);
22
+ _EventResponse_requestId.set(this, void 0);
26
23
  __classPrivateFieldSet(this, _EventResponse_event, event, "f");
27
24
  __classPrivateFieldSet(this, _EventResponse_engine, engine, "f");
28
- __classPrivateFieldSet(this, _EventResponse_executed, false, "f");
25
+ __classPrivateFieldSet(this, _EventResponse_requestId, event.requestId, "f");
29
26
  }
30
27
  /**
31
- * @desc Whether the server has responded to the request
32
- */
33
- get hasResponded() {
34
- return __classPrivateFieldGet(this, _EventResponse_executed, "f");
35
- }
36
- /**
37
- * @desc Accepts the request and optionally assigns data to the client
38
- * @param assigns - the data to assign to the client
39
- */
40
- accept(assigns) {
41
- __classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_manageAssigns).call(this, assigns);
42
- __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(this.requestId, __classPrivateFieldGet(this, _EventResponse_event, "f").sender, __classPrivateFieldGet(this, _EventResponse_event, "f").recipients, __classPrivateFieldGet(this, _EventResponse_event, "f").action, __classPrivateFieldGet(this, _EventResponse_event, "f").event, __classPrivateFieldGet(this, _EventResponse_event, "f").payload);
43
- return this;
44
- }
45
- /**
46
- * @desc Rejects the request and optionally assigns data to the client
47
- * @param message - the error message
48
- * @param errorCode - the error code
28
+ * @desc Assigns data to the client
49
29
  * @param assigns - the data to assign to the client
50
30
  */
51
- reject(message, errorCode, assigns) {
31
+ assign(assigns) {
52
32
  __classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_manageAssigns).call(this, assigns);
53
- const text = message || 'Unauthorized request';
54
- __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(this.requestId, pondsocket_common_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventResponse_event, "f").sender], pondsocket_common_1.ServerActions.ERROR, pondsocket_common_1.ErrorTypes.UNAUTHORIZED_BROADCAST, {
55
- message: text,
56
- code: errorCode || 403,
57
- });
58
33
  return this;
59
34
  }
60
35
  /**
61
- * @desc Emits a direct message to the client, accepting the request
36
+ * @desc Emits a direct message to the client
62
37
  * @param event - the event name
63
38
  * @param payload - the payload to send
64
- * @param assigns - the data to assign to the client
65
39
  */
66
- send(event, payload, assigns) {
67
- this.accept(assigns);
68
- __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(this.requestId, pondsocket_common_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventResponse_event, "f").sender], pondsocket_common_1.ServerActions.SYSTEM, event, payload);
69
- }
70
- /**
71
- * @desc Emits a direct message to the client without accepting the request
72
- * @param event - the event name
73
- * @param payload - the payload to send
74
- * @param assigns - the data to assign to the client
75
- */
76
- sendOnly(event, payload, assigns) {
77
- __classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_manageAssigns).call(this, assigns);
78
- __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(this.requestId, pondsocket_common_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventResponse_event, "f").sender], pondsocket_common_1.ServerActions.SYSTEM, event, payload);
40
+ reply(event, payload) {
41
+ __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventResponse_event, "f").sender], pondsocket_common_1.ServerActions.SYSTEM, event, payload, __classPrivateFieldGet(this, _EventResponse_requestId, "f"));
42
+ return this;
79
43
  }
80
44
  /**
81
45
  * @desc Sends a message to all clients in the channel
@@ -83,7 +47,7 @@ class EventResponse extends abstractResponse_1.PondResponse {
83
47
  * @param payload - the payload to send
84
48
  */
85
49
  broadcast(event, payload) {
86
- __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(this.requestId, __classPrivateFieldGet(this, _EventResponse_event, "f").sender, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
50
+ __classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_sendMessage).call(this, pondsocket_common_1.ChannelReceiver.ALL_USERS, event, payload);
87
51
  return this;
88
52
  }
89
53
  /**
@@ -91,8 +55,8 @@ class EventResponse extends abstractResponse_1.PondResponse {
91
55
  * @param event - the event to send
92
56
  * @param payload - the payload to send
93
57
  */
94
- broadcastFromUser(event, payload) {
95
- __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(this.requestId, __classPrivateFieldGet(this, _EventResponse_event, "f").sender, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
58
+ broadcastFrom(event, payload) {
59
+ __classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_sendMessage).call(this, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, event, payload);
96
60
  return this;
97
61
  }
98
62
  /**
@@ -101,8 +65,9 @@ class EventResponse extends abstractResponse_1.PondResponse {
101
65
  * @param payload - the payload to send
102
66
  * @param userIds - the ids of the clients to send the message to
103
67
  */
104
- sendToUsers(event, payload, userIds) {
105
- __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(this.requestId, __classPrivateFieldGet(this, _EventResponse_event, "f").sender, userIds, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
68
+ broadcastTo(event, payload, userIds) {
69
+ const ids = Array.isArray(userIds) ? userIds : [userIds];
70
+ __classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_sendMessage).call(this, ids, event, payload);
106
71
  return this;
107
72
  }
108
73
  /**
@@ -127,10 +92,26 @@ class EventResponse extends abstractResponse_1.PondResponse {
127
92
  * @desc Removes a user's presence from the channel
128
93
  * @param userId - the id of the user to remove
129
94
  */
130
- unTrackPresence(userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
95
+ removePresence(userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
131
96
  __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.removePresence(userId);
132
97
  return this;
133
98
  }
99
+ /**
100
+ * @desc Subscribes the client to a channel
101
+ * @param channel - the channel to subscribe to
102
+ */
103
+ subscribeTo(channel) {
104
+ __classPrivateFieldGet(this, _EventResponse_engine, "f").subscribeTo(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, channel);
105
+ return this;
106
+ }
107
+ /**
108
+ * @desc Unsubscribes the client from a channel
109
+ * @param channel - the channel to unsubscribe from
110
+ */
111
+ unsubscribeFrom(channel) {
112
+ __classPrivateFieldGet(this, _EventResponse_engine, "f").unsubscribeFrom(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, channel);
113
+ return this;
114
+ }
134
115
  /**
135
116
  * @desc Evicts a user from the channel
136
117
  * @param reason - the reason for the eviction
@@ -148,16 +129,10 @@ class EventResponse extends abstractResponse_1.PondResponse {
148
129
  }
149
130
  }
150
131
  exports.EventResponse = EventResponse;
151
- _EventResponse_event = new WeakMap(), _EventResponse_engine = new WeakMap(), _EventResponse_executed = new WeakMap(), _EventResponse_instances = new WeakSet(), _EventResponse_manageAssigns = function _EventResponse_manageAssigns(assigns) {
152
- __classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_performChecks).call(this);
132
+ _EventResponse_event = new WeakMap(), _EventResponse_engine = new WeakMap(), _EventResponse_requestId = new WeakMap(), _EventResponse_instances = new WeakSet(), _EventResponse_manageAssigns = function _EventResponse_manageAssigns(assigns) {
153
133
  if (assigns) {
154
134
  __classPrivateFieldGet(this, _EventResponse_engine, "f").updateAssigns(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, assigns);
155
135
  }
156
- }, _EventResponse_performChecks = function _EventResponse_performChecks() {
157
- if (__classPrivateFieldGet(this, _EventResponse_executed, "f")) {
158
- const message = 'Event response has already been executed';
159
- const code = 403;
160
- throw new pondError_1.ChannelError(message, code, __classPrivateFieldGet(this, _EventResponse_engine, "f").name);
161
- }
162
- __classPrivateFieldSet(this, _EventResponse_executed, true, "f");
136
+ }, _EventResponse_sendMessage = function _EventResponse_sendMessage(recipient, event, payload) {
137
+ __classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, recipient, pondsocket_common_1.ServerActions.BROADCAST, event, payload, __classPrivateFieldGet(this, _EventResponse_requestId, "f"));
163
138
  };