@sumaris-net/ngx-components 1.9.4 → 1.9.5

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.
@@ -10614,8 +10614,6 @@
10614
10614
  defaultLocale: 'fr',
10615
10615
  defaultLatLongFormat: 'DDMM',
10616
10616
  apolloFetchPolicy: 'cache-first',
10617
- // FIXME: GraphQL subscription never unsubscribe...
10618
- listenRemoteChanges: false,
10619
10617
  // FIXME: enable cache
10620
10618
  persistCache: false,
10621
10619
  // TODO: make this works
@@ -10668,6 +10666,13 @@
10668
10666
  // username: 'admq2', password: 'q22006'
10669
10667
  // Token auth (using Person.pubkey)
10670
10668
  username: 'admin@sumaris.net', password: 'admin',
10669
+ },
10670
+ account: {
10671
+ enableListenChanges: true,
10672
+ listenIntervalInSecond: 30
10673
+ },
10674
+ entityEditor: {
10675
+ enableListenChanges: false
10671
10676
  }
10672
10677
  });
10673
10678
 
@@ -14385,9 +14390,7 @@
14385
14390
  _this.cryptoService = cryptoService;
14386
14391
  _this.environment = environment;
14387
14392
  _this.typePolicies = typePolicies;
14388
- _this.connectionParams = {
14389
- reconnect: true
14390
- };
14393
+ _this.connectionParams = {};
14391
14394
  _this.onNetworkError = new rxjs.Subject();
14392
14395
  _this.customErrors = {};
14393
14396
  _this._debug = !environment.production;
@@ -14915,8 +14918,6 @@
14915
14918
  }
14916
14919
  var headers = new i2$1.HttpHeaders()
14917
14920
  .append('Authorization', authorization);
14918
- //.append('X-App-Name', environment.name)
14919
- //.append('X-App-Version', environment.version)
14920
14921
  // Use the setContext method to set the HTTP headers.
14921
14922
  operation.setContext(Object.assign(Object.assign({}, operation.getContext()), { headers: headers }));
14922
14923
  // Call the next link in the middleware chain.
@@ -15445,7 +15446,7 @@
15445
15446
  _this.onChange = new rxjs.Subject();
15446
15447
  _this.onAuthTokenChange = new rxjs.Subject();
15447
15448
  _this.onAuthBasicChange = new rxjs.Subject();
15448
- _this._stopWatching$ = new rxjs.Subject();
15449
+ _this._listenChangesSubscription = null;
15449
15450
  _this._cache = {
15450
15451
  loaded: false,
15451
15452
  keypair: null,
@@ -15458,6 +15459,7 @@
15458
15459
  };
15459
15460
  _this._$additionalFields = new rxjs.BehaviorSubject([]);
15460
15461
  _this._tokenType$ = new rxjs.BehaviorSubject(undefined);
15462
+ _this._enableListenChanges = (!environment.account || environment.account.enableListenChanges !== false);
15461
15463
  _this._debug = !environment.production;
15462
15464
  if (_this._debug)
15463
15465
  console.debug('[account-service] Creating service');
@@ -15701,6 +15703,9 @@
15701
15703
  // Emit events
15702
15704
  this.onLogin.next(this._data);
15703
15705
  this.onChange.next(this._data);
15706
+ // Listen remote changes
15707
+ if (this._enableListenChanges)
15708
+ this.startListenChanges();
15704
15709
  return [2 /*return*/, this._data];
15705
15710
  case 6:
15706
15711
  error_1 = _b.sent();
@@ -15855,13 +15860,16 @@
15855
15860
  // Emit event to observers
15856
15861
  this.onLogin.next(this._data);
15857
15862
  this.onChange.next(this._data);
15863
+ if (this._enableListenChanges)
15864
+ this.startListenChanges();
15858
15865
  return [2 /*return*/, this._data];
15859
15866
  }
15860
15867
  });
15861
15868
  });
15862
15869
  };
15863
- AccountService.prototype.refresh = function () {
15870
+ AccountService.prototype.reload = function () {
15864
15871
  return __awaiter(this, void 0, void 0, function () {
15872
+ var now;
15865
15873
  return __generator(this, function (_b) {
15866
15874
  switch (_b.label) {
15867
15875
  case 0:
@@ -15869,13 +15877,15 @@
15869
15877
  throw new Error('User not logged');
15870
15878
  if (this.network.offline)
15871
15879
  throw new Error('Cannot check account in offline mode');
15880
+ now = Date.now();
15881
+ console.debug("[account] Reloading account...");
15872
15882
  return [4 /*yield*/, this.loadData()];
15873
15883
  case 1:
15874
15884
  _b.sent();
15875
15885
  return [4 /*yield*/, this.saveLocally()];
15876
15886
  case 2:
15877
15887
  _b.sent();
15878
- console.debug('[account] Successfully reload account');
15888
+ console.debug("[account] Reloading account [OK] in " + (Date.now() - now) + "ms");
15879
15889
  // Emit login event to subscribers
15880
15890
  this.onLogin.next(this._data);
15881
15891
  this.onChange.next(this._data);
@@ -15972,7 +15982,7 @@
15972
15982
  if (hadAuthBasic)
15973
15983
  this.onAuthBasicChange.next(undefined);
15974
15984
  this.onChange.next(undefined);
15975
- this._stopWatching$.next();
15985
+ this.stopListenChanges();
15976
15986
  return [2 /*return*/];
15977
15987
  }
15978
15988
  });
@@ -16132,62 +16142,10 @@
16132
16142
  });
16133
16143
  });
16134
16144
  };
16135
- AccountService.prototype.watch = function () {
16136
- var _this = this;
16137
- if (!this._cache.pubkey)
16138
- throw new Error('Not logged in');
16139
- if (!this.started) {
16140
- // Wait service ready, then loop
16141
- return rxjs.from(this.ready())
16142
- .pipe(operators.switchMap(function () { return _this.watch(); }));
16143
- }
16144
- this._stopWatching$.next(); // Stop previous listening
16145
- console.debug('[account] [WS] Watching changes');
16146
- return this.graphql.subscribe({
16147
- query: AccountSubscriptions.listenChanges,
16148
- variables: {
16149
- interval: 10
16150
- },
16151
- error: {
16152
- code: ErrorCodes$2.SUBSCRIBE_ACCOUNT_ERROR,
16153
- message: 'ERROR.ACCOUNT.SUBSCRIBE_ACCOUNT_ERROR'
16154
- }
16155
- })
16156
- .pipe(
16157
- // Stop this pipe next time we call watch()
16158
- operators.takeUntil(this._stopWatching$), operators.map(function (_b) {
16159
- var data = _b.data;
16160
- var _a;
16161
- if (!data)
16162
- return;
16163
- var existingUpdateDate = toDateISOString((_a = _this._data) === null || _a === void 0 ? void 0 : _a.updateDate);
16164
- if (existingUpdateDate === data.updateDate)
16165
- return;
16166
- console.debug("[account] [WS] Detected update on {" + data.updateDate + "}");
16167
- return exports.Account.fromObject(data);
16168
- }), operators.filter(isNotNil));
16169
- };
16170
- AccountService.prototype.listenChanges = function () {
16171
- var self = this;
16172
- var subscription = this.watch()
16173
- .subscribe({
16174
- next: function (data) { return self.refresh(); },
16175
- error: function (err) {
16176
- if (err && +err.code === ServerErrorCodes.NOT_FOUND) {
16177
- console.info('[account] Account not exists anymore: force user to logout...', err);
16178
- self.logout();
16179
- }
16180
- else if (err && +err.code === ServerErrorCodes.UNAUTHORIZED) {
16181
- console.info('[account] Account not authorized: force user to logout...', err);
16182
- self.logout();
16183
- }
16184
- else {
16185
- console.warn('[account] [WS] Received error:', err);
16186
- }
16187
- }
16188
- });
16189
- subscription.add(function () { return console.debug('[account] [WS] Stop listening changes'); });
16190
- return subscription;
16145
+ AccountService.prototype.listenChanges = function (opts) {
16146
+ if (this._enableListenChanges)
16147
+ return new rxjs.Subscription(); // Already started: skip
16148
+ return this.startListenChanges(opts);
16191
16149
  };
16192
16150
  Object.defineProperty(AccountService.prototype, "additionalFields", {
16193
16151
  get: function () {
@@ -16217,6 +16175,7 @@
16217
16175
  };
16218
16176
  /* -- protected method -- */
16219
16177
  AccountService.prototype.resetData = function () {
16178
+ this.stopListenChanges();
16220
16179
  this._cache.loaded = false;
16221
16180
  this._cache.keypair = null;
16222
16181
  this._cache.authToken = null;
@@ -16367,6 +16326,8 @@
16367
16326
  // Emit event
16368
16327
  this.onLogin.next(this._data);
16369
16328
  this.onChange.next(this._data);
16329
+ if (this._enableListenChanges)
16330
+ this.startListenChanges();
16370
16331
  if (this._debug)
16371
16332
  console.debug("[account] Account restoration [OK] {pubkey: " + pubkey.substr(0, 8) + "}, {profile: " + this._cache.mainProfile + "}");
16372
16333
  return [2 /*return*/, account];
@@ -16616,6 +16577,79 @@
16616
16577
  });
16617
16578
  });
16618
16579
  };
16580
+ AccountService.prototype.startListenChanges = function (opts) {
16581
+ var _this = this;
16582
+ if (this._listenChangesSubscription)
16583
+ this.stopListenChanges(); // Stop previous subscription
16584
+ if (this.network.offline)
16585
+ return new rxjs.Subscription(); // Offline: skip
16586
+ var subscription = this.watchChanges(opts)
16587
+ .subscribe({
16588
+ next: function (data) {
16589
+ console.debug("[account] Remote update detected at " + data.updateDate);
16590
+ _this.reload();
16591
+ },
16592
+ error: function (err) {
16593
+ if (err && +err.code === ServerErrorCodes.NOT_FOUND) {
16594
+ console.info('[account] [WS] Account not exists anymore: force user to logout...', err);
16595
+ _this.logout();
16596
+ }
16597
+ else if (err && +err.code === ServerErrorCodes.UNAUTHORIZED) {
16598
+ console.info('[account] [WS] Account not authorized: force user to logout...', err);
16599
+ _this.logout();
16600
+ }
16601
+ else {
16602
+ console.warn('[account] [WS] Received error:', err);
16603
+ }
16604
+ }
16605
+ });
16606
+ // Add close log
16607
+ subscription.add(function () { return console.debug("[account] Stop watching remote changes"); });
16608
+ this._listenChangesSubscription = subscription;
16609
+ // Make sure to use the stop function, when unsubscribe
16610
+ return new rxjs.Subscription(function () { return _this.stopListenChanges(); });
16611
+ };
16612
+ AccountService.prototype.stopListenChanges = function () {
16613
+ if (this._listenChangesSubscription) {
16614
+ this._listenChangesSubscription.unsubscribe();
16615
+ this._listenChangesSubscription = null;
16616
+ }
16617
+ };
16618
+ AccountService.prototype.watchChanges = function (opts) {
16619
+ var _this = this;
16620
+ var _a;
16621
+ if (!this.started) {
16622
+ // Wait service ready, then loop
16623
+ return rxjs.from(this.ready())
16624
+ .pipe(operators.switchMap(function () { return _this.watchChanges(opts); }));
16625
+ }
16626
+ if (!this._cache.pubkey)
16627
+ throw new Error('Not logged in');
16628
+ if (this.network.offline)
16629
+ throw new Error('Cannot watch account changes: network is offline.');
16630
+ var variables = {
16631
+ interval: Math.max(0, (opts === null || opts === void 0 ? void 0 : opts.intervalInSecond) || ((_a = this.environment.account) === null || _a === void 0 ? void 0 : _a.listenIntervalInSecond) || 0)
16632
+ };
16633
+ console.debug("[account] Watching remote changes, every " + variables.interval + "s");
16634
+ return this.graphql.subscribe({
16635
+ query: AccountSubscriptions.listenChanges,
16636
+ variables: variables,
16637
+ error: {
16638
+ code: ErrorCodes$2.SUBSCRIBE_ACCOUNT_ERROR,
16639
+ message: 'ERROR.ACCOUNT.SUBSCRIBE_ACCOUNT_ERROR'
16640
+ }
16641
+ }).pipe(operators.map(function (_b) {
16642
+ var data = _b.data;
16643
+ return data;
16644
+ }),
16645
+ // Keep only if newer
16646
+ operators.filter(function (data) {
16647
+ var _a;
16648
+ var currentUpdateDate = fromDateISOString((_a = _this._data) === null || _a === void 0 ? void 0 : _a.updateDate);
16649
+ var remoteUpdateDate = fromDateISOString(data === null || data === void 0 ? void 0 : data.updateDate);
16650
+ return (remoteUpdateDate && (!currentUpdateDate || !remoteUpdateDate.isSame(currentUpdateDate, 'millisecond')));
16651
+ }));
16652
+ };
16619
16653
  AccountService.prototype.copyIdAndUpdateDate = function (source, target) {
16620
16654
  if (!source)
16621
16655
  return;
@@ -20905,7 +20939,7 @@
20905
20939
  case 4:
20906
20940
  confirmed = _a.sent();
20907
20941
  if (!(confirmed && this.isLogin)) return [3 /*break*/, 6];
20908
- return [4 /*yield*/, this.accountService.refresh()];
20942
+ return [4 /*yield*/, this.accountService.reload()];
20909
20943
  case 5:
20910
20944
  _a.sent();
20911
20945
  _a.label = 6;
@@ -21157,7 +21191,7 @@
21157
21191
  return __generator(this, function (_b) {
21158
21192
  this.disable();
21159
21193
  this.markAsLoading();
21160
- return [2 /*return*/, this.accountService.refresh()];
21194
+ return [2 /*return*/, this.accountService.reload()];
21161
21195
  });
21162
21196
  });
21163
21197
  };
@@ -27160,7 +27194,9 @@
27160
27194
  var AppEntityEditor = /** @class */ (function (_super) {
27161
27195
  __extends(AppEntityEditor, _super);
27162
27196
  function AppEntityEditor(injector, dataType, dataService, options) {
27163
- var _this = _super.call(this, injector.get(i3.ActivatedRoute), injector.get(i3.Router), injector.get(i1$5.AlertController), injector.get(i1$2.TranslateService), options) || this;
27197
+ var _this = this;
27198
+ var _a;
27199
+ _this = _super.call(this, injector.get(i3.ActivatedRoute), injector.get(i3.Router), injector.get(i1$5.AlertController), injector.get(i1$2.TranslateService), options) || this;
27164
27200
  _this.dataType = dataType;
27165
27201
  _this.dataService = dataService;
27166
27202
  _this.saving = false;
@@ -27168,7 +27204,7 @@
27168
27204
  _this.onUpdateView = new i0.EventEmitter();
27169
27205
  options = Object.assign({
27170
27206
  // Default options
27171
- enableListenChanges: (injector.get(ENVIRONMENT).listenRemoteChanges === true), pathIdAttribute: 'id', autoLoad: true, autoUpdateRoute: true, i18nPrefix: ''
27207
+ enableListenChanges: (((_a = injector.get(ENVIRONMENT).entityEditor) === null || _a === void 0 ? void 0 : _a.enableListenChanges) === true), pathIdAttribute: 'id', autoLoad: true, autoUpdateRoute: true, i18nPrefix: ''
27172
27208
  }, options);
27173
27209
  _this.settings = injector.get(LocalSettingsService);
27174
27210
  _this.errorTranslator = injector.get(FormErrorTranslator);