@senior-gestao-empresarial/angular-components 6.8.0 → 6.9.0

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.
@@ -3,8 +3,8 @@ import { Input, Component, NgModule, Injectable, Inject, ɵɵdefineInjectable,
3
3
  import { CommonModule } from '@angular/common';
4
4
  import { BreadcrumbModule as BreadcrumbModule$1 } from 'primeng/breadcrumb';
5
5
  import { NavigationEnd, PRIMARY_OUTLET, ActivatedRoute, Router, RouterModule } from '@angular/router';
6
- import { Subject, throwError, interval, of, ReplaySubject } from 'rxjs';
7
- import { takeUntil, filter, catchError, map, takeWhile, switchMap, first, finalize, take } from 'rxjs/operators';
6
+ import { Subject, throwError, interval, of, timer, iif, fromEvent, EMPTY, ReplaySubject } from 'rxjs';
7
+ import { takeUntil, filter, catchError, map, takeWhile, switchMap, take, finalize, first } from 'rxjs/operators';
8
8
  import * as moment_ from 'moment';
9
9
  import { FormField, FieldType } from '@seniorsistemas/angular-components';
10
10
  import { HttpParams, HttpClient } from '@angular/common/http';
@@ -6554,15 +6554,15 @@ NpsService = __decorate([
6554
6554
  var WebsocketService_1;
6555
6555
  let WebsocketService = WebsocketService_1 = class WebsocketService {
6556
6556
  constructor() {
6557
- /** @private */
6558
- this.focused = true;
6559
6557
  /** @private */
6560
6558
  this.wasConnected = false;
6561
6559
  /** @private */
6562
- this.connected = false;
6560
+ this.isConnected = false;
6563
6561
  /** @private */
6564
6562
  this.isConnecting = false;
6565
6563
  /** @private */
6564
+ this.isReconnecting = false;
6565
+ /** @private */
6566
6566
  this.primitiveManagers = new Map();
6567
6567
  /** @private */
6568
6568
  this.connect$ = new Subject();
@@ -6572,21 +6572,18 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6572
6572
  this.reconnect$ = new Subject();
6573
6573
  /** @private */
6574
6574
  this.error$ = new Subject();
6575
- window.onfocus = this.onFocus;
6576
- window.onblur = this.onBlur;
6575
+ this.subscribe$ = new Subject();
6577
6576
  this.connect();
6578
6577
  }
6579
6578
  /**
6580
- * Observable responsável por emitir uma notificação quando a conexão websocket é estabelecida.
6581
- * @return Um `Observable<void>` que emite uma notificação quando a conexão websocket é estabelecida.
6579
+ * Observable responsável por emitir uma notificação quando a conexão websocket é estabelecida pela primeira vez.
6580
+ * @return Um `Observable<void>` que emite uma notificação quando a conexão websocket é estabelecida pela primeira vez.
6582
6581
  */
6583
6582
  onConnect() {
6584
- setTimeout(() => {
6585
- if (this.isConnected()) {
6586
- this.connect$.next();
6587
- }
6588
- }, 0);
6589
- return this.connect$.pipe(first());
6583
+ if (this.isConnected || this.wasConnected) {
6584
+ return of(undefined).pipe(take(1));
6585
+ }
6586
+ return this.connect$.asObservable().pipe(take(1));
6590
6587
  }
6591
6588
  /**
6592
6589
  * Observable responsável por emitir uma notificação quando a conexão é desconectada.
@@ -6603,15 +6600,14 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6603
6600
  return this.reconnect$.asObservable();
6604
6601
  }
6605
6602
  /**
6606
- * Observable responsável por emitir uma notificação após o subscribe do evento.
6607
- * @return Um `Observable<void>` que emite uma notificação após o subscribe do evento.
6603
+ * Observable responsável por emitir uma notificação após o subscribe do evento pela primeira vez.
6604
+ * @return Um `Observable<void>` que emite uma notificação após o subscribe do evento pela primeira vez.
6608
6605
  */
6609
6606
  onSubscribe({ domain, service, primitive }) {
6610
- const primitiveManager = this.primitiveManagers.get(this.getPrimitiveManagerKey(domain, service, primitive));
6611
- if (!primitiveManager) {
6612
- throw new Error(`No subscription found for ${this.getPrimitiveManagerKey(domain, service, primitive)}`);
6613
- }
6614
- return primitiveManager.subscribe$.asObservable();
6607
+ const key = this.getPrimitiveManagerKey(domain, service, primitive);
6608
+ return this.onConnect().pipe(switchMap(() => this.primitiveManagers.has(key) && this.primitiveManagers.get(key).isSubscribed
6609
+ ? of(void 0).pipe(take(1))
6610
+ : this.subscribe$.pipe(filter(primitiveManager => this.getPrimitiveManagerKey(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive) === key), take(1), (map(() => void 0)))));
6615
6611
  }
6616
6612
  /**
6617
6613
  * Observable responsável por emitir uma notificação quando ocorre algum erro.
@@ -6640,25 +6636,21 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6640
6636
  primitive: primitive,
6641
6637
  stompSubscriptions: [],
6642
6638
  event$: new Subject(),
6643
- subscribe$: new ReplaySubject(1)
6639
+ isSubscribed: false,
6644
6640
  };
6645
6641
  this.primitiveManagers.set(key, primitiveManager);
6646
- if (this.isConnected()) {
6642
+ if (this.isConnected) {
6647
6643
  this.createStompSubscriptions(primitiveManager);
6648
- return primitiveManager.event$
6649
- .asObservable()
6650
- .pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitiveManager)));
6644
+ return primitiveManager.event$.asObservable().pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitiveManager)));
6651
6645
  }
6652
6646
  else {
6653
- if (!this.isConnecting) {
6647
+ if (!this.isReconnecting && !this.isConnecting) {
6654
6648
  this.connect();
6655
6649
  }
6656
6650
  this.connect$.pipe(first()).subscribe(() => {
6657
6651
  this.createStompSubscriptions(primitiveManager);
6658
6652
  });
6659
- return primitiveManager.event$
6660
- .asObservable()
6661
- .pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitiveManager)));
6653
+ return primitiveManager.event$.asObservable().pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitiveManager)));
6662
6654
  }
6663
6655
  }
6664
6656
  /** @private */
@@ -6668,18 +6660,20 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6668
6660
  const withoutTokenUrl = this.getSubscriptionUrlWithoutToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6669
6661
  const stompSubscriptionWithoutToken = this.createStompSubscription(withoutTokenUrl, primitiveManager);
6670
6662
  primitiveManager.stompSubscriptions = [stompSubscriptionWithToken, stompSubscriptionWithoutToken];
6671
- primitiveManager.subscribe$.next();
6663
+ primitiveManager.isSubscribed = true;
6664
+ this.subscribe$.next(primitiveManager);
6672
6665
  }
6673
6666
  /** @private */
6674
6667
  connect() {
6675
6668
  this.createStompClient();
6676
6669
  this.isConnecting = true;
6677
- this._stompClient.activate();
6678
6670
  this._stompClient.connect({}, () => {
6679
6671
  this.isConnecting = false;
6680
- this.setConnected(true);
6672
+ this.isReconnecting = false;
6673
+ this.isConnected = true;
6681
6674
  if (this.wasConnected) {
6682
6675
  this.reconnectPrimitives();
6676
+ this.connect$.next();
6683
6677
  this.reconnect$.next();
6684
6678
  }
6685
6679
  else {
@@ -6687,19 +6681,14 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6687
6681
  this.connect$.next();
6688
6682
  }
6689
6683
  }, (error) => {
6690
- this.setConnected(false);
6684
+ this.isConnected = false;
6691
6685
  this.error$.next(error);
6692
- this.reconnect();
6686
+ this.isReconnecting = true;
6687
+ timer(WebsocketService_1.RECONNECT_INTERVAL)
6688
+ .pipe(switchMap(() => iif(() => document.hidden, fromEvent(document, "visibilitychange").pipe(first()), EMPTY)))
6689
+ .subscribe({ complete: () => this.connect() });
6693
6690
  });
6694
6691
  }
6695
- /** @private */
6696
- onFocus() {
6697
- this.focused = true;
6698
- }
6699
- /** @private */
6700
- onBlur() {
6701
- this.focused = false;
6702
- }
6703
6692
  disconnect() {
6704
6693
  const observersCount = this.getObserversCount();
6705
6694
  if (observersCount > 0)
@@ -6711,25 +6700,16 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6711
6700
  }
6712
6701
  for (const primitiveManager of this.primitiveManagers.values()) {
6713
6702
  primitiveManager.event$.complete();
6714
- primitiveManager.subscribe$.complete();
6715
6703
  }
6716
6704
  this.primitiveManagers.clear();
6717
6705
  this._stompClient.disconnect();
6718
6706
  this._stompClient.deactivate();
6719
- this.setConnected(false);
6707
+ this.isConnected = false;
6720
6708
  this.isConnecting = false;
6721
6709
  this.wasConnected = false;
6722
6710
  this.disconnect$.next();
6723
6711
  }
6724
6712
  /** @private */
6725
- isConnected() {
6726
- return this.connected;
6727
- }
6728
- /** @private */
6729
- setConnected(connected) {
6730
- this.connected = connected;
6731
- }
6732
- /** @private */
6733
6713
  getSubscriptionUrlWithToken(domain, service, primitive) {
6734
6714
  const tenant = WebsocketService_1.TOKEN ? WebsocketService_1.TOKEN.username.split("@")[1] : null;
6735
6715
  const token = WebsocketService_1.TOKEN ? WebsocketService_1.TOKEN.access_token : null;
@@ -6751,30 +6731,11 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6751
6731
  createStompClient() {
6752
6732
  const ws = new SockJS(`${WebsocketService_1.WEBSOCKET_URL}subscription`, null, { timeout: WebsocketService_1.CONNECTION_TIMEOUT });
6753
6733
  this._stompClient = Stomp.over(ws);
6754
- // this._stompClient.debug = (str) => console.log(new Date().toISOString(), str);
6755
- this._stompClient.debug = () => { }; // Para remover os logs.
6756
- }
6757
- /** @private */
6758
- reconnect() {
6759
- if (this.connected)
6760
- this._stompClient.disconnect();
6761
- setTimeout(() => {
6762
- if (this.getObserversCount() === 0)
6763
- return;
6764
- if (this.focused) {
6765
- this.connect();
6766
- }
6767
- else {
6768
- this.reconnect();
6769
- }
6770
- }, WebsocketService_1.RECONNECT_TIMER);
6734
+ this._stompClient.debug = () => null;
6771
6735
  }
6772
6736
  /** @private */
6773
6737
  reconnectPrimitives() {
6774
6738
  for (const primitiveManager of this.primitiveManagers.values()) {
6775
- for (const stompSubscription of primitiveManager.stompSubscriptions) {
6776
- stompSubscription.unsubscribe();
6777
- }
6778
6739
  this.createStompSubscriptions(primitiveManager);
6779
6740
  }
6780
6741
  }
@@ -6793,7 +6754,6 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6793
6754
  if (hasObservers)
6794
6755
  return;
6795
6756
  primitiveManager.event$.complete();
6796
- primitiveManager.subscribe$.complete();
6797
6757
  for (const stompSubscription of primitiveManager.stompSubscriptions) {
6798
6758
  stompSubscription.unsubscribe();
6799
6759
  }
@@ -6807,7 +6767,7 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6807
6767
  }
6808
6768
  };
6809
6769
  /** @private */
6810
- WebsocketService.RECONNECT_TIMER = 3000;
6770
+ WebsocketService.RECONNECT_INTERVAL = 3000;
6811
6771
  /** @private */
6812
6772
  WebsocketService.CONNECTION_TIMEOUT = 15000;
6813
6773
  /** @private */