valdres 0.2.0-alpha.14 → 0.2.0-alpha.17
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 +79 -37
- package/dist/src/atom.d.ts +14 -2
- package/dist/src/atomFamily.d.ts +4 -2
- package/dist/src/createStore.d.ts +1 -2
- package/dist/src/lib/atomFamilyAtom.d.ts +8 -0
- package/dist/src/lib/globalAtom.d.ts +3 -2
- package/dist/src/lib/propagateUpdatedAtoms.d.ts +2 -1
- package/dist/src/lib/updateStateSubscribers.d.ts +1 -1
- package/dist/src/types/Atom.d.ts +3 -6
- package/dist/src/types/AtomDefaultValue.d.ts +2 -0
- package/dist/src/types/AtomFamily.d.ts +6 -4
- package/dist/src/types/AtomFamilyAtom.d.ts +6 -0
- package/dist/src/types/AtomFamilyGlobalAtom.d.ts +5 -0
- package/dist/src/types/AtomFamilySelector.d.ts +6 -0
- package/dist/src/types/AtomOnSet.d.ts +1 -1
- package/dist/src/types/AtomOptions.d.ts +3 -1
- package/dist/src/types/FamilyKey.d.ts +3 -0
- package/dist/src/types/GetValue.d.ts +3 -3
- package/dist/src/types/GlobalAtom.d.ts +5 -0
- package/dist/src/types/GlobalAtomSetSelfFunc.d.ts +1 -0
- package/dist/src/types/SetAtom.d.ts +1 -1
- package/dist/src/types/SetAtomValue.d.ts +1 -1
- package/dist/src/types/Store.d.ts +7 -1
- package/dist/src/utils/isAtom.d.ts +1 -1
- package/dist/src/utils/isAtomFamily.d.ts +1 -1
- package/dist/src/utils/isFamily.d.ts +1 -1
- package/dist/src/utils/isFamilyAtom.d.ts +2 -0
- package/dist/src/utils/isFamilyState.d.ts +3 -0
- package/dist/src/utils/isSelector.d.ts +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -6,6 +6,15 @@ var isPromiseLike = (object) => {
|
|
|
6
6
|
return object && object.then && typeof object.then === "function";
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
// src/utils/isAtom.ts
|
|
10
|
+
var isAtom = (state) => Object.hasOwn(state, "defaultValue");
|
|
11
|
+
|
|
12
|
+
// src/utils/isFamilyState.ts
|
|
13
|
+
var isFamilyState = (state) => state && Object.hasOwn(state, "family");
|
|
14
|
+
|
|
15
|
+
// src/utils/isFamilyAtom.ts
|
|
16
|
+
var isFamilyAtom = (state) => isFamilyState(state) && isAtom(state);
|
|
17
|
+
|
|
9
18
|
// src/lib/updateStateSubscribers.ts
|
|
10
19
|
var updateStateSubscribers = (state, data) => {
|
|
11
20
|
const subscribtions = data.subscriptions.get(state);
|
|
@@ -14,7 +23,7 @@ var updateStateSubscribers = (state, data) => {
|
|
|
14
23
|
subscribtion.callback();
|
|
15
24
|
}
|
|
16
25
|
}
|
|
17
|
-
if (state
|
|
26
|
+
if (isFamilyState(state)) {
|
|
18
27
|
const familySubscriptions = data.subscriptions.get(state.family);
|
|
19
28
|
if (familySubscriptions?.size) {
|
|
20
29
|
for (const subscribtion of familySubscriptions) {
|
|
@@ -72,9 +81,6 @@ var initAtom = (atom, data) => {
|
|
|
72
81
|
return value;
|
|
73
82
|
};
|
|
74
83
|
|
|
75
|
-
// src/utils/isAtom.ts
|
|
76
|
-
var isAtom = (state) => Object.hasOwn(state, "defaultValue");
|
|
77
|
-
|
|
78
84
|
// src/utils/isAtomFamily.ts
|
|
79
85
|
var isAtomFamily = (state) => state && Object.hasOwn(state, "__valdresAtomFamilyMap");
|
|
80
86
|
|
|
@@ -231,7 +237,7 @@ var propagateUpdatedAtoms = (atoms, data) => {
|
|
|
231
237
|
if (consumers && consumers.size) {
|
|
232
238
|
recursivlyResetSelectorTree(consumers, data, clearedSelectors);
|
|
233
239
|
}
|
|
234
|
-
if (atom
|
|
240
|
+
if (isFamilyAtom(atom)) {
|
|
235
241
|
const consumersFamily = data.stateConsumers.get(atom.family);
|
|
236
242
|
if (consumersFamily?.size) {
|
|
237
243
|
recursivlyResetSelectorTree(consumersFamily, data, clearedSelectors);
|
|
@@ -266,43 +272,64 @@ var setAtom = (atom, newValue, data, skipOnSet = false) => {
|
|
|
266
272
|
};
|
|
267
273
|
|
|
268
274
|
// src/lib/globalAtom.ts
|
|
275
|
+
var getFirstItemInSet = (set) => {
|
|
276
|
+
for (let item of set) {
|
|
277
|
+
return item;
|
|
278
|
+
}
|
|
279
|
+
throw new Error("Non Empty Set");
|
|
280
|
+
};
|
|
269
281
|
var globalAtom = (defaultValue, options) => {
|
|
270
282
|
const stores = new Set;
|
|
271
283
|
let value = defaultValue;
|
|
284
|
+
if (options.onSet)
|
|
285
|
+
throw new Error("onSet on globalAtom is currently not supported");
|
|
286
|
+
if (options.onInit)
|
|
287
|
+
throw new Error("onInit on globalAtom is currently not supported");
|
|
288
|
+
const onInit = (setSelf2, store) => {
|
|
289
|
+
setSelf2(value);
|
|
290
|
+
stores.add(store);
|
|
291
|
+
};
|
|
272
292
|
const onSet = (newValue, currentStore) => {
|
|
273
293
|
value = newValue;
|
|
274
294
|
if (stores.size > 1) {
|
|
275
295
|
for (const store of stores) {
|
|
276
296
|
if (store.id !== currentStore.id) {
|
|
277
|
-
setAtom(
|
|
297
|
+
setAtom(atom, value, store, true);
|
|
278
298
|
}
|
|
279
299
|
}
|
|
280
300
|
}
|
|
281
301
|
};
|
|
282
|
-
const
|
|
283
|
-
|
|
302
|
+
const setSelf = (newValue) => {
|
|
303
|
+
value = newValue;
|
|
304
|
+
if (stores.size > 0) {
|
|
305
|
+
getFirstItemInSet(stores);
|
|
306
|
+
const store = getFirstItemInSet(stores);
|
|
307
|
+
setAtom(atom, newValue, store);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
const atom = {
|
|
284
311
|
...options,
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
},
|
|
312
|
+
defaultValue,
|
|
313
|
+
label: options?.label,
|
|
314
|
+
onInit,
|
|
289
315
|
onSet,
|
|
290
|
-
|
|
316
|
+
setSelf
|
|
291
317
|
};
|
|
292
|
-
return
|
|
318
|
+
return atom;
|
|
293
319
|
};
|
|
294
320
|
|
|
295
321
|
// src/atom.ts
|
|
296
|
-
|
|
322
|
+
function atom(defaultValue, options) {
|
|
297
323
|
if (!options)
|
|
298
324
|
return { defaultValue };
|
|
299
|
-
if (options.global)
|
|
325
|
+
if (options.global) {
|
|
300
326
|
return globalAtom(defaultValue, options);
|
|
327
|
+
}
|
|
301
328
|
return {
|
|
302
329
|
defaultValue,
|
|
303
330
|
...options
|
|
304
331
|
};
|
|
305
|
-
}
|
|
332
|
+
}
|
|
306
333
|
// src/lib/stableStringify.ts
|
|
307
334
|
var stableStringifyRecurse = (x, key) => {
|
|
308
335
|
if (typeof x === "string" && !x.includes('"') && !x.includes("\\")) {
|
|
@@ -357,41 +384,56 @@ var stableStringify = (x) => {
|
|
|
357
384
|
return stableStringifyRecurse(x);
|
|
358
385
|
};
|
|
359
386
|
|
|
360
|
-
// src/
|
|
361
|
-
|
|
362
|
-
if (options.
|
|
387
|
+
// src/lib/atomFamilyAtom.ts
|
|
388
|
+
function atomFamilyAtom(defaultValue, options) {
|
|
389
|
+
if (options.global) {
|
|
363
390
|
return {
|
|
364
391
|
...options,
|
|
365
|
-
|
|
392
|
+
defaultValue
|
|
366
393
|
};
|
|
367
|
-
} else {
|
|
368
|
-
return options;
|
|
369
394
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
var atomFamily = (defaultValue, options) => {
|
|
395
|
+
return {
|
|
396
|
+
...options,
|
|
397
|
+
defaultValue
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// src/atomFamily.ts
|
|
402
|
+
function atomFamily(defaultValue, options) {
|
|
379
403
|
const map = new Map;
|
|
380
404
|
const atomFamily2 = (key) => {
|
|
381
405
|
const keyStringified = stableStringify(key);
|
|
382
406
|
if (map.has(keyStringified)) {
|
|
383
407
|
return map.get(keyStringified);
|
|
384
408
|
}
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
map.set(keyStringified, newAtom);
|
|
389
|
-
return newAtom;
|
|
409
|
+
const familyAtom = atomFamilyAtom(handleDefaultValue(defaultValue, key), createOptions(options, atomFamily2, Object.freeze(key), keyStringified));
|
|
410
|
+
map.set(keyStringified, familyAtom);
|
|
411
|
+
return familyAtom;
|
|
390
412
|
};
|
|
391
413
|
atomFamily2.__valdresAtomFamilyMap = map;
|
|
414
|
+
atomFamily2.release = (key) => map.delete(key);
|
|
392
415
|
if (options?.label)
|
|
393
416
|
atomFamily2.label = options.label;
|
|
394
417
|
return atomFamily2;
|
|
418
|
+
}
|
|
419
|
+
var createOptions = (options = {}, family, familyKey, keyStringified) => {
|
|
420
|
+
if (options.label) {
|
|
421
|
+
return {
|
|
422
|
+
...options,
|
|
423
|
+
label: options?.label + "_" + keyStringified,
|
|
424
|
+
family,
|
|
425
|
+
familyKey
|
|
426
|
+
};
|
|
427
|
+
} else {
|
|
428
|
+
return { ...options, family, familyKey };
|
|
429
|
+
}
|
|
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;
|
|
395
437
|
};
|
|
396
438
|
// src/lib/createStoreData.ts
|
|
397
439
|
var generateId = () => (Math.random() + 1).toString(36).substring(7);
|
package/dist/src/atom.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import type { Atom } from "./types/Atom";
|
|
2
|
+
import type { AtomDefaultValue } from "./types/AtomDefaultValue";
|
|
2
3
|
import type { AtomOptions } from "./types/AtomOptions";
|
|
3
|
-
import type {
|
|
4
|
-
|
|
4
|
+
import type { GlobalAtom } from "./types/GlobalAtom";
|
|
5
|
+
/**
|
|
6
|
+
* [Docs Reference](https://valdres.dev/valdres/api/atom)
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* const user = atom<string>("Default Value", { label: "userAtom"})
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export declare function atom<V>(defaultValue: AtomDefaultValue<V>, options: AtomOptions<V> & {
|
|
14
|
+
global: true;
|
|
15
|
+
}): GlobalAtom<V>;
|
|
16
|
+
export declare function atom<V>(defaultValue: AtomDefaultValue<V>, options?: AtomOptions<V>): Atom<V>;
|
package/dist/src/atomFamily.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { AtomFamily } from "./types/AtomFamily";
|
|
2
2
|
import type { AtomOptions } from "./types/AtomOptions";
|
|
3
|
+
import type { FamilyKey } from "./types/FamilyKey";
|
|
3
4
|
import type { Selector } from "./types/Selector";
|
|
4
5
|
import type { SelectorFamily } from "./types/SelectorFamily";
|
|
5
6
|
type DefaultValueCallback<Key, Value> = (arg: Key) => Value | Promise<Value>;
|
|
6
|
-
type AtomFamilyDefaultValue<
|
|
7
|
-
|
|
7
|
+
type AtomFamilyDefaultValue<Key, Value> = undefined | Value | DefaultValueCallback<Key, Value> | Selector<Value> | SelectorFamily<Value, Key>;
|
|
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>;
|
|
8
10
|
export {};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const createStore: (id?: string) => Store;
|
|
1
|
+
export declare const createStore: (id?: string) => import("..").Store;
|
|
@@ -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<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,3 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { GlobalAtom } from "./../types/GlobalAtom";
|
|
2
2
|
import type { AtomOptions } from "./../types/AtomOptions";
|
|
3
|
-
|
|
3
|
+
import type { AtomDefaultValue } from "../types/AtomDefaultValue";
|
|
4
|
+
export declare const globalAtom: <Value = any>(defaultValue: AtomDefaultValue<Value>, options: AtomOptions<Value>) => GlobalAtom<Value>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { Atom } from "../types/Atom";
|
|
2
2
|
import type { StoreData } from "../types/StoreData";
|
|
3
|
-
|
|
3
|
+
import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
|
|
4
|
+
export declare const propagateUpdatedAtoms: (atoms: (Atom<any> | AtomFamilyAtom<any, any>)[], data: StoreData) => void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { State } from "../types/State";
|
|
2
2
|
import type { StoreData } from "../types/StoreData";
|
|
3
|
-
export declare const updateStateSubscribers: (state: State, data: StoreData) => void;
|
|
3
|
+
export declare const updateStateSubscribers: <V>(state: State, data: StoreData) => void;
|
package/dist/src/types/Atom.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AtomDefaultValue } from "./AtomDefaultValue";
|
|
2
2
|
import type { AtomOnInit } from "./AtomOnInit";
|
|
3
3
|
import type { AtomOnSet } from "./AtomOnSet";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
defaultValue?: Value | (() => Value | Promise<Value>) | Selector<Value>;
|
|
4
|
+
export type Atom<Value = unknown> = {
|
|
5
|
+
defaultValue?: AtomDefaultValue<Value>;
|
|
7
6
|
label?: string;
|
|
8
|
-
family?: AtomFamily<Value, FamilyKey>;
|
|
9
|
-
familyKey?: FamilyKey;
|
|
10
7
|
onInit?: AtomOnInit<Value>;
|
|
11
8
|
onSet?: AtomOnSet<Value>;
|
|
12
9
|
onMount?: () => void | (() => void);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
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;
|
|
4
6
|
label?: string;
|
|
5
|
-
__valdresAtomFamilyMap: Map<Key,
|
|
7
|
+
__valdresAtomFamilyMap: Map<Key, AtomFamilyAtom<Value, Key>>;
|
|
6
8
|
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AtomFamilyAtom } from "./AtomFamilyAtom";
|
|
2
|
+
import type { GlobalAtomSetSelfFunc } from "./GlobalAtomSetSelfFunc";
|
|
3
|
+
export type AtomFamilyGlobalAtom<Value = unknown, Key = unknown> = AtomFamilyAtom<Value, Key> & {
|
|
4
|
+
setSelf: GlobalAtomSetSelfFunc<Value>;
|
|
5
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { StoreData } from "./StoreData";
|
|
2
|
-
export type AtomOnSet<Value =
|
|
2
|
+
export type AtomOnSet<Value = any> = (value: Value, store: StoreData) => void;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { AtomOnSet } from "./AtomOnSet";
|
|
2
2
|
import type { StoreData } from "./StoreData";
|
|
3
|
-
export type AtomOptions<Value =
|
|
3
|
+
export type AtomOptions<Value = unknown> = {
|
|
4
4
|
global?: boolean;
|
|
5
5
|
label?: string;
|
|
6
6
|
onInit?: (setSelf: (value: Value) => void, store: StoreData) => void;
|
|
7
7
|
onSet?: AtomOnSet<Value>;
|
|
8
8
|
onMount?: () => () => void;
|
|
9
|
+
maxAge?: number;
|
|
10
|
+
staleWhileRevalidate?: number;
|
|
9
11
|
};
|
|
@@ -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
|
|
6
|
-
<V
|
|
7
|
-
<V, K>(family: AtomFamily<
|
|
5
|
+
<V>(atom: Atom<V>): V;
|
|
6
|
+
<V>(selector: Selector<V>): V;
|
|
7
|
+
<V, K>(family: AtomFamily<K, V>): K[];
|
|
8
8
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type GlobalAtomSetSelfFunc<Value = unknown> = (value: Value) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export type SetAtomValue<
|
|
1
|
+
export type SetAtomValue<Value> = Value | ((current: Value) => Value);
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import type { Atom } from "./Atom";
|
|
2
|
+
import type { AtomFamilyAtom } from "./AtomFamilyAtom";
|
|
1
3
|
import type { GetValue } from "./GetValue";
|
|
2
4
|
import type { ResetAtom } from "./ResetAtom";
|
|
3
|
-
import type { SetAtom } from "./SetAtom";
|
|
4
5
|
import type { StoreData } from "./StoreData";
|
|
5
6
|
import type { SubscribeFn } from "./SubscribeFn";
|
|
6
7
|
import type { TransactionFn } from "./TransactionFn";
|
|
8
|
+
type SetAtom = {
|
|
9
|
+
<V, K>(atom: AtomFamilyAtom<V, K>, value: V): void;
|
|
10
|
+
<V>(atom: Atom<V>, value: V): void;
|
|
11
|
+
};
|
|
7
12
|
export type Store = {
|
|
8
13
|
data: StoreData;
|
|
9
14
|
get: GetValue;
|
|
@@ -12,3 +17,4 @@ export type Store = {
|
|
|
12
17
|
reset: ResetAtom;
|
|
13
18
|
txn: (callback: TransactionFn) => void;
|
|
14
19
|
};
|
|
20
|
+
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Atom } from "../types/Atom";
|
|
2
|
-
export declare const isAtom: (state: any) => state is Atom
|
|
2
|
+
export declare const isAtom: <V>(state: any) => state is Atom<V>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { AtomFamily } from "../types/AtomFamily";
|
|
2
|
-
export declare const isAtomFamily: (state: any) => state is AtomFamily
|
|
2
|
+
export declare const isAtomFamily: <K, V>(state: any) => state is AtomFamily<K, V>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const isFamily: (state: any) => state is import("../..").AtomFamily | import("../..").SelectorFamily;
|
|
1
|
+
export declare const isFamily: (state: any) => state is import("../..").AtomFamily<unknown, unknown> | import("../..").SelectorFamily;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Selector } from "../types/Selector";
|
|
2
|
-
export declare const isSelector: (state: any) => state is Selector
|
|
2
|
+
export declare const isSelector: <V>(state: any) => state is Selector<V>;
|
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.17",
|
|
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": "40d44bfa3d776180101f17371766c5746bb9cd3f"
|
|
43
43
|
}
|