@resolveio/server-lib 6.3.4 → 6.3.5-newrole

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.
@@ -147,10 +147,10 @@ var SubscriptionManager = /** @class */ (function () {
147
147
  switch (_a.label) {
148
148
  case 0:
149
149
  loggedInUser = userCopy[i];
150
- if (!(!loggedInUser.date || moment().diff(moment(loggedInUser.date), 'seconds') >= 60)) return [3 /*break*/, 5];
150
+ if (!(!loggedInUser.date || moment().diff(moment(loggedInUser.date), 'seconds') >= 60)) return [3 /*break*/, 3];
151
151
  if (!this_1._mainServer.getWS(loggedInUser.id_ws)) return [3 /*break*/, 1];
152
152
  this_1.unsubscribeAll(this_1._mainServer.getWS(loggedInUser.id_ws));
153
- return [3 /*break*/, 5];
153
+ return [3 /*break*/, 3];
154
154
  case 1:
155
155
  this_1._subscriptions.forEach(function (sub) {
156
156
  for (var j = sub.clients.length - 1; j >= 0; j--) {
@@ -162,15 +162,12 @@ var SubscriptionManager = /** @class */ (function () {
162
162
  });
163
163
  return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findById(loggedInUser._id)];
164
164
  case 2:
165
- if (!_a.sent()) return [3 /*break*/, 4];
166
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id }).exec()];
167
- case 3:
168
- _a.sent();
169
- _a.label = 4;
170
- case 4:
165
+ if (_a.sent()) {
166
+ logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id }).exec();
167
+ }
171
168
  this_1._loggedInUsers.splice(i, 1);
172
- _a.label = 5;
173
- case 5: return [2 /*return*/];
169
+ _a.label = 3;
170
+ case 3: return [2 /*return*/];
174
171
  }
175
172
  });
176
173
  };
@@ -235,80 +232,78 @@ var SubscriptionManager = /** @class */ (function () {
235
232
  subscriptionData[_i - 4] = arguments[_i];
236
233
  }
237
234
  return __awaiter(this, void 0, void 0, function () {
238
- var that, pub, valObj, valKeys, rootKeys, i, sub, _a, newLoggedInUser, begDate, endDate, billUser;
239
- var _b;
240
- return __generator(this, function (_c) {
241
- switch (_c.label) {
235
+ var that, pub, valObj, valKeys, rootKeys, i, sub, newLoggedInUser, begDate, endDate, billUser;
236
+ var _a;
237
+ return __generator(this, function (_b) {
238
+ switch (_b.label) {
242
239
  case 0:
243
240
  that = this;
244
241
  pub = that._publications[publication];
245
- if (!!pub) return [3 /*break*/, 1];
246
- console.error('No Publication: ' + publication);
247
- return [2 /*return*/];
248
- case 1:
249
- if (subscriptionData.length > 1 || subscriptionData[0]) {
250
- if (!pub.check) {
251
- console.error('No Check Function For Pub ' + publication);
252
- return [2 /*return*/];
253
- }
254
- else if (!pub.check._schema) {
255
- console.error('No Check Schema For Pub ' + publication);
256
- return [2 /*return*/];
257
- }
258
- else {
259
- valObj = {};
260
- valKeys = Object.keys(pub.check._schema);
261
- rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
262
- for (i = 0; i < subscriptionData.length; i++) {
263
- valObj[rootKeys[i]] = subscriptionData[i];
242
+ if (!pub) {
243
+ console.error('No Publication: ' + publication);
244
+ return [2 /*return*/];
245
+ }
246
+ else {
247
+ if (subscriptionData.length > 1 || subscriptionData[0]) {
248
+ if (!pub.check) {
249
+ console.error('No Check Function For Pub ' + publication);
250
+ return [2 /*return*/];
264
251
  }
265
- try {
266
- pub.check.validate(valObj);
252
+ else if (!pub.check._schema) {
253
+ console.error('No Check Schema For Pub ' + publication);
254
+ return [2 /*return*/];
267
255
  }
268
- catch (errors) {
269
- if (errors) {
270
- console.error('Error in Pub Check (' + publication + ')', errors);
271
- return [2 /*return*/];
256
+ else {
257
+ valObj = {};
258
+ valKeys = Object.keys(pub.check._schema);
259
+ rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
260
+ for (i = 0; i < subscriptionData.length; i++) {
261
+ valObj[rootKeys[i]] = subscriptionData[i];
262
+ }
263
+ try {
264
+ pub.check.validate(valObj);
265
+ }
266
+ catch (errors) {
267
+ if (errors) {
268
+ console.error('Error in Pub Check (' + publication + ')', errors);
269
+ return [2 /*return*/];
270
+ }
272
271
  }
273
272
  }
274
273
  }
275
- }
276
- sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
277
- // If sub found (another user watching same data), add client to same sub
278
- if (sub) {
279
- sub.clients.push({
280
- id_user: ws['id_user'],
281
- messageId: messageId,
282
- id_socket: ws['id_socket']
283
- });
284
- }
285
- // If sub not found, create new sub
286
- else {
287
- that._subscriptions.push({
288
- publication: publication,
289
- subscriptionData: subscriptionData,
290
- collections: that.getPublicationCollections(publication),
291
- clients: [{
292
- id_user: ws['id_user'],
293
- messageId: messageId,
294
- id_socket: ws['id_socket']
295
- }]
296
- });
297
- }
298
- if (!sub) {
299
274
  sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
275
+ // If sub found (another user watching same data), add client to same sub
276
+ if (sub) {
277
+ sub.clients.push({
278
+ id_user: ws['id_user'],
279
+ messageId: messageId,
280
+ id_socket: ws['id_socket']
281
+ });
282
+ }
283
+ // If sub not found, create new sub
284
+ else {
285
+ that._subscriptions.push({
286
+ publication: publication,
287
+ subscriptionData: subscriptionData,
288
+ collections: that.getPublicationCollections(publication),
289
+ clients: [{
290
+ id_user: ws['id_user'],
291
+ messageId: messageId,
292
+ id_socket: ws['id_socket']
293
+ }]
294
+ });
295
+ }
296
+ if (!sub) {
297
+ sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
298
+ }
299
+ // console.log('Subscribe', new Date(), 'Success', sub);
300
+ // Send pub data to client who just subscribed only, don't want to send to existing clients since nothing has changed
301
+ that.sendPubDataOnce(ws, messageId, sub, 'newSub', that.getPublicationCollections(publication)[0]);
302
+ if (that._publications[sub.publication].preFunction && !sub.preSubscriptionData) {
303
+ sub.preSubscriptionData = (_a = that._publications[sub.publication]).preFunction.apply(_a, sub.subscriptionData);
304
+ }
300
305
  }
301
- // console.log('Subscribe', new Date(), 'Success', sub);
302
- // Send pub data to client who just subscribed only, don't want to send to existing clients since nothing has changed
303
- that.sendPubDataOnce(ws, messageId, sub, 'newSub', that.getPublicationCollections(publication)[0]);
304
- if (!(that._publications[sub.publication].preFunction && !sub.preSubscriptionData)) return [3 /*break*/, 3];
305
- _a = sub;
306
- return [4 /*yield*/, (_b = that._publications[sub.publication]).preFunction.apply(_b, sub.subscriptionData)];
307
- case 2:
308
- _a.preSubscriptionData = _c.sent();
309
- _c.label = 3;
310
- case 3:
311
- if (!(publication === 'appversion')) return [3 /*break*/, 5];
306
+ if (!(publication === 'appversion')) return [3 /*break*/, 2];
312
307
  newLoggedInUser = {
313
308
  _id: mongoose_1.Types.ObjectId().toHexString(),
314
309
  __v: 0,
@@ -319,7 +314,7 @@ var SubscriptionManager = /** @class */ (function () {
319
314
  };
320
315
  this._loggedInUsers.push(newLoggedInUser);
321
316
  logged_in_users_collection_1.LoggedInUsers.create(newLoggedInUser);
322
- if (!(ws['user'] !== 'Admin' && index_1.ResolveIOServer.getSupportDB())) return [3 /*break*/, 5];
317
+ if (!(ws['user'] !== 'Admin' && index_1.ResolveIOServer.getSupportDB())) return [3 /*break*/, 2];
323
318
  begDate = moment().startOf('month');
324
319
  endDate = moment().endOf('month');
325
320
  return [4 /*yield*/, billing_logged_in_users_collection_1.BillingLoggedInUsers.findOne({
@@ -338,8 +333,8 @@ var SubscriptionManager = /** @class */ (function () {
338
333
  }
339
334
  ]
340
335
  })];
341
- case 4:
342
- billUser = _c.sent();
336
+ case 1:
337
+ billUser = _b.sent();
343
338
  if (!billUser) {
344
339
  billing_logged_in_users_collection_1.BillingLoggedInUsers.create({
345
340
  _id: mongoose_1.Types.ObjectId().toHexString(),
@@ -350,8 +345,8 @@ var SubscriptionManager = /** @class */ (function () {
350
345
  client: index_1.ResolveIOServer.getClientName()
351
346
  });
352
347
  }
353
- _c.label = 5;
354
- case 5: return [2 /*return*/];
348
+ _b.label = 2;
349
+ case 2: return [2 /*return*/];
355
350
  }
356
351
  });
357
352
  });
@@ -515,79 +510,61 @@ var SubscriptionManager = /** @class */ (function () {
515
510
  setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
516
511
  var uniqueOplog_1, uniquePubs, _loop_4, i, _loop_5, this_3, i, i, pub;
517
512
  return __generator(this, function (_a) {
518
- switch (_a.label) {
519
- case 0:
520
- if (!this._oplogQueue.length) return [3 /*break*/, 4];
521
- uniqueOplog_1 = common_1.deepCopy(this._oplogQueue);
522
- this._oplogQueue = [];
523
- uniquePubs = [];
524
- _loop_4 = function (i) {
525
- if (!that._subscriptions.some(function (a) { return a.collections.includes(uniqueOplog_1[i].collection); })) {
526
- uniqueOplog_1.splice(i, 1);
527
- }
528
- };
529
- for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
530
- _loop_4(i);
513
+ if (this._oplogQueue.length) {
514
+ uniqueOplog_1 = common_1.deepCopy(this._oplogQueue);
515
+ this._oplogQueue = [];
516
+ uniquePubs = [];
517
+ _loop_4 = function (i) {
518
+ if (!that._subscriptions.some(function (a) { return a.collections.includes(uniqueOplog_1[i].collection); })) {
519
+ uniqueOplog_1.splice(i, 1);
531
520
  }
532
- _loop_5 = function (i) {
533
- var oplog = uniqueOplog_1[i];
534
- var _loop_6 = function (j) {
535
- var _a;
536
- var sub = that._subscriptions[j];
537
- if (sub) {
538
- if (sub.collections.includes(oplog.collection)) {
539
- if (sub.preSubscriptionData) {
540
- if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
541
- if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData) && JSON.stringify(oplog) === JSON.stringify(a.oplog); }).length) {
542
- if (!sub.clients.length) {
543
- that._subscriptions.splice(j, 1);
544
- }
545
- else {
546
- sub['oplog'] = oplog;
547
- uniquePubs.push(common_1.deepCopy(sub));
548
- }
549
- if (this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
550
- this_3._nodeCache.del(this_3._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
551
- this_3._subCache.splice(this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
552
- }
521
+ };
522
+ for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
523
+ _loop_4(i);
524
+ }
525
+ _loop_5 = function (i) {
526
+ var oplog = uniqueOplog_1[i];
527
+ var _loop_6 = function (j) {
528
+ var _a;
529
+ var sub = that._subscriptions[j];
530
+ if (sub) {
531
+ if (sub.collections.includes(oplog.collection)) {
532
+ if (sub.preSubscriptionData) {
533
+ if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
534
+ if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData) && JSON.stringify(oplog) === JSON.stringify(a.oplog); }).length) {
535
+ if (!sub.clients.length) {
536
+ that._subscriptions.splice(j, 1);
537
+ }
538
+ else {
539
+ sub['oplog'] = oplog;
540
+ uniquePubs.push(common_1.deepCopy(sub));
553
541
  }
554
- return "continue";
555
- }
556
- }
557
- if (that._publications[sub.publication].preFunction) {
558
- if (!sub.clients.length) {
559
- that._subscriptions.splice(j, 1);
560
542
  if (this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
561
543
  this_3._nodeCache.del(this_3._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
562
544
  this_3._subCache.splice(this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
563
545
  }
564
546
  }
565
- else {
566
- if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData) && JSON.stringify(oplog) === JSON.stringify(a.oplog); }).length) {
567
- sub['oplog'] = oplog;
568
- uniquePubs.push(common_1.deepCopy(sub));
569
- }
547
+ return "continue";
548
+ }
549
+ }
550
+ if (that._publications[sub.publication].preFunction) {
551
+ if (!sub.clients.length) {
552
+ that._subscriptions.splice(j, 1);
553
+ if (this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
554
+ this_3._nodeCache.del(this_3._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
555
+ this_3._subCache.splice(this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
570
556
  }
571
557
  }
572
558
  else {
573
- if (that._publications[sub.publication].fetchFunction) {
574
- if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, __spreadArrays([oplog.doc], sub.subscriptionData))) {
575
- if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length) {
576
- if (!sub.clients.length) {
577
- that._subscriptions.splice(j, 1);
578
- if (this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
579
- this_3._nodeCache.del(this_3._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
580
- this_3._subCache.splice(this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
581
- }
582
- }
583
- else {
584
- sub['oplog'] = oplog;
585
- uniquePubs.push(common_1.deepCopy(sub));
586
- }
587
- }
588
- }
559
+ if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData) && JSON.stringify(oplog) === JSON.stringify(a.oplog); }).length) {
560
+ sub['oplog'] = oplog;
561
+ uniquePubs.push(common_1.deepCopy(sub));
589
562
  }
590
- else {
563
+ }
564
+ }
565
+ else {
566
+ if (that._publications[sub.publication].fetchFunction) {
567
+ if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, __spreadArrays([oplog.doc], sub.subscriptionData))) {
591
568
  if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length) {
592
569
  if (!sub.clients.length) {
593
570
  that._subscriptions.splice(j, 1);
@@ -603,31 +580,39 @@ var SubscriptionManager = /** @class */ (function () {
603
580
  }
604
581
  }
605
582
  }
583
+ else {
584
+ if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length) {
585
+ if (!sub.clients.length) {
586
+ that._subscriptions.splice(j, 1);
587
+ if (this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
588
+ this_3._nodeCache.del(this_3._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
589
+ this_3._subCache.splice(this_3._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
590
+ }
591
+ }
592
+ else {
593
+ sub['oplog'] = oplog;
594
+ uniquePubs.push(common_1.deepCopy(sub));
595
+ }
596
+ }
597
+ }
606
598
  }
607
599
  }
608
- };
609
- for (var j = that._subscriptions.length - 1; j >= 0; j--) {
610
- _loop_6(j);
611
600
  }
612
601
  };
613
- this_3 = this;
614
- for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
615
- _loop_5(i);
602
+ for (var j = that._subscriptions.length - 1; j >= 0; j--) {
603
+ _loop_6(j);
616
604
  }
617
- i = 0;
618
- _a.label = 1;
619
- case 1:
620
- if (!(i < uniquePubs.length)) return [3 /*break*/, 4];
605
+ };
606
+ this_3 = this;
607
+ for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
608
+ _loop_5(i);
609
+ }
610
+ for (i = 0; i < uniquePubs.length; i++) {
621
611
  pub = uniquePubs[i];
622
- return [4 /*yield*/, this.checkPubsForChanges(pub['oplog'].collection, pub['oplog'].doc, pub['oplog'].type, pub)];
623
- case 2:
624
- _a.sent();
625
- _a.label = 3;
626
- case 3:
627
- i++;
628
- return [3 /*break*/, 1];
629
- case 4: return [2 /*return*/];
612
+ this.checkPubsForChanges(pub['oplog'].collection, pub['oplog'].doc, pub['oplog'].type, pub);
613
+ }
630
614
  }
615
+ return [2 /*return*/];
631
616
  });
632
617
  }); }, 250);
633
618
  };
@@ -636,76 +621,46 @@ var SubscriptionManager = /** @class */ (function () {
636
621
  var that, subCopy, _loop_7, this_4, jj, _loop_8, zz;
637
622
  var _this = this;
638
623
  return __generator(this, function (_a) {
639
- switch (_a.label) {
640
- case 0:
641
- that = this;
642
- subCopy = common_1.deepCopy(that._subscriptions.filter(function (a) { return a.collections.includes(collection); }));
643
- _loop_7 = function (jj) {
644
- if (this_4._subCache[jj].collection === collection || subCopy.some(function (a) { return a.publication === _this._subCache[jj].publication; })) {
645
- this_4._nodeCache.del(this_4._subCache[jj].id);
646
- this_4._subCache.splice(jj, 1);
624
+ that = this;
625
+ subCopy = common_1.deepCopy(that._subscriptions.filter(function (a) { return a.collections.includes(collection); }));
626
+ _loop_7 = function (jj) {
627
+ if (this_4._subCache[jj].collection === collection || subCopy.some(function (a) { return a.publication === _this._subCache[jj].publication; })) {
628
+ this_4._nodeCache.del(this_4._subCache[jj].id);
629
+ this_4._subCache.splice(jj, 1);
630
+ }
631
+ };
632
+ this_4 = this;
633
+ for (jj = this._subCache.length - 1; jj >= 0; jj--) {
634
+ _loop_7(jj);
635
+ }
636
+ _loop_8 = function (zz) {
637
+ var sub = subCopy[zz];
638
+ if (that._publications[sub.publication].ws_specific) {
639
+ for (var aa = 0; aa < sub.clients.length; aa++) {
640
+ var client = sub.clients[aa];
641
+ for (var bb = 0; bb < that._wss.clients.size; bb++) {
642
+ var ws = Array.from(that._wss.clients)[bb];
643
+ if (ws['id_socket'] === client.id_socket) {
644
+ that.sendPubDataOnce(ws, client.messageId, sub, 'delete', collection);
645
+ }
647
646
  }
648
- };
649
- this_4 = this;
650
- for (jj = this._subCache.length - 1; jj >= 0; jj--) {
651
- _loop_7(jj);
652
647
  }
653
- _loop_8 = function (zz) {
654
- var sub, aa, client, bb, ws;
655
- return __generator(this, function (_a) {
656
- switch (_a.label) {
657
- case 0:
658
- sub = subCopy[zz];
659
- if (!that._publications[sub.publication].ws_specific) return [3 /*break*/, 7];
660
- aa = 0;
661
- _a.label = 1;
662
- case 1:
663
- if (!(aa < sub.clients.length)) return [3 /*break*/, 6];
664
- client = sub.clients[aa];
665
- bb = 0;
666
- _a.label = 2;
667
- case 2:
668
- if (!(bb < that._wss.clients.size)) return [3 /*break*/, 5];
669
- ws = Array.from(that._wss.clients)[bb];
670
- if (!(ws['id_socket'] === client.id_socket)) return [3 /*break*/, 4];
671
- return [4 /*yield*/, that.sendPubDataOnce(ws, client.messageId, sub, 'delete', collection)];
672
- case 3:
673
- _a.sent();
674
- _a.label = 4;
675
- case 4:
676
- bb++;
677
- return [3 /*break*/, 2];
678
- case 5:
679
- aa++;
680
- return [3 /*break*/, 1];
681
- case 6: return [3 /*break*/, 10];
682
- case 7:
683
- if (!!sub.clients.length) return [3 /*break*/, 8];
684
- if (that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
685
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
686
- }
687
- return [3 /*break*/, 10];
688
- case 8: return [4 /*yield*/, that.sendPubData(sub, 'delete', collection)];
689
- case 9:
690
- _a.sent();
691
- _a.label = 10;
692
- case 10: return [2 /*return*/];
693
- }
694
- });
695
- };
696
- zz = subCopy.length - 1;
697
- _a.label = 1;
698
- case 1:
699
- if (!(zz >= 0)) return [3 /*break*/, 4];
700
- return [5 /*yield**/, _loop_8(zz)];
701
- case 2:
702
- _a.sent();
703
- _a.label = 3;
704
- case 3:
705
- zz--;
706
- return [3 /*break*/, 1];
707
- case 4: return [2 /*return*/];
648
+ }
649
+ else {
650
+ if (!sub.clients.length) {
651
+ if (that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
652
+ that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
653
+ }
654
+ }
655
+ else {
656
+ that.sendPubData(sub, 'delete', collection);
657
+ }
658
+ }
659
+ };
660
+ for (zz = subCopy.length - 1; zz >= 0; zz--) {
661
+ _loop_8(zz);
708
662
  }
663
+ return [2 /*return*/];
709
664
  });
710
665
  });
711
666
  };
@@ -720,99 +675,78 @@ var SubscriptionManager = /** @class */ (function () {
720
675
  switch (_d.label) {
721
676
  case 0:
722
677
  that = this;
723
- if (!sub.preSubscriptionData) return [3 /*break*/, 2];
724
- if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 2];
725
- if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
726
- this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
727
- this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
678
+ if (sub.preSubscriptionData) {
679
+ if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
680
+ if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
681
+ this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
682
+ this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
683
+ }
684
+ that.sendPubData(sub, type, collection);
685
+ return [2 /*return*/];
686
+ }
728
687
  }
729
- return [4 /*yield*/, that.sendPubData(sub, type, collection)];
730
- case 1:
731
- _d.sent();
732
- return [2 /*return*/];
733
- case 2:
734
- if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 6];
688
+ if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 2];
735
689
  _a = sub;
736
690
  return [4 /*yield*/, that._publications[sub.publication].preFunction(sub.subscriptionData)];
737
- case 3:
691
+ case 1:
738
692
  _a.preSubscriptionData = _d.sent();
739
- if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 5];
740
- if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
741
- this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
742
- this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
743
- }
744
- return [4 /*yield*/, that.sendPubData(sub, type, collection)];
745
- case 4:
746
- _d.sent();
747
- _d.label = 5;
748
- case 5: return [3 /*break*/, 23];
749
- case 6:
750
- if (!that._publications[sub.publication].ws_specific) return [3 /*break*/, 18];
751
- if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
752
- this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
753
- this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
693
+ if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
694
+ if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
695
+ this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
696
+ this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
697
+ }
698
+ that.sendPubData(sub, type, collection);
754
699
  }
755
- k = 0;
756
- _d.label = 7;
757
- case 7:
758
- if (!(k < sub.clients.length)) return [3 /*break*/, 17];
759
- client = sub.clients[k];
760
- if (!that._publications[sub.publication].fetchFunction) return [3 /*break*/, 12];
761
- if (!(_b = that._publications[sub.publication]).fetchFunction.apply(_b, __spreadArrays([document, client.id_user], sub.subscriptionData))) return [3 /*break*/, 11];
762
- j = 0;
763
- _d.label = 8;
764
- case 8:
765
- if (!(j < that._wss.clients.size)) return [3 /*break*/, 11];
766
- ws = Array.from(that._wss.clients)[j];
767
- if (!(ws['id_socket'] === client.id_socket)) return [3 /*break*/, 10];
768
- return [4 /*yield*/, that.sendPubDataOnce(ws, client.messageId, sub, type, collection)];
769
- case 9:
770
- _d.sent();
771
- _d.label = 10;
772
- case 10:
773
- j++;
774
- return [3 /*break*/, 8];
775
- case 11: return [3 /*break*/, 16];
776
- case 12:
777
- j = 0;
778
- _d.label = 13;
779
- case 13:
780
- if (!(j < that._wss.clients.size)) return [3 /*break*/, 16];
781
- ws = Array.from(that._wss.clients)[j];
782
- if (!(ws['id_socket'] === client.id_socket)) return [3 /*break*/, 15];
783
- return [4 /*yield*/, that.sendPubDataOnce(ws, client.messageId, sub, type, collection)];
784
- case 14:
785
- _d.sent();
786
- _d.label = 15;
787
- case 15:
788
- j++;
789
- return [3 /*break*/, 13];
790
- case 16:
791
- k++;
792
- return [3 /*break*/, 7];
793
- case 17: return [3 /*break*/, 23];
794
- case 18:
795
- if (!that._publications[sub.publication].fetchFunction) return [3 /*break*/, 21];
796
- if (!(_c = that._publications[sub.publication]).fetchFunction.apply(_c, __spreadArrays([document], sub.subscriptionData))) return [3 /*break*/, 20];
797
- if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
798
- this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
799
- this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
700
+ return [3 /*break*/, 3];
701
+ case 2:
702
+ if (that._publications[sub.publication].ws_specific) {
703
+ if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
704
+ this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
705
+ this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
706
+ }
707
+ for (k = 0; k < sub.clients.length; k++) {
708
+ client = sub.clients[k];
709
+ if (that._publications[sub.publication].fetchFunction) {
710
+ if ((_b = that._publications[sub.publication]).fetchFunction.apply(_b, __spreadArrays([document, client.id_user], sub.subscriptionData))) {
711
+ for (j = 0; j < that._wss.clients.size; j++) {
712
+ ws = Array.from(that._wss.clients)[j];
713
+ if (ws['id_socket'] === client.id_socket) {
714
+ that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
715
+ }
716
+ }
717
+ }
718
+ }
719
+ else {
720
+ for (j = 0; j < that._wss.clients.size; j++) {
721
+ ws = Array.from(that._wss.clients)[j];
722
+ if (ws['id_socket'] === client.id_socket) {
723
+ that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
724
+ }
725
+ }
726
+ }
727
+ }
800
728
  }
801
- return [4 /*yield*/, that.sendPubData(sub, type, collection)];
802
- case 19:
803
- _d.sent();
804
- _d.label = 20;
805
- case 20: return [3 /*break*/, 23];
806
- case 21:
807
- if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
808
- this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
809
- this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
729
+ else {
730
+ if (that._publications[sub.publication].fetchFunction) {
731
+ if ((_c = that._publications[sub.publication]).fetchFunction.apply(_c, __spreadArrays([document], sub.subscriptionData))) {
732
+ if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
733
+ this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
734
+ this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
735
+ }
736
+ that.sendPubData(sub, type, collection);
737
+ }
738
+ // else
739
+ }
740
+ else {
741
+ if (this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
742
+ this._nodeCache.del(this._subCache.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })[0].id);
743
+ this._subCache.splice(this._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
744
+ }
745
+ that.sendPubData(sub, type, collection);
746
+ }
810
747
  }
811
- return [4 /*yield*/, that.sendPubData(sub, type, collection)];
812
- case 22:
813
- _d.sent();
814
- _d.label = 23;
815
- case 23: return [2 /*return*/];
748
+ _d.label = 3;
749
+ case 3: return [2 /*return*/];
816
750
  }
817
751
  });
818
752
  });