@senior-gestao-empresarial/angular-components 4.22.1 → 4.22.2-7c0b6b2c-1273-4faf-bd8a-91d677cec9bd

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();
@@ -6422,65 +6420,81 @@ var WebsocketService = /** @class */ (function () {
6422
6420
  WebsocketService_1 = WebsocketService;
6423
6421
  /**
6424
6422
  * Observable responsável por emitir uma notificação quando a conexão websocket é estabelecida.
6425
- * @return Um `Observable<void>` que emite uma notificação quando a conexão websocket é estabelecida ou já está em andamento.
6423
+ * @return Um `Observable<void>` que emite uma notificação quando a conexão websocket é estabelecida.
6426
6424
  */
6427
6425
  WebsocketService.prototype.onConnect = 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
- * @param identifierPath Caminho até a propriedade considerada o indentificador do registro.
6461
+ * @param identifierPath Caminho até a propriedade considerada o identificador do registro.
6450
6462
  * @return Um `observable` que emite notificações toda vez que o respectivo evento é publicado no sistema.
6451
6463
  */
6452
6464
  WebsocketService.prototype.onEvent = function (domain, service, primitive, identifierPath) {
6453
6465
  var _this = this;
6454
- var primitiveManager = this.primitiveManagers.get(primitive);
6455
- if (primitiveManager) {
6456
- return primitiveManager.subject.asObservable();
6466
+ if (this.primitiveManagers.has(primitive)) {
6467
+ return this.primitiveManagers.get(primitive).subject.asObservable();
6457
6468
  }
6458
- primitiveManager = {
6469
+ var primitiveManager = {
6470
+ domain: domain,
6471
+ service: service,
6472
+ primitive: primitive,
6473
+ stompSubscriptions: [],
6459
6474
  subject: new Subject(),
6460
6475
  identifierPath: identifierPath,
6461
6476
  publishedEvents: [],
6462
6477
  };
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
  /**
6480
6494
  * Retorna todos os eventos ouvidos pela primitiva com os respectivos identificadores.
6481
6495
  * @typeParam `<T>` Tipo do evento retornado pela primitiva.
6482
6496
  * @param primitive Primitiva que será "observada" pelo client.
6483
- * @param identifiers Array com os indentificadores interessados.
6497
+ * @param identifiers Array com os identificadores interessados.
6484
6498
  * @return Array contendo o último evento recebido de cada identificador fornecido.
6485
6499
  */
6486
6500
  WebsocketService.prototype.getPublishedEvents = function (primitive, identifiers) {
@@ -6520,13 +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);
6525
- this.primitiveManagers.set(primitive, primitiveManager);
6537
+ WebsocketService.prototype.createStompSubscriptions = function (primitiveManager) {
6538
+ var withTokenUrl = this.getSubscriptionUrlWithToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6526
6539
  var stompSubscriptionWithToken = this.createStompSubscription(withTokenUrl, primitiveManager);
6527
- var withoutTokenUrl = this.getSubscriptionUrlWithoutToken(domain, service, primitive);
6540
+ var withoutTokenUrl = this.getSubscriptionUrlWithoutToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6528
6541
  var stompSubscriptionWithoutToken = this.createStompSubscription(withoutTokenUrl, primitiveManager);
6529
- this.stompSubscriptions.set(primitive, [stompSubscriptionWithToken, stompSubscriptionWithoutToken]);
6542
+ primitiveManager.stompSubscriptions = [stompSubscriptionWithToken, stompSubscriptionWithoutToken];
6530
6543
  };
6531
6544
  WebsocketService.prototype.getIdentifierFromEvent = function (identifierPath, event) {
6532
6545
  var e_2, _a;
@@ -6551,19 +6564,24 @@ var WebsocketService = /** @class */ (function () {
6551
6564
  var _this = this;
6552
6565
  this.createStompClient();
6553
6566
  this.isConnecting = true;
6567
+ this._stompClient.activate();
6554
6568
  this._stompClient.connect({}, function () {
6555
- _this.setConnected(true);
6556
6569
  _this.isConnecting = false;
6557
- _this.publishOnConnect();
6558
- }, 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) {
6559
6580
  _this.setConnected(false);
6560
- // this.reconnectWebSocket();
6581
+ _this.error$.next(error);
6582
+ _this.reconnect();
6561
6583
  });
6562
6584
  };
6563
- WebsocketService.prototype.publishOnConnect = function () {
6564
- this.connect$.next();
6565
- this.connect$.observers = [];
6566
- };
6567
6585
  WebsocketService.prototype.onFocus = function () {
6568
6586
  this.focused = true;
6569
6587
  };
@@ -6571,59 +6589,55 @@ var WebsocketService = /** @class */ (function () {
6571
6589
  this.focused = false;
6572
6590
  };
6573
6591
  WebsocketService.prototype.disconnect = function () {
6574
- var e_3, _a, e_4, _b;
6575
- 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;
6576
6596
  try {
6577
- for (var _c = __values(this.primitiveManagers.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
6578
- var primitiveManager = _d.value;
6579
- observersCount += primitiveManager.subject.observers.length;
6580
- }
6581
- }
6582
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
6583
- finally {
6584
- try {
6585
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
6586
- }
6587
- finally { if (e_3) throw e_3.error; }
6588
- }
6589
- if (observersCount === 0) {
6590
- this.stompSubscriptions.forEach(function (stompSubscriptions) {
6591
- 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;
6592
6599
  try {
6593
- for (var stompSubscriptions_1 = __values(stompSubscriptions), stompSubscriptions_1_1 = stompSubscriptions_1.next(); !stompSubscriptions_1_1.done; stompSubscriptions_1_1 = stompSubscriptions_1.next()) {
6594
- 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;
6595
6602
  stompSubscription.unsubscribe();
6596
6603
  }
6597
6604
  }
6598
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
6605
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
6599
6606
  finally {
6600
6607
  try {
6601
- 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);
6602
6609
  }
6603
- finally { if (e_5) throw e_5.error; }
6610
+ finally { if (e_4) throw e_4.error; }
6604
6611
  }
6605
- });
6612
+ }
6613
+ }
6614
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
6615
+ finally {
6606
6616
  try {
6607
- for (var _e = __values(this.primitiveManagers.values()), _f = _e.next(); !_f.done; _f = _e.next()) {
6608
- var primitiveManager = _f.value;
6609
- primitiveManager.subject.complete();
6610
- }
6617
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
6611
6618
  }
6612
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
6613
- finally {
6614
- try {
6615
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
6616
- }
6617
- 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();
6618
6625
  }
6619
- this.stompSubscriptions.clear();
6620
- this.primitiveManagers.clear();
6621
- this._stompClient.disconnect();
6622
- this._stompClient.deactivate();
6623
- this.setConnected(false);
6624
- this.isConnecting = false;
6625
- this.disconnectSubject.next();
6626
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();
6627
6641
  };
6628
6642
  WebsocketService.prototype.isConnected = function () {
6629
6643
  return this.connected;
@@ -6651,46 +6665,104 @@ var WebsocketService = /** @class */ (function () {
6651
6665
  WebsocketService.prototype.createStompClient = function () {
6652
6666
  var ws = new SockJS(WebsocketService_1.WEBSOCKET_URL + "subscription", null, { timeout: WebsocketService_1.CONNECTION_TIMEOUT });
6653
6667
  this._stompClient = Stomp.over(ws);
6668
+ // this._stompClient.debug = (str) => console.log(new Date().toISOString(), str);
6654
6669
  this._stompClient.debug = function () { }; // Para remover os logs.
6655
6670
  };
6656
- WebsocketService.prototype.getBaseUrl = function () {
6657
- return this.cookieService.get("com.senior.base.url");
6658
- };
6659
- WebsocketService.prototype.getUserName = function () {
6660
- var token = this.cookieService.get("com.senior.token");
6661
- return token && JSON.parse(token).username;
6662
- };
6663
- WebsocketService.prototype.reconnectWebSocket = function () {
6671
+ WebsocketService.prototype.reconnect = function () {
6664
6672
  var _this = this;
6665
- this._stompClient.disconnect();
6666
- var baseUrl = this.getBaseUrl();
6667
- if (this.baseUrl !== null && baseUrl != this.baseUrl) {
6668
- return;
6669
- }
6670
- var username = this.getUserName();
6671
- if (this.username !== null && username != this.username) {
6672
- return;
6673
- }
6673
+ if (this.connected)
6674
+ this._stompClient.disconnect();
6674
6675
  setTimeout(function () {
6676
+ if (_this.getObserversCount() === 0)
6677
+ return;
6675
6678
  if (_this.focused) {
6676
6679
  _this.connect();
6677
6680
  }
6678
6681
  else {
6679
- _this.reconnectWebSocket();
6682
+ _this.reconnect();
6680
6683
  }
6681
6684
  }, WebsocketService_1.RECONNECT_TIMER);
6682
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
+ };
6683
6758
  var WebsocketService_1;
6684
6759
  WebsocketService.RECONNECT_TIMER = 3000;
6685
- WebsocketService.CONNECTION_TIMEOUT = 30000;
6760
+ WebsocketService.CONNECTION_TIMEOUT = 15000;
6686
6761
  WebsocketService.BASE_URL_COOKIE = "com.senior.base.url";
6687
6762
  WebsocketService.TOKEN_COOKIE = "com.senior.token";
6688
6763
  WebsocketService.TOKEN = JSON.parse(get(WebsocketService_1.TOKEN_COOKIE) || "{}");
6689
6764
  WebsocketService.WEBSOCKET_URL = get(WebsocketService_1.BASE_URL_COOKIE) + "/websocket/";
6690
- WebsocketService.ctorParameters = function () { return [
6691
- { type: CookieService }
6692
- ]; };
6693
- 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" });
6694
6766
  WebsocketService = WebsocketService_1 = __decorate([
6695
6767
  Injectable({
6696
6768
  providedIn: "root",