@sumaris-net/ngx-components 1.12.13 → 1.13.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.
@@ -1187,6 +1187,16 @@
1187
1187
  });
1188
1188
  });
1189
1189
  }
1190
+ function fromPromise(promise) {
1191
+ var $observable = new rxjs.Subject();
1192
+ promise
1193
+ .then(function (errors) {
1194
+ $observable.next(errors);
1195
+ $observable.complete();
1196
+ })
1197
+ .catch(function (err) { return $observable.error(err); });
1198
+ return $observable;
1199
+ }
1190
1200
 
1191
1201
  function createPromiseEventEmitter() {
1192
1202
  return new i0.EventEmitter(true);
@@ -3585,6 +3595,107 @@
3585
3595
  };
3586
3596
  return SharedFormArrayValidators;
3587
3597
  }());
3598
+ // @dynamic
3599
+ var SharedAsyncValidators = /** @class */ (function () {
3600
+ function SharedAsyncValidators() {
3601
+ }
3602
+ /**
3603
+ * Create an observable validator function. Will execute the validator, then cnvert the result into an observable
3604
+ * @param validatorFn any validator
3605
+ */
3606
+ SharedAsyncValidators.of = function (validatorFn) {
3607
+ return function (control) {
3608
+ var res = validatorFn(control);
3609
+ if (rxjs.isObservable(res)) {
3610
+ return res;
3611
+ } // Already an observable
3612
+ if (res instanceof Promise)
3613
+ return fromPromise(res);
3614
+ return rxjs.of(res);
3615
+ };
3616
+ };
3617
+ /**
3618
+ * Add a debounce time to a validator.
3619
+ * @param form
3620
+ * @param validatorFn
3621
+ * @param opts Use opts.stopSubject to stop the validator, before end
3622
+ */
3623
+ SharedAsyncValidators.debounceTime = function (validatorFn, opts) {
3624
+ // DEBUG only
3625
+ var debug = (opts === null || opts === void 0 ? void 0 : opts.debug) || false;
3626
+ var logPrefix = debug && "[debounceTime-validator] #" + SharedAsyncValidators.DEBOUNCE_TIME_VALIDATOR_ID++ + " - ";
3627
+ var debounceTime = toNumber(opts === null || opts === void 0 ? void 0 : opts.debounceTime, 250);
3628
+ // DEBUG
3629
+ if (debug)
3630
+ console.debug(logPrefix + ("New validator with a debounceTime at " + debounceTime + "ms"));
3631
+ var $disposeSubject = new rxjs.Subject();
3632
+ var disposeEvent$ = (opts === null || opts === void 0 ? void 0 : opts.dispose) ? rxjs.merge($disposeSubject, opts === null || opts === void 0 ? void 0 : opts.dispose)
3633
+ : $disposeSubject;
3634
+ // Make sure validator will return an Observable - This is need by the switchMap()
3635
+ var asyncValidatorFn = SharedAsyncValidators.of(validatorFn);
3636
+ // DEBUG
3637
+ if (debug && (opts === null || opts === void 0 ? void 0 : opts.dispose)) {
3638
+ opts === null || opts === void 0 ? void 0 : opts.dispose.pipe(operators.first()).subscribe(function () {
3639
+ console.debug(logPrefix + 'Stopping');
3640
+ });
3641
+ }
3642
+ return function (control) {
3643
+ if (debug)
3644
+ console.debug(logPrefix + 'Form ask validation...');
3645
+ // Stop previous observables
3646
+ $disposeSubject.next();
3647
+ var now;
3648
+ // Add a delay before execution
3649
+ return rxjs.timer(debounceTime)
3650
+ .pipe(
3651
+ // DEBUG
3652
+ operators.tap(function (_) {
3653
+ if (debug) {
3654
+ console.debug(logPrefix + 'Executing...');
3655
+ now = Date.now();
3656
+ }
3657
+ }), operators.switchMap(function (_) { return asyncValidatorFn(control); }),
3658
+ // DEBUG
3659
+ operators.tap(function (res) { return debug && console.debug(logPrefix + ("Finished in " + (Date.now() - now) + "ms (" + (res ? 'with errors' : 'no error') + ")"), res); }), operators.catchError(function (error) {
3660
+ console.error('[debounceTime-validator] Error while executing validator. Stopping job', error);
3661
+ $disposeSubject.next();
3662
+ throw error;
3663
+ }),
3664
+ // Refresh UI, after a successful execution
3665
+ operators.tap(function () { return (opts === null || opts === void 0 ? void 0 : opts.markForCheck) && (opts === null || opts === void 0 ? void 0 : opts.markForCheck()); }),
3666
+ // Make sure to stop, because of the switchMap
3667
+ operators.takeUntil(disposeEvent$));
3668
+ };
3669
+ };
3670
+ /**
3671
+ * Add an async validator, that can be disposed by the returned subscription
3672
+ * @param form
3673
+ * @param validatorFn the validator or job to execute
3674
+ * @param opts
3675
+ */
3676
+ SharedAsyncValidators.registerAsyncValidator = function (form, validatorFn, opts) {
3677
+ if (form.asyncValidator)
3678
+ throw Error('Form already have an async validator. Cannot configure job');
3679
+ var debounceTime = toNumber(opts === null || opts === void 0 ? void 0 : opts.debounceTime, 250);
3680
+ var $dispose = new rxjs.Subject();
3681
+ var asyncValidatorFn = SharedAsyncValidators.debounceTime(validatorFn, Object.assign(Object.assign({}, opts), { debounceTime: debounceTime, dispose: $dispose }));
3682
+ form.setAsyncValidators(asyncValidatorFn);
3683
+ var subscription = new rxjs.Subscription();
3684
+ // When unsubscribing, remove async validator
3685
+ subscription.add(function () {
3686
+ // Clear added validator. Must be done NOW (without delay) because generally a new Job is given just after
3687
+ form.clearAsyncValidators();
3688
+ // Stop the job, with a delay to let the last execution finished
3689
+ setTimeout(function () {
3690
+ $dispose.next();
3691
+ $dispose.unsubscribe();
3692
+ }, debounceTime);
3693
+ });
3694
+ return subscription;
3695
+ };
3696
+ return SharedAsyncValidators;
3697
+ }());
3698
+ SharedAsyncValidators.DEBOUNCE_TIME_VALIDATOR_ID = 0;
3588
3699
 
3589
3700
  /**
3590
3701
  * Fill a form using a source entity
@@ -30907,6 +31018,7 @@
30907
31018
  exports.SPACE_PLACEHOLDER_CHAR = SPACE_PLACEHOLDER_CHAR;
30908
31019
  exports.SelectPeerModal = SelectPeerModal;
30909
31020
  exports.SettingsPage = SettingsPage;
31021
+ exports.SharedAsyncValidators = SharedAsyncValidators;
30910
31022
  exports.SharedDirectivesModule = SharedDirectivesModule;
30911
31023
  exports.SharedFormArrayValidators = SharedFormArrayValidators;
30912
31024
  exports.SharedFormGroupValidators = SharedFormGroupValidators;
@@ -30999,6 +31111,7 @@
30999
31111
  exports.formatLatitude = formatLatitude;
31000
31112
  exports.formatLongitude = formatLongitude;
31001
31113
  exports.fromDateISOString = fromDateISOString;
31114
+ exports.fromPromise = fromPromise;
31002
31115
  exports.fromScrollEndEvent = fromScrollEndEvent;
31003
31116
  exports.fromUnixMsTimestamp = fromUnixMsTimestamp;
31004
31117
  exports.fromUnixTimestamp = fromUnixTimestamp;