@sumaris-net/ngx-components 1.9.3 → 1.9.6

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
 
@@ -14913,8 +14918,6 @@
14913
14918
  }
14914
14919
  var headers = new i2$1.HttpHeaders()
14915
14920
  .append('Authorization', authorization);
14916
- //.append('X-App-Name', environment.name)
14917
- //.append('X-App-Version', environment.version)
14918
14921
  // Use the setContext method to set the HTTP headers.
14919
14922
  operation.setContext(Object.assign(Object.assign({}, operation.getContext()), { headers: headers }));
14920
14923
  // Call the next link in the middleware chain.
@@ -15429,7 +15432,7 @@
15429
15432
  };
15430
15433
  var AccountService = /** @class */ (function (_super) {
15431
15434
  __extends(AccountService, _super);
15432
- function AccountService(cryptoService, network, graphql, settings, storage, file, environment) {
15435
+ function AccountService(cryptoService, network, graphql, settings, storage, file, translate, toastController, environment) {
15433
15436
  var _this = _super.call(this, graphql, environment) || this;
15434
15437
  _this.cryptoService = cryptoService;
15435
15438
  _this.network = network;
@@ -15437,13 +15440,15 @@
15437
15440
  _this.settings = settings;
15438
15441
  _this.storage = storage;
15439
15442
  _this.file = file;
15443
+ _this.translate = translate;
15444
+ _this.toastController = toastController;
15440
15445
  _this.environment = environment;
15441
15446
  _this.onLogin = new rxjs.Subject();
15442
15447
  _this.onLogout = new rxjs.Subject();
15443
15448
  _this.onChange = new rxjs.Subject();
15444
15449
  _this.onAuthTokenChange = new rxjs.Subject();
15445
15450
  _this.onAuthBasicChange = new rxjs.Subject();
15446
- _this._stopWatching$ = new rxjs.Subject();
15451
+ _this._listenChangesSubscription = null;
15447
15452
  _this._cache = {
15448
15453
  loaded: false,
15449
15454
  keypair: null,
@@ -15456,6 +15461,7 @@
15456
15461
  };
15457
15462
  _this._$additionalFields = new rxjs.BehaviorSubject([]);
15458
15463
  _this._tokenType$ = new rxjs.BehaviorSubject(undefined);
15464
+ _this._enableListenChanges = (!environment.account || environment.account.enableListenChanges !== false);
15459
15465
  _this._debug = !environment.production;
15460
15466
  if (_this._debug)
15461
15467
  console.debug('[account-service] Creating service');
@@ -15699,6 +15705,9 @@
15699
15705
  // Emit events
15700
15706
  this.onLogin.next(this._data);
15701
15707
  this.onChange.next(this._data);
15708
+ // Listen remote changes
15709
+ if (this._enableListenChanges)
15710
+ this.startListenChanges();
15702
15711
  return [2 /*return*/, this._data];
15703
15712
  case 6:
15704
15713
  error_1 = _b.sent();
@@ -15853,13 +15862,16 @@
15853
15862
  // Emit event to observers
15854
15863
  this.onLogin.next(this._data);
15855
15864
  this.onChange.next(this._data);
15865
+ if (this._enableListenChanges)
15866
+ this.startListenChanges();
15856
15867
  return [2 /*return*/, this._data];
15857
15868
  }
15858
15869
  });
15859
15870
  });
15860
15871
  };
15861
- AccountService.prototype.refresh = function () {
15872
+ AccountService.prototype.reload = function () {
15862
15873
  return __awaiter(this, void 0, void 0, function () {
15874
+ var now, wasLogin, error_6;
15863
15875
  return __generator(this, function (_b) {
15864
15876
  switch (_b.label) {
15865
15877
  case 0:
@@ -15867,17 +15879,34 @@
15867
15879
  throw new Error('User not logged');
15868
15880
  if (this.network.offline)
15869
15881
  throw new Error('Cannot check account in offline mode');
15870
- return [4 /*yield*/, this.loadData()];
15882
+ now = Date.now();
15883
+ console.debug("[account] Reloading account...");
15884
+ wasLogin = this.isLogin();
15885
+ _b.label = 1;
15871
15886
  case 1:
15887
+ _b.trys.push([1, 4, , 8]);
15888
+ return [4 /*yield*/, this.loadData()];
15889
+ case 2:
15872
15890
  _b.sent();
15873
15891
  return [4 /*yield*/, this.saveLocally()];
15874
- case 2:
15892
+ case 3:
15875
15893
  _b.sent();
15876
- console.debug('[account] Successfully reload account');
15894
+ console.debug("[account] Reloading account [OK] in " + (Date.now() - now) + "ms");
15877
15895
  // Emit login event to subscribers
15878
15896
  this.onLogin.next(this._data);
15879
15897
  this.onChange.next(this._data);
15880
15898
  return [2 /*return*/, this._data];
15899
+ case 4:
15900
+ error_6 = _b.sent();
15901
+ if (!(wasLogin && !this.isLogin())) return [3 /*break*/, 6];
15902
+ console.error("[account] Reloading account failed. Will force logout...", error_6);
15903
+ return [4 /*yield*/, this.logout()];
15904
+ case 5:
15905
+ _b.sent();
15906
+ return [3 /*break*/, 7];
15907
+ case 6: throw error_6;
15908
+ case 7: return [3 /*break*/, 8];
15909
+ case 8: return [2 /*return*/];
15881
15910
  }
15882
15911
  });
15883
15912
  });
@@ -15970,7 +15999,7 @@
15970
15999
  if (hadAuthBasic)
15971
16000
  this.onAuthBasicChange.next(undefined);
15972
16001
  this.onChange.next(undefined);
15973
- this._stopWatching$.next();
16002
+ this.stopListenChanges();
15974
16003
  return [2 /*return*/];
15975
16004
  }
15976
16005
  });
@@ -16130,62 +16159,10 @@
16130
16159
  });
16131
16160
  });
16132
16161
  };
16133
- AccountService.prototype.watch = function () {
16134
- var _this = this;
16135
- if (!this._cache.pubkey)
16136
- throw new Error('Not logged in');
16137
- if (!this.started) {
16138
- // Wait service ready, then loop
16139
- return rxjs.from(this.ready())
16140
- .pipe(operators.switchMap(function () { return _this.watch(); }));
16141
- }
16142
- this._stopWatching$.next(); // Stop previous listening
16143
- console.debug('[account] [WS] Watching changes');
16144
- return this.graphql.subscribe({
16145
- query: AccountSubscriptions.listenChanges,
16146
- variables: {
16147
- interval: 10
16148
- },
16149
- error: {
16150
- code: ErrorCodes$2.SUBSCRIBE_ACCOUNT_ERROR,
16151
- message: 'ERROR.ACCOUNT.SUBSCRIBE_ACCOUNT_ERROR'
16152
- }
16153
- })
16154
- .pipe(
16155
- // Stop this pipe next time we call watch()
16156
- operators.takeUntil(this._stopWatching$), operators.map(function (_b) {
16157
- var data = _b.data;
16158
- var _a;
16159
- if (!data)
16160
- return;
16161
- var existingUpdateDate = toDateISOString((_a = _this._data) === null || _a === void 0 ? void 0 : _a.updateDate);
16162
- if (existingUpdateDate === data.updateDate)
16163
- return;
16164
- console.debug("[account] [WS] Detected update on {" + data.updateDate + "}");
16165
- return exports.Account.fromObject(data);
16166
- }), operators.filter(isNotNil));
16167
- };
16168
- AccountService.prototype.listenChanges = function () {
16169
- var self = this;
16170
- var subscription = this.watch()
16171
- .subscribe({
16172
- next: function (data) { return self.refresh(); },
16173
- error: function (err) {
16174
- if (err && +err.code === ServerErrorCodes.NOT_FOUND) {
16175
- console.info('[account] Account not exists anymore: force user to logout...', err);
16176
- self.logout();
16177
- }
16178
- else if (err && +err.code === ServerErrorCodes.UNAUTHORIZED) {
16179
- console.info('[account] Account not authorized: force user to logout...', err);
16180
- self.logout();
16181
- }
16182
- else {
16183
- console.warn('[account] [WS] Received error:', err);
16184
- }
16185
- }
16186
- });
16187
- subscription.add(function () { return console.debug('[account] [WS] Stop listening changes'); });
16188
- return subscription;
16162
+ AccountService.prototype.listenChanges = function (opts) {
16163
+ if (this._enableListenChanges)
16164
+ return new rxjs.Subscription(); // Already started: skip
16165
+ return this.startListenChanges(opts);
16189
16166
  };
16190
16167
  Object.defineProperty(AccountService.prototype, "additionalFields", {
16191
16168
  get: function () {
@@ -16215,6 +16192,7 @@
16215
16192
  };
16216
16193
  /* -- protected method -- */
16217
16194
  AccountService.prototype.resetData = function () {
16195
+ this.stopListenChanges();
16218
16196
  this._cache.loaded = false;
16219
16197
  this._cache.keypair = null;
16220
16198
  this._cache.authToken = null;
@@ -16227,7 +16205,7 @@
16227
16205
  };
16228
16206
  AccountService.prototype.loadData = function (opts) {
16229
16207
  return __awaiter(this, void 0, void 0, function () {
16230
- var account, settings, error_6;
16208
+ var account, settings, error_7;
16231
16209
  return __generator(this, function (_b) {
16232
16210
  switch (_b.label) {
16233
16211
  case 0:
@@ -16264,11 +16242,11 @@
16264
16242
  _b.label = 4;
16265
16243
  case 4: return [2 /*return*/, this._data];
16266
16244
  case 5:
16267
- error_6 = _b.sent();
16245
+ error_7 = _b.sent();
16268
16246
  this.resetData();
16269
- if (error_6.code && error_6.message)
16270
- throw error_6;
16271
- console.error(error_6);
16247
+ if (error_7.code && error_7.message)
16248
+ throw error_7;
16249
+ console.error(error_7);
16272
16250
  throw {
16273
16251
  code: ErrorCodes$2.LOAD_ACCOUNT_ERROR,
16274
16252
  message: 'ERROR.LOAD_ACCOUNT_ERROR'
@@ -16292,7 +16270,7 @@
16292
16270
  };
16293
16271
  AccountService.prototype.restoreLocally = function () {
16294
16272
  return __awaiter(this, void 0, void 0, function () {
16295
- var values, pubkey, token, seckey, canRemoteAuth, error_7, jsonAccount, accountStr, account;
16273
+ var values, pubkey, token, seckey, canRemoteAuth, error_8, jsonAccount, accountStr, account;
16296
16274
  return __generator(this, function (_b) {
16297
16275
  switch (_b.label) {
16298
16276
  case 0: return [4 /*yield*/, Promise.all([
@@ -16330,7 +16308,7 @@
16330
16308
  throw new Error('Authentication failed');
16331
16309
  return [3 /*break*/, 5];
16332
16310
  case 4:
16333
- error_7 = _b.sent();
16311
+ error_8 = _b.sent();
16334
16312
  // Offline feature are enable: continue in offline mode
16335
16313
  if (this.settings.hasOfflineFeature()) {
16336
16314
  console.warn('[account] Unable to authenticate on pod: forcing offline mode');
@@ -16339,7 +16317,7 @@
16339
16317
  }
16340
16318
  // No offline features enable (=offline mode not allowed)
16341
16319
  else {
16342
- console.error(error_7);
16320
+ console.error(error_8);
16343
16321
  this.logout();
16344
16322
  return [2 /*return*/];
16345
16323
  }
@@ -16365,6 +16343,8 @@
16365
16343
  // Emit event
16366
16344
  this.onLogin.next(this._data);
16367
16345
  this.onChange.next(this._data);
16346
+ if (this._enableListenChanges)
16347
+ this.startListenChanges();
16368
16348
  if (this._debug)
16369
16349
  console.debug("[account] Account restoration [OK] {pubkey: " + pubkey.substr(0, 8) + "}, {profile: " + this._cache.mainProfile + "}");
16370
16350
  return [2 /*return*/, account];
@@ -16614,6 +16594,79 @@
16614
16594
  });
16615
16595
  });
16616
16596
  };
16597
+ AccountService.prototype.startListenChanges = function (opts) {
16598
+ var _this = this;
16599
+ if (this._listenChangesSubscription)
16600
+ this.stopListenChanges(); // Stop previous subscription
16601
+ if (this.network.offline)
16602
+ return new rxjs.Subscription(); // Offline: skip
16603
+ var subscription = this.watchChanges(opts)
16604
+ .subscribe({
16605
+ next: function (data) {
16606
+ console.debug("[account] Remote update detected at " + data.updateDate);
16607
+ _this.reload();
16608
+ },
16609
+ error: function (err) {
16610
+ if (err && +err.code === ServerErrorCodes.NOT_FOUND) {
16611
+ console.info('[account] [WS] Account not exists anymore: force user to logout...', err);
16612
+ _this.logout();
16613
+ }
16614
+ else if (err && +err.code === ServerErrorCodes.UNAUTHORIZED) {
16615
+ console.info('[account] [WS] Account not authorized: force user to logout...', err);
16616
+ _this.logout();
16617
+ }
16618
+ else {
16619
+ console.warn('[account] [WS] Received error:', err);
16620
+ }
16621
+ }
16622
+ });
16623
+ // Add close log
16624
+ subscription.add(function () { return console.debug("[account] Stop watching remote changes"); });
16625
+ this._listenChangesSubscription = subscription;
16626
+ // Make sure to use the stop function, when unsubscribe
16627
+ return new rxjs.Subscription(function () { return _this.stopListenChanges(); });
16628
+ };
16629
+ AccountService.prototype.stopListenChanges = function () {
16630
+ if (this._listenChangesSubscription) {
16631
+ this._listenChangesSubscription.unsubscribe();
16632
+ this._listenChangesSubscription = null;
16633
+ }
16634
+ };
16635
+ AccountService.prototype.watchChanges = function (opts) {
16636
+ var _this = this;
16637
+ var _a;
16638
+ if (!this.started) {
16639
+ // Wait service ready, then loop
16640
+ return rxjs.from(this.ready())
16641
+ .pipe(operators.switchMap(function () { return _this.watchChanges(opts); }));
16642
+ }
16643
+ if (!this._cache.pubkey)
16644
+ throw new Error('Not logged in');
16645
+ if (this.network.offline)
16646
+ throw new Error('Cannot watch account changes: network is offline.');
16647
+ var variables = {
16648
+ 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)
16649
+ };
16650
+ console.debug("[account] Watching remote changes, every " + variables.interval + "s");
16651
+ return this.graphql.subscribe({
16652
+ query: AccountSubscriptions.listenChanges,
16653
+ variables: variables,
16654
+ error: {
16655
+ code: ErrorCodes$2.SUBSCRIBE_ACCOUNT_ERROR,
16656
+ message: 'ERROR.ACCOUNT.SUBSCRIBE_ACCOUNT_ERROR'
16657
+ }
16658
+ }).pipe(operators.map(function (_b) {
16659
+ var data = _b.data;
16660
+ return data;
16661
+ }),
16662
+ // Keep only if newer
16663
+ operators.filter(function (data) {
16664
+ var _a;
16665
+ var currentUpdateDate = fromDateISOString((_a = _this._data) === null || _a === void 0 ? void 0 : _a.updateDate);
16666
+ var remoteUpdateDate = fromDateISOString(data === null || data === void 0 ? void 0 : data.updateDate);
16667
+ return (remoteUpdateDate && (!currentUpdateDate || !remoteUpdateDate.isSame(currentUpdateDate, 'millisecond')));
16668
+ }));
16669
+ };
16617
16670
  AccountService.prototype.copyIdAndUpdateDate = function (source, target) {
16618
16671
  if (!source)
16619
16672
  return;
@@ -16627,9 +16680,12 @@
16627
16680
  target.id = source.id || target.id;
16628
16681
  target.updateDate = source.updateDate || target.updateDate;
16629
16682
  };
16683
+ AccountService.prototype.showToast = function (opts) {
16684
+ return Toasts.show(this.toastController, this.translate, opts);
16685
+ };
16630
16686
  return AccountService;
16631
16687
  }(BaseGraphqlService));
16632
- 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" });
16688
+ 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" });
16633
16689
  AccountService.decorators = [
16634
16690
  { type: i0.Injectable, args: [{ providedIn: 'root' },] }
16635
16691
  ];
@@ -16640,6 +16696,8 @@
16640
16696
  { type: LocalSettingsService },
16641
16697
  { type: i3$2.Storage },
16642
16698
  { type: FileService },
16699
+ { type: i1$2.TranslateService, decorators: [{ type: i0.Optional }] },
16700
+ { type: i1$5.ToastController, decorators: [{ type: i0.Optional }] },
16643
16701
  { type: undefined, decorators: [{ type: i0.Inject, args: [ENVIRONMENT,] }] }
16644
16702
  ]; };
16645
16703
  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;
@@ -20903,7 +20961,7 @@
20903
20961
  case 4:
20904
20962
  confirmed = _a.sent();
20905
20963
  if (!(confirmed && this.isLogin)) return [3 /*break*/, 6];
20906
- return [4 /*yield*/, this.accountService.refresh()];
20964
+ return [4 /*yield*/, this.accountService.reload()];
20907
20965
  case 5:
20908
20966
  _a.sent();
20909
20967
  _a.label = 6;
@@ -21155,7 +21213,7 @@
21155
21213
  return __generator(this, function (_b) {
21156
21214
  this.disable();
21157
21215
  this.markAsLoading();
21158
- return [2 /*return*/, this.accountService.refresh()];
21216
+ return [2 /*return*/, this.accountService.reload()];
21159
21217
  });
21160
21218
  });
21161
21219
  };
@@ -27158,7 +27216,9 @@
27158
27216
  var AppEntityEditor = /** @class */ (function (_super) {
27159
27217
  __extends(AppEntityEditor, _super);
27160
27218
  function AppEntityEditor(injector, dataType, dataService, options) {
27161
- 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;
27219
+ var _this = this;
27220
+ var _a;
27221
+ _this = _super.call(this, injector.get(i3.ActivatedRoute), injector.get(i3.Router), injector.get(i1$5.AlertController), injector.get(i1$2.TranslateService), options) || this;
27162
27222
  _this.dataType = dataType;
27163
27223
  _this.dataService = dataService;
27164
27224
  _this.saving = false;
@@ -27166,7 +27226,7 @@
27166
27226
  _this.onUpdateView = new i0.EventEmitter();
27167
27227
  options = Object.assign({
27168
27228
  // Default options
27169
- enableListenChanges: (injector.get(ENVIRONMENT).listenRemoteChanges === true), pathIdAttribute: 'id', autoLoad: true, autoUpdateRoute: true, i18nPrefix: ''
27229
+ enableListenChanges: (((_a = injector.get(ENVIRONMENT).entityEditor) === null || _a === void 0 ? void 0 : _a.enableListenChanges) === true), pathIdAttribute: 'id', autoLoad: true, autoUpdateRoute: true, i18nPrefix: ''
27170
27230
  }, options);
27171
27231
  _this.settings = injector.get(LocalSettingsService);
27172
27232
  _this.errorTranslator = injector.get(FormErrorTranslator);