valdres 0.2.0-alpha.11 → 0.2.0-alpha.14

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
@@ -33,6 +33,9 @@ import equal2 from "fast-deep-equal";
33
33
  // src/lib/getState.ts
34
34
  import equal from "fast-deep-equal";
35
35
 
36
+ // src/utils/isSelector.ts
37
+ var isSelector = (state) => state && Object.hasOwn(state, "get");
38
+
36
39
  // src/lib/initAtom.ts
37
40
  var getAtomInitValue = (atom, data) => {
38
41
  if (atom.defaultValue === undefined) {
@@ -52,6 +55,8 @@ var getAtomInitValue = (atom, data) => {
52
55
  });
53
56
  }
54
57
  return value;
58
+ } else if (isSelector(atom.defaultValue)) {
59
+ return getState(atom.defaultValue, data);
55
60
  } else {
56
61
  return atom.defaultValue;
57
62
  }
@@ -70,11 +75,11 @@ var initAtom = (atom, data) => {
70
75
  // src/utils/isAtom.ts
71
76
  var isAtom = (state) => Object.hasOwn(state, "defaultValue");
72
77
 
73
- // src/utils/isSelector.ts
74
- var isSelector = (state) => Object.hasOwn(state, "get");
78
+ // src/utils/isAtomFamily.ts
79
+ var isAtomFamily = (state) => state && Object.hasOwn(state, "__valdresAtomFamilyMap");
75
80
 
76
- // src/utils/isFamily.ts
77
- var isFamily = (state) => Object.hasOwn(state, "_map");
81
+ // src/utils/isSelectorFamily.ts
82
+ var isSelectorFamily = (state) => state && Object.hasOwn(state, "__valdresSelectorFamilyMap");
78
83
 
79
84
  // src/lib/getState.ts
80
85
  function getState(state, data) {
@@ -84,8 +89,15 @@ function getState(state, data) {
84
89
  return initAtom(state, data);
85
90
  if (isSelector(state))
86
91
  return initSelector(state, data);
87
- if (isFamily(state)) {
88
- const array = Array.from(state._map.keys());
92
+ if (isAtomFamily(state)) {
93
+ const array = Array.from(state.__valdresAtomFamilyMap.keys());
94
+ if (equal(array, state._keyArray))
95
+ return state._keyArray;
96
+ state._keyArray = array;
97
+ return array;
98
+ }
99
+ if (isSelectorFamily(state)) {
100
+ const array = Array.from(state.__valdresSelectorFamilyMap.keys());
89
101
  if (equal(array, state._keyArray))
90
102
  return state._keyArray;
91
103
  state._keyArray = array;
@@ -253,7 +265,7 @@ var setAtom = (atom, newValue, data, skipOnSet = false) => {
253
265
  propagateUpdatedAtoms([atom], data);
254
266
  };
255
267
 
256
- // src/atom.ts
268
+ // src/lib/globalAtom.ts
257
269
  var globalAtom = (defaultValue, options) => {
258
270
  const stores = new Set;
259
271
  let value = defaultValue;
@@ -279,6 +291,8 @@ var globalAtom = (defaultValue, options) => {
279
291
  };
280
292
  return newAtom;
281
293
  };
294
+
295
+ // src/atom.ts
282
296
  var atom = (defaultValue, options) => {
283
297
  if (!options)
284
298
  return { defaultValue };
@@ -354,6 +368,13 @@ var createOptions = (options, key) => {
354
368
  return options;
355
369
  }
356
370
  };
371
+ var handleDefaultValue = (defaultValue, key) => {
372
+ if (isSelectorFamily(defaultValue))
373
+ return defaultValue(key);
374
+ if (typeof defaultValue === "function")
375
+ return () => defaultValue(key);
376
+ return defaultValue;
377
+ };
357
378
  var atomFamily = (defaultValue, options) => {
358
379
  const map = new Map;
359
380
  const atomFamily2 = (key) => {
@@ -361,13 +382,13 @@ var atomFamily = (defaultValue, options) => {
361
382
  if (map.has(keyStringified)) {
362
383
  return map.get(keyStringified);
363
384
  }
364
- const newAtom = atom(typeof defaultValue === "function" ? () => defaultValue(key) : defaultValue, options ? createOptions(options, keyStringified) : undefined);
385
+ const newAtom = atom(handleDefaultValue(defaultValue, key), options ? createOptions(options, keyStringified) : undefined);
365
386
  newAtom.family = atomFamily2;
366
387
  newAtom.familyKey = Object.freeze(key);
367
388
  map.set(keyStringified, newAtom);
368
389
  return newAtom;
369
390
  };
370
- atomFamily2._map = map;
391
+ atomFamily2.__valdresAtomFamilyMap = map;
371
392
  if (options?.label)
372
393
  atomFamily2.label = options.label;
373
394
  return atomFamily2;
@@ -386,15 +407,19 @@ var createStoreData = (id = generateId()) => ({
386
407
 
387
408
  // src/lib/resetAtom.ts
388
409
  var resetAtom = (atom2, data) => {
389
- const res = initAtom(atom2, data);
390
- if (!isPromiseLike(res)) {
410
+ let value = getAtomInitValue(atom2, data);
411
+ data.values.set(atom2, value);
412
+ if (!isPromiseLike(value)) {
391
413
  propagateUpdatedAtoms([atom2], data);
392
414
  }
393
- return res;
415
+ return value;
394
416
  };
395
417
 
418
+ // src/utils/isFamily.ts
419
+ var isFamily = (state) => isAtomFamily(state) || isSelectorFamily(state);
420
+
396
421
  // src/lib/unsubscribe.ts
397
- var unsubscribe = (state, subscription, data, mount) => {
422
+ var unsubscribe = (state, subscription, data, mount, maxAgeCleanup) => {
398
423
  const subscribers = data.subscriptions.get(state);
399
424
  if (subscribers) {
400
425
  subscribers.delete(subscription);
@@ -410,6 +435,10 @@ var unsubscribe = (state, subscription, data, mount) => {
410
435
  data.subscriptionsRequireEqualCheck.delete(state);
411
436
  }
412
437
  }
438
+ if (subscribers.size === 0) {
439
+ if (maxAgeCleanup)
440
+ maxAgeCleanup();
441
+ }
413
442
  if (mount) {
414
443
  if (subscribers.size === mount.mountSubscriptions.size) {
415
444
  if (typeof mount.onUnmount === "function") {
@@ -446,23 +475,50 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
446
475
  }
447
476
  subscribers.add(subscription);
448
477
  let mount;
449
- if (subscribers.size === 1 && state.onMount) {
450
- const store = storeFromStoreData(data);
451
- const mountSubscriptions = new Set;
452
- const originalSub = store.sub;
453
- store.sub = (state2, callback2) => {
454
- mountSubscriptions.add(callback2);
455
- return originalSub(state2, callback2);
456
- };
457
- mount = {
458
- onUnmount: state.onMount(store, state),
459
- mountSubscriptions
460
- };
478
+ let maxAgeCleanup;
479
+ if (subscribers.size === 1) {
480
+ if (isAtom(state) && state.maxAge) {
481
+ let timeout;
482
+ const interval = setInterval(() => {
483
+ let value = getAtomInitValue(state, data);
484
+ if (isPromiseLike(value)) {
485
+ if (state.staleWhileRevalidate) {
486
+ const oldValue = data.values.get(state);
487
+ timeout = setTimeout(() => {
488
+ const nowValue = data.values.get(state);
489
+ console.log("todo", oldValue);
490
+ }, state.staleWhileRevalidate);
491
+ value.then((res) => clearTimeout(timeout));
492
+ }
493
+ } else {
494
+ data.values.set(state, value);
495
+ propagateUpdatedAtoms([state], data);
496
+ }
497
+ }, state.maxAge);
498
+ maxAgeCleanup = () => {
499
+ clearInterval(interval);
500
+ if (timeout)
501
+ clearTimeout(timeout);
502
+ };
503
+ }
504
+ if (state.onMount) {
505
+ const store = storeFromStoreData(data);
506
+ const mountSubscriptions = new Set;
507
+ const originalSub = store.sub;
508
+ store.sub = (state2, callback2) => {
509
+ mountSubscriptions.add(callback2);
510
+ return originalSub(state2, callback2);
511
+ };
512
+ mount = {
513
+ onUnmount: state.onMount(store, state),
514
+ mountSubscriptions
515
+ };
516
+ }
461
517
  }
462
518
  if (requireDeepEqualCheckBeforeCallback && data.subscriptionsRequireEqualCheck.get(state) !== true) {
463
519
  data.subscriptionsRequireEqualCheck.set(state, true);
464
520
  }
465
- return () => unsubscribe(state, subscription, data, mount);
521
+ return () => unsubscribe(state, subscription, data, mount, maxAgeCleanup);
466
522
  };
467
523
 
468
524
  // src/lib/setAtoms.ts
@@ -473,6 +529,8 @@ var setAtoms = (pairs, data) => {
473
529
  const currentValue = getState(atom2, data);
474
530
  if (!equal5(currentValue, value)) {
475
531
  updatedAtoms.push(atom2);
532
+ if (atom2.onSet)
533
+ atom2.onSet(value, data);
476
534
  data.values.set(atom2, value);
477
535
  }
478
536
  }
@@ -636,7 +694,9 @@ var selectorFamily = (get, options) => {
636
694
  map.set(keyStringified, newSelector);
637
695
  return newSelector;
638
696
  };
639
- selectorFamily2._map = map;
697
+ selectorFamily2.__valdresSelectorFamilyMap = map;
698
+ if (options?.label)
699
+ selectorFamily2.label = options.label;
640
700
  return selectorFamily2;
641
701
  };
642
702
  export {
@@ -1,4 +1,4 @@
1
1
  import type { Atom } from "./types/Atom";
2
2
  import type { AtomOptions } from "./types/AtomOptions";
3
- export declare const globalAtom: <Value = any, FamilyKey = undefined>(defaultValue: Value | (() => Value | Promise<Value>), options: AtomOptions<Value>) => Atom<Value>;
4
- export declare const atom: <Value = any, FamilyKey = undefined>(defaultValue?: Value | (() => Value | Promise<Value>), options?: AtomOptions<Value>) => Atom<Value, FamilyKey>;
3
+ import type { Selector } from "./types/Selector";
4
+ export declare const atom: <Value = any, FamilyKey = undefined>(defaultValue?: Value | (() => Value | Promise<Value>) | Selector<Value>, options?: AtomOptions<Value>) => Atom<Value, FamilyKey>;
@@ -1,5 +1,8 @@
1
1
  import type { AtomFamily } from "./types/AtomFamily";
2
2
  import type { AtomOptions } from "./types/AtomOptions";
3
+ import type { Selector } from "./types/Selector";
4
+ import type { SelectorFamily } from "./types/SelectorFamily";
3
5
  type DefaultValueCallback<Key, Value> = (arg: Key) => Value | Promise<Value>;
4
- export declare const atomFamily: <Value = unknown, Key = unknown>(defaultValue?: Value | DefaultValueCallback<Key, Value>, options?: AtomOptions<Value>) => AtomFamily<Value, Key>;
6
+ type AtomFamilyDefaultValue<Value, Key> = undefined | Value | DefaultValueCallback<Key, Value> | Selector<Value> | SelectorFamily<Value, Key>;
7
+ export declare const atomFamily: <Value = unknown, Key = unknown>(defaultValue?: AtomFamilyDefaultValue<Value, Key>, options?: AtomOptions<Value>) => AtomFamily<Value, Key>;
5
8
  export {};
@@ -0,0 +1,3 @@
1
+ import type { Atom } from "./../types/Atom";
2
+ import type { AtomOptions } from "./../types/AtomOptions";
3
+ export declare const globalAtom: <Value = any, FamilyKey = undefined>(defaultValue: Value | (() => Value | Promise<Value>), options: AtomOptions<Value>) => Atom<Value>;
@@ -2,4 +2,4 @@ import type { Family } from "../types/Family";
2
2
  import type { State } from "../types/State";
3
3
  import type { StoreData } from "../types/StoreData";
4
4
  import type { Subscription } from "../types/Subscription";
5
- export declare const unsubscribe: <V>(state: State<V> | Family<V>, subscription: Subscription, data: StoreData, mount?: any) => void;
5
+ export declare const unsubscribe: <V>(state: State<V> | Family<V>, subscription: Subscription, data: StoreData, mount?: any, maxAgeCleanup?: any) => void;
@@ -1,12 +1,15 @@
1
1
  import type { AtomFamily } from "./AtomFamily";
2
2
  import type { AtomOnInit } from "./AtomOnInit";
3
3
  import type { AtomOnSet } from "./AtomOnSet";
4
+ import type { Selector } from "./Selector";
4
5
  export type Atom<Value = any, FamilyKey = undefined> = {
5
- defaultValue?: Value | (() => Value | Promise<Value>);
6
+ defaultValue?: Value | (() => Value | Promise<Value>) | Selector<Value>;
6
7
  label?: string;
7
8
  family?: AtomFamily<Value, FamilyKey>;
8
9
  familyKey?: FamilyKey;
9
10
  onInit?: AtomOnInit<Value>;
10
11
  onSet?: AtomOnSet<Value>;
11
12
  onMount?: () => void | (() => void);
13
+ maxAge?: number;
14
+ staleWhileRevalidate?: number;
12
15
  };
@@ -1,6 +1,6 @@
1
1
  import type { Atom } from "./Atom";
2
- export type AtomFamily<Value, Key> = {
2
+ export type AtomFamily<Value = any, Key = any> = {
3
3
  (key: Key, defaultOverride?: any): Atom<Value>;
4
4
  label?: string;
5
- _map: Map<Key, Atom<Value, Key>>;
5
+ __valdresAtomFamilyMap: Map<Key, Atom<Value, Key>>;
6
6
  };
@@ -1,5 +1,5 @@
1
1
  import type { Selector } from "./Selector";
2
- export type SelectorFamily<Value, Key> = {
2
+ export type SelectorFamily<Value = any, Key = any> = {
3
3
  (key: Key, defaultOverride?: any): Selector<Value>;
4
- _map: Map<Key, Selector<Value, Key>>;
4
+ __valdresSelectorFamilyMap: Map<Key, Selector<Value, Key>>;
5
5
  };
@@ -0,0 +1,2 @@
1
+ import type { AtomFamily } from "../types/AtomFamily";
2
+ export declare const isAtomFamily: (state: any) => state is AtomFamily;
@@ -1 +1 @@
1
- export declare const isFamily: (state: any) => boolean;
1
+ export declare const isFamily: (state: any) => state is import("../..").AtomFamily | import("../..").SelectorFamily;
@@ -0,0 +1,2 @@
1
+ import type { SelectorFamily } from "../types/SelectorFamily";
2
+ export declare const isSelectorFamily: (state: any) => state is SelectorFamily;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.11",
3
+ "version": "0.2.0-alpha.14",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"
@@ -39,5 +39,5 @@
39
39
  "access": "public",
40
40
  "registry": "https://registry.npmjs.org/"
41
41
  },
42
- "gitHead": "081fe9031067f895bb2f79dec2648a88ac891cc9"
42
+ "gitHead": "2a0e8feb476402603356206cb15d6e894dcecf81"
43
43
  }