@senior-gestao-empresarial/angular-components 5.1.1 → 6.0.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.
@@ -6153,14 +6153,23 @@ NpsService = __decorate([
6153
6153
  var WebsocketService_1;
6154
6154
  let WebsocketService = WebsocketService_1 = class WebsocketService {
6155
6155
  constructor() {
6156
+ /** @private */
6156
6157
  this.focused = true;
6158
+ /** @private */
6157
6159
  this.wasConnected = false;
6160
+ /** @private */
6158
6161
  this.connected = false;
6162
+ /** @private */
6159
6163
  this.isConnecting = false;
6164
+ /** @private */
6160
6165
  this.primitiveManagers = new Map();
6166
+ /** @private */
6161
6167
  this.connect$ = new Subject();
6168
+ /** @private */
6162
6169
  this.disconnect$ = new Subject();
6170
+ /** @private */
6163
6171
  this.reconnect$ = new Subject();
6172
+ /** @private */
6164
6173
  this.error$ = new Subject();
6165
6174
  window.onfocus = this.onFocus;
6166
6175
  window.onblur = this.onBlur;
@@ -6200,17 +6209,18 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6200
6209
  return this.error$.asObservable();
6201
6210
  }
6202
6211
  /**
6203
- * Observable responsável por emitir uma notificação quando um evento é publicado.
6204
6212
  * @typeParam `<T>` Tipo do objeto que o retorno do `observable` vai devolver.
6205
- * @param domain Dominio da primitiva.
6206
- * @param service Service da primitiva.
6207
- * @param primitive Primitiva que será "observada" pelo client.
6208
- * @param identifierPath Caminho até a propriedade considerada o identificador do registro.
6209
- * @return Um `observable` que emite notificações toda vez que o respectivo evento é publicado no sistema.
6213
+ * @param {Object} options Objeto de configuração do evento.
6214
+ * @param {string} options.domain Domínio da primitiva.
6215
+ * @param {string} options.service Serviço da primitiva.
6216
+ * @param {string} options.primitive Primitiva que será "observada".
6217
+ * @return Um Observable<T> que emite notificações toda vez que o respectivo evento é publicado.
6210
6218
  */
6211
- onEvent(domain, service, primitive, identifierPath) {
6212
- if (this.primitiveManagers.has(primitive)) {
6213
- return this.primitiveManagers.get(primitive).subject.asObservable();
6219
+ onEvent(options) {
6220
+ const { domain, service, primitive } = options;
6221
+ const key = this.getPrimitiveManagerKey(domain, service, primitive);
6222
+ if (this.primitiveManagers.has(key)) {
6223
+ return this.primitiveManagers.get(key).subject.asObservable();
6214
6224
  }
6215
6225
  const primitiveManager = {
6216
6226
  domain: domain,
@@ -6218,13 +6228,11 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6218
6228
  primitive: primitive,
6219
6229
  stompSubscriptions: [],
6220
6230
  subject: new Subject(),
6221
- identifierPath: identifierPath,
6222
- publishedEvents: [],
6223
6231
  };
6224
- this.primitiveManagers.set(primitive, primitiveManager);
6232
+ this.primitiveManagers.set(key, primitiveManager);
6225
6233
  if (this.isConnected()) {
6226
6234
  this.createStompSubscriptions(primitiveManager);
6227
- return primitiveManager.subject.pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitive)));
6235
+ return primitiveManager.subject.pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitiveManager)));
6228
6236
  }
6229
6237
  else {
6230
6238
  if (!this.isConnecting) {
@@ -6233,37 +6241,10 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6233
6241
  this.connect$.pipe(first()).subscribe(() => {
6234
6242
  this.createStompSubscriptions(primitiveManager);
6235
6243
  });
6236
- return primitiveManager.subject.pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitive)));
6237
- }
6238
- }
6239
- /**
6240
- * Retorna todos os eventos ouvidos pela primitiva com os respectivos identificadores.
6241
- * @typeParam `<T>` Tipo do evento retornado pela primitiva.
6242
- * @param primitive Primitiva que será "observada" pelo client.
6243
- * @param identifiers Array com os identificadores interessados.
6244
- * @return Array contendo o último evento recebido de cada identificador fornecido.
6245
- */
6246
- getPublishedEvents(primitive, identifiers) {
6247
- const publishedEvents = [];
6248
- const primitiveManager = this.primitiveManagers.get(primitive);
6249
- for (const identifier of identifiers) {
6250
- const event = primitiveManager.publishedEvents.find(x => this.getIdentifierFromEvent(primitiveManager.identifierPath, x) === identifier);
6251
- if (event) {
6252
- publishedEvents.push(event);
6253
- }
6254
- }
6255
- return publishedEvents;
6256
- }
6257
- addPublishedEvent(primitiveManager, event) {
6258
- const identifier = this.getIdentifierFromEvent(primitiveManager.identifierPath, event);
6259
- const eventIndex = primitiveManager.publishedEvents.findIndex(x => this.getIdentifierFromEvent(primitiveManager.identifierPath, x) === identifier);
6260
- if (eventIndex !== -1) {
6261
- primitiveManager.publishedEvents[eventIndex] = Object.assign(Object.assign({}, primitiveManager.publishedEvents[eventIndex]), event);
6262
- }
6263
- else {
6264
- primitiveManager.publishedEvents.push(event);
6244
+ return primitiveManager.subject.pipe(finalize(() => this.disconnectPrimitiveOnFinalize(primitiveManager)));
6265
6245
  }
6266
6246
  }
6247
+ /** @private */
6267
6248
  createStompSubscriptions(primitiveManager) {
6268
6249
  const withTokenUrl = this.getSubscriptionUrlWithToken(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6269
6250
  const stompSubscriptionWithToken = this.createStompSubscription(withTokenUrl, primitiveManager);
@@ -6271,14 +6252,7 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6271
6252
  const stompSubscriptionWithoutToken = this.createStompSubscription(withoutTokenUrl, primitiveManager);
6272
6253
  primitiveManager.stompSubscriptions = [stompSubscriptionWithToken, stompSubscriptionWithoutToken];
6273
6254
  }
6274
- getIdentifierFromEvent(identifierPath, event) {
6275
- const properties = identifierPath.split(".");
6276
- let identifier = event;
6277
- for (const property of properties) {
6278
- identifier = identifier[property];
6279
- }
6280
- return identifier;
6281
- }
6255
+ /** @private */
6282
6256
  connect() {
6283
6257
  this.createStompClient();
6284
6258
  this.isConnecting = true;
@@ -6300,9 +6274,11 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6300
6274
  this.reconnect();
6301
6275
  });
6302
6276
  }
6277
+ /** @private */
6303
6278
  onFocus() {
6304
6279
  this.focused = true;
6305
6280
  }
6281
+ /** @private */
6306
6282
  onBlur() {
6307
6283
  this.focused = false;
6308
6284
  }
@@ -6326,34 +6302,40 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6326
6302
  this.wasConnected = false;
6327
6303
  this.disconnect$.next();
6328
6304
  }
6305
+ /** @private */
6329
6306
  isConnected() {
6330
6307
  return this.connected;
6331
6308
  }
6309
+ /** @private */
6332
6310
  setConnected(connected) {
6333
6311
  this.connected = connected;
6334
6312
  }
6313
+ /** @private */
6335
6314
  getSubscriptionUrlWithToken(domain, service, primitive) {
6336
6315
  const tenant = WebsocketService_1.TOKEN ? WebsocketService_1.TOKEN.username.split("@")[1] : null;
6337
6316
  const token = WebsocketService_1.TOKEN ? WebsocketService_1.TOKEN.access_token : null;
6338
6317
  return `/topic/${tenant}/${token}/${domain}/${service}/${primitive}`;
6339
6318
  }
6319
+ /** @private */
6340
6320
  getSubscriptionUrlWithoutToken(domain, service, primitive) {
6341
6321
  const tenant = WebsocketService_1.TOKEN ? WebsocketService_1.TOKEN.username.split("@")[1] : null;
6342
6322
  return `/topic/${tenant}/${domain}/${service}/${primitive}`;
6343
6323
  }
6324
+ /** @private */
6344
6325
  createStompSubscription(destination, primitiveManager) {
6345
6326
  return this._stompClient.subscribe(destination, (message) => {
6346
6327
  const event = JSON.parse(message.body || "{}");
6347
- this.addPublishedEvent(primitiveManager, event);
6348
6328
  primitiveManager.subject.next(event);
6349
6329
  });
6350
6330
  }
6331
+ /** @private */
6351
6332
  createStompClient() {
6352
6333
  const ws = new SockJS(`${WebsocketService_1.WEBSOCKET_URL}subscription`, null, { timeout: WebsocketService_1.CONNECTION_TIMEOUT });
6353
6334
  this._stompClient = Stomp.over(ws);
6354
6335
  // this._stompClient.debug = (str) => console.log(new Date().toISOString(), str);
6355
6336
  this._stompClient.debug = () => { }; // Para remover os logs.
6356
6337
  }
6338
+ /** @private */
6357
6339
  reconnect() {
6358
6340
  if (this.connected)
6359
6341
  this._stompClient.disconnect();
@@ -6368,6 +6350,7 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6368
6350
  }
6369
6351
  }, WebsocketService_1.RECONNECT_TIMER);
6370
6352
  }
6353
+ /** @private */
6371
6354
  reconnectPrimitives() {
6372
6355
  for (const primitiveManager of this.primitiveManagers.values()) {
6373
6356
  for (const stompSubscription of primitiveManager.stompSubscriptions) {
@@ -6376,6 +6359,7 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6376
6359
  this.createStompSubscriptions(primitiveManager);
6377
6360
  }
6378
6361
  }
6362
+ /** @private */
6379
6363
  getObserversCount() {
6380
6364
  let observersCount = 0;
6381
6365
  for (const primitiveManager of this.primitiveManagers.values()) {
@@ -6383,26 +6367,35 @@ let WebsocketService = WebsocketService_1 = class WebsocketService {
6383
6367
  }
6384
6368
  return observersCount;
6385
6369
  }
6386
- disconnectPrimitiveOnFinalize(primitive) {
6387
- const primitiveManager = this.primitiveManagers.get(primitive);
6388
- if (!primitiveManager)
6389
- return;
6390
- // @IMPORTANT: Replace .observers.length with .observed in rxjs 7.0+
6370
+ /** @private */
6371
+ disconnectPrimitiveOnFinalize(primitiveManager) {
6372
+ // @IMPORTANT: Replace .observers.length === 1 with .observed in rxjs 7.0+
6391
6373
  const hasObservers = !(primitiveManager.subject.observers.length === 1);
6392
6374
  if (hasObservers)
6393
6375
  return;
6394
6376
  for (const stompSubscription of primitiveManager.stompSubscriptions) {
6395
6377
  stompSubscription.unsubscribe();
6396
6378
  }
6397
- this.primitiveManagers.delete(primitive);
6379
+ const key = this.getPrimitiveManagerKey(primitiveManager.domain, primitiveManager.service, primitiveManager.primitive);
6380
+ this.primitiveManagers.delete(key);
6398
6381
  this.disconnect();
6399
6382
  }
6383
+ /** @private */
6384
+ getPrimitiveManagerKey(domain, service, primitive) {
6385
+ return `${domain}/${service}/${primitive}`;
6386
+ }
6400
6387
  };
6388
+ /** @private */
6401
6389
  WebsocketService.RECONNECT_TIMER = 3000;
6390
+ /** @private */
6402
6391
  WebsocketService.CONNECTION_TIMEOUT = 15000;
6392
+ /** @private */
6403
6393
  WebsocketService.BASE_URL_COOKIE = "com.senior.base.url";
6394
+ /** @private */
6404
6395
  WebsocketService.TOKEN_COOKIE = "com.senior.token";
6396
+ /** @private */
6405
6397
  WebsocketService.TOKEN = JSON.parse(get(WebsocketService_1.TOKEN_COOKIE) || "{}");
6398
+ /** @private */
6406
6399
  WebsocketService.WEBSOCKET_URL = get(WebsocketService_1.BASE_URL_COOKIE) + "/websocket/";
6407
6400
  WebsocketService.ɵprov = ɵɵdefineInjectable({ factory: function WebsocketService_Factory() { return new WebsocketService(); }, token: WebsocketService, providedIn: "root" });
6408
6401
  WebsocketService = WebsocketService_1 = __decorate([