@regle/core 0.0.8 → 0.0.9

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.
package/dist/index.cjs CHANGED
@@ -297,6 +297,35 @@ function isEmpty(value) {
297
297
  // src/utils/composables.ts
298
298
  var import_vue3 = require("vue");
299
299
 
300
+ // src/utils/debounce.ts
301
+ function debounce(func, wait, immediate) {
302
+ let timeout;
303
+ const debouncedFn = (...args) => new Promise((resolve) => {
304
+ clearTimeout(timeout);
305
+ timeout = setTimeout(() => {
306
+ timeout = void 0;
307
+ if (!immediate) {
308
+ void Promise.resolve(func.apply(this, [...args])).then(resolve);
309
+ }
310
+ }, wait);
311
+ if (immediate && !timeout) {
312
+ void Promise.resolve(func.apply(this, [...args])).then(resolve);
313
+ }
314
+ });
315
+ debouncedFn.cancel = () => {
316
+ clearTimeout(timeout);
317
+ timeout = void 0;
318
+ };
319
+ debouncedFn.doImmediately = (...args) => new Promise((resolve) => {
320
+ clearTimeout(timeout);
321
+ timeout = setTimeout(() => {
322
+ timeout = void 0;
323
+ void Promise.resolve(func.apply(this, [...args])).then(resolve);
324
+ }, 0);
325
+ });
326
+ return debouncedFn;
327
+ }
328
+
300
329
  // src/core/useRegle/guards/ruleDef.guards.ts
301
330
  function isNestedRulesDef(state, rule) {
302
331
  return isObject(state.value) && isObject(rule.value) && !Object.entries(rule.value).some((rule2) => isRuleDef(rule2));
@@ -602,8 +631,11 @@ function createReactiveFieldStatus({
602
631
  function createReactiveRulesResult() {
603
632
  const declaredRules = rulesDef.value;
604
633
  const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
634
+ $localOptions.value = Object.fromEntries(
635
+ Object.entries(declaredRules).filter(([ruleKey]) => ruleKey.startsWith("$"))
636
+ );
605
637
  $rules.value = Object.fromEntries(
606
- Object.entries(declaredRules).map(([ruleKey, rule]) => {
638
+ Object.entries(declaredRules).filter(([ruleKey]) => !ruleKey.startsWith("$")).map(([ruleKey, rule]) => {
607
639
  if (rule) {
608
640
  const ruleRef = (0, import_vue6.toRef)(() => rule);
609
641
  return [
@@ -636,15 +668,17 @@ function createReactiveFieldStatus({
636
668
  storage.setDirtyEntry(path, $dirty.value);
637
669
  });
638
670
  const $unwatchState = (0, import_vue6.watch)(state, () => {
639
- if ((0, import_vue6.unref)(options.autoDirty)) {
671
+ if (scopeState.$autoDirty.value) {
640
672
  if (!$dirty.value) {
641
673
  $dirty.value = true;
642
674
  }
643
675
  }
644
- if (!(0, import_vue6.unref)(options.lazy)) {
676
+ if (!scopeState.$lazy.value) {
645
677
  $commit();
678
+ if (!scopeState.$rewardEarly.value !== false) {
679
+ $clearExternalErrors();
680
+ }
646
681
  }
647
- $externalErrors.value = [];
648
682
  });
649
683
  function $unwatch() {
650
684
  if ($rules.value) {
@@ -664,11 +698,32 @@ function createReactiveFieldStatus({
664
698
  }
665
699
  function $watch() {
666
700
  scopeState = scope.run(() => {
701
+ const $debounce = (0, import_vue6.computed)(() => {
702
+ return $localOptions.value.$debounce;
703
+ });
704
+ const $lazy = (0, import_vue6.computed)(() => {
705
+ if ($localOptions.value.$lazy) {
706
+ return $localOptions.value.$lazy;
707
+ }
708
+ return (0, import_vue6.unref)(options.lazy);
709
+ });
710
+ const $rewardEarly = (0, import_vue6.computed)(() => {
711
+ if ($localOptions.value.$rewardEarly) {
712
+ return $localOptions.value.$rewardEarly;
713
+ }
714
+ return (0, import_vue6.unref)(options.rewardEarly);
715
+ });
716
+ const $autoDirty = (0, import_vue6.computed)(() => {
717
+ if ($localOptions.value.$autoDirty) {
718
+ return $localOptions.value.$autoDirty;
719
+ }
720
+ return (0, import_vue6.unref)(options.autoDirty);
721
+ });
667
722
  const $error = (0, import_vue6.computed)(() => {
668
723
  return $invalid.value && !$pending.value && $dirty.value;
669
724
  });
670
725
  const $pending = (0, import_vue6.computed)(() => {
671
- if (triggerPunishment.value || !(0, import_vue6.unref)(options.rewardEarly)) {
726
+ if (triggerPunishment.value || !$rewardEarly.value) {
672
727
  return Object.entries($rules.value).some(([key, ruleResult]) => {
673
728
  return ruleResult.$pending;
674
729
  });
@@ -676,7 +731,9 @@ function createReactiveFieldStatus({
676
731
  return false;
677
732
  });
678
733
  const $invalid = (0, import_vue6.computed)(() => {
679
- if (triggerPunishment.value || !(0, import_vue6.unref)(options.rewardEarly)) {
734
+ if ($externalErrors.value?.length) {
735
+ return true;
736
+ } else if (triggerPunishment.value || !$rewardEarly.value) {
680
737
  return Object.entries($rules.value).some(([key, ruleResult]) => {
681
738
  return !ruleResult.$valid;
682
739
  });
@@ -684,7 +741,9 @@ function createReactiveFieldStatus({
684
741
  return false;
685
742
  });
686
743
  const $valid = (0, import_vue6.computed)(() => {
687
- if ((0, import_vue6.unref)(options.rewardEarly)) {
744
+ if ($externalErrors.value?.length) {
745
+ return false;
746
+ } else if ($rewardEarly.value) {
688
747
  return Object.entries($rules.value).every(([key, ruleResult]) => {
689
748
  return ruleResult.$valid;
690
749
  });
@@ -696,14 +755,19 @@ function createReactiveFieldStatus({
696
755
  $error,
697
756
  $pending,
698
757
  $invalid,
699
- $valid
758
+ $valid,
759
+ $debounce,
760
+ $lazy,
761
+ $rewardEarly,
762
+ $autoDirty
700
763
  };
701
764
  });
702
765
  }
703
766
  const $rules = (0, import_vue6.ref)();
767
+ const $localOptions = (0, import_vue6.ref)();
704
768
  createReactiveRulesResult();
705
769
  const $unwatchValid = (0, import_vue6.watch)(scopeState.$valid, (valid) => {
706
- if ((0, import_vue6.unref)(options.rewardEarly) && valid) {
770
+ if (scopeState.$rewardEarly.value && valid) {
707
771
  triggerPunishment.value = false;
708
772
  }
709
773
  });
@@ -714,15 +778,18 @@ function createReactiveFieldStatus({
714
778
  function $touch() {
715
779
  $dirty.value = true;
716
780
  }
717
- function $commit() {
781
+ const $commit = debounce($commitHandler, scopeState.$debounce.value ?? 0);
782
+ function $commitHandler() {
718
783
  Object.entries($rules.value).map(([key, rule]) => {
719
784
  return rule.$validate();
720
785
  });
721
786
  }
722
- async function $validate() {
787
+ const $validate = debounce($validateHandler, scopeState.$debounce.value ?? 0);
788
+ async function $validateHandler() {
723
789
  try {
790
+ $clearExternalErrors();
724
791
  triggerPunishment.value = true;
725
- const results = await Promise.all(
792
+ const results = await Promise.allSettled(
726
793
  Object.entries($rules.value).map(([key, rule]) => {
727
794
  return rule.$validate();
728
795
  })