@sumaris-net/ngx-components 1.9.4 → 1.9.7

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,14 @@
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
+ listenIntervalInSeconds: 30
10673
+ },
10674
+ entityEditor: {
10675
+ enableListenChanges: true,
10676
+ listenIntervalInSeconds: 30
10671
10677
  }
10672
10678
  });
10673
10679
 
@@ -14385,9 +14391,7 @@
14385
14391
  _this.cryptoService = cryptoService;
14386
14392
  _this.environment = environment;
14387
14393
  _this.typePolicies = typePolicies;
14388
- _this.connectionParams = {
14389
- reconnect: true
14390
- };
14394
+ _this.connectionParams = {};
14391
14395
  _this.onNetworkError = new rxjs.Subject();
14392
14396
  _this.customErrors = {};
14393
14397
  _this._debug = !environment.production;
@@ -14915,8 +14919,6 @@
14915
14919
  }
14916
14920
  var headers = new i2$1.HttpHeaders()
14917
14921
  .append('Authorization', authorization);
14918
- //.append('X-App-Name', environment.name)
14919
- //.append('X-App-Version', environment.version)
14920
14922
  // Use the setContext method to set the HTTP headers.
14921
14923
  operation.setContext(Object.assign(Object.assign({}, operation.getContext()), { headers: headers }));
14922
14924
  // Call the next link in the middleware chain.
@@ -15431,7 +15433,7 @@
15431
15433
  };
15432
15434
  var AccountService = /** @class */ (function (_super) {
15433
15435
  __extends(AccountService, _super);
15434
- function AccountService(cryptoService, network, graphql, settings, storage, file, environment) {
15436
+ function AccountService(cryptoService, network, graphql, settings, storage, file, translate, toastController, environment) {
15435
15437
  var _this = _super.call(this, graphql, environment) || this;
15436
15438
  _this.cryptoService = cryptoService;
15437
15439
  _this.network = network;
@@ -15439,13 +15441,15 @@
15439
15441
  _this.settings = settings;
15440
15442
  _this.storage = storage;
15441
15443
  _this.file = file;
15444
+ _this.translate = translate;
15445
+ _this.toastController = toastController;
15442
15446
  _this.environment = environment;
15443
15447
  _this.onLogin = new rxjs.Subject();
15444
15448
  _this.onLogout = new rxjs.Subject();
15445
15449
  _this.onChange = new rxjs.Subject();
15446
15450
  _this.onAuthTokenChange = new rxjs.Subject();
15447
15451
  _this.onAuthBasicChange = new rxjs.Subject();
15448
- _this._stopWatching$ = new rxjs.Subject();
15452
+ _this._listenChangesSubscription = null;
15449
15453
  _this._cache = {
15450
15454
  loaded: false,
15451
15455
  keypair: null,
@@ -15458,6 +15462,7 @@
15458
15462
  };
15459
15463
  _this._$additionalFields = new rxjs.BehaviorSubject([]);
15460
15464
  _this._tokenType$ = new rxjs.BehaviorSubject(undefined);
15465
+ _this._enableListenChanges = (!environment.account || environment.account.enableListenChanges !== false);
15461
15466
  _this._debug = !environment.production;
15462
15467
  if (_this._debug)
15463
15468
  console.debug('[account-service] Creating service');
@@ -15701,6 +15706,9 @@
15701
15706
  // Emit events
15702
15707
  this.onLogin.next(this._data);
15703
15708
  this.onChange.next(this._data);
15709
+ // Listen remote changes
15710
+ if (this._enableListenChanges)
15711
+ this.startListenChanges();
15704
15712
  return [2 /*return*/, this._data];
15705
15713
  case 6:
15706
15714
  error_1 = _b.sent();
@@ -15855,13 +15863,16 @@
15855
15863
  // Emit event to observers
15856
15864
  this.onLogin.next(this._data);
15857
15865
  this.onChange.next(this._data);
15866
+ if (this._enableListenChanges)
15867
+ this.startListenChanges();
15858
15868
  return [2 /*return*/, this._data];
15859
15869
  }
15860
15870
  });
15861
15871
  });
15862
15872
  };
15863
- AccountService.prototype.refresh = function () {
15873
+ AccountService.prototype.reload = function () {
15864
15874
  return __awaiter(this, void 0, void 0, function () {
15875
+ var now, wasLogin, error_6;
15865
15876
  return __generator(this, function (_b) {
15866
15877
  switch (_b.label) {
15867
15878
  case 0:
@@ -15869,17 +15880,34 @@
15869
15880
  throw new Error('User not logged');
15870
15881
  if (this.network.offline)
15871
15882
  throw new Error('Cannot check account in offline mode');
15872
- return [4 /*yield*/, this.loadData()];
15883
+ now = Date.now();
15884
+ console.debug("[account] Reloading account...");
15885
+ wasLogin = this.isLogin();
15886
+ _b.label = 1;
15873
15887
  case 1:
15888
+ _b.trys.push([1, 4, , 8]);
15889
+ return [4 /*yield*/, this.loadData()];
15890
+ case 2:
15874
15891
  _b.sent();
15875
15892
  return [4 /*yield*/, this.saveLocally()];
15876
- case 2:
15893
+ case 3:
15877
15894
  _b.sent();
15878
- console.debug('[account] Successfully reload account');
15895
+ console.debug("[account] Reloading account [OK] in " + (Date.now() - now) + "ms");
15879
15896
  // Emit login event to subscribers
15880
15897
  this.onLogin.next(this._data);
15881
15898
  this.onChange.next(this._data);
15882
15899
  return [2 /*return*/, this._data];
15900
+ case 4:
15901
+ error_6 = _b.sent();
15902
+ if (!(wasLogin && !this.isLogin())) return [3 /*break*/, 6];
15903
+ console.error("[account] Reloading account failed. Will force logout...", error_6);
15904
+ return [4 /*yield*/, this.logout()];
15905
+ case 5:
15906
+ _b.sent();
15907
+ return [3 /*break*/, 7];
15908
+ case 6: throw error_6;
15909
+ case 7: return [3 /*break*/, 8];
15910
+ case 8: return [2 /*return*/];
15883
15911
  }
15884
15912
  });
15885
15913
  });
@@ -15972,7 +16000,7 @@
15972
16000
  if (hadAuthBasic)
15973
16001
  this.onAuthBasicChange.next(undefined);
15974
16002
  this.onChange.next(undefined);
15975
- this._stopWatching$.next();
16003
+ this.stopListenChanges();
15976
16004
  return [2 /*return*/];
15977
16005
  }
15978
16006
  });
@@ -16132,62 +16160,10 @@
16132
16160
  });
16133
16161
  });
16134
16162
  };
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;
16163
+ AccountService.prototype.listenChanges = function (opts) {
16164
+ if (this._enableListenChanges)
16165
+ return new rxjs.Subscription(); // Already started: skip
16166
+ return this.startListenChanges(opts);
16191
16167
  };
16192
16168
  Object.defineProperty(AccountService.prototype, "additionalFields", {
16193
16169
  get: function () {
@@ -16217,6 +16193,7 @@
16217
16193
  };
16218
16194
  /* -- protected method -- */
16219
16195
  AccountService.prototype.resetData = function () {
16196
+ this.stopListenChanges();
16220
16197
  this._cache.loaded = false;
16221
16198
  this._cache.keypair = null;
16222
16199
  this._cache.authToken = null;
@@ -16229,7 +16206,7 @@
16229
16206
  };
16230
16207
  AccountService.prototype.loadData = function (opts) {
16231
16208
  return __awaiter(this, void 0, void 0, function () {
16232
- var account, settings, error_6;
16209
+ var account, settings, error_7;
16233
16210
  return __generator(this, function (_b) {
16234
16211
  switch (_b.label) {
16235
16212
  case 0:
@@ -16266,11 +16243,11 @@
16266
16243
  _b.label = 4;
16267
16244
  case 4: return [2 /*return*/, this._data];
16268
16245
  case 5:
16269
- error_6 = _b.sent();
16246
+ error_7 = _b.sent();
16270
16247
  this.resetData();
16271
- if (error_6.code && error_6.message)
16272
- throw error_6;
16273
- console.error(error_6);
16248
+ if (error_7.code && error_7.message)
16249
+ throw error_7;
16250
+ console.error(error_7);
16274
16251
  throw {
16275
16252
  code: ErrorCodes$2.LOAD_ACCOUNT_ERROR,
16276
16253
  message: 'ERROR.LOAD_ACCOUNT_ERROR'
@@ -16294,7 +16271,7 @@
16294
16271
  };
16295
16272
  AccountService.prototype.restoreLocally = function () {
16296
16273
  return __awaiter(this, void 0, void 0, function () {
16297
- var values, pubkey, token, seckey, canRemoteAuth, error_7, jsonAccount, accountStr, account;
16274
+ var values, pubkey, token, seckey, canRemoteAuth, error_8, jsonAccount, accountStr, account;
16298
16275
  return __generator(this, function (_b) {
16299
16276
  switch (_b.label) {
16300
16277
  case 0: return [4 /*yield*/, Promise.all([
@@ -16332,7 +16309,7 @@
16332
16309
  throw new Error('Authentication failed');
16333
16310
  return [3 /*break*/, 5];
16334
16311
  case 4:
16335
- error_7 = _b.sent();
16312
+ error_8 = _b.sent();
16336
16313
  // Offline feature are enable: continue in offline mode
16337
16314
  if (this.settings.hasOfflineFeature()) {
16338
16315
  console.warn('[account] Unable to authenticate on pod: forcing offline mode');
@@ -16341,7 +16318,7 @@
16341
16318
  }
16342
16319
  // No offline features enable (=offline mode not allowed)
16343
16320
  else {
16344
- console.error(error_7);
16321
+ console.error(error_8);
16345
16322
  this.logout();
16346
16323
  return [2 /*return*/];
16347
16324
  }
@@ -16367,6 +16344,8 @@
16367
16344
  // Emit event
16368
16345
  this.onLogin.next(this._data);
16369
16346
  this.onChange.next(this._data);
16347
+ if (this._enableListenChanges)
16348
+ this.startListenChanges();
16370
16349
  if (this._debug)
16371
16350
  console.debug("[account] Account restoration [OK] {pubkey: " + pubkey.substr(0, 8) + "}, {profile: " + this._cache.mainProfile + "}");
16372
16351
  return [2 /*return*/, account];
@@ -16616,6 +16595,79 @@
16616
16595
  });
16617
16596
  });
16618
16597
  };
16598
+ AccountService.prototype.startListenChanges = function (opts) {
16599
+ var _this = this;
16600
+ if (this._listenChangesSubscription)
16601
+ this.stopListenChanges(); // Stop previous subscription
16602
+ if (this.network.offline)
16603
+ return new rxjs.Subscription(); // Offline: skip
16604
+ var subscription = this.watchChanges(opts)
16605
+ .subscribe({
16606
+ next: function (data) {
16607
+ console.debug("[account] Remote update detected at " + data.updateDate);
16608
+ _this.reload();
16609
+ },
16610
+ error: function (err) {
16611
+ if (err && +err.code === ServerErrorCodes.NOT_FOUND) {
16612
+ console.info('[account] [WS] Account not exists anymore: force user to logout...', err);
16613
+ _this.logout();
16614
+ }
16615
+ else if (err && +err.code === ServerErrorCodes.UNAUTHORIZED) {
16616
+ console.info('[account] [WS] Account not authorized: force user to logout...', err);
16617
+ _this.logout();
16618
+ }
16619
+ else {
16620
+ console.warn('[account] [WS] Received error:', err);
16621
+ }
16622
+ }
16623
+ });
16624
+ // Add close log
16625
+ subscription.add(function () { return console.debug("[account] Stop watching remote changes"); });
16626
+ this._listenChangesSubscription = subscription;
16627
+ // Make sure to use the stop function, when unsubscribe
16628
+ return new rxjs.Subscription(function () { return _this.stopListenChanges(); });
16629
+ };
16630
+ AccountService.prototype.stopListenChanges = function () {
16631
+ if (this._listenChangesSubscription) {
16632
+ this._listenChangesSubscription.unsubscribe();
16633
+ this._listenChangesSubscription = null;
16634
+ }
16635
+ };
16636
+ AccountService.prototype.watchChanges = function (opts) {
16637
+ var _this = this;
16638
+ var _a;
16639
+ if (!this.started) {
16640
+ // Wait service ready, then loop
16641
+ return rxjs.from(this.ready())
16642
+ .pipe(operators.switchMap(function () { return _this.watchChanges(opts); }));
16643
+ }
16644
+ if (!this._cache.pubkey)
16645
+ throw new Error('Not logged in');
16646
+ if (this.network.offline)
16647
+ throw new Error('Cannot watch account changes: network is offline.');
16648
+ var variables = {
16649
+ interval: Math.max(0, (opts === null || opts === void 0 ? void 0 : opts.intervalInSeconds) || ((_a = this.environment.account) === null || _a === void 0 ? void 0 : _a.listenIntervalInSeconds) || 0)
16650
+ };
16651
+ console.debug("[account] Watching remote changes, every " + variables.interval + "s");
16652
+ return this.graphql.subscribe({
16653
+ query: AccountSubscriptions.listenChanges,
16654
+ variables: variables,
16655
+ error: {
16656
+ code: ErrorCodes$2.SUBSCRIBE_ACCOUNT_ERROR,
16657
+ message: 'ERROR.ACCOUNT.SUBSCRIBE_ACCOUNT_ERROR'
16658
+ }
16659
+ }).pipe(operators.map(function (_b) {
16660
+ var data = _b.data;
16661
+ return data;
16662
+ }),
16663
+ // Keep only if newer
16664
+ operators.filter(function (data) {
16665
+ var _a;
16666
+ var currentUpdateDate = fromDateISOString((_a = _this._data) === null || _a === void 0 ? void 0 : _a.updateDate);
16667
+ var remoteUpdateDate = fromDateISOString(data === null || data === void 0 ? void 0 : data.updateDate);
16668
+ return (remoteUpdateDate && (!currentUpdateDate || !remoteUpdateDate.isSame(currentUpdateDate, 'millisecond')));
16669
+ }));
16670
+ };
16619
16671
  AccountService.prototype.copyIdAndUpdateDate = function (source, target) {
16620
16672
  if (!source)
16621
16673
  return;
@@ -16629,9 +16681,12 @@
16629
16681
  target.id = source.id || target.id;
16630
16682
  target.updateDate = source.updateDate || target.updateDate;
16631
16683
  };
16684
+ AccountService.prototype.showToast = function (opts) {
16685
+ return Toasts.show(this.toastController, this.translate, opts);
16686
+ };
16632
16687
  return AccountService;
16633
16688
  }(BaseGraphqlService));
16634
- AccountService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function AccountService_Factory() { return new AccountService(i0__namespace.ɵɵinject(CryptoService), i0__namespace.ɵɵinject(NetworkService), i0__namespace.ɵɵinject(GraphqlService), i0__namespace.ɵɵinject(LocalSettingsService), i0__namespace.ɵɵinject(i3__namespace$1.Storage), i0__namespace.ɵɵinject(FileService), i0__namespace.ɵɵinject(ENVIRONMENT)); }, token: AccountService, providedIn: "root" });
16689
+ AccountService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function AccountService_Factory() { return new AccountService(i0__namespace.ɵɵinject(CryptoService), i0__namespace.ɵɵinject(NetworkService), i0__namespace.ɵɵinject(GraphqlService), i0__namespace.ɵɵinject(LocalSettingsService), i0__namespace.ɵɵinject(i3__namespace$1.Storage), i0__namespace.ɵɵinject(FileService), i0__namespace.ɵɵinject(i1__namespace$1.TranslateService, 8), i0__namespace.ɵɵinject(i1__namespace$4.ToastController, 8), i0__namespace.ɵɵinject(ENVIRONMENT)); }, token: AccountService, providedIn: "root" });
16635
16690
  AccountService.decorators = [
16636
16691
  { type: i0.Injectable, args: [{ providedIn: 'root' },] }
16637
16692
  ];
@@ -16642,6 +16697,8 @@
16642
16697
  { type: LocalSettingsService },
16643
16698
  { type: i3$2.Storage },
16644
16699
  { type: FileService },
16700
+ { type: i1$2.TranslateService, decorators: [{ type: i0.Optional }] },
16701
+ { type: i1$5.ToastController, decorators: [{ type: i0.Optional }] },
16645
16702
  { type: undefined, decorators: [{ type: i0.Inject, args: [ENVIRONMENT,] }] }
16646
16703
  ]; };
16647
16704
  var templateObject_1$4, templateObject_2$4, templateObject_3$3, templateObject_4$3, templateObject_5$2, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12;
@@ -20905,7 +20962,7 @@
20905
20962
  case 4:
20906
20963
  confirmed = _a.sent();
20907
20964
  if (!(confirmed && this.isLogin)) return [3 /*break*/, 6];
20908
- return [4 /*yield*/, this.accountService.refresh()];
20965
+ return [4 /*yield*/, this.accountService.reload()];
20909
20966
  case 5:
20910
20967
  _a.sent();
20911
20968
  _a.label = 6;
@@ -21157,7 +21214,7 @@
21157
21214
  return __generator(this, function (_b) {
21158
21215
  this.disable();
21159
21216
  this.markAsLoading();
21160
- return [2 /*return*/, this.accountService.refresh()];
21217
+ return [2 /*return*/, this.accountService.reload()];
21161
21218
  });
21162
21219
  });
21163
21220
  };
@@ -27160,7 +27217,9 @@
27160
27217
  var AppEntityEditor = /** @class */ (function (_super) {
27161
27218
  __extends(AppEntityEditor, _super);
27162
27219
  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;
27220
+ var _this = this;
27221
+ var _a;
27222
+ _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
27223
  _this.dataType = dataType;
27165
27224
  _this.dataService = dataService;
27166
27225
  _this.saving = false;
@@ -27168,7 +27227,7 @@
27168
27227
  _this.onUpdateView = new i0.EventEmitter();
27169
27228
  options = Object.assign({
27170
27229
  // Default options
27171
- enableListenChanges: (injector.get(ENVIRONMENT).listenRemoteChanges === true), pathIdAttribute: 'id', autoLoad: true, autoUpdateRoute: true, i18nPrefix: ''
27230
+ enableListenChanges: (((_a = injector.get(ENVIRONMENT).entityEditor) === null || _a === void 0 ? void 0 : _a.enableListenChanges) === true), pathIdAttribute: 'id', autoLoad: true, autoUpdateRoute: true, i18nPrefix: ''
27172
27231
  }, options);
27173
27232
  _this.settings = injector.get(LocalSettingsService);
27174
27233
  _this.errorTranslator = injector.get(FormErrorTranslator);