valdres 0.2.0-alpha.31 → 0.2.0-alpha.32

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
@@ -611,15 +611,18 @@ var store = (id) => {
611
611
  return storeFromStoreData(data);
612
612
  };
613
613
  // package.json
614
- var version = "0.2.0-alpha.31";
614
+ var version = "0.2.0-alpha.32";
615
615
 
616
- // src/lib/globalStore.ts
616
+ // src/globalStore.ts
617
617
  if (globalThis.__valdres__) {
618
618
  throw new Error(`Error! An instance of valdres is already loaded`);
619
619
  } else {
620
620
  globalThis.__valdres__ = version;
621
621
  }
622
- var globalStore = store("global");
622
+ var globalStore = Object.assign(store("valdres-global-store"), {
623
+ atoms: new Map,
624
+ atomFamilies: new Map
625
+ });
623
626
 
624
627
  // src/lib/globalAtom.ts
625
628
  var globalAtom = (defaultValue, options) => {
@@ -670,7 +673,7 @@ var globalAtom = (defaultValue, options) => {
670
673
  equal: equal2,
671
674
  ...options,
672
675
  defaultValue,
673
- label: options?.label,
676
+ name: options?.name,
674
677
  onInit,
675
678
  onSet,
676
679
  setSelf,
@@ -696,7 +699,7 @@ function atom(defaultValue, options) {
696
699
  ...options
697
700
  };
698
701
  }
699
- // src/atomFamily.ts
702
+ // src/lib/createAtomFamily.ts
700
703
  import equal5 from "fast-deep-equal/es6";
701
704
 
702
705
  // src/lib/atomFamilyAtom.ts
@@ -772,13 +775,13 @@ var selector = (get, options) => {
772
775
  return { equal: equal4, ...options, get };
773
776
  };
774
777
 
775
- // src/atomFamily.ts
778
+ // src/lib/createAtomFamily.ts
776
779
  var createOptions = (options = {}, family, familyKey, keyStringified) => {
777
- if (options.label) {
780
+ if (options.name) {
778
781
  return {
779
782
  equal: equal5,
780
783
  ...options,
781
- label: options?.label + "_" + keyStringified,
784
+ name: options?.name + "_" + keyStringified,
782
785
  family,
783
786
  familyKey
784
787
  };
@@ -793,25 +796,46 @@ var handleDefaultValue = (defaultValue, key) => {
793
796
  return () => defaultValue(key);
794
797
  return defaultValue;
795
798
  };
796
- function atomFamily(defaultValue, options) {
799
+ var createAtomFamily = (defaultValue, options) => {
797
800
  const map = new Map;
798
- const atomFamily2 = (key) => {
801
+ const keysAtom = atom(new Set);
802
+ const atomFamily = Object.assign((key) => {
799
803
  const keyStringified = stableStringify(key);
800
804
  if (map.has(keyStringified)) {
801
805
  return map.get(keyStringified);
802
806
  }
803
- const familyAtom = atomFamilyAtom(handleDefaultValue(defaultValue, key), createOptions(options, atomFamily2, Object.freeze(key), keyStringified));
807
+ const familyAtom = atomFamilyAtom(handleDefaultValue(defaultValue, key), createOptions(options, atomFamily, Object.freeze(key), keyStringified));
804
808
  map.set(keyStringified, familyAtom);
805
809
  return familyAtom;
806
- };
807
- atomFamily2.__valdresAtomFamilyMap = map;
808
- atomFamily2.release = (key) => map.delete(key);
809
- const keysAtom = atom(new Set);
810
- atomFamily2.__keysAtom = keysAtom;
811
- atomFamily2.__keysSelector = selector((get) => Array.from(get(keysAtom)));
812
- if (options?.label)
813
- atomFamily2.label = options.label;
814
- return atomFamily2;
810
+ }, {
811
+ __valdresAtomFamilyMap: map,
812
+ release: (key) => map.delete(key),
813
+ __keysAtom: keysAtom,
814
+ __keysSelector: selector((get) => Array.from(get(keysAtom)))
815
+ });
816
+ if (options?.name)
817
+ Object.defineProperty(atomFamily, "name", {
818
+ value: options.name,
819
+ writable: false
820
+ });
821
+ return atomFamily;
822
+ };
823
+
824
+ // src/lib/createGlobalAtomFamily.ts
825
+ var createGlobalAtomFamily = (defaultValue, options) => {
826
+ if (globalStore.atomFamilies.has(options.name)) {
827
+ return globalStore.atomFamilies.get(options.name);
828
+ }
829
+ const family = createAtomFamily(defaultValue, options);
830
+ globalStore.atomFamilies.set(options.name, family);
831
+ return family;
832
+ };
833
+
834
+ // src/atomFamily.ts
835
+ function atomFamily(defaultValue, options) {
836
+ if (options?.global)
837
+ return createGlobalAtomFamily(defaultValue, options);
838
+ return createAtomFamily(defaultValue, options);
815
839
  }
816
840
  // src/createStoreWithSelectorSet.ts
817
841
  var setSelector = (selector2, values, store2) => {
@@ -833,11 +857,11 @@ var createStoreWithSelectorSet = (id) => {
833
857
  // src/selectorFamily.ts
834
858
  import equal6 from "fast-deep-equal/es6";
835
859
  var createOptions2 = (options = {}, family, familyKey, keyStringified) => {
836
- if (options.label) {
860
+ if (options.name) {
837
861
  return {
838
862
  equal: equal6,
839
863
  ...options,
840
- label: options?.label + "_" + keyStringified,
864
+ name: options?.name + "_" + keyStringified,
841
865
  family,
842
866
  familyKey
843
867
  };
@@ -856,8 +880,11 @@ var selectorFamily = (get, options) => {
856
880
  return newSelector;
857
881
  };
858
882
  selectorFamily2.__valdresSelectorFamilyMap = map;
859
- if (options?.label)
860
- selectorFamily2.label = options.label;
883
+ if (options?.name)
884
+ Object.defineProperty(selectorFamily2, "name", {
885
+ value: options.name,
886
+ writable: false
887
+ });
861
888
  return selectorFamily2;
862
889
  };
863
890
  // src/utils/isFamilySelector.ts
@@ -7,7 +7,7 @@ import type { GlobalAtom } from "./types/GlobalAtom";
7
7
  *
8
8
  * @example
9
9
  *
10
- * const user = atom<string>("Default Value", { label: "userAtom"})
10
+ * const user = atom<string>("Default Value", { name: "userAtom"})
11
11
  *
12
12
  */
13
13
  export declare function atom<V>(defaultValue: AtomDefaultValue<V>, options: AtomOptions<V> & {
@@ -1,10 +1,4 @@
1
- import type { AtomFamily } from "./types/AtomFamily";
2
1
  import type { AtomOptions } from "./types/AtomOptions";
3
2
  import type { FamilyKey } from "./types/FamilyKey";
4
- import type { Selector } from "./types/Selector";
5
- import type { SelectorFamily } from "./types/SelectorFamily";
6
- type DefaultValueCallback<Key, Value> = (arg: Key) => Value | Promise<Value>;
7
- type AtomFamilyDefaultValue<Key, Value> = undefined | Value | DefaultValueCallback<Key, Value> | Selector<Value> | SelectorFamily<Key, Value>;
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>;
10
- export {};
3
+ import type { AtomFamilyDefaultValue } from "./types/AtomFamilyDefaultValue";
4
+ export declare function atomFamily<Key = FamilyKey, Value = unknown>(defaultValue?: AtomFamilyDefaultValue<Key, Value>, options?: AtomOptions<Value>): import("..").AtomFamily<Key, Value>;
@@ -0,0 +1,4 @@
1
+ export declare const globalStore: import("..").Store & {
2
+ atoms: Map<any, any>;
3
+ atomFamilies: Map<any, any>;
4
+ };
@@ -0,0 +1,4 @@
1
+ import type { AtomFamily } from "../types/AtomFamily";
2
+ import type { AtomOptions } from "../types/AtomOptions";
3
+ import type { AtomFamilyDefaultValue } from "../types/AtomFamilyDefaultValue";
4
+ export declare const createAtomFamily: <Key, Value>(defaultValue: AtomFamilyDefaultValue<Key, Value>, options?: AtomOptions<Value>) => AtomFamily<Key, Value>;
@@ -0,0 +1,5 @@
1
+ import type { AtomOptions } from "../types/AtomOptions";
2
+ import type { FamilyKey } from "../types/FamilyKey";
3
+ import type { AtomFamilyDefaultValue } from "../types/AtomFamilyDefaultValue";
4
+ import type { AtomFamily } from "../types/AtomFamily";
5
+ export declare const createGlobalAtomFamily: <Key = FamilyKey, Value = unknown>(defaultValue: AtomFamilyDefaultValue<Key, Value>, options: AtomOptions<Value>) => AtomFamily<Key, Value>;
@@ -5,7 +5,7 @@ import type { EqualFunc } from "./EqualFunc";
5
5
  export type Atom<Value = unknown> = {
6
6
  equal: EqualFunc<Value>;
7
7
  defaultValue?: AtomDefaultValue<Value>;
8
- label?: string;
8
+ name?: string;
9
9
  onInit?: AtomOnInit<Value>;
10
10
  onSet?: AtomOnSet<Value>;
11
11
  onMount?: () => void | (() => void);
@@ -5,7 +5,7 @@ import type { Selector } from "./Selector";
5
5
  export type AtomFamily<Key = FamilyKey, Value = unknown> = {
6
6
  (key: Key): AtomFamilyAtom<Key, Value>;
7
7
  release: (key: Key) => void;
8
- label?: string;
8
+ name?: string;
9
9
  __valdresAtomFamilyMap: Map<Key, AtomFamilyAtom<Key, Value>>;
10
10
  __keysAtom: Atom<Set<Key>>;
11
11
  __keysSelector: Selector<Key[]>;
@@ -0,0 +1,5 @@
1
+ import type { Selector } from "./Selector";
2
+ import type { SelectorFamily } from "./SelectorFamily";
3
+ type DefaultValueCallback<Key, Value> = (arg: Key) => Value | Promise<Value>;
4
+ export type AtomFamilyDefaultValue<Key, Value> = undefined | Value | DefaultValueCallback<Key, Value> | Selector<Value> | SelectorFamily<Key, Value>;
5
+ export {};
@@ -3,7 +3,7 @@ import type { EqualFunc } from "./EqualFunc";
3
3
  import type { StoreData } from "./StoreData";
4
4
  export type AtomOptions<Value = unknown> = {
5
5
  global?: boolean;
6
- label?: string;
6
+ name?: string;
7
7
  onInit?: (setSelf: (value: Value) => void, store: StoreData) => (() => void) | void;
8
8
  onSet?: AtomOnSet<Value>;
9
9
  onMount?: () => () => void;
@@ -4,7 +4,7 @@ import type { SelectorFamily } from "./SelectorFamily";
4
4
  export type Selector<Value = unknown, FamilyKey = undefined> = {
5
5
  get: (get: GetValue, storeId: string) => Value;
6
6
  equal: EqualFunc<Value>;
7
- label?: string;
7
+ name?: string;
8
8
  family?: SelectorFamily<FamilyKey, Value>;
9
9
  familyKey?: FamilyKey;
10
10
  onMount?: () => void | (() => void);
@@ -1,5 +1,5 @@
1
1
  import type { EqualFunc } from "./EqualFunc";
2
2
  export type SelectorOptions<Value> = {
3
- label?: string;
3
+ name?: string;
4
4
  equal?: EqualFunc<Value>;
5
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.31",
3
+ "version": "0.2.0-alpha.32",
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": "7196fb6f087130428b9b0c2ea76b9d1690b45fb4"
42
+ "gitHead": "27342cf65a13e2ec86f5f400d814385d0c8de2cd"
43
43
  }
@@ -1 +0,0 @@
1
- export declare const globalStore: import("../..").Store;