valdres 1.0.0-beta.5 → 1.0.0-beta.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.
package/dist/index.js
CHANGED
|
@@ -97,10 +97,10 @@ var equal = (a, b, updatedAtomsSet) => {
|
|
|
97
97
|
};
|
|
98
98
|
|
|
99
99
|
// src/utils/isAtom.ts
|
|
100
|
-
var isAtom = (state) => Object.hasOwn(state, "defaultValue");
|
|
100
|
+
var isAtom = (state) => state && Object.hasOwn(state, "defaultValue");
|
|
101
101
|
|
|
102
102
|
// src/utils/isGlobalAtom.ts
|
|
103
|
-
var isGlobalAtom = (state) => Object.hasOwn(state, "stores");
|
|
103
|
+
var isGlobalAtom = (state) => state && Object.hasOwn(state, "stores");
|
|
104
104
|
|
|
105
105
|
// src/utils/isSelector.ts
|
|
106
106
|
var isSelector = (state) => state && Object.hasOwn(state, "get");
|
|
@@ -628,6 +628,14 @@ var evaluate = (selector, data, initializedAtomsSet, circularDependencySet) => {
|
|
|
628
628
|
};
|
|
629
629
|
|
|
630
630
|
// src/lib/propagateUpdatedAtoms.ts
|
|
631
|
+
var notifyEntryFor = (notify, data) => {
|
|
632
|
+
let entry = notify.get(data);
|
|
633
|
+
if (entry === undefined) {
|
|
634
|
+
entry = { subscriptions: new Set, families: new Map };
|
|
635
|
+
notify.set(data, entry);
|
|
636
|
+
}
|
|
637
|
+
return entry;
|
|
638
|
+
};
|
|
631
639
|
var reEvaluateSelector = (selector, data, updatedAtoms, depsChange, existingValue) => {
|
|
632
640
|
try {
|
|
633
641
|
const rawValue = evaluateSelector(selector, data, updatedAtoms, undefined, depsChange);
|
|
@@ -674,6 +682,35 @@ var callSubscribers = (subscriptions, families) => {
|
|
|
674
682
|
if (hasError)
|
|
675
683
|
throw firstError;
|
|
676
684
|
};
|
|
685
|
+
var notifyDeferred = (notify) => {
|
|
686
|
+
let firstError;
|
|
687
|
+
let hasError = false;
|
|
688
|
+
for (const entry of notify.values()) {
|
|
689
|
+
if (entry.subscriptions.size > 0) {
|
|
690
|
+
try {
|
|
691
|
+
callSubscribers(entry.subscriptions, entry.families);
|
|
692
|
+
} catch (error) {
|
|
693
|
+
if (!hasError) {
|
|
694
|
+
firstError = error;
|
|
695
|
+
hasError = true;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
if (hasError)
|
|
701
|
+
throw firstError;
|
|
702
|
+
};
|
|
703
|
+
var collectFamilyAtomsForNotify = (entry, changedByFamily) => {
|
|
704
|
+
for (const [family, atoms] of changedByFamily) {
|
|
705
|
+
let target = entry.families.get(family);
|
|
706
|
+
if (target === undefined) {
|
|
707
|
+
target = new Set;
|
|
708
|
+
entry.families.set(family, target);
|
|
709
|
+
}
|
|
710
|
+
for (const atom of atoms)
|
|
711
|
+
target.add(atom);
|
|
712
|
+
}
|
|
713
|
+
};
|
|
677
714
|
var addSetToSet = (fromSet, toSet) => {
|
|
678
715
|
if (fromSet && fromSet.size > 0) {
|
|
679
716
|
for (const item of fromSet) {
|
|
@@ -681,20 +718,24 @@ var addSetToSet = (fromSet, toSet) => {
|
|
|
681
718
|
}
|
|
682
719
|
}
|
|
683
720
|
};
|
|
684
|
-
var propagateDeletedAtoms = (atoms, data, subscriptions = new Set,
|
|
721
|
+
var propagateDeletedAtoms = (atoms, data, subscriptions = new Set, deletedFamilyAtoms = new Map, timestamp = performance.now(), notify) => {
|
|
722
|
+
const notifyEntry = notify ? notifyEntryFor(notify, data) : undefined;
|
|
723
|
+
if (notifyEntry) {
|
|
724
|
+
subscriptions = notifyEntry.subscriptions;
|
|
725
|
+
}
|
|
685
726
|
const selectors = new Set;
|
|
686
727
|
for (const atom of atoms) {
|
|
687
728
|
addSetToSet(data.stateDependents.get(atom), selectors);
|
|
688
729
|
addSetToSet(data.subscriptions.get(atom), subscriptions);
|
|
689
730
|
if (isFamilyAtom(atom)) {
|
|
690
|
-
if (!
|
|
691
|
-
|
|
731
|
+
if (!deletedFamilyAtoms.has(atom.family)) {
|
|
732
|
+
deletedFamilyAtoms.set(atom.family, new Set);
|
|
692
733
|
}
|
|
693
|
-
|
|
734
|
+
deletedFamilyAtoms.get(atom.family).add(atom);
|
|
694
735
|
}
|
|
695
736
|
}
|
|
696
|
-
if (
|
|
697
|
-
for (const [family, familyAtoms] of
|
|
737
|
+
if (deletedFamilyAtoms.size > 0) {
|
|
738
|
+
for (const [family, familyAtoms] of deletedFamilyAtoms) {
|
|
698
739
|
addSetToSet(data.stateDependents.get(family), selectors);
|
|
699
740
|
addSetToSet(data.subscriptions.get(family), subscriptions);
|
|
700
741
|
if (familyAtoms.size === 0)
|
|
@@ -702,10 +743,12 @@ var propagateDeletedAtoms = (atoms, data, subscriptions = new Set, families = ne
|
|
|
702
743
|
deleteFamilyAtomsFromSet(family, familyAtoms, data, timestamp);
|
|
703
744
|
}
|
|
704
745
|
}
|
|
705
|
-
propagateDirtySelectors(atoms, selectors, data, subscriptions,
|
|
706
|
-
if (
|
|
746
|
+
propagateDirtySelectors(atoms, selectors, data, subscriptions, deletedFamilyAtoms, false, notify);
|
|
747
|
+
if (notifyEntry)
|
|
748
|
+
collectFamilyAtomsForNotify(notifyEntry, deletedFamilyAtoms);
|
|
749
|
+
if (deletedFamilyAtoms.size > 0 && data.scopes && data.scopes.size > 0) {
|
|
707
750
|
const scopeFamilies = new Map;
|
|
708
|
-
for (const family of
|
|
751
|
+
for (const family of deletedFamilyAtoms.keys()) {
|
|
709
752
|
const scopesWithFamily = data.scopeValueIndex.get(family);
|
|
710
753
|
if (scopesWithFamily) {
|
|
711
754
|
for (const scope of scopesWithFamily) {
|
|
@@ -719,11 +762,11 @@ var propagateDeletedAtoms = (atoms, data, subscriptions = new Set, families = ne
|
|
|
719
762
|
}
|
|
720
763
|
}
|
|
721
764
|
for (const [scope, familiesInScope] of scopeFamilies) {
|
|
722
|
-
propagateInScope(familiesInScope, scope, false,
|
|
765
|
+
propagateInScope(familiesInScope, scope, false, notify);
|
|
723
766
|
}
|
|
724
767
|
}
|
|
725
768
|
};
|
|
726
|
-
var propagateAtomUpdate = (atoms, data, isInitOnly = false,
|
|
769
|
+
var propagateAtomUpdate = (atoms, data, isInitOnly = false, notify) => {
|
|
727
770
|
if (atoms.length === 1) {
|
|
728
771
|
const atom = atoms[0];
|
|
729
772
|
if (!isFamilyAtom(atom) && !isAtomFamily(atom)) {
|
|
@@ -731,28 +774,32 @@ var propagateAtomUpdate = (atoms, data, isInitOnly = false, evaluatedSelectors)
|
|
|
731
774
|
if ((!dependents || dependents.size === 0) && (!data.scopes || data.scopes.size === 0)) {
|
|
732
775
|
const subs = data.subscriptions.get(atom);
|
|
733
776
|
if (subs && subs.size > 0) {
|
|
734
|
-
|
|
777
|
+
if (notify)
|
|
778
|
+
addSetToSet(subs, notifyEntryFor(notify, data).subscriptions);
|
|
779
|
+
else
|
|
780
|
+
callSubscribers(subs);
|
|
735
781
|
}
|
|
736
782
|
return;
|
|
737
783
|
}
|
|
738
784
|
}
|
|
739
785
|
}
|
|
740
|
-
const
|
|
741
|
-
const
|
|
786
|
+
const notifyEntry = notify ? notifyEntryFor(notify, data) : undefined;
|
|
787
|
+
const subscriptions = notifyEntry ? notifyEntry.subscriptions : new Set;
|
|
788
|
+
const updatedFamilyAtoms = new Map;
|
|
742
789
|
const selectors = new Set;
|
|
743
790
|
for (const atom of atoms) {
|
|
744
791
|
addSetToSet(data.stateDependents.get(atom), selectors);
|
|
745
792
|
addSetToSet(data.subscriptions.get(atom), subscriptions);
|
|
746
793
|
if (isFamilyAtom(atom)) {
|
|
747
|
-
if (!
|
|
748
|
-
|
|
794
|
+
if (!updatedFamilyAtoms.has(atom.family)) {
|
|
795
|
+
updatedFamilyAtoms.set(atom.family, new Set);
|
|
749
796
|
}
|
|
750
|
-
|
|
797
|
+
updatedFamilyAtoms.get(atom.family).add(atom);
|
|
751
798
|
}
|
|
752
799
|
}
|
|
753
|
-
if (
|
|
800
|
+
if (updatedFamilyAtoms.size > 0) {
|
|
754
801
|
const timestamp = performance.now();
|
|
755
|
-
for (const [family, familyAtoms] of
|
|
802
|
+
for (const [family, familyAtoms] of updatedFamilyAtoms) {
|
|
756
803
|
addSetToSet(data.stateDependents.get(family), selectors);
|
|
757
804
|
addSetToSet(data.subscriptions.get(family), subscriptions);
|
|
758
805
|
if (familyAtoms.size === 0)
|
|
@@ -762,35 +809,37 @@ var propagateAtomUpdate = (atoms, data, isInitOnly = false, evaluatedSelectors)
|
|
|
762
809
|
}
|
|
763
810
|
if (data.scopes && data.scopes.size > 0) {
|
|
764
811
|
for (const atom of atoms) {
|
|
765
|
-
if (isAtomFamily(atom) && !
|
|
812
|
+
if (isAtomFamily(atom) && !updatedFamilyAtoms.has(atom)) {
|
|
766
813
|
recursivelyUpdateIndexes(data, atom);
|
|
767
814
|
}
|
|
768
815
|
}
|
|
769
816
|
}
|
|
770
|
-
propagateDirtySelectors(atoms, selectors, data, subscriptions,
|
|
817
|
+
propagateDirtySelectors(atoms, selectors, data, subscriptions, updatedFamilyAtoms, isInitOnly, notify);
|
|
818
|
+
if (notifyEntry)
|
|
819
|
+
collectFamilyAtomsForNotify(notifyEntry, updatedFamilyAtoms);
|
|
771
820
|
if (data.scopes && data.scopes.size > 0) {
|
|
772
|
-
propagateToScopes(atoms, data, isInitOnly,
|
|
821
|
+
propagateToScopes(atoms, data, isInitOnly, notify);
|
|
773
822
|
}
|
|
774
823
|
};
|
|
775
|
-
var propagateInScope = (atoms, data, isInitOnly = false,
|
|
776
|
-
const subscriptions = new Set;
|
|
824
|
+
var propagateInScope = (atoms, data, isInitOnly = false, notify) => {
|
|
825
|
+
const subscriptions = notify ? notifyEntryFor(notify, data).subscriptions : new Set;
|
|
777
826
|
const families = new Map;
|
|
778
827
|
const selectors = new Set;
|
|
779
828
|
for (const atom of atoms) {
|
|
780
829
|
addSetToSet(data.stateDependents.get(atom), selectors);
|
|
781
830
|
}
|
|
782
|
-
propagateDirtySelectors(atoms, selectors, data, subscriptions, families, isInitOnly,
|
|
831
|
+
propagateDirtySelectors(atoms, selectors, data, subscriptions, families, isInitOnly, notify);
|
|
783
832
|
if (data.scopes && data.scopes.size > 0) {
|
|
784
|
-
propagateToScopes(atoms, data, isInitOnly,
|
|
833
|
+
propagateToScopes(atoms, data, isInitOnly, notify);
|
|
785
834
|
}
|
|
786
835
|
};
|
|
787
|
-
var propagateToScopes = (atoms, data, isInitOnly,
|
|
836
|
+
var propagateToScopes = (atoms, data, isInitOnly, notify) => {
|
|
788
837
|
if (atoms.length === 1) {
|
|
789
838
|
const atom = atoms[0];
|
|
790
839
|
const shadowingScopes = isAtomFamily(atom) ? undefined : data.scopeValueIndex.get(atom);
|
|
791
840
|
for (const [, scope] of data.scopes) {
|
|
792
841
|
if (!shadowingScopes || !shadowingScopes.has(scope)) {
|
|
793
|
-
propagateInScope(atoms, scope, isInitOnly,
|
|
842
|
+
propagateInScope(atoms, scope, isInitOnly, notify);
|
|
794
843
|
}
|
|
795
844
|
}
|
|
796
845
|
return;
|
|
@@ -810,7 +859,7 @@ var propagateToScopes = (atoms, data, isInitOnly, evaluatedSelectors) => {
|
|
|
810
859
|
}
|
|
811
860
|
if (!anyShadowed) {
|
|
812
861
|
for (const [, scope] of data.scopes) {
|
|
813
|
-
propagateInScope(atoms, scope, isInitOnly,
|
|
862
|
+
propagateInScope(atoms, scope, isInitOnly, notify);
|
|
814
863
|
}
|
|
815
864
|
return;
|
|
816
865
|
}
|
|
@@ -827,20 +876,20 @@ var propagateToScopes = (atoms, data, isInitOnly, evaluatedSelectors) => {
|
|
|
827
876
|
}
|
|
828
877
|
}
|
|
829
878
|
if (atomsToUpdateInScope.length > 0) {
|
|
830
|
-
propagateInScope(atomsToUpdateInScope, scope, isInitOnly,
|
|
879
|
+
propagateInScope(atomsToUpdateInScope, scope, isInitOnly, notify);
|
|
831
880
|
}
|
|
832
881
|
}
|
|
833
882
|
};
|
|
834
|
-
var propagateDirtySelectors = (updatedAtoms, selectors, data, subscriptions, families, isInitOnly = false,
|
|
883
|
+
var propagateDirtySelectors = (updatedAtoms, selectors, data, subscriptions, families, isInitOnly = false, notify) => {
|
|
835
884
|
const updatedInitializedAtoms = new Set(updatedAtoms);
|
|
836
885
|
if (selectors.size > 0) {
|
|
837
|
-
propagateSelectorUpdates(selectors, data, subscriptions, updatedInitializedAtoms, isInitOnly
|
|
886
|
+
propagateSelectorUpdates(selectors, data, subscriptions, updatedInitializedAtoms, isInitOnly);
|
|
838
887
|
}
|
|
839
|
-
if (subscriptions.size > 0) {
|
|
888
|
+
if (!notify && subscriptions.size > 0) {
|
|
840
889
|
callSubscribers(subscriptions, families);
|
|
841
890
|
}
|
|
842
891
|
};
|
|
843
|
-
var propagateDownstreamTopo = (seeds, data, collectedSubscribers, updatedInitializedAtoms, isInitOnly
|
|
892
|
+
var propagateDownstreamTopo = (seeds, data, collectedSubscribers, updatedInitializedAtoms, isInitOnly) => {
|
|
844
893
|
const closure = new Set(seeds);
|
|
845
894
|
{
|
|
846
895
|
const stack = [...seeds];
|
|
@@ -901,10 +950,6 @@ var propagateDownstreamTopo = (seeds, data, collectedSubscribers, updatedInitial
|
|
|
901
950
|
let head = 0;
|
|
902
951
|
while (head < ready.length) {
|
|
903
952
|
const selector = ready[head++];
|
|
904
|
-
if (evaluatedSelectors !== undefined && evaluatedSelectors.has(selector)) {
|
|
905
|
-
advance(selector, false);
|
|
906
|
-
continue;
|
|
907
|
-
}
|
|
908
953
|
const currentValue = data.values.get(selector);
|
|
909
954
|
if (isPromiseLike(currentValue) && isInitOnly) {
|
|
910
955
|
advance(selector, false);
|
|
@@ -924,8 +969,6 @@ var propagateDownstreamTopo = (seeds, data, collectedSubscribers, updatedInitial
|
|
|
924
969
|
depsChange.added = undefined;
|
|
925
970
|
depsChange.removed = undefined;
|
|
926
971
|
const wasValueUpdated = reEvaluateSelector(selector, data, updatedInitializedAtoms, depsChange, currentValue);
|
|
927
|
-
if (evaluatedSelectors !== undefined)
|
|
928
|
-
evaluatedSelectors.add(selector);
|
|
929
972
|
const added = depsChange.added;
|
|
930
973
|
const removed = depsChange.removed;
|
|
931
974
|
if (added || removed) {
|
|
@@ -978,8 +1021,6 @@ var propagateDownstreamTopo = (seeds, data, collectedSubscribers, updatedInitial
|
|
|
978
1021
|
depsChange.added = undefined;
|
|
979
1022
|
depsChange.removed = undefined;
|
|
980
1023
|
const wasValueUpdated = reEvaluateSelector(selector, data, updatedInitializedAtoms, depsChange, currentValue);
|
|
981
|
-
if (evaluatedSelectors !== undefined)
|
|
982
|
-
evaluatedSelectors.add(selector);
|
|
983
1024
|
const added = depsChange.added;
|
|
984
1025
|
const removed = depsChange.removed;
|
|
985
1026
|
if ((added || removed) && isLive(selector, data)) {
|
|
@@ -1009,15 +1050,12 @@ var propagateDownstreamTopo = (seeds, data, collectedSubscribers, updatedInitial
|
|
|
1009
1050
|
work = next;
|
|
1010
1051
|
}
|
|
1011
1052
|
};
|
|
1012
|
-
var propagateSelectorUpdates = (selectors, data, collectedSubscribers, updatedInitializedAtoms, isInitOnly = false
|
|
1053
|
+
var propagateSelectorUpdates = (selectors, data, collectedSubscribers, updatedInitializedAtoms, isInitOnly = false) => {
|
|
1013
1054
|
if (selectors.size === 0)
|
|
1014
1055
|
return;
|
|
1015
1056
|
let downstreamSeeds;
|
|
1016
1057
|
const depsChange = {};
|
|
1017
1058
|
for (const selector of selectors) {
|
|
1018
|
-
if (evaluatedSelectors !== undefined && evaluatedSelectors.has(selector)) {
|
|
1019
|
-
continue;
|
|
1020
|
-
}
|
|
1021
1059
|
const currentValue = data.values.get(selector);
|
|
1022
1060
|
if (isPromiseLike(currentValue) && isInitOnly)
|
|
1023
1061
|
continue;
|
|
@@ -1030,8 +1068,6 @@ var propagateSelectorUpdates = (selectors, data, collectedSubscribers, updatedIn
|
|
|
1030
1068
|
depsChange.added = undefined;
|
|
1031
1069
|
depsChange.removed = undefined;
|
|
1032
1070
|
const wasValueUpdated = reEvaluateSelector(selector, data, updatedInitializedAtoms, depsChange, currentValue);
|
|
1033
|
-
if (evaluatedSelectors !== undefined)
|
|
1034
|
-
evaluatedSelectors.add(selector);
|
|
1035
1071
|
const added = depsChange.added;
|
|
1036
1072
|
const removed = depsChange.removed;
|
|
1037
1073
|
if ((added || removed) && isLive(selector, data)) {
|
|
@@ -1061,14 +1097,14 @@ var propagateSelectorUpdates = (selectors, data, collectedSubscribers, updatedIn
|
|
|
1061
1097
|
}
|
|
1062
1098
|
}
|
|
1063
1099
|
if (downstreamSeeds && downstreamSeeds.size > 0) {
|
|
1064
|
-
propagateDownstreamTopo(downstreamSeeds, data, collectedSubscribers, updatedInitializedAtoms, isInitOnly
|
|
1100
|
+
propagateDownstreamTopo(downstreamSeeds, data, collectedSubscribers, updatedInitializedAtoms, isInitOnly);
|
|
1065
1101
|
}
|
|
1066
1102
|
};
|
|
1067
1103
|
|
|
1068
1104
|
// src/lib/isFunction.ts
|
|
1069
1105
|
var isFunction = (value) => typeof value === "function";
|
|
1070
1106
|
|
|
1071
|
-
// src/lib/
|
|
1107
|
+
// src/lib/resolvePendingDefault.ts
|
|
1072
1108
|
var resolvePendingDefault = (atom, data, value) => {
|
|
1073
1109
|
let cur = data;
|
|
1074
1110
|
while (cur) {
|
|
@@ -1081,6 +1117,8 @@ var resolvePendingDefault = (atom, data, value) => {
|
|
|
1081
1117
|
cur = "parent" in cur ? cur.parent : undefined;
|
|
1082
1118
|
}
|
|
1083
1119
|
};
|
|
1120
|
+
|
|
1121
|
+
// src/lib/setAtom.ts
|
|
1084
1122
|
var handlePromise = (atom, promise, currentValue, data, skipOnSet) => {
|
|
1085
1123
|
setValueInData(atom, promise, data);
|
|
1086
1124
|
promise.then((resolvedValue) => {
|
|
@@ -1670,7 +1708,8 @@ var writeAtoms = (pairs, data, initializedAtomsSet, skipOnSet = false, onSetQueu
|
|
|
1670
1708
|
const updatedAtoms = [];
|
|
1671
1709
|
for (let [atom, value] of pairs) {
|
|
1672
1710
|
const currentValue = getState(atom, data, initializedAtomsSet);
|
|
1673
|
-
const
|
|
1711
|
+
const currentIsPromise = isPromiseLike(currentValue);
|
|
1712
|
+
const areEqual = currentIsPromise || isPromiseLike(value) ? currentValue === value : atom.equal(currentValue, value);
|
|
1674
1713
|
if (!areEqual) {
|
|
1675
1714
|
updatedAtoms.push(atom);
|
|
1676
1715
|
value = setValueInData(atom, value, data);
|
|
@@ -1680,6 +1719,9 @@ var writeAtoms = (pairs, data, initializedAtomsSet, skipOnSet = false, onSetQueu
|
|
|
1680
1719
|
else
|
|
1681
1720
|
atom.onSet(value, data);
|
|
1682
1721
|
}
|
|
1722
|
+
if (currentIsPromise && !isPromiseLike(value)) {
|
|
1723
|
+
resolvePendingDefault(atom, data, value);
|
|
1724
|
+
}
|
|
1683
1725
|
} else {
|
|
1684
1726
|
setValueInData(atom, value, data);
|
|
1685
1727
|
}
|
|
@@ -1877,13 +1919,14 @@ class Transaction {
|
|
|
1877
1919
|
if (!this._scopedTransactions) {
|
|
1878
1920
|
const initializedAtomsSet = new Set;
|
|
1879
1921
|
if (this._deleteSet?.size) {
|
|
1880
|
-
const
|
|
1922
|
+
const notify2 = new Map;
|
|
1881
1923
|
const updatedAtoms = writeAtoms(this._atomMap, this.data, initializedAtomsSet);
|
|
1882
1924
|
if (updatedAtoms.length > 0) {
|
|
1883
|
-
propagateAtomUpdate(updatedAtoms, this.data, false,
|
|
1925
|
+
propagateAtomUpdate(updatedAtoms, this.data, false, notify2);
|
|
1884
1926
|
}
|
|
1885
1927
|
deleteAtomFamilyAtoms(this._deleteSet, this.data);
|
|
1886
|
-
propagateDeletedAtoms([...this._deleteSet], this.data, undefined, undefined, undefined,
|
|
1928
|
+
propagateDeletedAtoms([...this._deleteSet], this.data, undefined, undefined, undefined, notify2);
|
|
1929
|
+
notifyDeferred(notify2);
|
|
1887
1930
|
} else {
|
|
1888
1931
|
setAtoms(this._atomMap, this.data, initializedAtomsSet);
|
|
1889
1932
|
}
|
|
@@ -1905,15 +1948,16 @@ class Transaction {
|
|
|
1905
1948
|
atom.onSet(value, data);
|
|
1906
1949
|
}
|
|
1907
1950
|
}
|
|
1908
|
-
const
|
|
1951
|
+
const notify = new Map;
|
|
1909
1952
|
for (const { data, updatedAtoms, deleted } of plan) {
|
|
1910
1953
|
if (updatedAtoms.length > 0) {
|
|
1911
|
-
propagateAtomUpdate(updatedAtoms, data, false,
|
|
1954
|
+
propagateAtomUpdate(updatedAtoms, data, false, notify);
|
|
1912
1955
|
}
|
|
1913
1956
|
if (deleted) {
|
|
1914
|
-
propagateDeletedAtoms(deleted, data, undefined, undefined, undefined,
|
|
1957
|
+
propagateDeletedAtoms(deleted, data, undefined, undefined, undefined, notify);
|
|
1915
1958
|
}
|
|
1916
1959
|
}
|
|
1960
|
+
notifyDeferred(notify);
|
|
1917
1961
|
};
|
|
1918
1962
|
collectStores = (plan) => {
|
|
1919
1963
|
plan.push({
|
|
@@ -2669,38 +2713,27 @@ var index = (family, callback, options) => {
|
|
|
2669
2713
|
const map = new Map;
|
|
2670
2714
|
const index2 = (term) => {
|
|
2671
2715
|
const termKey = stableStringify(term);
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
const
|
|
2676
|
-
const
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
deletedAtoms.forEach((atom2) => {
|
|
2682
|
-
termIndexSelectorSet.delete(atom2);
|
|
2683
|
-
termIndexSelectorMap.delete(atom2);
|
|
2716
|
+
const existing = map.get(termKey);
|
|
2717
|
+
if (existing)
|
|
2718
|
+
return existing;
|
|
2719
|
+
const predicateSelectors = new WeakMap;
|
|
2720
|
+
const predicateFor = (atom2) => {
|
|
2721
|
+
let sel = predicateSelectors.get(atom2);
|
|
2722
|
+
if (!sel) {
|
|
2723
|
+
sel = selector((get) => callback(get(atom2), term), {
|
|
2724
|
+
name: `index:callback:selector:${atom2.name}`
|
|
2684
2725
|
});
|
|
2685
|
-
|
|
2686
|
-
termIndexSelectorSet.add(atom2);
|
|
2687
|
-
termIndexSelectorMap.set(atom2, selector((get2) => callback(get2(atom2), term), {
|
|
2688
|
-
name: `index:callback:selector:${atom2.name}`
|
|
2689
|
-
}));
|
|
2690
|
-
});
|
|
2691
|
-
return new Set(termIndexSelectorSet);
|
|
2692
|
-
} else {
|
|
2693
|
-
return termIndexSelectorSet;
|
|
2726
|
+
predicateSelectors.set(atom2, sel);
|
|
2694
2727
|
}
|
|
2695
|
-
|
|
2728
|
+
return sel;
|
|
2729
|
+
};
|
|
2696
2730
|
const filteredSelector = selector((get) => {
|
|
2697
|
-
const set = get(termIndexSelector);
|
|
2698
2731
|
const res = [];
|
|
2699
|
-
|
|
2700
|
-
|
|
2732
|
+
const members = get(family);
|
|
2733
|
+
for (const atom2 of members) {
|
|
2734
|
+
if (get(predicateFor(atom2)))
|
|
2701
2735
|
res.push(atom2);
|
|
2702
|
-
|
|
2703
|
-
});
|
|
2736
|
+
}
|
|
2704
2737
|
return res;
|
|
2705
2738
|
}, {
|
|
2706
2739
|
name: `index:${options?.name}:${termKey}:termSelector`
|
|
@@ -2748,9 +2781,9 @@ var isFamilySelector = (state) => isFamilyState(state) && isSelector(state);
|
|
|
2748
2781
|
|
|
2749
2782
|
// src/index.ts
|
|
2750
2783
|
if (globalThis.__valdres__) {
|
|
2751
|
-
throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"1.0.0-beta.
|
|
2784
|
+
throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"1.0.0-beta.6"}`);
|
|
2752
2785
|
} else {
|
|
2753
|
-
globalThis.__valdres__ = "1.0.0-beta.
|
|
2786
|
+
globalThis.__valdres__ = "1.0.0-beta.6";
|
|
2754
2787
|
}
|
|
2755
2788
|
export {
|
|
2756
2789
|
store,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AtomFamily } from "./types/AtomFamily";
|
|
2
|
+
import type { AtomFamilyAtom } from "./types/AtomFamilyAtom";
|
|
2
3
|
import type { Selector } from "./types/Selector";
|
|
3
4
|
export declare const index: <Term, Value extends any, FamilyArgs extends [any, ...any[]] = [any, ...any[]]>(family: AtomFamily<Value, FamilyArgs>, callback: (value: Value, term: Term) => boolean, options?: {
|
|
4
5
|
name?: string;
|
|
5
|
-
}) => ((term: Term) => Selector<
|
|
6
|
+
}) => ((term: Term) => Selector<AtomFamilyAtom<Value, FamilyArgs>[]>);
|
|
@@ -7,7 +7,13 @@ import type { Subscription } from "../types/Subscription";
|
|
|
7
7
|
export type { AtomFamilyIndex, } from "./atomFamilyIndex";
|
|
8
8
|
export { cloneAtomFamilyIndex, createAtomFamilyIndex, renderAtomFamilyIndex, } from "./atomFamilyIndex";
|
|
9
9
|
type AtomInput = Atom<any> | AtomFamilyAtom<any, any> | AtomFamily<any, any>;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
type NotifyStoreEntry = {
|
|
11
|
+
subscriptions: Set<Subscription>;
|
|
12
|
+
families: Map<AtomFamily<any>, Set<AtomFamilyAtom<any>>>;
|
|
13
|
+
};
|
|
14
|
+
export type NotifyTarget = Map<StoreData, NotifyStoreEntry>;
|
|
15
|
+
export declare const notifyDeferred: (notify: NotifyTarget) => void;
|
|
16
|
+
export declare const propagateDeletedAtoms: (atoms: AtomFamilyAtom<any, any>[], data: StoreData, subscriptions?: Set<Subscription>, deletedFamilyAtoms?: Map<AtomFamily<any>, Set<AtomFamilyAtom<any>>>, timestamp?: number, notify?: NotifyTarget) => void;
|
|
17
|
+
export declare const propagateAtomUpdate: (atoms: AtomInput[], data: StoreData, isInitOnly?: boolean, notify?: NotifyTarget) => void;
|
|
18
|
+
export declare const propagateInScope: (atoms: AtomInput[], data: StoreData, isInitOnly?: boolean, notify?: NotifyTarget) => void;
|
|
19
|
+
export declare const propagateDirtySelectors: (updatedAtoms: Atom[], selectors: Set<Selector>, data: StoreData, subscriptions: Set<Subscription>, families: Map<AtomFamily<any>, Set<AtomFamilyAtom<any>>>, isInitOnly?: boolean, notify?: NotifyTarget) => void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Atom } from "../types/Atom";
|
|
2
|
+
import type { StoreData } from "../types/StoreData";
|
|
3
|
+
/** Resolve any outstanding pending-default suspense placeholder for `atom`
|
|
4
|
+
* with `value` and remove it. The placeholder may have been registered in a
|
|
5
|
+
* parent store (atoms with no `defaultValue` init in whichever scope first
|
|
6
|
+
* reads them, which `getState` resolves by walking up to root), so we walk
|
|
7
|
+
* the scope chain rather than only checking `data`.
|
|
8
|
+
*
|
|
9
|
+
* Called by every write path that lands a value on an atom that may carry a
|
|
10
|
+
* placeholder — `setAtom` (sync + async resolve) and `writeAtoms` (the
|
|
11
|
+
* transaction path).
|
|
12
|
+
*
|
|
13
|
+
* Caller contract: pass a SETTLED value, never a promise. The placeholder must
|
|
14
|
+
* resolve to the eventual real value, so an in-flight promise stored
|
|
15
|
+
* mid-sequence must not consume it — a later settled write still has to find
|
|
16
|
+
* the entry here. `setAtom` guarantees this by routing promises through
|
|
17
|
+
* `handlePromise` instead; `writeAtoms` by gating on `!isPromiseLike(value)`.
|
|
18
|
+
*
|
|
19
|
+
* Optional optimization (not required): a caller that already knows the prior
|
|
20
|
+
* value may skip the call when it isn't promise-like — a placeholder is always
|
|
21
|
+
* stored as a promise, so a non-promise prior value can't have one. `writeAtoms`
|
|
22
|
+
* does this (gating on `isPromiseLike(currentValue)`) to keep the write hot
|
|
23
|
+
* path off this function. `setAtom` calls unconditionally; the walk is a couple
|
|
24
|
+
* of WeakMap lookups that bail immediately when there's nothing to resolve.
|
|
25
|
+
*
|
|
26
|
+
* Idempotent and commit-safe: the entry is deleted on first resolve and a
|
|
27
|
+
* settled promise ignores later `resolve` calls, so calling this from more
|
|
28
|
+
* than one store pass in a single cross-scope commit is harmless. `resolve`
|
|
29
|
+
* only schedules the placeholder's `.then` microtasks — it runs no subscriber
|
|
30
|
+
* code synchronously — so calling it during a transaction's write phase
|
|
31
|
+
* cannot expose a half-applied commit. */
|
|
32
|
+
export declare const resolvePendingDefault: <Value>(atom: Atom<Value>, data: StoreData, value: Value) => void;
|