valdres 0.2.0-alpha.12 → 0.2.0-alpha.15

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
@@ -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.family) {
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.family) {
240
+ if (isFamilyAtom(atom)) {
235
241
  const consumersFamily = data.stateConsumers.get(atom.family);
236
242
  if (consumersFamily?.size) {
237
243
  recursivlyResetSelectorTree(consumersFamily, data, clearedSelectors);
@@ -265,42 +271,65 @@ var setAtom = (atom, newValue, data, skipOnSet = false) => {
265
271
  propagateUpdatedAtoms([atom], data);
266
272
  };
267
273
 
268
- // src/atom.ts
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(newAtom, value, store, true);
297
+ setAtom(atom, value, store, true);
278
298
  }
279
299
  }
280
300
  }
281
301
  };
282
- const newAtom = {
283
- defaultValue,
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
- onInit: (setSelf, store) => {
286
- setSelf(value);
287
- stores.add(store);
288
- },
312
+ defaultValue,
313
+ label: options?.label,
314
+ onInit,
289
315
  onSet,
290
- label: options?.label
316
+ setSelf
291
317
  };
292
- return newAtom;
318
+ return atom;
293
319
  };
294
- var atom = (defaultValue, options) => {
320
+
321
+ // src/atom.ts
322
+ function atom(defaultValue, options) {
295
323
  if (!options)
296
324
  return { defaultValue };
297
- if (options.global)
325
+ if (options.global) {
298
326
  return globalAtom(defaultValue, options);
327
+ }
299
328
  return {
300
329
  defaultValue,
301
330
  ...options
302
331
  };
303
- };
332
+ }
304
333
  // src/lib/stableStringify.ts
305
334
  var stableStringifyRecurse = (x, key) => {
306
335
  if (typeof x === "string" && !x.includes('"') && !x.includes("\\")) {
@@ -355,41 +384,56 @@ var stableStringify = (x) => {
355
384
  return stableStringifyRecurse(x);
356
385
  };
357
386
 
358
- // src/atomFamily.ts
359
- var createOptions = (options, key) => {
360
- if (options.label) {
387
+ // src/lib/atomFamilyAtom.ts
388
+ function atomFamilyAtom(defaultValue, options) {
389
+ if (options.global) {
361
390
  return {
362
391
  ...options,
363
- label: options?.label + "_" + key
392
+ defaultValue
364
393
  };
365
- } else {
366
- return options;
367
394
  }
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
- };
376
- var atomFamily = (defaultValue, options) => {
395
+ return {
396
+ ...options,
397
+ defaultValue
398
+ };
399
+ }
400
+
401
+ // src/atomFamily.ts
402
+ function atomFamily(defaultValue, options) {
377
403
  const map = new Map;
378
404
  const atomFamily2 = (key) => {
379
405
  const keyStringified = stableStringify(key);
380
406
  if (map.has(keyStringified)) {
381
407
  return map.get(keyStringified);
382
408
  }
383
- const newAtom = atom(handleDefaultValue(defaultValue, key), options ? createOptions(options, keyStringified) : undefined);
384
- newAtom.family = atomFamily2;
385
- newAtom.familyKey = Object.freeze(key);
386
- map.set(keyStringified, newAtom);
387
- return newAtom;
409
+ const familyAtom = atomFamilyAtom(handleDefaultValue(defaultValue, key), createOptions(options, atomFamily2, Object.freeze(key), keyStringified));
410
+ map.set(keyStringified, familyAtom);
411
+ return familyAtom;
388
412
  };
389
413
  atomFamily2.__valdresAtomFamilyMap = map;
414
+ atomFamily2.release = (key) => map.delete(key);
390
415
  if (options?.label)
391
416
  atomFamily2.label = options.label;
392
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;
393
437
  };
394
438
  // src/lib/createStoreData.ts
395
439
  var generateId = () => (Math.random() + 1).toString(36).substring(7);
@@ -527,6 +571,8 @@ var setAtoms = (pairs, data) => {
527
571
  const currentValue = getState(atom2, data);
528
572
  if (!equal5(currentValue, value)) {
529
573
  updatedAtoms.push(atom2);
574
+ if (atom2.onSet)
575
+ atom2.onSet(value, data);
530
576
  data.values.set(atom2, value);
531
577
  }
532
578
  }
@@ -1,5 +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 { Selector } from "./types/Selector";
4
- export declare const globalAtom: <Value = any, FamilyKey = undefined>(defaultValue: Value | (() => Value | Promise<Value>), options: AtomOptions<Value>) => Atom<Value>;
5
- export declare const atom: <Value = any, FamilyKey = undefined>(defaultValue?: Value | (() => Value | Promise<Value>) | Selector<Value>, options?: AtomOptions<Value>) => Atom<Value, FamilyKey>;
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>;
@@ -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<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>;
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
- import type { Store } from "./types/Store";
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>;
@@ -0,0 +1,4 @@
1
+ import type { GlobalAtom } from "./../types/GlobalAtom";
2
+ import type { AtomOptions } from "./../types/AtomOptions";
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
- export declare const propagateUpdatedAtoms: (atoms: Atom[], data: StoreData) => void;
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;
@@ -1,12 +1,9 @@
1
- import type { AtomFamily } from "./AtomFamily";
1
+ import type { AtomDefaultValue } from "./AtomDefaultValue";
2
2
  import type { AtomOnInit } from "./AtomOnInit";
3
3
  import type { AtomOnSet } from "./AtomOnSet";
4
- import type { Selector } from "./Selector";
5
- export type Atom<Value = any, FamilyKey = undefined> = {
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);
@@ -0,0 +1,2 @@
1
+ import type { Selector } from "./Selector";
2
+ export type AtomDefaultValue<Value = any> = Value | (() => Value | Promise<Value>) | Selector<Value> | undefined;
@@ -1,6 +1,8 @@
1
- import type { Atom } from "./Atom";
2
- export type AtomFamily<Value = any, Key = any> = {
3
- (key: Key, defaultOverride?: any): Atom<Value>;
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, Atom<Value, Key>>;
7
+ __valdresAtomFamilyMap: Map<Key, AtomFamilyAtom<Value, Key>>;
6
8
  };
@@ -0,0 +1,6 @@
1
+ import type { Atom } from "./Atom";
2
+ import type { AtomFamily } from "./AtomFamily";
3
+ export type AtomFamilyAtom<Value = unknown, Key = unknown> = Atom<Value> & {
4
+ family: AtomFamily<Value, Key>;
5
+ familyKey: Key;
6
+ };
@@ -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
+ };
@@ -0,0 +1,6 @@
1
+ import type { Selector } from "./Selector";
2
+ import type { SelectorFamily } from "./SelectorFamily";
3
+ export type AtomFamilySelector<Value = unknown, Key = unknown> = Selector<Value> & {
4
+ family: SelectorFamily<Value, Key>;
5
+ familyKey: Key;
6
+ };
@@ -1,2 +1,2 @@
1
1
  import type { StoreData } from "./StoreData";
2
- export type AtomOnSet<Value = unknown> = (value: Value, store: StoreData) => void;
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 = any> = {
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
  };
@@ -0,0 +1,3 @@
1
+ export type FamilyKey = PropertyKey | PropertyKey[] | {
2
+ [key: PropertyKey]: FamilyKey;
3
+ };
@@ -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, K>(atom: Atom<V>): V;
6
- <V, K>(selector: Selector<V>): V;
7
- <V, K>(family: AtomFamily<V, K>): K[];
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,5 @@
1
+ import type { Atom } from "./Atom";
2
+ import type { GlobalAtomSetSelfFunc } from "./GlobalAtomSetSelfFunc";
3
+ export type GlobalAtom<Value = unknown> = Atom<Value> & {
4
+ setSelf: GlobalAtomSetSelfFunc<Value>;
5
+ };
@@ -0,0 +1 @@
1
+ export type GlobalAtomSetSelfFunc<Value = unknown> = (value: Value) => void;
@@ -1,3 +1,3 @@
1
1
  import type { Atom } from "./Atom";
2
2
  import type { SetAtomValue } from "./SetAtomValue";
3
- export type SetAtom<V = any> = (atom: Atom<V>, value: SetAtomValue<V>) => void;
3
+ export type SetAtom<Value = any> = (atom: Atom<Value>, value: SetAtomValue<Value>) => void;
@@ -1 +1 @@
1
- export type SetAtomValue<V> = V | ((current: V) => V);
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;
@@ -0,0 +1,2 @@
1
+ import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
2
+ export declare const isFamilyAtom: <K, V>(state: any) => state is AtomFamilyAtom<K, V>;
@@ -0,0 +1,3 @@
1
+ import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
2
+ import type { AtomFamilySelector } from "../types/AtomFamilySelector";
3
+ export declare const isFamilyState: <K, V>(state: any) => state is AtomFamilyAtom<K, V> | AtomFamilySelector<K, V>;
@@ -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.12",
3
+ "version": "0.2.0-alpha.15",
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": "a4d1c93d890e47c6d2fc17639b1b08282466b11b"
42
+ "gitHead": "a8f9987f895b33026266267fff02b41158991986"
43
43
  }