valdres 0.2.0-alpha.20 → 0.2.0-alpha.21

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.
Files changed (69) hide show
  1. package/dist/index.js +226 -144
  2. package/dist/{index.d.ts → types/index.d.ts} +8 -2
  3. package/dist/{src → types/src}/atom.d.ts +1 -1
  4. package/dist/types/src/lib/atomFamilyAtom.d.ts +8 -0
  5. package/dist/types/src/lib/createStoreData.d.ts +3 -0
  6. package/dist/{src → types/src}/lib/initAtom.d.ts +2 -1
  7. package/dist/types/src/lib/storeFromStoreData.d.ts +4 -0
  8. package/dist/types/src/store.d.ts +1 -0
  9. package/dist/types/src/types/AtomFamily.d.ts +12 -0
  10. package/dist/{src → types/src}/types/AtomFamilyAtom.d.ts +2 -2
  11. package/dist/{src → types/src}/types/AtomFamilyGlobalAtom.d.ts +1 -1
  12. package/dist/{src → types/src}/types/Family.d.ts +1 -1
  13. package/dist/types/src/types/GetValue.d.ts +8 -0
  14. package/dist/{src → types/src}/types/SetAtom.d.ts +1 -1
  15. package/dist/{src → types/src}/types/Store.d.ts +8 -4
  16. package/dist/{src → types/src}/types/StoreData.d.ts +9 -1
  17. package/dist/types/src/utils/isFamily.d.ts +1 -0
  18. package/dist/types/src/utils/isFamilySelector.d.ts +2 -0
  19. package/dist/types/src/utils/isSelectorFamily.d.ts +2 -0
  20. package/package.json +6 -6
  21. package/dist/src/createStore.d.ts +0 -1
  22. package/dist/src/lib/atomFamilyAtom.d.ts +0 -8
  23. package/dist/src/lib/createStoreData.d.ts +0 -2
  24. package/dist/src/lib/storeFromStoreData.d.ts +0 -3
  25. package/dist/src/types/AtomFamily.d.ts +0 -8
  26. package/dist/src/types/GetValue.d.ts +0 -8
  27. package/dist/src/utils/isFamily.d.ts +0 -1
  28. package/dist/src/utils/isSelectorFamily.d.ts +0 -2
  29. package/dist/{src → types/src}/atomFamily.d.ts +0 -0
  30. package/dist/{src → types/src}/createStoreWithSelectorSet.d.ts +0 -0
  31. package/dist/{src → types/src}/lib/getState.d.ts +1 -1
  32. /package/dist/{src → types/src}/lib/globalAtom.d.ts +0 -0
  33. /package/dist/{src → types/src}/lib/initSelector.d.ts +0 -0
  34. /package/dist/{src → types/src}/lib/propagateUpdatedAtoms.d.ts +0 -0
  35. /package/dist/{src → types/src}/lib/resetAtom.d.ts +0 -0
  36. /package/dist/{src → types/src}/lib/setAtom.d.ts +0 -0
  37. /package/dist/{src → types/src}/lib/setAtoms.d.ts +0 -0
  38. /package/dist/{src → types/src}/lib/stableStringify.d.ts +0 -0
  39. /package/dist/{src → types/src}/lib/subscribe.d.ts +0 -0
  40. /package/dist/{src → types/src}/lib/transaction.d.ts +0 -0
  41. /package/dist/{src → types/src}/lib/unsubscribe.d.ts +0 -0
  42. /package/dist/{src → types/src}/lib/updateSelectorSubscribers.d.ts +0 -0
  43. /package/dist/{src → types/src}/lib/updateStateSubscribers.d.ts +0 -0
  44. /package/dist/{src → types/src}/selector.d.ts +0 -0
  45. /package/dist/{src → types/src}/selectorFamily.d.ts +0 -0
  46. /package/dist/{src → types/src}/types/Atom.d.ts +0 -0
  47. /package/dist/{src → types/src}/types/AtomDefaultValue.d.ts +0 -0
  48. /package/dist/{src → types/src}/types/AtomFamilySelector.d.ts +0 -0
  49. /package/dist/{src → types/src}/types/AtomOnInit.d.ts +0 -0
  50. /package/dist/{src → types/src}/types/AtomOnSet.d.ts +0 -0
  51. /package/dist/{src → types/src}/types/AtomOptions.d.ts +0 -0
  52. /package/dist/{src → types/src}/types/FamilyKey.d.ts +0 -0
  53. /package/dist/{src → types/src}/types/GlobalAtom.d.ts +0 -0
  54. /package/dist/{src → types/src}/types/GlobalAtomSetSelfFunc.d.ts +0 -0
  55. /package/dist/{src → types/src}/types/ResetAtom.d.ts +0 -0
  56. /package/dist/{src → types/src}/types/Selector.d.ts +0 -0
  57. /package/dist/{src → types/src}/types/SelectorFamily.d.ts +0 -0
  58. /package/dist/{src → types/src}/types/SelectorOptions.d.ts +0 -0
  59. /package/dist/{src → types/src}/types/SetAtomValue.d.ts +0 -0
  60. /package/dist/{src → types/src}/types/State.d.ts +0 -0
  61. /package/dist/{src → types/src}/types/SubscribeFn.d.ts +0 -0
  62. /package/dist/{src → types/src}/types/Subscription.d.ts +0 -0
  63. /package/dist/{src → types/src}/types/TransactionFn.d.ts +0 -0
  64. /package/dist/{src → types/src}/utils/isAtom.d.ts +0 -0
  65. /package/dist/{src → types/src}/utils/isAtomFamily.d.ts +0 -0
  66. /package/dist/{src → types/src}/utils/isFamilyAtom.d.ts +0 -0
  67. /package/dist/{src → types/src}/utils/isFamilyState.d.ts +0 -0
  68. /package/dist/{src → types/src}/utils/isPromiseLike.d.ts +0 -0
  69. /package/dist/{src → types/src}/utils/isSelector.d.ts +0 -0
package/dist/index.js CHANGED
@@ -1,14 +1,26 @@
1
1
  // src/lib/setAtom.ts
2
- import equal4 from "fast-deep-equal";
2
+ import equal4 from "fast-deep-equal/es6";
3
3
 
4
4
  // src/utils/isPromiseLike.ts
5
5
  var isPromiseLike = (object) => {
6
6
  return object && object.then && typeof object.then === "function";
7
7
  };
8
8
 
9
+ // src/lib/getState.ts
10
+ import equal3 from "fast-deep-equal/es6";
11
+
9
12
  // src/utils/isAtom.ts
10
13
  var isAtom = (state) => Object.hasOwn(state, "defaultValue");
11
14
 
15
+ // src/utils/isAtomFamily.ts
16
+ var isAtomFamily = (state) => state && Object.hasOwn(state, "__valdresAtomFamilyMap");
17
+
18
+ // src/utils/isSelector.ts
19
+ var isSelector = (state) => state && Object.hasOwn(state, "get");
20
+
21
+ // src/utils/isSelectorFamily.ts
22
+ var isSelectorFamily = (state) => state && Object.hasOwn(state, "__valdresSelectorFamilyMap");
23
+
12
24
  // src/utils/isFamilyState.ts
13
25
  var isFamilyState = (state) => state && Object.hasOwn(state, "family");
14
26
 
@@ -34,85 +46,10 @@ var updateStateSubscribers = (state, data) => {
34
46
  };
35
47
 
36
48
  // src/lib/updateSelectorSubscribers.ts
37
- import equal3 from "fast-deep-equal";
38
-
39
- // src/lib/initSelector.ts
40
- import equal2 from "fast-deep-equal";
41
-
42
- // src/lib/getState.ts
43
- import equal from "fast-deep-equal";
44
-
45
- // src/utils/isSelector.ts
46
- var isSelector = (state) => state && Object.hasOwn(state, "get");
47
-
48
- // src/lib/initAtom.ts
49
- var getAtomInitValue = (atom, data) => {
50
- if (atom.defaultValue === undefined) {
51
- let promiseResolve;
52
- const promise = new Promise((resolve) => {
53
- promiseResolve = resolve;
54
- });
55
- promise.__isEmptyAtomPromise__ = true;
56
- promise.__resolveEmptyAtomPromise__ = promiseResolve;
57
- return promise;
58
- } else if (typeof atom.defaultValue === "function") {
59
- const value = atom.defaultValue();
60
- if (isPromiseLike(value)) {
61
- value.then((resolvedValue) => {
62
- data.values.set(atom, resolvedValue);
63
- propagateUpdatedAtoms([atom], data);
64
- });
65
- }
66
- return value;
67
- } else if (isSelector(atom.defaultValue)) {
68
- return getState(atom.defaultValue, data);
69
- } else {
70
- return atom.defaultValue;
71
- }
72
- };
73
- var initAtom = (atom, data) => {
74
- let value = getAtomInitValue(atom, data);
75
- data.values.set(atom, value);
76
- if (atom.onInit)
77
- atom.onInit((newVal) => {
78
- value = newVal;
79
- setAtom(atom, newVal, data, true);
80
- }, data);
81
- return value;
82
- };
83
-
84
- // src/utils/isAtomFamily.ts
85
- var isAtomFamily = (state) => state && Object.hasOwn(state, "__valdresAtomFamilyMap");
86
-
87
- // src/utils/isSelectorFamily.ts
88
- var isSelectorFamily = (state) => state && Object.hasOwn(state, "__valdresSelectorFamilyMap");
89
-
90
- // src/lib/getState.ts
91
- function getState(state, data) {
92
- if (data.values.has(state))
93
- return data.values.get(state);
94
- if (isAtom(state))
95
- return initAtom(state, data);
96
- if (isSelector(state))
97
- return initSelector(state, data);
98
- if (isAtomFamily(state)) {
99
- const array = Array.from(state.__valdresAtomFamilyMap.keys());
100
- if (equal(array, state._keyArray))
101
- return state._keyArray;
102
- state._keyArray = array;
103
- return array;
104
- }
105
- if (isSelectorFamily(state)) {
106
- const array = Array.from(state.__valdresSelectorFamilyMap.keys());
107
- if (equal(array, state._keyArray))
108
- return state._keyArray;
109
- state._keyArray = array;
110
- return array;
111
- }
112
- throw new Error("Invalid object passed to get");
113
- }
49
+ import equal2 from "fast-deep-equal/es6";
114
50
 
115
51
  // src/lib/initSelector.ts
52
+ import equal from "fast-deep-equal/es6";
116
53
  class SuspendAndWaitForResolveError extends Error {
117
54
  promise;
118
55
  constructor(promise) {
@@ -180,7 +117,7 @@ var initSelector = (selector, data) => {
180
117
  const value = handleSelectorResult(tmpValue, selector, data);
181
118
  if (data.expiredValues.has(selector)) {
182
119
  const expiredValue = data.expiredValues.get(selector);
183
- if (equal2(expiredValue, value)) {
120
+ if (equal(expiredValue, value)) {
184
121
  data.values.set(selector, expiredValue);
185
122
  return expiredValue;
186
123
  }
@@ -199,7 +136,7 @@ var updateSelectorSubscribers = (selector, data) => {
199
136
  try {
200
137
  const oldValue = data.expiredValues.get(selector);
201
138
  const newValue = initSelector(selector, data);
202
- if (equal3(newValue, oldValue))
139
+ if (equal2(newValue, oldValue))
203
140
  return;
204
141
  } catch (e) {
205
142
  }
@@ -252,6 +189,85 @@ var propagateUpdatedAtoms = (atoms, data) => {
252
189
  }
253
190
  };
254
191
 
192
+ // src/lib/initAtom.ts
193
+ var getAtomInitValue = (atom, data) => {
194
+ if (atom.defaultValue === undefined) {
195
+ let promiseResolve;
196
+ const promise = new Promise((resolve) => {
197
+ promiseResolve = resolve;
198
+ });
199
+ promise.__isEmptyAtomPromise__ = true;
200
+ promise.__resolveEmptyAtomPromise__ = promiseResolve;
201
+ return promise;
202
+ } else if (typeof atom.defaultValue === "function") {
203
+ const value = atom.defaultValue();
204
+ if (isPromiseLike(value)) {
205
+ value.then((resolvedValue) => {
206
+ data.values.set(atom, resolvedValue);
207
+ propagateUpdatedAtoms([atom], data);
208
+ });
209
+ }
210
+ return value;
211
+ } else if (isSelector(atom.defaultValue)) {
212
+ return getState(atom.defaultValue, data);
213
+ } else {
214
+ return atom.defaultValue;
215
+ }
216
+ };
217
+ var initAtom = (atom, data) => {
218
+ let value = getAtomInitValue(atom, data);
219
+ data.values.set(atom, value);
220
+ if (isFamilyAtom(atom)) {
221
+ const currentKeySet = getState(atom.family.__keysAtom, data);
222
+ if (!currentKeySet.has(atom.familyKey)) {
223
+ const newSet = new Set(currentKeySet);
224
+ newSet.add(atom.familyKey);
225
+ setAtom(atom.family.__keysAtom, newSet, data);
226
+ }
227
+ }
228
+ if (atom.onInit)
229
+ atom.onInit((newVal) => {
230
+ value = newVal;
231
+ setAtom(atom, newVal, data, true);
232
+ }, data);
233
+ return value;
234
+ };
235
+
236
+ // src/lib/getState.ts
237
+ function getState(state, data) {
238
+ if (data.values.has(state))
239
+ return data.values.get(state);
240
+ if (isAtom(state)) {
241
+ if ("parent" in data)
242
+ return getState(state, data.parent);
243
+ return initAtom(state, data);
244
+ }
245
+ if (isSelector(state))
246
+ return initSelector(state, data);
247
+ if (isAtomFamily(state)) {
248
+ if ("parent" in data) {
249
+ const closestData = findClosestStoreWithAtomInitialized(state.__keysAtom, data);
250
+ return getState(state.__keysSelector, closestData);
251
+ }
252
+ return getState(state.__keysSelector, data);
253
+ }
254
+ if (isSelectorFamily(state)) {
255
+ const array = Array.from(state.__valdresSelectorFamilyMap.keys());
256
+ if (equal3(array, state._keyArray))
257
+ return state._keyArray;
258
+ state._keyArray = array;
259
+ return array;
260
+ }
261
+ throw new Error("Invalid object passed to get");
262
+ }
263
+ var findClosestStoreWithAtomInitialized = (atom, data) => {
264
+ if ("parent" in data === false)
265
+ return data;
266
+ if (data.values.has(atom))
267
+ return data;
268
+ return findClosestStoreWithAtomInitialized(atom, data.parent);
269
+ };
270
+
255
271
  // src/lib/setAtom.ts
256
272
  var setAtom = (atom, newValue, data, skipOnSet = false) => {
257
273
  const currentValue = getState(atom, data);
@@ -330,6 +346,20 @@ function atom(defaultValue, options) {
330
346
  ...options
331
347
  };
332
348
  }
349
+ // src/lib/atomFamilyAtom.ts
350
+ function atomFamilyAtom(defaultValue, options) {
351
+ if (options.global) {
352
+ return {
353
+ ...options,
354
+ defaultValue
355
+ };
356
+ }
357
+ return {
358
+ ...options,
359
+ defaultValue
360
+ };
361
+ }
362
+
333
363
  // src/lib/stableStringify.ts
334
364
  var stableStringifyRecurse = (x, key) => {
335
365
  if (typeof x === "string" && !x.includes('"') && !x.includes("\\")) {
@@ -384,21 +414,33 @@ var stableStringify = (x) => {
384
414
  return stableStringifyRecurse(x);
385
415
  };
386
416
 
387
- // src/lib/atomFamilyAtom.ts
388
- function atomFamilyAtom(defaultValue, options) {
389
- if (options.global) {
417
+ // src/selector.ts
418
+ var selector = (get, options) => {
419
+ if (!options)
420
+ return { get };
421
+ return { ...options, get };
422
+ };
423
+
424
+ // src/atomFamily.ts
425
+ var createOptions = (options = {}, family, familyKey, keyStringified) => {
426
+ if (options.label) {
390
427
  return {
391
428
  ...options,
392
- defaultValue
429
+ label: options?.label + "_" + keyStringified,
430
+ family,
431
+ familyKey
393
432
  };
433
+ } else {
434
+ return { ...options, family, familyKey };
394
435
  }
395
- return {
396
- ...options,
397
- defaultValue
398
- };
399
- }
400
-
401
- // src/atomFamily.ts
436
+ };
437
+ var handleDefaultValue = (defaultValue, key) => {
438
+ if (isSelectorFamily(defaultValue))
439
+ return defaultValue(key);
440
+ if (typeof defaultValue === "function")
441
+ return () => defaultValue(key);
442
+ return defaultValue;
443
+ };
402
444
  function atomFamily(defaultValue, options) {
403
445
  const map = new Map;
404
446
  const atomFamily2 = (key) => {
@@ -412,40 +454,38 @@ function atomFamily(defaultValue, options) {
412
454
  };
413
455
  atomFamily2.__valdresAtomFamilyMap = map;
414
456
  atomFamily2.release = (key) => map.delete(key);
457
+ const keysAtom = atom(new Set);
458
+ atomFamily2.__keysAtom = keysAtom;
459
+ atomFamily2.__keysSelector = selector((get) => Array.from(get(keysAtom)));
415
460
  if (options?.label)
416
461
  atomFamily2.label = options.label;
417
462
  return atomFamily2;
418
463
  }
419
- var createOptions = (options = {}, family, familyKey, keyStringified) => {
420
- if (options.label) {
464
+ // src/lib/createStoreData.ts
465
+ var generateId = () => (Math.random() + 1).toString(36).substring(7);
466
+ var generateStoreData = (id = generateId()) => {
467
+ return {
468
+ id,
469
+ values: new WeakMap,
470
+ expiredValues: new WeakMap,
471
+ subscriptions: new WeakMap,
472
+ subscriptionsRequireEqualCheck: new WeakMap,
473
+ stateConsumers: new WeakMap,
474
+ stateDependencies: new WeakMap,
475
+ scopes: {}
476
+ };
477
+ };
478
+ function createStoreData(id, parent) {
479
+ if (parent) {
421
480
  return {
422
- ...options,
423
- label: options?.label + "_" + keyStringified,
424
- family,
425
- familyKey
481
+ ...generateStoreData(id),
482
+ parent,
483
+ scopeConsumers: parent ? new Set : undefined
426
484
  };
427
485
  } else {
428
- return { ...options, family, familyKey };
486
+ return generateStoreData(id);
429
487
  }
430
- };
431
- var handleDefaultValue = (defaultValue, key) => {
432
- if (isSelectorFamily(defaultValue))
433
- return defaultValue(key);
434
- if (typeof defaultValue === "function")
435
- return () => defaultValue(key);
436
- return defaultValue;
437
- };
438
- // src/lib/createStoreData.ts
439
- var generateId = () => (Math.random() + 1).toString(36).substring(7);
440
- var createStoreData = (id = generateId()) => ({
441
- id,
442
- values: new WeakMap,
443
- expiredValues: new WeakMap,
444
- subscriptions: new WeakMap,
445
- subscriptionsRequireEqualCheck: new WeakMap,
446
- stateConsumers: new WeakMap,
447
- stateDependencies: new WeakMap
448
- });
488
+ }
449
489
 
450
490
  // src/lib/resetAtom.ts
451
491
  var resetAtom = (atom2, data) => {
@@ -480,6 +520,7 @@ var unsubscribe = (state, subscription, data, mount, maxAgeCleanup) => {
480
520
  if (subscribers.size === 0) {
481
521
  if (maxAgeCleanup)
482
522
  maxAgeCleanup();
523
+ data.subscriptions.delete(state);
483
524
  }
484
525
  if (mount) {
485
526
  if (subscribers.size === mount.mountSubscriptions.size) {
@@ -498,6 +539,14 @@ var initSubscribers = (state, data) => {
498
539
  return set;
499
540
  };
500
541
  var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) => {
542
+ if ("parent" in data && !data.values.has(state) && isAtom(state)) {
543
+ const parentUnsubscribe = subscribe(state, callback, requireDeepEqualCheckBeforeCallback, data.parent);
544
+ let originalCallback = callback;
545
+ callback = () => {
546
+ parentUnsubscribe();
547
+ originalCallback();
548
+ };
549
+ }
501
550
  const subscribers = data.subscriptions.get(state) || initSubscribers(state, data);
502
551
  if (isSelector(state) && !data.values.has(state)) {
503
552
  initSelector(state, data);
@@ -564,7 +613,7 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
564
613
  };
565
614
 
566
615
  // src/lib/setAtoms.ts
567
- import equal5 from "fast-deep-equal";
616
+ import equal5 from "fast-deep-equal/es6";
568
617
  var setAtoms = (pairs, data) => {
569
618
  const updatedAtoms = [];
570
619
  for (let [atom2, value] of pairs) {
@@ -636,9 +685,9 @@ var transaction = (callback, data) => {
636
685
  const currentValue = txnGet(atom2);
637
686
  value = value(currentValue);
638
687
  }
639
- for (const selector of findDependencies(atom2, data)) {
640
- dirtySelectors.add(selector);
641
- txnSelectorCache.delete(selector);
688
+ for (const selector2 of findDependencies(atom2, data)) {
689
+ dirtySelectors.add(selector2);
690
+ txnSelectorCache.delete(selector2);
642
691
  }
643
692
  if (txnSubscribers.get(atom2)?.size) {
644
693
  recursivlyResetTxnSelectorCache(atom2, txnSubscribers, txnSelectorCache);
@@ -667,7 +716,7 @@ Only \`atom\` cam be set.
667
716
  var InvalidStateSetError = `Invalid state object passed to set().
668
717
  Only \`atom\` can be set.
669
718
  `;
670
- var storeFromStoreData = (data) => {
719
+ function storeFromStoreData(data, detach) {
671
720
  const get = (state) => getState(state, data);
672
721
  const set = (state, value) => {
673
722
  if (isAtom(state))
@@ -679,24 +728,51 @@ var storeFromStoreData = (data) => {
679
728
  const reset = (atom2) => resetAtom(atom2, data);
680
729
  const sub = (state, callback, deepEqualCheckBeforeCallback = true) => subscribe(state, callback, deepEqualCheckBeforeCallback, data);
681
730
  const txn = (callback) => transaction(callback, data);
682
- return {
683
- get,
684
- set,
685
- sub,
686
- txn,
687
- reset,
688
- data
731
+ const scope = (scopeId) => {
732
+ let scopedStoreData;
733
+ if (scopeId in data.scopes) {
734
+ scopedStoreData = data.scopes[scopeId];
735
+ } else {
736
+ scopedStoreData = createStoreData(scopeId, data);
737
+ data.scopes[scopeId] = scopedStoreData;
738
+ }
739
+ const detach2 = () => {
740
+ scopedStoreData.scopeConsumers.delete(detach2);
741
+ if (scopedStoreData.scopeConsumers.size === 0) {
742
+ delete data.scopes[scopeId];
743
+ }
744
+ };
745
+ scopedStoreData.scopeConsumers.add(detach2);
746
+ const newStore = storeFromStoreData(data.scopes[scopeId], detach2);
747
+ return newStore;
689
748
  };
690
- };
749
+ if (detach) {
750
+ return {
751
+ get,
752
+ set,
753
+ sub,
754
+ txn,
755
+ reset,
756
+ data,
757
+ scope,
758
+ detach
759
+ };
760
+ } else {
761
+ return {
762
+ get,
763
+ set,
764
+ sub,
765
+ txn,
766
+ reset,
767
+ data,
768
+ scope
769
+ };
770
+ }
771
+ }
691
772
 
692
- // src/createStore.ts
693
- var createStore = (id) => {
694
- const data = createStoreData(id);
695
- return storeFromStoreData(data);
696
- };
697
773
  // src/createStoreWithSelectorSet.ts
698
- var setSelector = (selector, values, store) => {
699
- return selector.set(store.set, store.get, store.reset, ...values);
774
+ var setSelector = (selector2, values, store) => {
775
+ return selector2.set(store.set, store.get, store.reset, ...values);
700
776
  };
701
777
  var createStoreWithSelectorSet = (id) => {
702
778
  const data = createStoreData(id);
@@ -711,12 +787,6 @@ var createStoreWithSelectorSet = (id) => {
711
787
  store.kind = "storeWithSelectorSet";
712
788
  return store;
713
789
  };
714
- // src/selector.ts
715
- var selector = (get, options) => {
716
- if (!options)
717
- return { get };
718
- return { ...options, get };
719
- };
720
790
  // src/selectorFamily.ts
721
791
  var createOptions2 = (options, key) => {
722
792
  if (options.label) {
@@ -744,15 +814,27 @@ var selectorFamily = (get, options) => {
744
814
  selectorFamily2.label = options.label;
745
815
  return selectorFamily2;
746
816
  };
817
+ // src/store.ts
818
+ var store = (id) => {
819
+ const data = createStoreData(id);
820
+ return storeFromStoreData(data);
821
+ };
822
+ // src/utils/isFamilySelector.ts
823
+ var isFamilySelector = (state) => isFamilyState(state) && isSelector(state);
747
824
  export {
825
+ store,
748
826
  selectorFamily,
749
827
  selector,
828
+ isSelectorFamily,
750
829
  isSelector,
751
830
  isPromiseLike,
831
+ isFamilyState,
832
+ isFamilySelector,
833
+ isFamilyAtom,
752
834
  isFamily,
835
+ isAtomFamily,
753
836
  isAtom,
754
837
  createStoreWithSelectorSet,
755
- createStore,
756
838
  atomFamily,
757
839
  atom
758
840
  };
@@ -1,16 +1,22 @@
1
1
  export { atom } from "./src/atom";
2
2
  export { atomFamily } from "./src/atomFamily";
3
- export { createStore } from "./src/createStore";
4
3
  export { createStoreWithSelectorSet } from "./src/createStoreWithSelectorSet";
5
4
  export { selector } from "./src/selector";
6
5
  export { selectorFamily } from "./src/selectorFamily";
6
+ export { store } from "./src/store";
7
7
  export { isAtom } from "./src/utils/isAtom";
8
- export { isSelector } from "./src/utils/isSelector";
8
+ export { isAtomFamily } from "./src/utils/isAtomFamily";
9
9
  export { isFamily } from "./src/utils/isFamily";
10
+ export { isFamilyAtom } from "./src/utils/isFamilyAtom";
11
+ export { isFamilySelector } from "./src/utils/isFamilySelector";
12
+ export { isFamilyState } from "./src/utils/isFamilyState";
10
13
  export { isPromiseLike } from "./src/utils/isPromiseLike";
14
+ export { isSelector } from "./src/utils/isSelector";
15
+ export { isSelectorFamily } from "./src/utils/isSelectorFamily";
11
16
  export type { Atom } from "./src/types/Atom";
12
17
  export type { AtomFamily } from "./src/types/AtomFamily";
13
18
  export type { GetValue } from "./src/types/GetValue";
19
+ export type { ResetAtom } from "./src/types/ResetAtom";
14
20
  export type { Selector } from "./src/types/Selector";
15
21
  export type { SelectorFamily } from "./src/types/SelectorFamily";
16
22
  export type { SetAtom } from "./src/types/SetAtom";
@@ -13,4 +13,4 @@ import type { GlobalAtom } from "./types/GlobalAtom";
13
13
  export declare function atom<V>(defaultValue: AtomDefaultValue<V>, options: AtomOptions<V> & {
14
14
  global: true;
15
15
  }): GlobalAtom<V>;
16
- export declare function atom<V>(defaultValue: AtomDefaultValue<V>, options?: AtomOptions<V>): Atom<V>;
16
+ export declare function atom<V>(defaultValue?: AtomDefaultValue<V>, options?: AtomOptions<V>): Atom<V>;
@@ -0,0 +1,8 @@
1
+ import type { AtomDefaultValue } from "../types/AtomDefaultValue";
2
+ import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
3
+ import type { AtomFamilyGlobalAtom } from "../types/AtomFamilyGlobalAtom";
4
+ import type { AtomOptions } from "../types/AtomOptions";
5
+ export declare function atomFamilyAtom<Key, Value>(defaultValue: AtomDefaultValue<Value>, options: AtomOptions<Value> & {
6
+ global: true;
7
+ }): AtomFamilyGlobalAtom<Key, Value>;
8
+ export declare function atomFamilyAtom<Key, Value>(defaultValue: AtomDefaultValue<Value>, options: AtomOptions<Value>): AtomFamilyAtom<Key, Value>;
@@ -0,0 +1,3 @@
1
+ import type { ScopedStoreData, StoreData } from "../types/StoreData";
2
+ export declare function createStoreData(id?: string, parent?: undefined): StoreData;
3
+ export declare function createStoreData(id: string, parent: StoreData): ScopedStoreData;
@@ -1,4 +1,5 @@
1
1
  import type { Atom } from "../types/Atom";
2
+ import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
2
3
  import type { StoreData } from "../types/StoreData";
3
4
  export declare const getAtomInitValue: <V>(atom: Atom<V>, data: StoreData) => any;
4
- export declare const initAtom: <V>(atom: Atom<V>, data: StoreData) => any;
5
+ export declare const initAtom: <V, K>(atom: Atom<V> | AtomFamilyAtom<K, V>, data: StoreData) => any;
@@ -0,0 +1,4 @@
1
+ import type { ScopedStore, Store } from "../types/Store";
2
+ import type { ScopedStoreData, StoreData } from "../types/StoreData";
3
+ export declare function storeFromStoreData(data: ScopedStoreData, detach: () => void): ScopedStore;
4
+ export declare function storeFromStoreData(data: StoreData): Store;
@@ -0,0 +1 @@
1
+ export declare const store: (id?: string) => import("..").Store;
@@ -0,0 +1,12 @@
1
+ import type { Atom } from "./Atom";
2
+ import type { AtomFamilyAtom } from "./AtomFamilyAtom";
3
+ import type { FamilyKey } from "./FamilyKey";
4
+ import type { Selector } from "./Selector";
5
+ export type AtomFamily<Key = FamilyKey, Value = unknown> = {
6
+ (key: Key): AtomFamilyAtom<Key, Value>;
7
+ release: (key: Key) => void;
8
+ label?: string;
9
+ __valdresAtomFamilyMap: Map<Key, AtomFamilyAtom<Key, Value>>;
10
+ __keysAtom: Atom<Set<Key>>;
11
+ __keysSelector: Selector<Key[]>;
12
+ };
@@ -1,6 +1,6 @@
1
1
  import type { Atom } from "./Atom";
2
2
  import type { AtomFamily } from "./AtomFamily";
3
- export type AtomFamilyAtom<Value = unknown, Key = unknown> = Atom<Value> & {
4
- family: AtomFamily<Value, Key>;
3
+ export type AtomFamilyAtom<Key = unknown, Value = unknown> = Atom<Value> & {
4
+ family: AtomFamily<Key, Value>;
5
5
  familyKey: Key;
6
6
  };
@@ -1,5 +1,5 @@
1
1
  import type { AtomFamilyAtom } from "./AtomFamilyAtom";
2
2
  import type { GlobalAtomSetSelfFunc } from "./GlobalAtomSetSelfFunc";
3
- export type AtomFamilyGlobalAtom<Value = unknown, Key = unknown> = AtomFamilyAtom<Value, Key> & {
3
+ export type AtomFamilyGlobalAtom<Key = unknown, Value = unknown> = AtomFamilyAtom<Key, Value> & {
4
4
  setSelf: GlobalAtomSetSelfFunc<Value>;
5
5
  };
@@ -1,3 +1,3 @@
1
1
  import type { SelectorFamily } from "./SelectorFamily";
2
2
  import type { AtomFamily } from "./AtomFamily";
3
- export type Family<V, K = any> = AtomFamily<V, K> | SelectorFamily<V, K>;
3
+ export type Family<Key, Value = any> = AtomFamily<Key, Value> | SelectorFamily<Key, Value>;
@@ -0,0 +1,8 @@
1
+ import type { Atom } from "./Atom";
2
+ import type { AtomFamily } from "./AtomFamily";
3
+ import type { Selector } from "./Selector";
4
+ export type GetValue = {
5
+ <V>(atom: Atom<V>, scopeId?: string): V;
6
+ <V>(selector: Selector<V>, scopeId?: string): V;
7
+ <V, K>(family: AtomFamily<K, V>, scopeId?: string): K[];
8
+ };
@@ -1,3 +1,3 @@
1
1
  import type { Atom } from "./Atom";
2
2
  import type { SetAtomValue } from "./SetAtomValue";
3
- export type SetAtom<Value = any> = (atom: Atom<Value>, value: SetAtomValue<Value>) => void;
3
+ export type SetAtom<Value = any> = (atom: Atom<Value>, value: SetAtomValue<Value>, scopeId?: string) => void;
@@ -2,19 +2,23 @@ import type { Atom } from "./Atom";
2
2
  import type { AtomFamilyAtom } from "./AtomFamilyAtom";
3
3
  import type { GetValue } from "./GetValue";
4
4
  import type { ResetAtom } from "./ResetAtom";
5
- import type { StoreData } from "./StoreData";
5
+ import type { ScopedStoreData, StoreData } from "./StoreData";
6
6
  import type { SubscribeFn } from "./SubscribeFn";
7
7
  import type { TransactionFn } from "./TransactionFn";
8
8
  type SetAtom = {
9
- <V, K>(atom: AtomFamilyAtom<V, K>, value: V): void;
9
+ <V, K>(atom: AtomFamilyAtom<K, V>, value: V): void;
10
10
  <V>(atom: Atom<V>, value: V): void;
11
11
  };
12
- export type Store = {
13
- data: StoreData;
12
+ export type Store<T = StoreData> = {
13
+ data: T;
14
14
  get: GetValue;
15
15
  set: SetAtom;
16
16
  sub: SubscribeFn;
17
17
  reset: ResetAtom;
18
18
  txn: (callback: TransactionFn) => void;
19
+ scope: (scopeId: string) => ScopedStore;
20
+ };
21
+ export type ScopedStore = Store<ScopedStoreData> & {
22
+ detach: () => void;
19
23
  };
20
24
  export {};
@@ -1,4 +1,4 @@
1
- export type StoreData = {
1
+ export type RootStoreData = {
2
2
  id: string;
3
3
  values: WeakMap<WeakKey, any>;
4
4
  expiredValues: WeakMap<WeakKey, any>;
@@ -6,4 +6,12 @@ export type StoreData = {
6
6
  subscriptionsRequireEqualCheck: WeakMap<WeakKey, boolean>;
7
7
  stateConsumers: WeakMap<WeakKey, any>;
8
8
  stateDependencies: WeakMap<WeakKey, any>;
9
+ scopes: {
10
+ [scopeId: string]: ScopedStoreData;
11
+ };
9
12
  };
13
+ export type ScopedStoreData = RootStoreData & {
14
+ parent: StoreData;
15
+ scopeConsumers: Set<() => void>;
16
+ };
17
+ export type StoreData = RootStoreData | ScopedStoreData;
@@ -0,0 +1 @@
1
+ export declare const isFamily: (state: any) => state is import("../..").SelectorFamily<unknown, unknown> | import("../..").AtomFamily<unknown, unknown>;
@@ -0,0 +1,2 @@
1
+ import type { AtomFamilySelector } from "../types/AtomFamilySelector";
2
+ export declare const isFamilySelector: <K, V>(state: any) => state is AtomFamilySelector<K, V>;
@@ -0,0 +1,2 @@
1
+ import type { SelectorFamily } from "../types/SelectorFamily";
2
+ export declare const isSelectorFamily: <K, V>(state: any) => state is SelectorFamily<K, V>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.20",
3
+ "version": "0.2.0-alpha.21",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"
@@ -12,15 +12,15 @@
12
12
  },
13
13
  "type": "module",
14
14
  "exports": {
15
- "types": "./dist/index.d.ts",
16
- "default": "./dist/index.js"
15
+ "default": "./dist/index.js",
16
+ "types": "./dist/types/index.d.ts"
17
17
  },
18
18
  "files": [
19
19
  "dist"
20
20
  ],
21
21
  "scripts": {
22
- "build": "rm -rf dist && NODE_ENV=production bun scripts/build.ts",
23
- "build:types": "tsc",
22
+ "build": "NODE_ENV=production bun build index.ts --outdir ./dist --packages external",
23
+ "build:types": "rm -rf dist/types && tsc",
24
24
  "test": "bun test"
25
25
  },
26
26
  "dependencies": {
@@ -39,5 +39,5 @@
39
39
  "access": "public",
40
40
  "registry": "https://registry.npmjs.org/"
41
41
  },
42
- "gitHead": "de1b98e5a5dfe28014234a5813c9c046f10b551c"
42
+ "gitHead": "6d564893ff8e43a913e9d7afa531e83d3d4e69b1"
43
43
  }
@@ -1 +0,0 @@
1
- export declare const createStore: (id?: string) => import("..").Store;
@@ -1,8 +0,0 @@
1
- import type { AtomDefaultValue } from "../types/AtomDefaultValue";
2
- import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
3
- import type { AtomFamilyGlobalAtom } from "../types/AtomFamilyGlobalAtom";
4
- import type { AtomOptions } from "../types/AtomOptions";
5
- export declare function atomFamilyAtom<V, K>(defaultValue: AtomDefaultValue<V>, options: AtomOptions<V> & {
6
- global: true;
7
- }): AtomFamilyGlobalAtom<V, K>;
8
- export declare function atomFamilyAtom<V, K>(defaultValue: AtomDefaultValue<V>, options: AtomOptions<V>): AtomFamilyAtom<V, K>;
@@ -1,2 +0,0 @@
1
- import type { StoreData } from "../types/StoreData";
2
- export declare const createStoreData: (id?: string) => StoreData;
@@ -1,3 +0,0 @@
1
- import type { Store } from "../types/Store";
2
- import type { StoreData } from "../types/StoreData";
3
- export declare const storeFromStoreData: (data: StoreData) => Store;
@@ -1,8 +0,0 @@
1
- import type { AtomFamilyAtom } from "./AtomFamilyAtom";
2
- import type { FamilyKey } from "./FamilyKey";
3
- export type AtomFamily<Key = FamilyKey, Value = unknown> = {
4
- (key: Key): AtomFamilyAtom<Value, Key>;
5
- release: (key: Key) => void;
6
- label?: string;
7
- __valdresAtomFamilyMap: Map<Key, AtomFamilyAtom<Value, Key>>;
8
- };
@@ -1,8 +0,0 @@
1
- import type { Atom } from "./Atom";
2
- import type { AtomFamily } from "./AtomFamily";
3
- import type { Selector } from "./Selector";
4
- export type GetValue = {
5
- <V>(atom: Atom<V>): V;
6
- <V>(selector: Selector<V>): V;
7
- <V, K>(family: AtomFamily<K, V>): K[];
8
- };
@@ -1 +0,0 @@
1
- export declare const isFamily: (state: any) => state is import("../..").AtomFamily<unknown, unknown> | import("../..").SelectorFamily;
@@ -1,2 +0,0 @@
1
- import type { SelectorFamily } from "../types/SelectorFamily";
2
- export declare const isSelectorFamily: (state: any) => state is SelectorFamily;
File without changes
@@ -1,7 +1,7 @@
1
- import type { StoreData } from "../types/StoreData";
2
1
  import type { Atom } from "../types/Atom";
3
2
  import type { AtomFamily } from "../types/AtomFamily";
4
3
  import type { Selector } from "../types/Selector";
4
+ import type { StoreData } from "../types/StoreData";
5
5
  export declare function getState<V, K>(atom: Atom<V>, data: StoreData): V;
6
6
  export declare function getState<V, K>(selector: Selector<V>, data: StoreData): V;
7
7
  export declare function getState<V, K>(family: AtomFamily<V, K>, data: StoreData): K[];
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes