@senior-gestao-empresarial/angular-components 4.22.2 → 4.23.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.
@@ -4,7 +4,7 @@ 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
6
  import { Subject, throwError, interval, of, ReplaySubject } from 'rxjs';
7
- import { takeUntil, filter, catchError, map, takeWhile, switchMap, first, take } from 'rxjs/operators';
7
+ import { takeUntil, filter, catchError, map, takeWhile, switchMap, first, finalize, take } 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';
@@ -15,7 +15,6 @@ import { user } from '@seniorsistemas/senior-platform-data';
15
15
  import { FormControl, FormGroup } from '@angular/forms';
16
16
  import { get } from 'js-cookie';
17
17
  import { Stomp } from '@stomp/stompjs';
18
- import { CookieService } from 'ngx-cookie-service';
19
18
  import * as SockJS from 'sockjs-client';
20
19
 
21
20
  var BreadcrumbComponent = /** @class */ (function () {
@@ -6404,17 +6403,16 @@ var NpsService = /** @class */ (function () {
6404
6403
  }());
6405
6404
 
6406
6405
  var WebsocketService = /** @class */ (function () {
6407
- function WebsocketService(cookieService) {
6408
- this.cookieService = cookieService;
6409
- this.baseUrl = null;
6410
- this.username = null;
6406
+ function WebsocketService() {
6411
6407
  this.focused = true;
6408
+ this.wasConnected = false;
6412
6409
  this.connected = false;
6413
6410
  this.isConnecting = false;
6414
- this.stompSubscriptions = new Map();
6415
6411
  this.primitiveManagers = new Map();
6416
- this.disconnectSubject = new Subject();
6417
6412
  this.connect$ = new Subject();
6413
+ this.disconnect$ = new Subject();
6414
+ this.reconnect$ = new Subject();
6415
+ this.error$ = new Subject();
6418
6416
  window.onfocus = this.onFocus;
6419
6417
  window.onblur = this.onBlur;
6420
6418
  this.connect();
@@ -6428,22 +6426,36 @@ var WebsocketService = /** @class */ (function () {
6428
6426
  var _this = this;
6429
6427
  setTimeout(function () {
6430
6428
  if (_this.isConnected()) {
6431
- _this.publishOnConnect();
6429
+ _this.connect$.next();
6432
6430
  }
6433
6431
  }, 0);
6434
- return this.connect$.asObservable();
6432
+ return this.connect$.pipe(first());
6435
6433
  };
6436
6434
  /**
6437
- * Observable responsável por emitir uma notificação quando a conexão é finalizada.
6438
- * @return Um `Observable<void>` que emite uma notificação quando a conexão é finalizada.
6435
+ * Observable responsável por emitir uma notificação quando a conexão é desconectada.
6436
+ * @return Um `Observable<void>` que emite uma notificação quando a conexão é desconectada.
6439
6437
  */
6440
6438
  WebsocketService.prototype.onDisconnect = function () {
6441
- return this.disconnectSubject.asObservable();
6439
+ return this.disconnect$.asObservable();
6440
+ };
6441
+ /**
6442
+ * Observable responsável por emitir uma notificação quando a conexão é reconectada.
6443
+ * @return Um `Observable<void>` que emite uma notificação quando a conexão é reconectada.
6444
+ */
6445
+ WebsocketService.prototype.onReconnect = function () {
6446
+ return this.reconnect$.asObservable();
6447
+ };
6448
+ /**
6449
+ * Observable responsável por emitir uma notificação quando ocorre algum erro.
6450
+ * @return Um `Observable<FrameImpl>` que emite uma notificação quando ocorre algum erro.
6451
+ */
6452
+ WebsocketService.prototype.onError = function () {
6453
+ return this.error$.asObservable();
6442
6454
  };
6443
6455
  /**
6444
6456
  * Observable responsável por emitir uma notificação quando um evento é publicado.
6445
6457
  * @typeParam `<T>` Tipo do objeto que o retorno do `observable` vai devolver.
6446
- * @param domain Dominio da primitva.
6458
+ * @param domain Dominio da primitiva.
6447
6459
  * @param service Service da primitiva.
6448
6460
  * @param primitive Primitiva que será "observada" pelo client.
6449
6461
  * @param identifierPath Caminho até a propriedade considerada o identificador do registro.
@@ -6455,25 +6467,27 @@ var WebsocketService = /** @class */ (function () {
6455
6467
  return this.primitiveManagers.get(primitive).subject.asObservable();
6456
6468
  }
6457
6469
  var primitiveManager = {
6470
+ domain: domain,
6471
+ service: service,
6472
+ primitive: primitive,
6473
+ stompSubscriptions: [],
6458
6474
  subject: new Subject(),
6459
6475
  identifierPath: identifierPath,
6460
6476
  publishedEvents: [],
6461
6477
  };
6462
6478
  this.primitiveManagers.set(primitive, primitiveManager);
6463
6479
  if (this.isConnected()) {
6464
- this.createStompSubscriptions(domain, service, primitive, primitiveManager);
6465
- return primitiveManager.subject.asObservable();
6480
+ this.createStompSubscriptions(primitiveManager);
6481
+ return primitiveManager.subject.pipe(finalize(function () { return _this.disconnectPrimitiveOnFinalize(primitive); }));
6466
6482
  }
6467
6483
  else {
6468
6484
  if (!this.isConnecting) {
6469
6485
  this.connect();
6470
6486
  }
6471
- this.onConnect()
6472
- .pipe(first())
6473
- .subscribe(function () {
6474
- _this.createStompSubscriptions(domain, service, primitive, primitiveManager);
6487
+ this.connect$.pipe(first()).subscribe(function () {
6488
+ _this.createStompSubscriptions(primitiveManager);
6475
6489
  });
6476
- return primitiveManager.subject.asObservable();
6490
+ return primitiveManager.subject.pipe(finalize(function () { return _this.disconnectPrimitiveOnFinalize(primitive); }));
6477
6491
  }
6478
6492
  };
6479
6493
  /**
@@ -6520,12 +6534,12 @@ var WebsocketService = /** @class */ (function () {
6520
6534
  primitiveManager.publishedEvents.push(event);
6521
6535
  }
6522
6536
  };
6523
- WebsocketService.prototype.createStompSubscriptions = function (domain, service, primitive, primitiveManager) {
6524
- var withTokenUrl = this.getSubscriptionUrlWithToken(domain, service, primitive);
6537
+ WebsocketService.prototype.createStompSubscriptions = function (primitiveManager) {
6538
+ var withTokenUrl = this.getSubscriptionUrlWithToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6525
6539
  var stompSubscriptionWithToken = this.createStompSubscription(withTokenUrl, primitiveManager);
6526
- var withoutTokenUrl = this.getSubscriptionUrlWithoutToken(domain, service, primitive);
6540
+ var withoutTokenUrl = this.getSubscriptionUrlWithoutToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6527
6541
  var stompSubscriptionWithoutToken = this.createStompSubscription(withoutTokenUrl, primitiveManager);
6528
- this.stompSubscriptions.set(primitive, [stompSubscriptionWithToken, stompSubscriptionWithoutToken]);
6542
+ primitiveManager.stompSubscriptions = [stompSubscriptionWithToken, stompSubscriptionWithoutToken];
6529
6543
  };
6530
6544
  WebsocketService.prototype.getIdentifierFromEvent = function (identifierPath, event) {
6531
6545
  var e_2, _a;
@@ -6550,19 +6564,24 @@ var WebsocketService = /** @class */ (function () {
6550
6564
  var _this = this;
6551
6565
  this.createStompClient();
6552
6566
  this.isConnecting = true;
6567
+ this._stompClient.activate();
6553
6568
  this._stompClient.connect({}, function () {
6554
- _this.setConnected(true);
6555
6569
  _this.isConnecting = false;
6556
- _this.publishOnConnect();
6557
- }, function () {
6570
+ _this.setConnected(true);
6571
+ if (_this.wasConnected) {
6572
+ _this.reconnectPrimitives();
6573
+ _this.reconnect$.next();
6574
+ }
6575
+ else {
6576
+ _this.wasConnected = true;
6577
+ _this.connect$.next();
6578
+ }
6579
+ }, function (error) {
6558
6580
  _this.setConnected(false);
6559
- // this.reconnectWebSocket();
6581
+ _this.error$.next(error);
6582
+ _this.reconnect();
6560
6583
  });
6561
6584
  };
6562
- WebsocketService.prototype.publishOnConnect = function () {
6563
- this.connect$.next();
6564
- this.connect$.observers = [];
6565
- };
6566
6585
  WebsocketService.prototype.onFocus = function () {
6567
6586
  this.focused = true;
6568
6587
  };
@@ -6570,59 +6589,55 @@ var WebsocketService = /** @class */ (function () {
6570
6589
  this.focused = false;
6571
6590
  };
6572
6591
  WebsocketService.prototype.disconnect = function () {
6573
- var e_3, _a, e_4, _b;
6574
- var observersCount = 0;
6592
+ var e_3, _a, e_4, _b, e_5, _c;
6593
+ var observersCount = this.getObserversCount();
6594
+ if (observersCount > 0)
6595
+ return;
6575
6596
  try {
6576
- for (var _c = __values(this.primitiveManagers.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
6577
- var primitiveManager = _d.value;
6578
- observersCount += primitiveManager.subject.observers.length;
6579
- }
6580
- }
6581
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
6582
- finally {
6583
- try {
6584
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
6585
- }
6586
- finally { if (e_3) throw e_3.error; }
6587
- }
6588
- if (observersCount === 0) {
6589
- this.stompSubscriptions.forEach(function (stompSubscriptions) {
6590
- var e_5, _a;
6597
+ for (var _d = __values(this.primitiveManagers.values()), _e = _d.next(); !_e.done; _e = _d.next()) {
6598
+ var primitiveManager = _e.value;
6591
6599
  try {
6592
- for (var stompSubscriptions_1 = __values(stompSubscriptions), stompSubscriptions_1_1 = stompSubscriptions_1.next(); !stompSubscriptions_1_1.done; stompSubscriptions_1_1 = stompSubscriptions_1.next()) {
6593
- var stompSubscription = stompSubscriptions_1_1.value;
6600
+ for (var _f = (e_4 = void 0, __values(primitiveManager.stompSubscriptions)), _g = _f.next(); !_g.done; _g = _f.next()) {
6601
+ var stompSubscription = _g.value;
6594
6602
  stompSubscription.unsubscribe();
6595
6603
  }
6596
6604
  }
6597
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
6605
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
6598
6606
  finally {
6599
6607
  try {
6600
- if (stompSubscriptions_1_1 && !stompSubscriptions_1_1.done && (_a = stompSubscriptions_1.return)) _a.call(stompSubscriptions_1);
6608
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
6601
6609
  }
6602
- finally { if (e_5) throw e_5.error; }
6610
+ finally { if (e_4) throw e_4.error; }
6603
6611
  }
6604
- });
6612
+ }
6613
+ }
6614
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
6615
+ finally {
6605
6616
  try {
6606
- for (var _e = __values(this.primitiveManagers.values()), _f = _e.next(); !_f.done; _f = _e.next()) {
6607
- var primitiveManager = _f.value;
6608
- primitiveManager.subject.complete();
6609
- }
6617
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
6610
6618
  }
6611
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
6612
- finally {
6613
- try {
6614
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
6615
- }
6616
- finally { if (e_4) throw e_4.error; }
6619
+ finally { if (e_3) throw e_3.error; }
6620
+ }
6621
+ try {
6622
+ for (var _h = __values(this.primitiveManagers.values()), _j = _h.next(); !_j.done; _j = _h.next()) {
6623
+ var primitiveManager = _j.value;
6624
+ primitiveManager.subject.complete();
6617
6625
  }
6618
- this.stompSubscriptions.clear();
6619
- this.primitiveManagers.clear();
6620
- this._stompClient.disconnect();
6621
- this._stompClient.deactivate();
6622
- this.setConnected(false);
6623
- this.isConnecting = false;
6624
- this.disconnectSubject.next();
6625
6626
  }
6627
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
6628
+ finally {
6629
+ try {
6630
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
6631
+ }
6632
+ finally { if (e_5) throw e_5.error; }
6633
+ }
6634
+ this.primitiveManagers.clear();
6635
+ this._stompClient.disconnect();
6636
+ this._stompClient.deactivate();
6637
+ this.setConnected(false);
6638
+ this.isConnecting = false;
6639
+ this.wasConnected = false;
6640
+ this.disconnect$.next();
6626
6641
  };
6627
6642
  WebsocketService.prototype.isConnected = function () {
6628
6643
  return this.connected;
@@ -6650,46 +6665,104 @@ var WebsocketService = /** @class */ (function () {
6650
6665
  WebsocketService.prototype.createStompClient = function () {
6651
6666
  var ws = new SockJS(WebsocketService_1.WEBSOCKET_URL + "subscription", null, { timeout: WebsocketService_1.CONNECTION_TIMEOUT });
6652
6667
  this._stompClient = Stomp.over(ws);
6668
+ // this._stompClient.debug = (str) => console.log(new Date().toISOString(), str);
6653
6669
  this._stompClient.debug = function () { }; // Para remover os logs.
6654
6670
  };
6655
- WebsocketService.prototype.getBaseUrl = function () {
6656
- return this.cookieService.get("com.senior.base.url");
6657
- };
6658
- WebsocketService.prototype.getUserName = function () {
6659
- var token = this.cookieService.get("com.senior.token");
6660
- return token && JSON.parse(token).username;
6661
- };
6662
- WebsocketService.prototype.reconnectWebSocket = function () {
6671
+ WebsocketService.prototype.reconnect = function () {
6663
6672
  var _this = this;
6664
- this._stompClient.disconnect();
6665
- var baseUrl = this.getBaseUrl();
6666
- if (this.baseUrl !== null && baseUrl != this.baseUrl) {
6667
- return;
6668
- }
6669
- var username = this.getUserName();
6670
- if (this.username !== null && username != this.username) {
6671
- return;
6672
- }
6673
+ if (this.connected)
6674
+ this._stompClient.disconnect();
6673
6675
  setTimeout(function () {
6676
+ if (_this.getObserversCount() === 0)
6677
+ return;
6674
6678
  if (_this.focused) {
6675
6679
  _this.connect();
6676
6680
  }
6677
6681
  else {
6678
- _this.reconnectWebSocket();
6682
+ _this.reconnect();
6679
6683
  }
6680
6684
  }, WebsocketService_1.RECONNECT_TIMER);
6681
6685
  };
6686
+ WebsocketService.prototype.reconnectPrimitives = function () {
6687
+ var e_6, _a, e_7, _b;
6688
+ try {
6689
+ for (var _c = __values(this.primitiveManagers.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
6690
+ var primitiveManager = _d.value;
6691
+ try {
6692
+ for (var _e = (e_7 = void 0, __values(primitiveManager.stompSubscriptions)), _f = _e.next(); !_f.done; _f = _e.next()) {
6693
+ var stompSubscription = _f.value;
6694
+ stompSubscription.unsubscribe();
6695
+ }
6696
+ }
6697
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
6698
+ finally {
6699
+ try {
6700
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
6701
+ }
6702
+ finally { if (e_7) throw e_7.error; }
6703
+ }
6704
+ this.createStompSubscriptions(primitiveManager);
6705
+ }
6706
+ }
6707
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
6708
+ finally {
6709
+ try {
6710
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
6711
+ }
6712
+ finally { if (e_6) throw e_6.error; }
6713
+ }
6714
+ };
6715
+ WebsocketService.prototype.getObserversCount = function () {
6716
+ var e_8, _a;
6717
+ var observersCount = 0;
6718
+ try {
6719
+ for (var _b = __values(this.primitiveManagers.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
6720
+ var primitiveManager = _c.value;
6721
+ observersCount += primitiveManager.subject.observers.length;
6722
+ }
6723
+ }
6724
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
6725
+ finally {
6726
+ try {
6727
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6728
+ }
6729
+ finally { if (e_8) throw e_8.error; }
6730
+ }
6731
+ return observersCount;
6732
+ };
6733
+ WebsocketService.prototype.disconnectPrimitiveOnFinalize = function (primitive) {
6734
+ var e_9, _a;
6735
+ var primitiveManager = this.primitiveManagers.get(primitive);
6736
+ if (!primitiveManager)
6737
+ return;
6738
+ // @IMPORTANT: Replace .observers.length with .observed in rxjs 7.0+
6739
+ var hasObservers = !(primitiveManager.subject.observers.length === 1);
6740
+ if (hasObservers)
6741
+ return;
6742
+ try {
6743
+ for (var _b = __values(primitiveManager.stompSubscriptions), _c = _b.next(); !_c.done; _c = _b.next()) {
6744
+ var stompSubscription = _c.value;
6745
+ stompSubscription.unsubscribe();
6746
+ }
6747
+ }
6748
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
6749
+ finally {
6750
+ try {
6751
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6752
+ }
6753
+ finally { if (e_9) throw e_9.error; }
6754
+ }
6755
+ this.primitiveManagers.delete(primitive);
6756
+ this.disconnect();
6757
+ };
6682
6758
  var WebsocketService_1;
6683
6759
  WebsocketService.RECONNECT_TIMER = 3000;
6684
- WebsocketService.CONNECTION_TIMEOUT = 30000;
6760
+ WebsocketService.CONNECTION_TIMEOUT = 15000;
6685
6761
  WebsocketService.BASE_URL_COOKIE = "com.senior.base.url";
6686
6762
  WebsocketService.TOKEN_COOKIE = "com.senior.token";
6687
6763
  WebsocketService.TOKEN = JSON.parse(get(WebsocketService_1.TOKEN_COOKIE) || "{}");
6688
6764
  WebsocketService.WEBSOCKET_URL = get(WebsocketService_1.BASE_URL_COOKIE) + "/websocket/";
6689
- WebsocketService.ctorParameters = function () { return [
6690
- { type: CookieService }
6691
- ]; };
6692
- WebsocketService.ɵprov = ɵɵdefineInjectable({ factory: function WebsocketService_Factory() { return new WebsocketService(ɵɵinject(CookieService)); }, token: WebsocketService, providedIn: "root" });
6765
+ WebsocketService.ɵprov = ɵɵdefineInjectable({ factory: function WebsocketService_Factory() { return new WebsocketService(); }, token: WebsocketService, providedIn: "root" });
6693
6766
  WebsocketService = WebsocketService_1 = __decorate([
6694
6767
  Injectable({
6695
6768
  providedIn: "root",