active-connect-ng2 0.2.28 → 0.2.30

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.
@@ -75,6 +75,7 @@ class WebsocketClient {
75
75
  this._token = '';
76
76
  this.connectionEstablishedOnce = false;
77
77
  this.messageId = 0;
78
+ this.rejectCallbacks = new Map();
78
79
  this.expectedMethods = new Map();
79
80
  this.closed = false;
80
81
  if (supportsCache)
@@ -149,7 +150,7 @@ class WebsocketClient {
149
150
  this.ws = new WebSocket(url + (this.supportsCache ? '?cache=true' : ''));
150
151
  this.ws.onerror = (err) => {
151
152
  this.ws.close();
152
- console.log(err);
153
+ console.log('ActiveConnect: ' + err);
153
154
  };
154
155
  this.ws.onopen = () => {
155
156
  this.Connected = true;
@@ -195,9 +196,10 @@ class WebsocketClient {
195
196
  }
196
197
  }
197
198
  expectMethod(method, messageId) {
198
- return new Promise((resolve) => {
199
+ return new Promise((resolve, reject) => {
199
200
  if (messageId) {
200
201
  this.expectedMethods.set(messageId, [resolve]);
202
+ this.rejectCallbacks.set(messageId, reject);
201
203
  }
202
204
  else {
203
205
  if (this.expectedMethods.has(method)) {
@@ -215,6 +217,13 @@ class WebsocketClient {
215
217
  this.handleOutboundCacheRequest(value);
216
218
  }
217
219
  if (messageId) {
220
+ if (method) {
221
+ const reject = this.rejectCallbacks.get(messageId);
222
+ if (reject) {
223
+ reject(value);
224
+ return;
225
+ }
226
+ }
218
227
  const callback = this.expectedMethods.get(messageId);
219
228
  if (callback) {
220
229
  const func = callback.shift();
@@ -247,20 +256,29 @@ class WebsocketClient {
247
256
  }
248
257
  handleOutboundCacheRequest(method) {
249
258
  if (this.dbService) {
250
- this.dbService.getByKey('outbound', method).subscribe((item) => {
251
- if (item) {
252
- this.send('___cache', {
253
- method,
254
- specificHash: item.specificHash || null,
255
- });
256
- }
257
- else {
258
- this.send('___cache', {
259
- method,
260
- specificHash: null,
261
- });
262
- }
263
- });
259
+ try {
260
+ this.dbService.getByKey('outbound', method).subscribe((item) => {
261
+ if (item) {
262
+ this.send('___cache', {
263
+ method,
264
+ specificHash: item.specificHash || null,
265
+ });
266
+ }
267
+ else {
268
+ this.send('___cache', {
269
+ method,
270
+ specificHash: null,
271
+ });
272
+ }
273
+ });
274
+ }
275
+ catch (e) {
276
+ this.send('___cache', {
277
+ method,
278
+ specificHash: null,
279
+ });
280
+ console.error('ActiveConnect: ' + e);
281
+ }
264
282
  }
265
283
  else {
266
284
  this.send('___cache', {
@@ -422,29 +440,51 @@ function Outbound(method, requestingRequired, cached, sortBy) {
422
440
  if (!target.___requested)
423
441
  target.___requested = {};
424
442
  if (!cached) {
425
- if (_this.dbService)
426
- _this.dbService.deleteByKey('outbound', method).subscribe(() => { });
443
+ if (_this.dbService) {
444
+ try {
445
+ _this.dbService
446
+ .deleteByKey('outbound', method)
447
+ .subscribe(() => { });
448
+ }
449
+ catch (e) {
450
+ console.error('ActiveConnect: could not delete indexdb cache entry');
451
+ }
452
+ }
427
453
  }
428
454
  if (data == 'cache_restore') {
429
455
  if (_this.dbService) {
430
- _this.dbService
431
- .getByKey('outbound', method)
432
- .subscribe((result) => {
433
- target.___received[propertyKey] = true;
434
- target.___data[propertyKey] = result.data;
435
- target.loading.set(propertyKey, false);
436
- });
456
+ try {
457
+ _this.dbService
458
+ .getByKey('outbound', method)
459
+ .subscribe((result) => {
460
+ target.___received[propertyKey] = true;
461
+ target.___data[propertyKey] = result.data;
462
+ target.loading.set(propertyKey, false);
463
+ });
464
+ }
465
+ catch (e) {
466
+ console.error('ActiveConnect: Unable to restore cached data.');
467
+ console.error(e);
468
+ }
437
469
  }
438
470
  else {
439
- console.error('Active - Connect: Caching / restore not possible as the indexedDB is not accessible');
471
+ console.error('ActiveConnect: Caching / restore not possible as the indexedDB is not accessible');
440
472
  }
441
473
  }
442
474
  else if (data == 'cache_delete') {
443
475
  if (_this.dbService) {
444
- _this.dbService.deleteByKey('outbound', method).subscribe(() => { });
476
+ try {
477
+ _this.dbService
478
+ .deleteByKey('outbound', method)
479
+ .subscribe(() => { });
480
+ }
481
+ catch (e) {
482
+ console.error('ActiveConnect: Unable to delete cached data');
483
+ console.error(e);
484
+ }
445
485
  }
446
486
  else {
447
- console.error('Active - Connect: Caching / restore not possible as the indexedDB is not accessible');
487
+ console.error('ActiveConnect: Caching / restore not possible as the indexedDB is not accessible');
448
488
  }
449
489
  target.___received[propertyKey] = false;
450
490
  target.___data[propertyKey] = undefined;
@@ -486,22 +526,34 @@ function Outbound(method, requestingRequired, cached, sortBy) {
486
526
  if (cached && specificHash) {
487
527
  if (_this.dbService) {
488
528
  if (data && data?.length > 0) {
489
- _this.dbService
490
- .update('outbound', {
491
- method,
492
- data,
493
- specificHash,
494
- })
495
- .subscribe(() => { });
529
+ try {
530
+ _this.dbService
531
+ .update('outbound', {
532
+ method,
533
+ data,
534
+ specificHash,
535
+ })
536
+ .subscribe(() => { });
537
+ }
538
+ catch (e) {
539
+ console.error('ActiveConnect: Unable to update cached data');
540
+ console.error(e);
541
+ }
496
542
  }
497
543
  else {
498
- _this.dbService
499
- .deleteByKey('outbound', method)
500
- .subscribe(() => { });
544
+ try {
545
+ _this.dbService
546
+ .deleteByKey('outbound', method)
547
+ .subscribe(() => { });
548
+ }
549
+ catch (e) {
550
+ console.error('ActiveConnect: Unable to delete cached data');
551
+ console.error(e);
552
+ }
501
553
  }
502
554
  }
503
555
  else {
504
- console.error('Active-Connect: Caching not possible as the indexedDB has not been initialized');
556
+ console.error('ActiveConnect: Caching not possible as the indexedDB has not been initialized');
505
557
  }
506
558
  }
507
559
  }
@@ -514,22 +566,34 @@ function Outbound(method, requestingRequired, cached, sortBy) {
514
566
  if (cached && specificHash) {
515
567
  if (_this.dbService) {
516
568
  if (data && data?.length > 0) {
517
- _this.dbService
518
- .update('outbound', {
519
- method,
520
- data,
521
- specificHash,
522
- })
523
- .subscribe(() => { });
569
+ try {
570
+ _this.dbService
571
+ .update('outbound', {
572
+ method,
573
+ data,
574
+ specificHash,
575
+ })
576
+ .subscribe(() => { });
577
+ }
578
+ catch (e) {
579
+ console.error('ActiveConnect: Unable to update cached data');
580
+ console.error(e);
581
+ }
524
582
  }
525
583
  else {
526
- _this.dbService
527
- .deleteByKey('outbound', method)
528
- .subscribe(() => { });
584
+ try {
585
+ _this.dbService
586
+ .deleteByKey('outbound', method)
587
+ .subscribe(() => { });
588
+ }
589
+ catch (e) {
590
+ console.error('ActiveConnect: Unable to update cached data');
591
+ console.error(e);
592
+ }
529
593
  }
530
594
  }
531
595
  else {
532
- console.error('Active-Connect: Caching not possible as the indexedDB has not been initialized');
596
+ console.error('ActiveConnect: Caching not possible as the indexedDB has not been initialized');
533
597
  }
534
598
  }
535
599
  }
@@ -588,7 +652,12 @@ function Route(method, loadingKey, dontEnsureTransmission) {
588
652
  const promise = original.bind(this)(...data);
589
653
  let res = null;
590
654
  if (this.client) {
591
- res = await this.client.send(method, data[0], dontEnsureTransmission);
655
+ res = await this.client
656
+ .send(method, data[0], dontEnsureTransmission)
657
+ ?.catch(() => {
658
+ if (loadingKey)
659
+ this.loadingElements[loadingKey]--;
660
+ });
592
661
  }
593
662
  await promise;
594
663
  if (loadingKey)
@@ -664,45 +733,70 @@ class OutboundObject {
664
733
  this.loadedGroupChanged = null;
665
734
  this.target.loading = new Map();
666
735
  if (this.client.dbService) {
667
- this.client.dbService
668
- .getByKey('outbound', method)
669
- .subscribe((result) => {
670
- if (result) {
671
- this.previouslyCachedCount = result.data?.length || null;
672
- }
673
- });
736
+ try {
737
+ this.client.dbService
738
+ .getByKey('outbound', method)
739
+ .subscribe((result) => {
740
+ if (result) {
741
+ this.previouslyCachedCount = result.data?.length || null;
742
+ }
743
+ });
744
+ }
745
+ catch (e) {
746
+ console.error('ActiveConnect: Unable to read cached data');
747
+ console.error(e);
748
+ }
674
749
  }
675
750
  const _this = this;
676
751
  WebsocketClient.expectOutbound(method, function setOutbound(data, specificHash, insertedOrGroupData, updatedOrGroupId, deleted, length, _client) {
677
752
  if (!cached) {
678
- if (_client.dbService)
679
- _client.dbService
680
- .deleteByKey('outbound', method)
681
- .subscribe(() => { });
753
+ if (_client.dbService) {
754
+ try {
755
+ _client.dbService
756
+ .deleteByKey('outbound', method)
757
+ .subscribe(() => { });
758
+ }
759
+ catch (e) {
760
+ console.error('ActiveConnect: Unable to delete cached data');
761
+ console.error(e);
762
+ }
763
+ }
682
764
  }
683
765
  if (data == 'cache_restore') {
684
766
  if (_client.dbService) {
685
- _client.dbService
686
- .getByKey('outbound', method)
687
- .subscribe((result) => {
688
- _this.setData(result.data);
689
- if (result.length)
690
- _this._length = result.length;
691
- _this.loading = false;
692
- });
767
+ try {
768
+ _client.dbService
769
+ .getByKey('outbound', method)
770
+ .subscribe((result) => {
771
+ _this.setData(result.data);
772
+ if (result.length)
773
+ _this._length = result.length;
774
+ _this.loading = false;
775
+ });
776
+ }
777
+ catch (e) {
778
+ console.error('ActiveConnect: Unable to restore cached data');
779
+ console.error(e);
780
+ }
693
781
  }
694
782
  else {
695
- console.error('Active - Connect: Caching / restore not possible as the indexedDB is not accessible');
783
+ console.error('ActiveConnect: Caching / restore not possible as the indexedDB is not accessible');
696
784
  }
697
785
  }
698
786
  else if (data == 'cache_delete') {
699
787
  if (_client.dbService) {
700
- _client.dbService
701
- .deleteByKey('outbound', method)
702
- .subscribe(() => { });
788
+ try {
789
+ _client.dbService
790
+ .deleteByKey('outbound', method)
791
+ .subscribe(() => { });
792
+ }
793
+ catch (e) {
794
+ console.error('ActiveConnect: Unable to delete cached data');
795
+ console.error(e);
796
+ }
703
797
  }
704
798
  else {
705
- console.error('Active - Connect: Caching / restore not possible as the indexedDB is not accessible');
799
+ console.error('ActiveConnect: Caching / restore not possible as the indexedDB is not accessible');
706
800
  }
707
801
  _this.loading = false;
708
802
  _this.requested = false;
@@ -761,24 +855,30 @@ class OutboundObject {
761
855
  _this.loading = false;
762
856
  if (cached && specificHash) {
763
857
  if (_client.dbService) {
764
- if (_this.data && _this.data?.length > 0) {
765
- _client.dbService
766
- .update('outbound', {
767
- method,
768
- data: _this.data,
769
- specificHash,
770
- length,
771
- })
772
- .subscribe(() => { });
858
+ try {
859
+ if (_this.data && _this.data?.length > 0) {
860
+ _client.dbService
861
+ .update('outbound', {
862
+ method,
863
+ data: _this.data,
864
+ specificHash,
865
+ length,
866
+ })
867
+ .subscribe(() => { });
868
+ }
869
+ else {
870
+ _client.dbService
871
+ .deleteByKey('outbound', method)
872
+ .subscribe(() => { });
873
+ }
773
874
  }
774
- else {
775
- _client.dbService
776
- .deleteByKey('outbound', method)
777
- .subscribe(() => { });
875
+ catch (e) {
876
+ console.error('ActiveConnect: Unable to update cached data.');
877
+ console.error(e);
778
878
  }
779
879
  }
780
880
  else {
781
- console.error('Active-Connect: Caching not possible as the indexedDB has not been initialized');
881
+ console.error('ActiveConnect: Caching not possible as the indexedDB has not been initialized');
782
882
  }
783
883
  }
784
884
  }
@@ -794,24 +894,30 @@ class OutboundObject {
794
894
  _this.loading = false;
795
895
  if (cached && specificHash) {
796
896
  if (_client.dbService) {
797
- if (data && data?.length > 0) {
798
- _client.dbService
799
- .update('outbound', {
800
- method,
801
- data,
802
- specificHash,
803
- length,
804
- })
805
- .subscribe(() => { });
897
+ try {
898
+ if (data && data?.length > 0) {
899
+ _client.dbService
900
+ .update('outbound', {
901
+ method,
902
+ data,
903
+ specificHash,
904
+ length,
905
+ })
906
+ .subscribe(() => { });
907
+ }
908
+ else {
909
+ _client.dbService
910
+ .deleteByKey('outbound', method)
911
+ .subscribe(() => { });
912
+ }
806
913
  }
807
- else {
808
- _client.dbService
809
- .deleteByKey('outbound', method)
810
- .subscribe(() => { });
914
+ catch (e) {
915
+ console.error('ActiveConnect: Unable to update cached data.');
916
+ console.error(e);
811
917
  }
812
918
  }
813
919
  else {
814
- console.error('Active-Connect: Caching not possible as the indexedDB has not been initialized');
920
+ console.error('ActiveConnect: Caching not possible as the indexedDB has not been initialized');
815
921
  }
816
922
  }
817
923
  }