valdres 0.2.0-alpha.30 → 0.2.0-alpha.32
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 +451 -410
- package/dist/types/index.d.ts +1 -0
- package/dist/types/src/atom.d.ts +1 -1
- package/dist/types/src/atomFamily.d.ts +2 -8
- package/dist/types/src/globalStore.d.ts +4 -0
- package/dist/types/src/lib/createAtomFamily.d.ts +4 -0
- package/dist/types/src/lib/createGlobalAtomFamily.d.ts +5 -0
- package/dist/types/src/lib/globalAtom.d.ts +1 -1
- package/dist/types/src/lib/subscribe.d.ts +1 -1
- package/dist/types/src/selector.d.ts +1 -1
- package/dist/types/src/selectorFamily.d.ts +2 -1
- package/dist/types/src/types/Atom.d.ts +3 -1
- package/dist/types/src/types/AtomFamily.d.ts +1 -1
- package/dist/types/src/types/AtomFamilyDefaultValue.d.ts +5 -0
- package/dist/types/src/types/AtomOptions.d.ts +3 -1
- package/dist/types/src/types/EqualFunc.d.ts +1 -0
- package/dist/types/src/types/GetValue.d.ts +3 -3
- package/dist/types/src/types/GlobalAtom.d.ts +5 -1
- package/dist/types/src/types/GlobalAtomGetSelfFunc.d.ts +1 -0
- package/dist/types/src/types/Selector.d.ts +3 -1
- package/dist/types/src/types/SelectorOptions.d.ts +4 -2
- package/dist/types/src/types/State.d.ts +2 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
// src/atom.ts
|
|
2
|
+
import equal3 from "fast-deep-equal/es6";
|
|
3
|
+
|
|
4
|
+
// src/lib/globalAtom.ts
|
|
5
|
+
import equal2 from "fast-deep-equal/es6";
|
|
6
|
+
|
|
1
7
|
// src/utils/isAtom.ts
|
|
2
8
|
var isAtom = (state) => Object.hasOwn(state, "defaultValue");
|
|
3
9
|
|
|
@@ -7,42 +13,137 @@ var isFamilyState = (state) => state && Object.hasOwn(state, "family");
|
|
|
7
13
|
// src/utils/isFamilyAtom.ts
|
|
8
14
|
var isFamilyAtom = (state) => isFamilyState(state) && isAtom(state);
|
|
9
15
|
|
|
16
|
+
// src/lib/updateStateSubscribers.ts
|
|
17
|
+
var updateStateSubscribers = (state, data) => {
|
|
18
|
+
const subscribtions = data.subscriptions.get(state);
|
|
19
|
+
if (subscribtions?.size) {
|
|
20
|
+
for (const subscribtion of subscribtions) {
|
|
21
|
+
subscribtion.callback();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (isFamilyState(state)) {
|
|
25
|
+
const familySubscriptions = data.subscriptions.get(state.family);
|
|
26
|
+
if (familySubscriptions?.size) {
|
|
27
|
+
for (const subscribtion of familySubscriptions) {
|
|
28
|
+
subscribtion.callback(state.familyKey);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
10
34
|
// src/utils/isPromiseLike.ts
|
|
11
35
|
var isPromiseLike = (object) => {
|
|
12
36
|
return object && object.then && typeof object.then === "function";
|
|
13
37
|
};
|
|
14
38
|
|
|
15
|
-
// src/utils/isSelector.ts
|
|
16
|
-
var isSelector = (state) => state && Object.hasOwn(state, "get");
|
|
17
|
-
|
|
18
39
|
// src/lib/getState.ts
|
|
19
|
-
import
|
|
40
|
+
import equal from "fast-deep-equal/es6";
|
|
20
41
|
|
|
21
42
|
// src/utils/isAtomFamily.ts
|
|
22
43
|
var isAtomFamily = (state) => state && Object.hasOwn(state, "__valdresAtomFamilyMap");
|
|
23
44
|
|
|
45
|
+
// src/utils/isSelector.ts
|
|
46
|
+
var isSelector = (state) => state && Object.hasOwn(state, "get");
|
|
47
|
+
|
|
24
48
|
// src/utils/isSelectorFamily.ts
|
|
25
49
|
var isSelectorFamily = (state) => state && Object.hasOwn(state, "__valdresSelectorFamilyMap");
|
|
26
50
|
|
|
27
|
-
// src/lib/
|
|
28
|
-
|
|
51
|
+
// src/lib/setAtom.ts
|
|
52
|
+
var setAtom = (atom, newValue, data, skipOnSet = false) => {
|
|
53
|
+
const currentValue = getState(atom, data);
|
|
54
|
+
if (typeof newValue === "function") {
|
|
55
|
+
newValue = newValue(currentValue);
|
|
56
|
+
if (isPromiseLike(newValue) || isPromiseLike(currentValue))
|
|
57
|
+
throw new Error("Todo, how should we handle this?");
|
|
58
|
+
}
|
|
59
|
+
if (atom.equal(currentValue, newValue))
|
|
60
|
+
return;
|
|
61
|
+
data.values.set(atom, newValue);
|
|
62
|
+
if (atom.onSet && !skipOnSet)
|
|
63
|
+
atom.onSet(newValue, data);
|
|
64
|
+
if (currentValue?.__isEmptyAtomPromise__) {
|
|
65
|
+
currentValue.__resolveEmptyAtomPromise__(newValue);
|
|
66
|
+
}
|
|
67
|
+
propagateUpdatedAtoms([atom], data);
|
|
68
|
+
};
|
|
29
69
|
|
|
30
|
-
// src/lib/
|
|
31
|
-
var
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
70
|
+
// src/lib/initAtom.ts
|
|
71
|
+
var getAtomInitValue = (atom, data) => {
|
|
72
|
+
if (atom.defaultValue === undefined) {
|
|
73
|
+
let promiseResolve;
|
|
74
|
+
const promise = new Promise((resolve) => {
|
|
75
|
+
promiseResolve = resolve;
|
|
76
|
+
});
|
|
77
|
+
promise.__isEmptyAtomPromise__ = true;
|
|
78
|
+
promise.__resolveEmptyAtomPromise__ = promiseResolve;
|
|
79
|
+
return promise;
|
|
80
|
+
} else if (typeof atom.defaultValue === "function") {
|
|
81
|
+
const value = atom.defaultValue();
|
|
82
|
+
if (isPromiseLike(value)) {
|
|
83
|
+
value.then((resolvedValue) => {
|
|
84
|
+
data.values.set(atom, resolvedValue);
|
|
85
|
+
propagateUpdatedAtoms([atom], data);
|
|
86
|
+
});
|
|
36
87
|
}
|
|
88
|
+
return value;
|
|
89
|
+
} else if (isSelector(atom.defaultValue)) {
|
|
90
|
+
return getState(atom.defaultValue, data);
|
|
91
|
+
} else {
|
|
92
|
+
return atom.defaultValue;
|
|
37
93
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
94
|
+
};
|
|
95
|
+
var initAtom = (atom, data) => {
|
|
96
|
+
let value = getAtomInitValue(atom, data);
|
|
97
|
+
data.values.set(atom, value);
|
|
98
|
+
if (isFamilyAtom(atom)) {
|
|
99
|
+
const currentKeySet = getState(atom.family.__keysAtom, data);
|
|
100
|
+
if (!currentKeySet.has(atom.familyKey)) {
|
|
101
|
+
const newSet = new Set(currentKeySet);
|
|
102
|
+
newSet.add(atom.familyKey);
|
|
103
|
+
setAtom(atom.family.__keysAtom, newSet, data);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (atom.onInit)
|
|
107
|
+
atom.onInit((newVal) => {
|
|
108
|
+
value = newVal;
|
|
109
|
+
setAtom(atom, newVal, data, true);
|
|
110
|
+
}, data);
|
|
111
|
+
return value;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// src/lib/getState.ts
|
|
115
|
+
function getState(state, data) {
|
|
116
|
+
if (data.values.has(state))
|
|
117
|
+
return data.values.get(state);
|
|
118
|
+
if (isAtom(state)) {
|
|
119
|
+
if ("parent" in data)
|
|
120
|
+
return getState(state, data.parent);
|
|
121
|
+
return initAtom(state, data);
|
|
122
|
+
}
|
|
123
|
+
if (isSelector(state))
|
|
124
|
+
return initSelector(state, data);
|
|
125
|
+
if (isAtomFamily(state)) {
|
|
126
|
+
if ("parent" in data) {
|
|
127
|
+
const closestData = findClosestStoreWithAtomInitialized(state.__keysAtom, data);
|
|
128
|
+
return getState(state.__keysSelector, closestData);
|
|
44
129
|
}
|
|
130
|
+
return getState(state.__keysSelector, data);
|
|
45
131
|
}
|
|
132
|
+
if (isSelectorFamily(state)) {
|
|
133
|
+
const array = Array.from(state.__valdresSelectorFamilyMap.keys());
|
|
134
|
+
if (equal(array, state._keyArray))
|
|
135
|
+
return state._keyArray;
|
|
136
|
+
state._keyArray = array;
|
|
137
|
+
return array;
|
|
138
|
+
}
|
|
139
|
+
throw new Error("Invalid object passed to get");
|
|
140
|
+
}
|
|
141
|
+
var findClosestStoreWithAtomInitialized = (atom, data) => {
|
|
142
|
+
if ("parent" in data === false)
|
|
143
|
+
return data;
|
|
144
|
+
if (data.values.has(atom))
|
|
145
|
+
return data;
|
|
146
|
+
return findClosestStoreWithAtomInitialized(atom, data.parent);
|
|
46
147
|
};
|
|
47
148
|
|
|
48
149
|
// src/lib/initSelector.ts
|
|
@@ -113,7 +214,7 @@ var initSelector = (selector, data) => {
|
|
|
113
214
|
const value = handleSelectorResult(tmpValue, selector, data);
|
|
114
215
|
if (data.expiredValues.has(selector)) {
|
|
115
216
|
const expiredValue = data.expiredValues.get(selector);
|
|
116
|
-
if (equal(expiredValue, value)) {
|
|
217
|
+
if (selector.equal(expiredValue, value)) {
|
|
117
218
|
data.values.set(selector, expiredValue);
|
|
118
219
|
return expiredValue;
|
|
119
220
|
}
|
|
@@ -122,43 +223,7 @@ var initSelector = (selector, data) => {
|
|
|
122
223
|
return value;
|
|
123
224
|
};
|
|
124
225
|
|
|
125
|
-
// src/lib/getState.ts
|
|
126
|
-
function getState(state, data) {
|
|
127
|
-
if (data.values.has(state))
|
|
128
|
-
return data.values.get(state);
|
|
129
|
-
if (isAtom(state)) {
|
|
130
|
-
if ("parent" in data)
|
|
131
|
-
return getState(state, data.parent);
|
|
132
|
-
return initAtom(state, data);
|
|
133
|
-
}
|
|
134
|
-
if (isSelector(state))
|
|
135
|
-
return initSelector(state, data);
|
|
136
|
-
if (isAtomFamily(state)) {
|
|
137
|
-
if ("parent" in data) {
|
|
138
|
-
const closestData = findClosestStoreWithAtomInitialized(state.__keysAtom, data);
|
|
139
|
-
return getState(state.__keysSelector, closestData);
|
|
140
|
-
}
|
|
141
|
-
return getState(state.__keysSelector, data);
|
|
142
|
-
}
|
|
143
|
-
if (isSelectorFamily(state)) {
|
|
144
|
-
const array = Array.from(state.__valdresSelectorFamilyMap.keys());
|
|
145
|
-
if (equal2(array, state._keyArray))
|
|
146
|
-
return state._keyArray;
|
|
147
|
-
state._keyArray = array;
|
|
148
|
-
return array;
|
|
149
|
-
}
|
|
150
|
-
throw new Error("Invalid object passed to get");
|
|
151
|
-
}
|
|
152
|
-
var findClosestStoreWithAtomInitialized = (atom, data) => {
|
|
153
|
-
if ("parent" in data === false)
|
|
154
|
-
return data;
|
|
155
|
-
if (data.values.has(atom))
|
|
156
|
-
return data;
|
|
157
|
-
return findClosestStoreWithAtomInitialized(atom, data.parent);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
226
|
// src/lib/updateSelectorSubscribers.ts
|
|
161
|
-
import equal3 from "fast-deep-equal/es6";
|
|
162
227
|
var updateSelectorSubscribers = (selector, data) => {
|
|
163
228
|
const subscribtions = data.subscriptions.get(selector);
|
|
164
229
|
const familySubscriptions = selector.family && data.subscriptions.get(selector.family);
|
|
@@ -168,7 +233,7 @@ var updateSelectorSubscribers = (selector, data) => {
|
|
|
168
233
|
try {
|
|
169
234
|
const oldValue = data.expiredValues.get(selector);
|
|
170
235
|
const newValue = initSelector(selector, data);
|
|
171
|
-
if (
|
|
236
|
+
if (selector.equal(newValue, oldValue))
|
|
172
237
|
return;
|
|
173
238
|
} catch (e) {
|
|
174
239
|
}
|
|
@@ -178,316 +243,49 @@ var updateSelectorSubscribers = (selector, data) => {
|
|
|
178
243
|
subscribtion.callback();
|
|
179
244
|
}
|
|
180
245
|
}
|
|
181
|
-
if (familySubscriptions?.size) {
|
|
182
|
-
for (const subscribtion of familySubscriptions) {
|
|
183
|
-
subscribtion.callback(selector.familyKey);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
// src/lib/propagateUpdatedAtoms.ts
|
|
189
|
-
var recursivlyResetSelectorTree = (selectors, data, clearedSelectors) => {
|
|
190
|
-
for (const selector of selectors) {
|
|
191
|
-
if (!clearedSelectors.has(selector)) {
|
|
192
|
-
clearedSelectors.add(selector);
|
|
193
|
-
data.expiredValues.set(selector, data.values.get(selector));
|
|
194
|
-
data.values.delete(selector);
|
|
195
|
-
const consumers = data.stateConsumers.get(selector);
|
|
196
|
-
if (consumers?.size) {
|
|
197
|
-
recursivlyResetSelectorTree(consumers, data, clearedSelectors);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
var propagateUpdatedAtoms = (atoms, data) => {
|
|
203
|
-
const clearedSelectors = new Set;
|
|
204
|
-
for (const atom of atoms) {
|
|
205
|
-
const consumers = data.stateConsumers.get(atom);
|
|
206
|
-
if (consumers && consumers.size) {
|
|
207
|
-
recursivlyResetSelectorTree(consumers, data, clearedSelectors);
|
|
208
|
-
}
|
|
209
|
-
if (isFamilyAtom(atom)) {
|
|
210
|
-
const consumersFamily = data.stateConsumers.get(atom.family);
|
|
211
|
-
if (consumersFamily?.size) {
|
|
212
|
-
recursivlyResetSelectorTree(consumersFamily, data, clearedSelectors);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
for (const selector of clearedSelectors) {
|
|
217
|
-
updateSelectorSubscribers(selector, data);
|
|
218
|
-
}
|
|
219
|
-
for (const atom of atoms) {
|
|
220
|
-
updateStateSubscribers(atom, data);
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// src/lib/setAtom.ts
|
|
225
|
-
import equal4 from "fast-deep-equal/es6";
|
|
226
|
-
var setAtom = (atom, newValue, data, skipOnSet = false) => {
|
|
227
|
-
const currentValue = getState(atom, data);
|
|
228
|
-
if (typeof newValue === "function") {
|
|
229
|
-
newValue = newValue(currentValue);
|
|
230
|
-
if (isPromiseLike(newValue) || isPromiseLike(currentValue))
|
|
231
|
-
throw new Error("Todo, how should we handle this?");
|
|
232
|
-
}
|
|
233
|
-
if (equal4(currentValue, newValue))
|
|
234
|
-
return;
|
|
235
|
-
data.values.set(atom, newValue);
|
|
236
|
-
if (atom.onSet && !skipOnSet)
|
|
237
|
-
atom.onSet(newValue, data);
|
|
238
|
-
if (currentValue?.__isEmptyAtomPromise__) {
|
|
239
|
-
currentValue.__resolveEmptyAtomPromise__(newValue);
|
|
240
|
-
}
|
|
241
|
-
propagateUpdatedAtoms([atom], data);
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
// src/lib/initAtom.ts
|
|
245
|
-
var getAtomInitValue = (atom, data) => {
|
|
246
|
-
if (atom.defaultValue === undefined) {
|
|
247
|
-
let promiseResolve;
|
|
248
|
-
const promise = new Promise((resolve) => {
|
|
249
|
-
promiseResolve = resolve;
|
|
250
|
-
});
|
|
251
|
-
promise.__isEmptyAtomPromise__ = true;
|
|
252
|
-
promise.__resolveEmptyAtomPromise__ = promiseResolve;
|
|
253
|
-
return promise;
|
|
254
|
-
} else if (typeof atom.defaultValue === "function") {
|
|
255
|
-
const value = atom.defaultValue();
|
|
256
|
-
if (isPromiseLike(value)) {
|
|
257
|
-
value.then((resolvedValue) => {
|
|
258
|
-
data.values.set(atom, resolvedValue);
|
|
259
|
-
propagateUpdatedAtoms([atom], data);
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
return value;
|
|
263
|
-
} else if (isSelector(atom.defaultValue)) {
|
|
264
|
-
return getState(atom.defaultValue, data);
|
|
265
|
-
} else {
|
|
266
|
-
return atom.defaultValue;
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
var initAtom = (atom, data) => {
|
|
270
|
-
let value = getAtomInitValue(atom, data);
|
|
271
|
-
data.values.set(atom, value);
|
|
272
|
-
if (isFamilyAtom(atom)) {
|
|
273
|
-
const currentKeySet = getState(atom.family.__keysAtom, data);
|
|
274
|
-
if (!currentKeySet.has(atom.familyKey)) {
|
|
275
|
-
const newSet = new Set(currentKeySet);
|
|
276
|
-
newSet.add(atom.familyKey);
|
|
277
|
-
setAtom(atom.family.__keysAtom, newSet, data);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
if (atom.onInit)
|
|
281
|
-
atom.onInit((newVal) => {
|
|
282
|
-
value = newVal;
|
|
283
|
-
setAtom(atom, newVal, data, true);
|
|
284
|
-
}, data);
|
|
285
|
-
return value;
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
// src/lib/globalAtom.ts
|
|
289
|
-
var getFirstItemInSet = (set) => {
|
|
290
|
-
for (let item of set) {
|
|
291
|
-
return item;
|
|
292
|
-
}
|
|
293
|
-
throw new Error("Non Empty Set");
|
|
294
|
-
};
|
|
295
|
-
var globalAtom = (defaultValue, options) => {
|
|
296
|
-
const stores = new Set;
|
|
297
|
-
let value = defaultValue;
|
|
298
|
-
let initialized = false;
|
|
299
|
-
let onReset;
|
|
300
|
-
if (options.onSet)
|
|
301
|
-
throw new Error("onSet on globalAtom is currently not supported");
|
|
302
|
-
const onInit = (setSelf2, data) => {
|
|
303
|
-
if (!initialized) {
|
|
304
|
-
if (value === defaultValue) {
|
|
305
|
-
value = getAtomInitValue(atom, data);
|
|
306
|
-
}
|
|
307
|
-
setSelf2(value);
|
|
308
|
-
initialized = true;
|
|
309
|
-
if (options.onInit) {
|
|
310
|
-
onReset = options.onInit((newVal) => {
|
|
311
|
-
setSelf2(newVal);
|
|
312
|
-
value = newVal;
|
|
313
|
-
}, data);
|
|
314
|
-
}
|
|
315
|
-
} else {
|
|
316
|
-
setSelf2(value);
|
|
317
|
-
}
|
|
318
|
-
stores.add(data);
|
|
319
|
-
};
|
|
320
|
-
const onSet = (newValue, currentStore) => {
|
|
321
|
-
value = newValue;
|
|
322
|
-
if (stores.size > 1) {
|
|
323
|
-
for (const store of stores) {
|
|
324
|
-
if (store.id !== currentStore.id) {
|
|
325
|
-
setAtom(atom, value, store, true);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
const setSelf = (newValue) => {
|
|
331
|
-
value = newValue;
|
|
332
|
-
if (stores.size > 0) {
|
|
333
|
-
getFirstItemInSet(stores);
|
|
334
|
-
const store = getFirstItemInSet(stores);
|
|
335
|
-
setAtom(atom, newValue, store);
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
const resetSelf = () => {
|
|
339
|
-
value = defaultValue;
|
|
340
|
-
initialized = false;
|
|
341
|
-
for (const store of stores) {
|
|
342
|
-
if (store.stateDependencies.has(atom)) {
|
|
343
|
-
throw new Error("TODO: Reset support for stateDependencies");
|
|
344
|
-
}
|
|
345
|
-
store.values.delete(atom);
|
|
346
|
-
store.expiredValues.delete(atom);
|
|
347
|
-
propagateUpdatedAtoms([atom], store);
|
|
348
|
-
stores.delete(store);
|
|
349
|
-
onReset?.();
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
const atom = {
|
|
353
|
-
...options,
|
|
354
|
-
defaultValue,
|
|
355
|
-
label: options?.label,
|
|
356
|
-
onInit,
|
|
357
|
-
onSet,
|
|
358
|
-
setSelf,
|
|
359
|
-
resetSelf,
|
|
360
|
-
get currentValue() {
|
|
361
|
-
return value;
|
|
362
|
-
}
|
|
363
|
-
};
|
|
364
|
-
return atom;
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
// src/atom.ts
|
|
368
|
-
function atom(defaultValue, options) {
|
|
369
|
-
if (!options)
|
|
370
|
-
return { defaultValue };
|
|
371
|
-
if (options.global) {
|
|
372
|
-
return globalAtom(defaultValue, options);
|
|
373
|
-
}
|
|
374
|
-
return {
|
|
375
|
-
defaultValue,
|
|
376
|
-
...options
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
// src/lib/atomFamilyAtom.ts
|
|
380
|
-
function atomFamilyAtom(defaultValue, options) {
|
|
381
|
-
if (options.global) {
|
|
382
|
-
return globalAtom(defaultValue, options);
|
|
383
|
-
}
|
|
384
|
-
return {
|
|
385
|
-
...options,
|
|
386
|
-
defaultValue
|
|
387
|
-
};
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// src/lib/stableStringify.ts
|
|
391
|
-
var stableStringifyRecurse = (x, key) => {
|
|
392
|
-
if (typeof x === "string" && !x.includes('"') && !x.includes("\\")) {
|
|
393
|
-
return `"${x}"`;
|
|
394
|
-
}
|
|
395
|
-
switch (typeof x) {
|
|
396
|
-
case "undefined":
|
|
397
|
-
return "";
|
|
398
|
-
case "boolean":
|
|
399
|
-
return x ? "true" : "false";
|
|
400
|
-
case "number":
|
|
401
|
-
case "symbol":
|
|
402
|
-
return String(x);
|
|
403
|
-
case "string":
|
|
404
|
-
return JSON.stringify(x);
|
|
405
|
-
case "function":
|
|
406
|
-
return `__FUNCTION(${x.toString()})__`;
|
|
407
|
-
}
|
|
408
|
-
if (x === null) {
|
|
409
|
-
return "null";
|
|
410
|
-
}
|
|
411
|
-
if (typeof x !== "object") {
|
|
412
|
-
return JSON.stringify(x) ?? "";
|
|
413
|
-
}
|
|
414
|
-
if (isPromiseLike(x)) {
|
|
415
|
-
return "__PROMISE__";
|
|
416
|
-
}
|
|
417
|
-
if (Array.isArray(x)) {
|
|
418
|
-
return `[${x.map((v, i) => stableStringifyRecurse(v, i.toString()))}]`;
|
|
419
|
-
}
|
|
420
|
-
if (typeof x.toJSON === "function") {
|
|
421
|
-
return stableStringifyRecurse(x.toJSON(key), key);
|
|
422
|
-
}
|
|
423
|
-
if (x instanceof Map) {
|
|
424
|
-
const obj = {};
|
|
425
|
-
for (const [k, v] of x) {
|
|
426
|
-
obj[typeof k === "string" ? k : stringify(k, opt)] = v;
|
|
427
|
-
}
|
|
428
|
-
return stableStringifyRecurse(obj, key);
|
|
429
|
-
}
|
|
430
|
-
if (x instanceof Set) {
|
|
431
|
-
return stableStringifyRecurse(Array.from(x).sort((a, b) => stableStringifyRecurse(a).localeCompare(stableStringifyRecurse(b))), key);
|
|
432
|
-
}
|
|
433
|
-
if (Symbol !== undefined && x[Symbol.iterator] != null && typeof x[Symbol.iterator] === "function") {
|
|
434
|
-
return stableStringifyRecurse(Array.from(x), key);
|
|
246
|
+
if (familySubscriptions?.size) {
|
|
247
|
+
for (const subscribtion of familySubscriptions) {
|
|
248
|
+
subscribtion.callback(selector.familyKey);
|
|
249
|
+
}
|
|
435
250
|
}
|
|
436
|
-
return `{${Object.keys(x).filter((k) => x[k] !== undefined).sort().map((k) => `${stableStringifyRecurse(k)}:${stableStringifyRecurse(x[k], k)}`).join(",")}}`;
|
|
437
|
-
};
|
|
438
|
-
var stableStringify = (x) => {
|
|
439
|
-
if (typeof x === "string" || typeof x === "boolean" || typeof x === "number")
|
|
440
|
-
return x;
|
|
441
|
-
return stableStringifyRecurse(x);
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
// src/selector.ts
|
|
445
|
-
var selector = (get, options) => {
|
|
446
|
-
if (!options)
|
|
447
|
-
return { get };
|
|
448
|
-
return { ...options, get };
|
|
449
251
|
};
|
|
450
252
|
|
|
451
|
-
// src/
|
|
452
|
-
var
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
253
|
+
// src/lib/propagateUpdatedAtoms.ts
|
|
254
|
+
var recursivlyResetSelectorTree = (selectors, data, clearedSelectors) => {
|
|
255
|
+
for (const selector of selectors) {
|
|
256
|
+
if (!clearedSelectors.has(selector)) {
|
|
257
|
+
clearedSelectors.add(selector);
|
|
258
|
+
data.expiredValues.set(selector, data.values.get(selector));
|
|
259
|
+
data.values.delete(selector);
|
|
260
|
+
const consumers = data.stateConsumers.get(selector);
|
|
261
|
+
if (consumers?.size) {
|
|
262
|
+
recursivlyResetSelectorTree(consumers, data, clearedSelectors);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
462
265
|
}
|
|
463
266
|
};
|
|
464
|
-
var
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
};
|
|
471
|
-
function atomFamily(defaultValue, options) {
|
|
472
|
-
const map = new Map;
|
|
473
|
-
const atomFamily2 = (key) => {
|
|
474
|
-
const keyStringified = stableStringify(key);
|
|
475
|
-
if (map.has(keyStringified)) {
|
|
476
|
-
return map.get(keyStringified);
|
|
267
|
+
var propagateUpdatedAtoms = (atoms, data) => {
|
|
268
|
+
const clearedSelectors = new Set;
|
|
269
|
+
for (const atom of atoms) {
|
|
270
|
+
const consumers = data.stateConsumers.get(atom);
|
|
271
|
+
if (consumers && consumers.size) {
|
|
272
|
+
recursivlyResetSelectorTree(consumers, data, clearedSelectors);
|
|
477
273
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
}
|
|
274
|
+
if (isFamilyAtom(atom)) {
|
|
275
|
+
const consumersFamily = data.stateConsumers.get(atom.family);
|
|
276
|
+
if (consumersFamily?.size) {
|
|
277
|
+
recursivlyResetSelectorTree(consumersFamily, data, clearedSelectors);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
for (const selector of clearedSelectors) {
|
|
282
|
+
updateSelectorSubscribers(selector, data);
|
|
283
|
+
}
|
|
284
|
+
for (const atom of atoms) {
|
|
285
|
+
updateStateSubscribers(atom, data);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
491
289
|
// src/lib/createStoreData.ts
|
|
492
290
|
var generateId = () => (Math.random() + 1).toString(36).substring(7);
|
|
493
291
|
var generateStoreData = (id = generateId()) => {
|
|
@@ -515,11 +313,11 @@ function createStoreData(id, parent) {
|
|
|
515
313
|
}
|
|
516
314
|
|
|
517
315
|
// src/lib/resetAtom.ts
|
|
518
|
-
var resetAtom = (
|
|
519
|
-
let value = getAtomInitValue(
|
|
520
|
-
data.values.set(
|
|
316
|
+
var resetAtom = (atom, data) => {
|
|
317
|
+
let value = getAtomInitValue(atom, data);
|
|
318
|
+
data.values.set(atom, value);
|
|
521
319
|
if (!isPromiseLike(value)) {
|
|
522
|
-
propagateUpdatedAtoms([
|
|
320
|
+
propagateUpdatedAtoms([atom], data);
|
|
523
321
|
}
|
|
524
322
|
return value;
|
|
525
323
|
};
|
|
@@ -566,18 +364,24 @@ var initSubscribers = (state, data) => {
|
|
|
566
364
|
return set;
|
|
567
365
|
};
|
|
568
366
|
var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) => {
|
|
367
|
+
let parentUnsubscribe;
|
|
569
368
|
if ("parent" in data && !data.values.has(state) && isAtom(state)) {
|
|
570
|
-
const
|
|
571
|
-
|
|
369
|
+
const originalCallback = callback;
|
|
370
|
+
parentUnsubscribe = subscribe(state, originalCallback, requireDeepEqualCheckBeforeCallback, data.parent);
|
|
572
371
|
callback = () => {
|
|
573
|
-
parentUnsubscribe
|
|
372
|
+
if (parentUnsubscribe) {
|
|
373
|
+
parentUnsubscribe();
|
|
374
|
+
parentUnsubscribe = undefined;
|
|
375
|
+
}
|
|
574
376
|
originalCallback();
|
|
575
377
|
};
|
|
378
|
+
} else if (!data.values.has(state) && isAtom(state)) {
|
|
379
|
+
initAtom(state, data);
|
|
576
380
|
}
|
|
577
|
-
const subscribers = data.subscriptions.get(state) || initSubscribers(state, data);
|
|
578
381
|
if (isSelector(state) && !data.values.has(state)) {
|
|
579
382
|
initSelector(state, data);
|
|
580
383
|
}
|
|
384
|
+
const subscribers = data.subscriptions.get(state) || initSubscribers(state, data);
|
|
581
385
|
let subscription;
|
|
582
386
|
if (isFamily(state)) {
|
|
583
387
|
subscription = {
|
|
@@ -636,20 +440,24 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
|
|
|
636
440
|
if (requireDeepEqualCheckBeforeCallback && data.subscriptionsRequireEqualCheck.get(state) !== true) {
|
|
637
441
|
data.subscriptionsRequireEqualCheck.set(state, true);
|
|
638
442
|
}
|
|
639
|
-
return () =>
|
|
443
|
+
return () => {
|
|
444
|
+
if (parentUnsubscribe) {
|
|
445
|
+
parentUnsubscribe();
|
|
446
|
+
}
|
|
447
|
+
unsubscribe(state, subscription, data, mount, maxAgeCleanup);
|
|
448
|
+
};
|
|
640
449
|
};
|
|
641
450
|
|
|
642
451
|
// src/lib/setAtoms.ts
|
|
643
|
-
import equal5 from "fast-deep-equal/es6";
|
|
644
452
|
var setAtoms = (pairs, data) => {
|
|
645
453
|
const updatedAtoms = [];
|
|
646
|
-
for (let [
|
|
647
|
-
const currentValue = getState(
|
|
648
|
-
if (!
|
|
649
|
-
updatedAtoms.push(
|
|
650
|
-
if (
|
|
651
|
-
|
|
652
|
-
data.values.set(
|
|
454
|
+
for (let [atom, value] of pairs) {
|
|
455
|
+
const currentValue = getState(atom, data);
|
|
456
|
+
if (!atom.equal(currentValue, value)) {
|
|
457
|
+
updatedAtoms.push(atom);
|
|
458
|
+
if (atom.onSet)
|
|
459
|
+
atom.onSet(value, data);
|
|
460
|
+
data.values.set(atom, value);
|
|
653
461
|
}
|
|
654
462
|
}
|
|
655
463
|
propagateUpdatedAtoms(updatedAtoms, data);
|
|
@@ -705,25 +513,25 @@ var transaction = (callback, data) => {
|
|
|
705
513
|
throw new Error("Unsupported state");
|
|
706
514
|
}
|
|
707
515
|
};
|
|
708
|
-
const txnSet = (
|
|
709
|
-
if (!isAtom(
|
|
516
|
+
const txnSet = (atom, value) => {
|
|
517
|
+
if (!isAtom(atom))
|
|
710
518
|
throw new Error("Not an atom");
|
|
711
519
|
if (typeof value === "function") {
|
|
712
|
-
const currentValue = txnGet(
|
|
520
|
+
const currentValue = txnGet(atom);
|
|
713
521
|
value = value(currentValue);
|
|
714
522
|
}
|
|
715
|
-
for (const
|
|
716
|
-
dirtySelectors.add(
|
|
717
|
-
txnSelectorCache.delete(
|
|
523
|
+
for (const selector of findDependencies(atom, data)) {
|
|
524
|
+
dirtySelectors.add(selector);
|
|
525
|
+
txnSelectorCache.delete(selector);
|
|
718
526
|
}
|
|
719
|
-
if (txnSubscribers.get(
|
|
720
|
-
recursivlyResetTxnSelectorCache(
|
|
527
|
+
if (txnSubscribers.get(atom)?.size) {
|
|
528
|
+
recursivlyResetTxnSelectorCache(atom, txnSubscribers, txnSelectorCache);
|
|
721
529
|
}
|
|
722
|
-
txnAtomMap.set(
|
|
530
|
+
txnAtomMap.set(atom, value);
|
|
723
531
|
};
|
|
724
|
-
const txnReset = (
|
|
725
|
-
const value = getAtomInitValue(
|
|
726
|
-
txnAtomMap.set(
|
|
532
|
+
const txnReset = (atom) => {
|
|
533
|
+
const value = getAtomInitValue(atom, data);
|
|
534
|
+
txnAtomMap.set(atom, value);
|
|
727
535
|
return value;
|
|
728
536
|
};
|
|
729
537
|
const commit = () => {
|
|
@@ -752,7 +560,7 @@ function storeFromStoreData(data, detach) {
|
|
|
752
560
|
throw new Error(SelectorProvidedToSetError);
|
|
753
561
|
throw new Error(InvalidStateSetError);
|
|
754
562
|
};
|
|
755
|
-
const reset = (
|
|
563
|
+
const reset = (atom) => resetAtom(atom, data);
|
|
756
564
|
const sub = (state, callback, deepEqualCheckBeforeCallback = true) => subscribe(state, callback, deepEqualCheckBeforeCallback, data);
|
|
757
565
|
const txn = (callback) => transaction(callback, data);
|
|
758
566
|
const scope = (scopeId) => {
|
|
@@ -797,32 +605,268 @@ function storeFromStoreData(data, detach) {
|
|
|
797
605
|
}
|
|
798
606
|
}
|
|
799
607
|
|
|
608
|
+
// src/store.ts
|
|
609
|
+
var store = (id) => {
|
|
610
|
+
const data = createStoreData(id);
|
|
611
|
+
return storeFromStoreData(data);
|
|
612
|
+
};
|
|
613
|
+
// package.json
|
|
614
|
+
var version = "0.2.0-alpha.32";
|
|
615
|
+
|
|
616
|
+
// src/globalStore.ts
|
|
617
|
+
if (globalThis.__valdres__) {
|
|
618
|
+
throw new Error(`Error! An instance of valdres is already loaded`);
|
|
619
|
+
} else {
|
|
620
|
+
globalThis.__valdres__ = version;
|
|
621
|
+
}
|
|
622
|
+
var globalStore = Object.assign(store("valdres-global-store"), {
|
|
623
|
+
atoms: new Map,
|
|
624
|
+
atomFamilies: new Map
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
// src/lib/globalAtom.ts
|
|
628
|
+
var globalAtom = (defaultValue, options) => {
|
|
629
|
+
const stores = new Set;
|
|
630
|
+
let value;
|
|
631
|
+
let initialized = false;
|
|
632
|
+
let onReset;
|
|
633
|
+
if (options.onSet)
|
|
634
|
+
throw new Error("onSet on globalAtom is currently not supported");
|
|
635
|
+
const onInit = (setSelf2, data) => {
|
|
636
|
+
setSelf2(globalStore.get(atom));
|
|
637
|
+
if (!initialized && options.onInit) {
|
|
638
|
+
onReset = options.onInit((newVal) => {
|
|
639
|
+
setSelf2(newVal);
|
|
640
|
+
value = newVal;
|
|
641
|
+
}, data);
|
|
642
|
+
initialized = true;
|
|
643
|
+
}
|
|
644
|
+
stores.add(data);
|
|
645
|
+
};
|
|
646
|
+
const onSet = (newValue, currentStore) => {
|
|
647
|
+
value = newValue;
|
|
648
|
+
if (stores.size > 1) {
|
|
649
|
+
for (const store2 of stores) {
|
|
650
|
+
if (store2.id !== currentStore.id) {
|
|
651
|
+
setAtom(atom, value, store2, true);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
const getSelf = () => globalStore.get(atom);
|
|
657
|
+
const setSelf = (newValue) => globalStore.set(atom, newValue);
|
|
658
|
+
const resetSelf = () => {
|
|
659
|
+
value = undefined;
|
|
660
|
+
initialized = false;
|
|
661
|
+
for (const store2 of stores) {
|
|
662
|
+
if (store2.stateDependencies.has(atom)) {
|
|
663
|
+
throw new Error("TODO: Reset support for stateDependencies");
|
|
664
|
+
}
|
|
665
|
+
store2.values.delete(atom);
|
|
666
|
+
store2.expiredValues.delete(atom);
|
|
667
|
+
propagateUpdatedAtoms([atom], store2);
|
|
668
|
+
stores.delete(store2);
|
|
669
|
+
onReset?.();
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
const atom = {
|
|
673
|
+
equal: equal2,
|
|
674
|
+
...options,
|
|
675
|
+
defaultValue,
|
|
676
|
+
name: options?.name,
|
|
677
|
+
onInit,
|
|
678
|
+
onSet,
|
|
679
|
+
setSelf,
|
|
680
|
+
getSelf,
|
|
681
|
+
resetSelf,
|
|
682
|
+
get stores() {
|
|
683
|
+
return stores;
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
return atom;
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
// src/atom.ts
|
|
690
|
+
function atom(defaultValue, options) {
|
|
691
|
+
if (!options)
|
|
692
|
+
return { equal: equal3, defaultValue };
|
|
693
|
+
if (options.global) {
|
|
694
|
+
return globalAtom(defaultValue, options);
|
|
695
|
+
}
|
|
696
|
+
return {
|
|
697
|
+
equal: equal3,
|
|
698
|
+
defaultValue,
|
|
699
|
+
...options
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
// src/lib/createAtomFamily.ts
|
|
703
|
+
import equal5 from "fast-deep-equal/es6";
|
|
704
|
+
|
|
705
|
+
// src/lib/atomFamilyAtom.ts
|
|
706
|
+
function atomFamilyAtom(defaultValue, options) {
|
|
707
|
+
if (options.global) {
|
|
708
|
+
return globalAtom(defaultValue, options);
|
|
709
|
+
}
|
|
710
|
+
return {
|
|
711
|
+
...options,
|
|
712
|
+
defaultValue
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// src/lib/stableStringify.ts
|
|
717
|
+
var stableStringifyRecurse = (x, key) => {
|
|
718
|
+
if (typeof x === "string" && !x.includes('"') && !x.includes("\\")) {
|
|
719
|
+
return `"${x}"`;
|
|
720
|
+
}
|
|
721
|
+
switch (typeof x) {
|
|
722
|
+
case "undefined":
|
|
723
|
+
return "";
|
|
724
|
+
case "boolean":
|
|
725
|
+
return x ? "true" : "false";
|
|
726
|
+
case "number":
|
|
727
|
+
case "symbol":
|
|
728
|
+
return String(x);
|
|
729
|
+
case "string":
|
|
730
|
+
return JSON.stringify(x);
|
|
731
|
+
case "function":
|
|
732
|
+
return `__FUNCTION(${x.toString()})__`;
|
|
733
|
+
}
|
|
734
|
+
if (x === null) {
|
|
735
|
+
return "null";
|
|
736
|
+
}
|
|
737
|
+
if (typeof x !== "object") {
|
|
738
|
+
return JSON.stringify(x) ?? "";
|
|
739
|
+
}
|
|
740
|
+
if (isPromiseLike(x)) {
|
|
741
|
+
return "__PROMISE__";
|
|
742
|
+
}
|
|
743
|
+
if (Array.isArray(x)) {
|
|
744
|
+
return `[${x.map((v, i) => stableStringifyRecurse(v, i.toString()))}]`;
|
|
745
|
+
}
|
|
746
|
+
if (typeof x.toJSON === "function") {
|
|
747
|
+
return stableStringifyRecurse(x.toJSON(key), key);
|
|
748
|
+
}
|
|
749
|
+
if (x instanceof Map) {
|
|
750
|
+
const obj = {};
|
|
751
|
+
for (const [k, v] of x) {
|
|
752
|
+
obj[typeof k === "string" ? k : stringify(k, opt)] = v;
|
|
753
|
+
}
|
|
754
|
+
return stableStringifyRecurse(obj, key);
|
|
755
|
+
}
|
|
756
|
+
if (x instanceof Set) {
|
|
757
|
+
return stableStringifyRecurse(Array.from(x).sort((a, b) => stableStringifyRecurse(a).localeCompare(stableStringifyRecurse(b))), key);
|
|
758
|
+
}
|
|
759
|
+
if (Symbol !== undefined && x[Symbol.iterator] != null && typeof x[Symbol.iterator] === "function") {
|
|
760
|
+
return stableStringifyRecurse(Array.from(x), key);
|
|
761
|
+
}
|
|
762
|
+
return `{${Object.keys(x).filter((k) => x[k] !== undefined).sort().map((k) => `${stableStringifyRecurse(k)}:${stableStringifyRecurse(x[k], k)}`).join(",")}}`;
|
|
763
|
+
};
|
|
764
|
+
var stableStringify = (x) => {
|
|
765
|
+
if (typeof x === "string" || typeof x === "boolean" || typeof x === "number")
|
|
766
|
+
return x;
|
|
767
|
+
return stableStringifyRecurse(x);
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
// src/selector.ts
|
|
771
|
+
import equal4 from "fast-deep-equal/es6";
|
|
772
|
+
var selector = (get, options) => {
|
|
773
|
+
if (!options)
|
|
774
|
+
return { equal: equal4, get };
|
|
775
|
+
return { equal: equal4, ...options, get };
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// src/lib/createAtomFamily.ts
|
|
779
|
+
var createOptions = (options = {}, family, familyKey, keyStringified) => {
|
|
780
|
+
if (options.name) {
|
|
781
|
+
return {
|
|
782
|
+
equal: equal5,
|
|
783
|
+
...options,
|
|
784
|
+
name: options?.name + "_" + keyStringified,
|
|
785
|
+
family,
|
|
786
|
+
familyKey
|
|
787
|
+
};
|
|
788
|
+
} else {
|
|
789
|
+
return { equal: equal5, ...options, family, familyKey };
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
var handleDefaultValue = (defaultValue, key) => {
|
|
793
|
+
if (isSelectorFamily(defaultValue))
|
|
794
|
+
return defaultValue(key);
|
|
795
|
+
if (typeof defaultValue === "function")
|
|
796
|
+
return () => defaultValue(key);
|
|
797
|
+
return defaultValue;
|
|
798
|
+
};
|
|
799
|
+
var createAtomFamily = (defaultValue, options) => {
|
|
800
|
+
const map = new Map;
|
|
801
|
+
const keysAtom = atom(new Set);
|
|
802
|
+
const atomFamily = Object.assign((key) => {
|
|
803
|
+
const keyStringified = stableStringify(key);
|
|
804
|
+
if (map.has(keyStringified)) {
|
|
805
|
+
return map.get(keyStringified);
|
|
806
|
+
}
|
|
807
|
+
const familyAtom = atomFamilyAtom(handleDefaultValue(defaultValue, key), createOptions(options, atomFamily, Object.freeze(key), keyStringified));
|
|
808
|
+
map.set(keyStringified, familyAtom);
|
|
809
|
+
return familyAtom;
|
|
810
|
+
}, {
|
|
811
|
+
__valdresAtomFamilyMap: map,
|
|
812
|
+
release: (key) => map.delete(key),
|
|
813
|
+
__keysAtom: keysAtom,
|
|
814
|
+
__keysSelector: selector((get) => Array.from(get(keysAtom)))
|
|
815
|
+
});
|
|
816
|
+
if (options?.name)
|
|
817
|
+
Object.defineProperty(atomFamily, "name", {
|
|
818
|
+
value: options.name,
|
|
819
|
+
writable: false
|
|
820
|
+
});
|
|
821
|
+
return atomFamily;
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
// src/lib/createGlobalAtomFamily.ts
|
|
825
|
+
var createGlobalAtomFamily = (defaultValue, options) => {
|
|
826
|
+
if (globalStore.atomFamilies.has(options.name)) {
|
|
827
|
+
return globalStore.atomFamilies.get(options.name);
|
|
828
|
+
}
|
|
829
|
+
const family = createAtomFamily(defaultValue, options);
|
|
830
|
+
globalStore.atomFamilies.set(options.name, family);
|
|
831
|
+
return family;
|
|
832
|
+
};
|
|
833
|
+
|
|
834
|
+
// src/atomFamily.ts
|
|
835
|
+
function atomFamily(defaultValue, options) {
|
|
836
|
+
if (options?.global)
|
|
837
|
+
return createGlobalAtomFamily(defaultValue, options);
|
|
838
|
+
return createAtomFamily(defaultValue, options);
|
|
839
|
+
}
|
|
800
840
|
// src/createStoreWithSelectorSet.ts
|
|
801
|
-
var setSelector = (selector2, values,
|
|
802
|
-
return selector2.set(
|
|
841
|
+
var setSelector = (selector2, values, store2) => {
|
|
842
|
+
return selector2.set(store2.set, store2.get, store2.reset, ...values);
|
|
803
843
|
};
|
|
804
844
|
var createStoreWithSelectorSet = (id) => {
|
|
805
845
|
const data = createStoreData(id);
|
|
806
|
-
const
|
|
807
|
-
|
|
846
|
+
const store2 = storeFromStoreData(data);
|
|
847
|
+
store2.set = (state, value, ...rest) => {
|
|
808
848
|
if (isAtom(state))
|
|
809
849
|
return setAtom(state, value, data);
|
|
810
850
|
if (isSelector(state))
|
|
811
|
-
return setSelector(state, [value, ...rest],
|
|
851
|
+
return setSelector(state, [value, ...rest], store2);
|
|
812
852
|
throw new Error("Invalid state object");
|
|
813
853
|
};
|
|
814
|
-
|
|
815
|
-
return
|
|
854
|
+
store2.kind = "storeWithSelectorSet";
|
|
855
|
+
return store2;
|
|
816
856
|
};
|
|
817
857
|
// src/selectorFamily.ts
|
|
818
|
-
|
|
819
|
-
|
|
858
|
+
import equal6 from "fast-deep-equal/es6";
|
|
859
|
+
var createOptions2 = (options = {}, family, familyKey, keyStringified) => {
|
|
860
|
+
if (options.name) {
|
|
820
861
|
return {
|
|
862
|
+
equal: equal6,
|
|
821
863
|
...options,
|
|
822
|
-
|
|
864
|
+
name: options?.name + "_" + keyStringified,
|
|
865
|
+
family,
|
|
866
|
+
familyKey
|
|
823
867
|
};
|
|
824
868
|
} else {
|
|
825
|
-
return options;
|
|
869
|
+
return { equal: equal6, ...options, family, familyKey };
|
|
826
870
|
}
|
|
827
871
|
};
|
|
828
872
|
var selectorFamily = (get, options) => {
|
|
@@ -831,21 +875,18 @@ var selectorFamily = (get, options) => {
|
|
|
831
875
|
const keyStringified = stableStringify(key);
|
|
832
876
|
if (map.has(keyStringified))
|
|
833
877
|
return map.get(keyStringified);
|
|
834
|
-
const newSelector = selector((selectorArgs) => get(key)(selectorArgs),
|
|
835
|
-
newSelector.family = selectorFamily2;
|
|
878
|
+
const newSelector = selector((selectorArgs) => get(key)(selectorArgs), createOptions2(options, selectorFamily2, key, keyStringified));
|
|
836
879
|
map.set(keyStringified, newSelector);
|
|
837
880
|
return newSelector;
|
|
838
881
|
};
|
|
839
882
|
selectorFamily2.__valdresSelectorFamilyMap = map;
|
|
840
|
-
if (options?.
|
|
841
|
-
selectorFamily2
|
|
883
|
+
if (options?.name)
|
|
884
|
+
Object.defineProperty(selectorFamily2, "name", {
|
|
885
|
+
value: options.name,
|
|
886
|
+
writable: false
|
|
887
|
+
});
|
|
842
888
|
return selectorFamily2;
|
|
843
889
|
};
|
|
844
|
-
// src/store.ts
|
|
845
|
-
var store = (id) => {
|
|
846
|
-
const data = createStoreData(id);
|
|
847
|
-
return storeFromStoreData(data);
|
|
848
|
-
};
|
|
849
890
|
// src/utils/isFamilySelector.ts
|
|
850
891
|
var isFamilySelector = (state) => isFamilyState(state) && isSelector(state);
|
|
851
892
|
export {
|
package/dist/types/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export { isSelector } from "./src/utils/isSelector";
|
|
|
15
15
|
export { isSelectorFamily } from "./src/utils/isSelectorFamily";
|
|
16
16
|
export type { Atom } from "./src/types/Atom";
|
|
17
17
|
export type { AtomFamily } from "./src/types/AtomFamily";
|
|
18
|
+
export type { FamilyKey } from "./src/types/FamilyKey";
|
|
18
19
|
export type { GetValue } from "./src/types/GetValue";
|
|
19
20
|
export type { GlobalAtom } from "./src/types/GlobalAtom";
|
|
20
21
|
export type { ResetAtom } from "./src/types/ResetAtom";
|
package/dist/types/src/atom.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { GlobalAtom } from "./types/GlobalAtom";
|
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
*
|
|
10
|
-
* const user = atom<string>("Default Value", {
|
|
10
|
+
* const user = atom<string>("Default Value", { name: "userAtom"})
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
13
13
|
export declare function atom<V>(defaultValue: AtomDefaultValue<V>, options: AtomOptions<V> & {
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import type { AtomFamily } from "./types/AtomFamily";
|
|
2
1
|
import type { AtomOptions } from "./types/AtomOptions";
|
|
3
2
|
import type { FamilyKey } from "./types/FamilyKey";
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
type DefaultValueCallback<Key, Value> = (arg: Key) => Value | Promise<Value>;
|
|
7
|
-
type AtomFamilyDefaultValue<Key, Value> = undefined | Value | DefaultValueCallback<Key, Value> | Selector<Value> | SelectorFamily<Key, Value>;
|
|
8
|
-
/** [Docs Reference](https://valdres.dev/valdres/api/atomFamily) */
|
|
9
|
-
export declare function atomFamily<Key = FamilyKey, Value = unknown>(defaultValue?: AtomFamilyDefaultValue<Key, Value>, options?: AtomOptions<Value>): AtomFamily<Key, Value>;
|
|
10
|
-
export {};
|
|
3
|
+
import type { AtomFamilyDefaultValue } from "./types/AtomFamilyDefaultValue";
|
|
4
|
+
export declare function atomFamily<Key = FamilyKey, Value = unknown>(defaultValue?: AtomFamilyDefaultValue<Key, Value>, options?: AtomOptions<Value>): import("..").AtomFamily<Key, Value>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AtomFamily } from "../types/AtomFamily";
|
|
2
|
+
import type { AtomOptions } from "../types/AtomOptions";
|
|
3
|
+
import type { AtomFamilyDefaultValue } from "../types/AtomFamilyDefaultValue";
|
|
4
|
+
export declare const createAtomFamily: <Key, Value>(defaultValue: AtomFamilyDefaultValue<Key, Value>, options?: AtomOptions<Value>) => AtomFamily<Key, Value>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AtomOptions } from "../types/AtomOptions";
|
|
2
|
+
import type { FamilyKey } from "../types/FamilyKey";
|
|
3
|
+
import type { AtomFamilyDefaultValue } from "../types/AtomFamilyDefaultValue";
|
|
4
|
+
import type { AtomFamily } from "../types/AtomFamily";
|
|
5
|
+
export declare const createGlobalAtomFamily: <Key = FamilyKey, Value = unknown>(defaultValue: AtomFamilyDefaultValue<Key, Value>, options: AtomOptions<Value>) => AtomFamily<Key, Value>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { AtomDefaultValue } from "../types/AtomDefaultValue";
|
|
2
2
|
import type { AtomOptions } from "./../types/AtomOptions";
|
|
3
3
|
import type { GlobalAtom } from "./../types/GlobalAtom";
|
|
4
|
-
export declare const globalAtom: <Value =
|
|
4
|
+
export declare const globalAtom: <Value = unknown>(defaultValue: AtomDefaultValue<Value>, options: AtomOptions<Value>) => GlobalAtom<Value>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Family } from "../types/Family";
|
|
2
2
|
import type { State } from "../types/State";
|
|
3
3
|
import type { StoreData } from "../types/StoreData";
|
|
4
|
-
export declare const subscribe: <V>(state: State<V> | Family<V>, callback:
|
|
4
|
+
export declare const subscribe: <V>(state: State<V> | Family<V>, callback: () => void, requireDeepEqualCheckBeforeCallback: boolean, data: StoreData) => () => void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { GetValue } from "./types/GetValue";
|
|
2
2
|
import type { Selector } from "./types/Selector";
|
|
3
3
|
import type { SelectorOptions } from "./types/SelectorOptions";
|
|
4
|
-
export declare const selector: <Value, FamilyKey = undefined>(get: (get: GetValue, storeId: string) => Value, options?: SelectorOptions) => Selector<Value, FamilyKey>;
|
|
4
|
+
export declare const selector: <Value, FamilyKey = undefined>(get: (get: GetValue, storeId: string) => Value, options?: SelectorOptions<Value>) => Selector<Value, FamilyKey>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { SelectorFamily } from "./types/SelectorFamily";
|
|
2
2
|
import type { SelectorOptions } from "./types/SelectorOptions";
|
|
3
|
-
|
|
3
|
+
import type { GetValue } from "./types/GetValue";
|
|
4
|
+
export declare const selectorFamily: <Key, Value>(get: (key: Key) => (get: GetValue) => Value, options?: SelectorOptions<Value>) => SelectorFamily<Key, Value>;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { AtomDefaultValue } from "./AtomDefaultValue";
|
|
2
2
|
import type { AtomOnInit } from "./AtomOnInit";
|
|
3
3
|
import type { AtomOnSet } from "./AtomOnSet";
|
|
4
|
+
import type { EqualFunc } from "./EqualFunc";
|
|
4
5
|
export type Atom<Value = unknown> = {
|
|
6
|
+
equal: EqualFunc<Value>;
|
|
5
7
|
defaultValue?: AtomDefaultValue<Value>;
|
|
6
|
-
|
|
8
|
+
name?: string;
|
|
7
9
|
onInit?: AtomOnInit<Value>;
|
|
8
10
|
onSet?: AtomOnSet<Value>;
|
|
9
11
|
onMount?: () => void | (() => void);
|
|
@@ -5,7 +5,7 @@ import type { Selector } from "./Selector";
|
|
|
5
5
|
export type AtomFamily<Key = FamilyKey, Value = unknown> = {
|
|
6
6
|
(key: Key): AtomFamilyAtom<Key, Value>;
|
|
7
7
|
release: (key: Key) => void;
|
|
8
|
-
|
|
8
|
+
name?: string;
|
|
9
9
|
__valdresAtomFamilyMap: Map<Key, AtomFamilyAtom<Key, Value>>;
|
|
10
10
|
__keysAtom: Atom<Set<Key>>;
|
|
11
11
|
__keysSelector: Selector<Key[]>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Selector } from "./Selector";
|
|
2
|
+
import type { SelectorFamily } from "./SelectorFamily";
|
|
3
|
+
type DefaultValueCallback<Key, Value> = (arg: Key) => Value | Promise<Value>;
|
|
4
|
+
export type AtomFamilyDefaultValue<Key, Value> = undefined | Value | DefaultValueCallback<Key, Value> | Selector<Value> | SelectorFamily<Key, Value>;
|
|
5
|
+
export {};
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { AtomOnSet } from "./AtomOnSet";
|
|
2
|
+
import type { EqualFunc } from "./EqualFunc";
|
|
2
3
|
import type { StoreData } from "./StoreData";
|
|
3
4
|
export type AtomOptions<Value = unknown> = {
|
|
4
5
|
global?: boolean;
|
|
5
|
-
|
|
6
|
+
name?: string;
|
|
6
7
|
onInit?: (setSelf: (value: Value) => void, store: StoreData) => (() => void) | void;
|
|
7
8
|
onSet?: AtomOnSet<Value>;
|
|
8
9
|
onMount?: () => () => void;
|
|
9
10
|
maxAge?: number;
|
|
10
11
|
staleWhileRevalidate?: number;
|
|
12
|
+
equal?: EqualFunc<Value>;
|
|
11
13
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type EqualFunc<Value> = (a: Value, b: Value) => boolean;
|
|
@@ -2,7 +2,7 @@ import type { Atom } from "./Atom";
|
|
|
2
2
|
import type { AtomFamily } from "./AtomFamily";
|
|
3
3
|
import type { Selector } from "./Selector";
|
|
4
4
|
export type GetValue = {
|
|
5
|
-
<V>(atom: Atom<V
|
|
6
|
-
<V>(selector: Selector<V
|
|
7
|
-
<V, K>(family: AtomFamily<K, V
|
|
5
|
+
<V>(atom: Atom<V>): V;
|
|
6
|
+
<V>(selector: Selector<V>): V;
|
|
7
|
+
<V, K>(family: AtomFamily<K, V>): K[];
|
|
8
8
|
};
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { Atom } from "./Atom";
|
|
2
|
-
import type {
|
|
2
|
+
import type { GlobalAtomGetSelfFunc } from "./GlobalAtomGetSelfFunc";
|
|
3
3
|
import type { GlobalAtomResetSelfFunc } from "./GlobalAtomResetSelfFunc";
|
|
4
|
+
import type { GlobalAtomSetSelfFunc } from "./GlobalAtomSetSelfFunc";
|
|
5
|
+
import type { StoreData } from "./StoreData";
|
|
4
6
|
export type GlobalAtom<Value = unknown> = Atom<Value> & {
|
|
5
7
|
setSelf: GlobalAtomSetSelfFunc<Value>;
|
|
6
8
|
resetSelf: GlobalAtomResetSelfFunc;
|
|
9
|
+
getSelf: GlobalAtomGetSelfFunc<Value>;
|
|
10
|
+
stores: Set<StoreData>;
|
|
7
11
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type GlobalAtomGetSelfFunc<Value = unknown> = () => Value;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { EqualFunc } from "./EqualFunc";
|
|
1
2
|
import type { GetValue } from "./GetValue";
|
|
2
3
|
import type { SelectorFamily } from "./SelectorFamily";
|
|
3
4
|
export type Selector<Value = unknown, FamilyKey = undefined> = {
|
|
4
5
|
get: (get: GetValue, storeId: string) => Value;
|
|
5
|
-
|
|
6
|
+
equal: EqualFunc<Value>;
|
|
7
|
+
name?: string;
|
|
6
8
|
family?: SelectorFamily<FamilyKey, Value>;
|
|
7
9
|
familyKey?: FamilyKey;
|
|
8
10
|
onMount?: () => void | (() => void);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valdres",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.32",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Eigil Sagafos"
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
"react": ">=18",
|
|
34
34
|
"react-dom": ">=18",
|
|
35
35
|
"recoil": "0.7.7",
|
|
36
|
-
"typescript": "5.6.
|
|
36
|
+
"typescript": "5.6.3"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public",
|
|
40
40
|
"registry": "https://registry.npmjs.org/"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "27342cf65a13e2ec86f5f400d814385d0c8de2cd"
|
|
43
43
|
}
|