@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
  let BreadcrumbComponent = class BreadcrumbComponent {
@@ -6014,17 +6013,16 @@ NpsService = __decorate([
6014
6013
 
6015
6014
  var WebsocketService_1;
6016
6015
  let WebsocketService = WebsocketService_1 = class WebsocketService {
6017
- constructor(cookieService) {
6018
- this.cookieService = cookieService;
6019
- this.baseUrl = null;
6020
- this.username = null;
6016
+ constructor() {
6021
6017
  this.focused = true;
6018
+ this.wasConnected = false;
6022
6019
  this.connected = false;
6023
6020
  this.isConnecting = false;
6024
- this.stompSubscriptions = new Map();
6025
6021
  this.primitiveManagers = new Map();
6026
- this.disconnectSubject = new Subject();
6027
6022
  this.connect$ = new Subject();
6023
+ this.disconnect$ = new Subject();
6024
+ this.reconnect$ = new Subject();
6025
+ this.error$ = new Subject();
6028
6026
  window.onfocus = this.onFocus;
6029
6027
  window.onblur = this.onBlur;
6030
6028
  this.connect();
@@ -6036,22 +6034,36 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6036
6034
  onConnect() {
6037
6035
  setTimeout(() => {
6038
6036
  if (this.isConnected()) {
6039
- this.publishOnConnect();
6037
+ this.connect$.next();
6040
6038
  }
6041
6039
  }, 0);
6042
- return this.connect$.asObservable();
6040
+ return this.connect$.pipe(first());
6043
6041
  }
6044
6042
  /**
6045
- * Observable responsável por emitir uma notificação quando a conexão é finalizada.
6046
- * @return Um `Observable<void>` que emite uma notificação quando a conexão é finalizada.
6043
+ * Observable responsável por emitir uma notificação quando a conexão é desconectada.
6044
+ * @return Um `Observable<void>` que emite uma notificação quando a conexão é desconectada.
6047
6045
  */
6048
6046
  onDisconnect() {
6049
- return this.disconnectSubject.asObservable();
6047
+ return this.disconnect$.asObservable();
6048
+ }
6049
+ /**
6050
+ * Observable responsável por emitir uma notificação quando a conexão é reconectada.
6051
+ * @return Um `Observable<void>` que emite uma notificação quando a conexão é reconectada.
6052
+ */
6053
+ onReconnect() {
6054
+ return this.reconnect$.asObservable();
6055
+ }
6056
+ /**
6057
+ * Observable responsável por emitir uma notificação quando ocorre algum erro.
6058
+ * @return Um `Observable<FrameImpl>` que emite uma notificação quando ocorre algum erro.
6059
+ */
6060
+ onError() {
6061
+ return this.error$.asObservable();
6050
6062
  }
6051
6063
  /**
6052
6064
  * Observable responsável por emitir uma notificação quando um evento é publicado.
6053
6065
  * @typeParam `<T>` Tipo do objeto que o retorno do `observable` vai devolver.
6054
- * @param domain Dominio da primitva.
6066
+ * @param domain Dominio da primitiva.
6055
6067
  * @param service Service da primitiva.
6056
6068
  * @param primitive Primitiva que será "observada" pelo client.
6057
6069
  * @param identifierPath Caminho até a propriedade considerada o identificador do registro.
@@ -6062,25 +6074,27 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6062
6074
  return this.primitiveManagers.get(primitive).subject.asObservable();
6063
6075
  }
6064
6076
  const primitiveManager = {
6077
+ domain: domain,
6078
+ service: service,
6079
+ primitive: primitive,
6080
+ stompSubscriptions: [],
6065
6081
  subject: new Subject(),
6066
6082
  identifierPath: identifierPath,
6067
6083
  publishedEvents: [],
6068
6084
  };
6069
6085
  this.primitiveManagers.set(primitive, primitiveManager);
6070
6086
  if (this.isConnected()) {
6071
- this.createStompSubscriptions(domain, service, primitive, primitiveManager);
6072
- return primitiveManager.subject.asObservable();
6087
+ this.createStompSubscriptions(primitiveManager);
6088
+ return primitiveManager.subject.pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitive)));
6073
6089
  }
6074
6090
  else {
6075
6091
  if (!this.isConnecting) {
6076
6092
  this.connect();
6077
6093
  }
6078
- this.onConnect()
6079
- .pipe(first())
6080
- .subscribe(() => {
6081
- this.createStompSubscriptions(domain, service, primitive, primitiveManager);
6094
+ this.connect$.pipe(first()).subscribe(() => {
6095
+ this.createStompSubscriptions(primitiveManager);
6082
6096
  });
6083
- return primitiveManager.subject.asObservable();
6097
+ return primitiveManager.subject.pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitive)));
6084
6098
  }
6085
6099
  }
6086
6100
  /**
@@ -6111,12 +6125,12 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6111
6125
  primitiveManager.publishedEvents.push(event);
6112
6126
  }
6113
6127
  }
6114
- createStompSubscriptions(domain, service, primitive, primitiveManager) {
6115
- const withTokenUrl = this.getSubscriptionUrlWithToken(domain, service, primitive);
6128
+ createStompSubscriptions(primitiveManager) {
6129
+ const withTokenUrl = this.getSubscriptionUrlWithToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6116
6130
  const stompSubscriptionWithToken = this.createStompSubscription(withTokenUrl, primitiveManager);
6117
- const withoutTokenUrl = this.getSubscriptionUrlWithoutToken(domain, service, primitive);
6131
+ const withoutTokenUrl = this.getSubscriptionUrlWithoutToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6118
6132
  const stompSubscriptionWithoutToken = this.createStompSubscription(withoutTokenUrl, primitiveManager);
6119
- this.stompSubscriptions.set(primitive, [stompSubscriptionWithToken, stompSubscriptionWithoutToken]);
6133
+ primitiveManager.stompSubscriptions = [stompSubscriptionWithToken, stompSubscriptionWithoutToken];
6120
6134
  }
6121
6135
  getIdentifierFromEvent(identifierPath, event) {
6122
6136
  const properties = identifierPath.split(".");
@@ -6129,19 +6143,24 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6129
6143
  connect() {
6130
6144
  this.createStompClient();
6131
6145
  this.isConnecting = true;
6146
+ this._stompClient.activate();
6132
6147
  this._stompClient.connect({}, () => {
6133
- this.setConnected(true);
6134
6148
  this.isConnecting = false;
6135
- this.publishOnConnect();
6136
- }, () => {
6149
+ this.setConnected(true);
6150
+ if (this.wasConnected) {
6151
+ this.reconnectPrimitives();
6152
+ this.reconnect$.next();
6153
+ }
6154
+ else {
6155
+ this.wasConnected = true;
6156
+ this.connect$.next();
6157
+ }
6158
+ }, (error) => {
6137
6159
  this.setConnected(false);
6138
- // this.reconnectWebSocket();
6160
+ this.error$.next(error);
6161
+ this.reconnect();
6139
6162
  });
6140
6163
  }
6141
- publishOnConnect() {
6142
- this.connect$.next();
6143
- this.connect$.observers = [];
6144
- }
6145
6164
  onFocus() {
6146
6165
  this.focused = true;
6147
6166
  }
@@ -6149,27 +6168,24 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6149
6168
  this.focused = false;
6150
6169
  }
6151
6170
  disconnect() {
6152
- let observersCount = 0;
6171
+ const observersCount = this.getObserversCount();
6172
+ if (observersCount > 0)
6173
+ return;
6153
6174
  for (const primitiveManager of this.primitiveManagers.values()) {
6154
- observersCount += primitiveManager.subject.observers.length;
6155
- }
6156
- if (observersCount === 0) {
6157
- this.stompSubscriptions.forEach(stompSubscriptions => {
6158
- for (const stompSubscription of stompSubscriptions) {
6159
- stompSubscription.unsubscribe();
6160
- }
6161
- });
6162
- for (const primitiveManager of this.primitiveManagers.values()) {
6163
- primitiveManager.subject.complete();
6175
+ for (const stompSubscription of primitiveManager.stompSubscriptions) {
6176
+ stompSubscription.unsubscribe();
6164
6177
  }
6165
- this.stompSubscriptions.clear();
6166
- this.primitiveManagers.clear();
6167
- this._stompClient.disconnect();
6168
- this._stompClient.deactivate();
6169
- this.setConnected(false);
6170
- this.isConnecting = false;
6171
- this.disconnectSubject.next();
6172
6178
  }
6179
+ for (const primitiveManager of this.primitiveManagers.values()) {
6180
+ primitiveManager.subject.complete();
6181
+ }
6182
+ this.primitiveManagers.clear();
6183
+ this._stompClient.disconnect();
6184
+ this._stompClient.deactivate();
6185
+ this.setConnected(false);
6186
+ this.isConnecting = false;
6187
+ this.wasConnected = false;
6188
+ this.disconnect$.next();
6173
6189
  }
6174
6190
  isConnected() {
6175
6191
  return this.connected;
@@ -6196,45 +6212,60 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6196
6212
  createStompClient() {
6197
6213
  const ws = new SockJS(`${WebsocketService_1.WEBSOCKET_URL}subscription`, null, { timeout: WebsocketService_1.CONNECTION_TIMEOUT });
6198
6214
  this._stompClient = Stomp.over(ws);
6215
+ // this._stompClient.debug = (str) => console.log(new Date().toISOString(), str);
6199
6216
  this._stompClient.debug = () => { }; // Para remover os logs.
6200
6217
  }
6201
- getBaseUrl() {
6202
- return this.cookieService.get("com.senior.base.url");
6203
- }
6204
- getUserName() {
6205
- const token = this.cookieService.get("com.senior.token");
6206
- return token && JSON.parse(token).username;
6207
- }
6208
- reconnectWebSocket() {
6209
- this._stompClient.disconnect();
6210
- const baseUrl = this.getBaseUrl();
6211
- if (this.baseUrl !== null && baseUrl != this.baseUrl) {
6212
- return;
6213
- }
6214
- const username = this.getUserName();
6215
- if (this.username !== null && username != this.username) {
6216
- return;
6217
- }
6218
+ reconnect() {
6219
+ if (this.connected)
6220
+ this._stompClient.disconnect();
6218
6221
  setTimeout(() => {
6222
+ if (this.getObserversCount() === 0)
6223
+ return;
6219
6224
  if (this.focused) {
6220
6225
  this.connect();
6221
6226
  }
6222
6227
  else {
6223
- this.reconnectWebSocket();
6228
+ this.reconnect();
6224
6229
  }
6225
6230
  }, WebsocketService_1.RECONNECT_TIMER);
6226
6231
  }
6232
+ reconnectPrimitives() {
6233
+ for (const primitiveManager of this.primitiveManagers.values()) {
6234
+ for (const stompSubscription of primitiveManager.stompSubscriptions) {
6235
+ stompSubscription.unsubscribe();
6236
+ }
6237
+ this.createStompSubscriptions(primitiveManager);
6238
+ }
6239
+ }
6240
+ getObserversCount() {
6241
+ let observersCount = 0;
6242
+ for (const primitiveManager of this.primitiveManagers.values()) {
6243
+ observersCount += primitiveManager.subject.observers.length;
6244
+ }
6245
+ return observersCount;
6246
+ }
6247
+ disconnectPrimitiveOnFinalize(primitive) {
6248
+ const primitiveManager = this.primitiveManagers.get(primitive);
6249
+ if (!primitiveManager)
6250
+ return;
6251
+ // @IMPORTANT: Replace .observers.length with .observed in rxjs 7.0+
6252
+ const hasObservers = !(primitiveManager.subject.observers.length === 1);
6253
+ if (hasObservers)
6254
+ return;
6255
+ for (const stompSubscription of primitiveManager.stompSubscriptions) {
6256
+ stompSubscription.unsubscribe();
6257
+ }
6258
+ this.primitiveManagers.delete(primitive);
6259
+ this.disconnect();
6260
+ }
6227
6261
  };
6228
6262
  WebsocketService.RECONNECT_TIMER = 3000;
6229
- WebsocketService.CONNECTION_TIMEOUT = 30000;
6263
+ WebsocketService.CONNECTION_TIMEOUT = 15000;
6230
6264
  WebsocketService.BASE_URL_COOKIE = "com.senior.base.url";
6231
6265
  WebsocketService.TOKEN_COOKIE = "com.senior.token";
6232
6266
  WebsocketService.TOKEN = JSON.parse(get(WebsocketService_1.TOKEN_COOKIE) || "{}");
6233
6267
  WebsocketService.WEBSOCKET_URL = get(WebsocketService_1.BASE_URL_COOKIE) + "/websocket/";
6234
- WebsocketService.ctorParameters = () => [
6235
- { type: CookieService }
6236
- ];
6237
- WebsocketService.ɵprov = ɵɵdefineInjectable({ factory: function WebsocketService_Factory() { return new WebsocketService(ɵɵinject(CookieService)); }, token: WebsocketService, providedIn: "root" });
6268
+ WebsocketService.ɵprov = ɵɵdefineInjectable({ factory: function WebsocketService_Factory() { return new WebsocketService(); }, token: WebsocketService, providedIn: "root" });
6238
6269
  WebsocketService = WebsocketService_1 = __decorate([
6239
6270
  Injectable({
6240
6271
  providedIn: "root",