@senior-gestao-empresarial/angular-components 4.22.1 → 4.22.2-7ecfc706-3270-4664-9d10-a6918ef49acd

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, finalize, first, 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,7 +6420,7 @@ 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;
@@ -6434,53 +6432,69 @@ var WebsocketService = /** @class */ (function () {
6434
6432
  return this.connect$.asObservable();
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,13 +6564,22 @@ 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.publishOnConnect();
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
6585
  WebsocketService.prototype.publishOnConnect = function () {
@@ -6571,59 +6593,54 @@ var WebsocketService = /** @class */ (function () {
6571
6593
  this.focused = false;
6572
6594
  };
6573
6595
  WebsocketService.prototype.disconnect = function () {
6574
- var e_3, _a, e_4, _b;
6575
- var observersCount = 0;
6596
+ var e_3, _a, e_4, _b, e_5, _c;
6597
+ var observersCount = this.getObserversCount();
6598
+ if (observersCount > 0)
6599
+ return;
6576
6600
  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;
6601
+ for (var _d = __values(this.primitiveManagers.values()), _e = _d.next(); !_e.done; _e = _d.next()) {
6602
+ var primitiveManager = _e.value;
6592
6603
  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;
6604
+ for (var _f = (e_4 = void 0, __values(primitiveManager.stompSubscriptions)), _g = _f.next(); !_g.done; _g = _f.next()) {
6605
+ var stompSubscription = _g.value;
6595
6606
  stompSubscription.unsubscribe();
6596
6607
  }
6597
6608
  }
6598
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
6609
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
6599
6610
  finally {
6600
6611
  try {
6601
- if (stompSubscriptions_1_1 && !stompSubscriptions_1_1.done && (_a = stompSubscriptions_1.return)) _a.call(stompSubscriptions_1);
6612
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
6602
6613
  }
6603
- finally { if (e_5) throw e_5.error; }
6614
+ finally { if (e_4) throw e_4.error; }
6604
6615
  }
6605
- });
6616
+ }
6617
+ }
6618
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
6619
+ finally {
6606
6620
  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
- }
6621
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
6611
6622
  }
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; }
6623
+ finally { if (e_3) throw e_3.error; }
6624
+ }
6625
+ try {
6626
+ for (var _h = __values(this.primitiveManagers.values()), _j = _h.next(); !_j.done; _j = _h.next()) {
6627
+ var primitiveManager = _j.value;
6628
+ primitiveManager.subject.complete();
6618
6629
  }
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
6630
  }
6631
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
6632
+ finally {
6633
+ try {
6634
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
6635
+ }
6636
+ finally { if (e_5) throw e_5.error; }
6637
+ }
6638
+ this.primitiveManagers.clear();
6639
+ this._stompClient.disconnect();
6640
+ this._stompClient.deactivate();
6641
+ this.setConnected(false);
6642
+ this.isConnecting = false;
6643
+ this.disconnect$.next();
6627
6644
  };
6628
6645
  WebsocketService.prototype.isConnected = function () {
6629
6646
  return this.connected;
@@ -6651,46 +6668,104 @@ var WebsocketService = /** @class */ (function () {
6651
6668
  WebsocketService.prototype.createStompClient = function () {
6652
6669
  var ws = new SockJS(WebsocketService_1.WEBSOCKET_URL + "subscription", null, { timeout: WebsocketService_1.CONNECTION_TIMEOUT });
6653
6670
  this._stompClient = Stomp.over(ws);
6671
+ // this._stompClient.debug = (str) => console.log(new Date().toISOString(), str);
6654
6672
  this._stompClient.debug = function () { }; // Para remover os logs.
6655
6673
  };
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 () {
6674
+ WebsocketService.prototype.reconnect = function () {
6664
6675
  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
- }
6676
+ if (this.connected)
6677
+ this._stompClient.disconnect();
6674
6678
  setTimeout(function () {
6679
+ if (_this.getObserversCount() === 0)
6680
+ return;
6675
6681
  if (_this.focused) {
6676
6682
  _this.connect();
6677
6683
  }
6678
6684
  else {
6679
- _this.reconnectWebSocket();
6685
+ _this.reconnect();
6680
6686
  }
6681
6687
  }, WebsocketService_1.RECONNECT_TIMER);
6682
6688
  };
6689
+ WebsocketService.prototype.reconnectPrimitives = function () {
6690
+ var e_6, _a, e_7, _b;
6691
+ try {
6692
+ for (var _c = __values(this.primitiveManagers.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
6693
+ var primitiveManager = _d.value;
6694
+ try {
6695
+ for (var _e = (e_7 = void 0, __values(primitiveManager.stompSubscriptions)), _f = _e.next(); !_f.done; _f = _e.next()) {
6696
+ var stompSubscription = _f.value;
6697
+ stompSubscription.unsubscribe();
6698
+ }
6699
+ }
6700
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
6701
+ finally {
6702
+ try {
6703
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
6704
+ }
6705
+ finally { if (e_7) throw e_7.error; }
6706
+ }
6707
+ this.createStompSubscriptions(primitiveManager);
6708
+ }
6709
+ }
6710
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
6711
+ finally {
6712
+ try {
6713
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
6714
+ }
6715
+ finally { if (e_6) throw e_6.error; }
6716
+ }
6717
+ };
6718
+ WebsocketService.prototype.getObserversCount = function () {
6719
+ var e_8, _a;
6720
+ var observersCount = 0;
6721
+ try {
6722
+ for (var _b = __values(this.primitiveManagers.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
6723
+ var primitiveManager = _c.value;
6724
+ observersCount += primitiveManager.subject.observers.length;
6725
+ }
6726
+ }
6727
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
6728
+ finally {
6729
+ try {
6730
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6731
+ }
6732
+ finally { if (e_8) throw e_8.error; }
6733
+ }
6734
+ return observersCount;
6735
+ };
6736
+ WebsocketService.prototype.disconnectPrimitiveOnFinalize = function (primitive) {
6737
+ var e_9, _a;
6738
+ var primitiveManager = this.primitiveManagers.get(primitive);
6739
+ if (!primitiveManager)
6740
+ return;
6741
+ // @IMPORTANT: Replace .observers.length with .observed in rxjs 7.0+
6742
+ var hasObservers = !(primitiveManager.subject.observers.length === 1);
6743
+ if (hasObservers)
6744
+ return;
6745
+ try {
6746
+ for (var _b = __values(primitiveManager.stompSubscriptions), _c = _b.next(); !_c.done; _c = _b.next()) {
6747
+ var stompSubscription = _c.value;
6748
+ stompSubscription.unsubscribe();
6749
+ }
6750
+ }
6751
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
6752
+ finally {
6753
+ try {
6754
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6755
+ }
6756
+ finally { if (e_9) throw e_9.error; }
6757
+ }
6758
+ this.primitiveManagers.delete(primitive);
6759
+ this.disconnect();
6760
+ };
6683
6761
  var WebsocketService_1;
6684
6762
  WebsocketService.RECONNECT_TIMER = 3000;
6685
- WebsocketService.CONNECTION_TIMEOUT = 30000;
6763
+ WebsocketService.CONNECTION_TIMEOUT = 15000;
6686
6764
  WebsocketService.BASE_URL_COOKIE = "com.senior.base.url";
6687
6765
  WebsocketService.TOKEN_COOKIE = "com.senior.token";
6688
6766
  WebsocketService.TOKEN = JSON.parse(get(WebsocketService_1.TOKEN_COOKIE) || "{}");
6689
6767
  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" });
6768
+ WebsocketService.ɵprov = ɵɵdefineInjectable({ factory: function WebsocketService_Factory() { return new WebsocketService(); }, token: WebsocketService, providedIn: "root" });
6694
6769
  WebsocketService = WebsocketService_1 = __decorate([
6695
6770
  Injectable({
6696
6771
  providedIn: "root",