valdres 0.2.0-alpha.11 → 0.2.0-alpha.12
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 +82 -26
- package/dist/src/atom.d.ts +2 -1
- package/dist/src/atomFamily.d.ts +4 -1
- package/dist/src/lib/unsubscribe.d.ts +1 -1
- package/dist/src/types/Atom.d.ts +4 -1
- package/dist/src/types/AtomFamily.d.ts +2 -2
- package/dist/src/types/SelectorFamily.d.ts +2 -2
- package/dist/src/utils/isAtomFamily.d.ts +2 -0
- package/dist/src/utils/isFamily.d.ts +1 -1
- package/dist/src/utils/isSelectorFamily.d.ts +2 -0
- package/package.json +2 -2
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/
|
|
74
|
-
var
|
|
78
|
+
// src/utils/isAtomFamily.ts
|
|
79
|
+
var isAtomFamily = (state) => state && Object.hasOwn(state, "__valdresAtomFamilyMap");
|
|
75
80
|
|
|
76
|
-
// src/utils/
|
|
77
|
-
var
|
|
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 (
|
|
88
|
-
const array = Array.from(state.
|
|
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;
|
|
@@ -354,6 +366,13 @@ var createOptions = (options, key) => {
|
|
|
354
366
|
return options;
|
|
355
367
|
}
|
|
356
368
|
};
|
|
369
|
+
var handleDefaultValue = (defaultValue, key) => {
|
|
370
|
+
if (isSelectorFamily(defaultValue))
|
|
371
|
+
return defaultValue(key);
|
|
372
|
+
if (typeof defaultValue === "function")
|
|
373
|
+
return () => defaultValue(key);
|
|
374
|
+
return defaultValue;
|
|
375
|
+
};
|
|
357
376
|
var atomFamily = (defaultValue, options) => {
|
|
358
377
|
const map = new Map;
|
|
359
378
|
const atomFamily2 = (key) => {
|
|
@@ -361,13 +380,13 @@ var atomFamily = (defaultValue, options) => {
|
|
|
361
380
|
if (map.has(keyStringified)) {
|
|
362
381
|
return map.get(keyStringified);
|
|
363
382
|
}
|
|
364
|
-
const newAtom = atom(
|
|
383
|
+
const newAtom = atom(handleDefaultValue(defaultValue, key), options ? createOptions(options, keyStringified) : undefined);
|
|
365
384
|
newAtom.family = atomFamily2;
|
|
366
385
|
newAtom.familyKey = Object.freeze(key);
|
|
367
386
|
map.set(keyStringified, newAtom);
|
|
368
387
|
return newAtom;
|
|
369
388
|
};
|
|
370
|
-
atomFamily2.
|
|
389
|
+
atomFamily2.__valdresAtomFamilyMap = map;
|
|
371
390
|
if (options?.label)
|
|
372
391
|
atomFamily2.label = options.label;
|
|
373
392
|
return atomFamily2;
|
|
@@ -386,15 +405,19 @@ var createStoreData = (id = generateId()) => ({
|
|
|
386
405
|
|
|
387
406
|
// src/lib/resetAtom.ts
|
|
388
407
|
var resetAtom = (atom2, data) => {
|
|
389
|
-
|
|
390
|
-
|
|
408
|
+
let value = getAtomInitValue(atom2, data);
|
|
409
|
+
data.values.set(atom2, value);
|
|
410
|
+
if (!isPromiseLike(value)) {
|
|
391
411
|
propagateUpdatedAtoms([atom2], data);
|
|
392
412
|
}
|
|
393
|
-
return
|
|
413
|
+
return value;
|
|
394
414
|
};
|
|
395
415
|
|
|
416
|
+
// src/utils/isFamily.ts
|
|
417
|
+
var isFamily = (state) => isAtomFamily(state) || isSelectorFamily(state);
|
|
418
|
+
|
|
396
419
|
// src/lib/unsubscribe.ts
|
|
397
|
-
var unsubscribe = (state, subscription, data, mount) => {
|
|
420
|
+
var unsubscribe = (state, subscription, data, mount, maxAgeCleanup) => {
|
|
398
421
|
const subscribers = data.subscriptions.get(state);
|
|
399
422
|
if (subscribers) {
|
|
400
423
|
subscribers.delete(subscription);
|
|
@@ -410,6 +433,10 @@ var unsubscribe = (state, subscription, data, mount) => {
|
|
|
410
433
|
data.subscriptionsRequireEqualCheck.delete(state);
|
|
411
434
|
}
|
|
412
435
|
}
|
|
436
|
+
if (subscribers.size === 0) {
|
|
437
|
+
if (maxAgeCleanup)
|
|
438
|
+
maxAgeCleanup();
|
|
439
|
+
}
|
|
413
440
|
if (mount) {
|
|
414
441
|
if (subscribers.size === mount.mountSubscriptions.size) {
|
|
415
442
|
if (typeof mount.onUnmount === "function") {
|
|
@@ -446,23 +473,50 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
|
|
|
446
473
|
}
|
|
447
474
|
subscribers.add(subscription);
|
|
448
475
|
let mount;
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
476
|
+
let maxAgeCleanup;
|
|
477
|
+
if (subscribers.size === 1) {
|
|
478
|
+
if (isAtom(state) && state.maxAge) {
|
|
479
|
+
let timeout;
|
|
480
|
+
const interval = setInterval(() => {
|
|
481
|
+
let value = getAtomInitValue(state, data);
|
|
482
|
+
if (isPromiseLike(value)) {
|
|
483
|
+
if (state.staleWhileRevalidate) {
|
|
484
|
+
const oldValue = data.values.get(state);
|
|
485
|
+
timeout = setTimeout(() => {
|
|
486
|
+
const nowValue = data.values.get(state);
|
|
487
|
+
console.log("todo", oldValue);
|
|
488
|
+
}, state.staleWhileRevalidate);
|
|
489
|
+
value.then((res) => clearTimeout(timeout));
|
|
490
|
+
}
|
|
491
|
+
} else {
|
|
492
|
+
data.values.set(state, value);
|
|
493
|
+
propagateUpdatedAtoms([state], data);
|
|
494
|
+
}
|
|
495
|
+
}, state.maxAge);
|
|
496
|
+
maxAgeCleanup = () => {
|
|
497
|
+
clearInterval(interval);
|
|
498
|
+
if (timeout)
|
|
499
|
+
clearTimeout(timeout);
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
if (state.onMount) {
|
|
503
|
+
const store = storeFromStoreData(data);
|
|
504
|
+
const mountSubscriptions = new Set;
|
|
505
|
+
const originalSub = store.sub;
|
|
506
|
+
store.sub = (state2, callback2) => {
|
|
507
|
+
mountSubscriptions.add(callback2);
|
|
508
|
+
return originalSub(state2, callback2);
|
|
509
|
+
};
|
|
510
|
+
mount = {
|
|
511
|
+
onUnmount: state.onMount(store, state),
|
|
512
|
+
mountSubscriptions
|
|
513
|
+
};
|
|
514
|
+
}
|
|
461
515
|
}
|
|
462
516
|
if (requireDeepEqualCheckBeforeCallback && data.subscriptionsRequireEqualCheck.get(state) !== true) {
|
|
463
517
|
data.subscriptionsRequireEqualCheck.set(state, true);
|
|
464
518
|
}
|
|
465
|
-
return () => unsubscribe(state, subscription, data, mount);
|
|
519
|
+
return () => unsubscribe(state, subscription, data, mount, maxAgeCleanup);
|
|
466
520
|
};
|
|
467
521
|
|
|
468
522
|
// src/lib/setAtoms.ts
|
|
@@ -636,7 +690,9 @@ var selectorFamily = (get, options) => {
|
|
|
636
690
|
map.set(keyStringified, newSelector);
|
|
637
691
|
return newSelector;
|
|
638
692
|
};
|
|
639
|
-
selectorFamily2.
|
|
693
|
+
selectorFamily2.__valdresSelectorFamilyMap = map;
|
|
694
|
+
if (options?.label)
|
|
695
|
+
selectorFamily2.label = options.label;
|
|
640
696
|
return selectorFamily2;
|
|
641
697
|
};
|
|
642
698
|
export {
|
package/dist/src/atom.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Atom } from "./types/Atom";
|
|
2
2
|
import type { AtomOptions } from "./types/AtomOptions";
|
|
3
|
+
import type { Selector } from "./types/Selector";
|
|
3
4
|
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>)
|
|
5
|
+
export declare const atom: <Value = any, FamilyKey = undefined>(defaultValue?: Value | (() => Value | Promise<Value>) | Selector<Value>, options?: AtomOptions<Value>) => Atom<Value, FamilyKey>;
|
package/dist/src/atomFamily.d.ts
CHANGED
|
@@ -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
|
-
|
|
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 {};
|
|
@@ -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;
|
package/dist/src/types/Atom.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
4
|
+
__valdresSelectorFamilyMap: Map<Key, Selector<Value, Key>>;
|
|
5
5
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const isFamily: (state: any) =>
|
|
1
|
+
export declare const isFamily: (state: any) => state is import("../..").AtomFamily | import("../..").SelectorFamily;
|
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.12",
|
|
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": "
|
|
42
|
+
"gitHead": "a4d1c93d890e47c6d2fc17639b1b08282466b11b"
|
|
43
43
|
}
|