valdres 0.2.0-pre.11 → 0.2.0-pre.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
@@ -278,7 +278,7 @@ function getState(state, data, initializedAtomsSet, circularDependencySet) {
278
278
  const closestData = findClosestStoreWithAtomInitialized(state, data);
279
279
  return getState(state, closestData, initializedAtomsSet, circularDependencySet);
280
280
  }
281
- data.values.set(state, []);
281
+ data.values.set(state, new FamilyIndex(state, data).toArray());
282
282
  initializedAtomsSet.add(state);
283
283
  return data.values.get(state);
284
284
  }
@@ -422,28 +422,125 @@ var addSetToSet = (fromSet, toSet) => {
422
422
  }
423
423
  }
424
424
  };
425
- var findClosestStoreWithAtomInitialized2 = (atom, data) => {
426
- if ("parent" in data === false)
427
- return data;
428
- if (data.values.has(atom))
429
- return data;
430
- return findClosestStoreWithAtomInitialized2(atom, data.parent);
425
+ class FamilyIndex {
426
+ map;
427
+ family;
428
+ data;
429
+ parentIndex;
430
+ constructor(family, data, parentIndex, map) {
431
+ this.family = family;
432
+ this.data = data;
433
+ this.parentIndex = parentIndex;
434
+ this.map = new Map(map);
435
+ }
436
+ add(atoms) {
437
+ atoms.forEach((atom) => {
438
+ this.map.set(atom, true);
439
+ });
440
+ }
441
+ delete(atoms) {
442
+ atoms.forEach((atom) => {
443
+ this.map.set(atom, false);
444
+ });
445
+ }
446
+ has(atom) {
447
+ return this.map.has(atom);
448
+ }
449
+ clone() {
450
+ return new FamilyIndex(this.family, this.data, this.parentIndex, this.map);
451
+ }
452
+ toSet() {
453
+ let set;
454
+ if (this.parentIndex) {
455
+ set = new Set(this.parentIndex.toSet());
456
+ } else {
457
+ set = new Set;
458
+ }
459
+ for (const [key, value] of this.map) {
460
+ if (value === true)
461
+ set.add(key);
462
+ if (value === false)
463
+ set.delete(key);
464
+ }
465
+ return set;
466
+ }
467
+ toArray() {
468
+ const arr = [...this.toSet()];
469
+ arr.__index = this;
470
+ return arr;
471
+ }
472
+ }
473
+ var deleteFamilyAtomsFromSet = (family, familyAtoms, data) => {
474
+ const index = findFamilyIndex(family, data);
475
+ index.delete(familyAtoms);
476
+ data.values.set(family, index.toArray());
477
+ recursivlyUpdateIndexes(data, family);
478
+ };
479
+ var initFamilyIndex = (family, data) => {
480
+ if (data.values.has(family))
481
+ return;
482
+ if ("parent" in data) {
483
+ initFamilyIndex(family, data.parent);
484
+ const parentIndex = data.parent.values.get(family).__index;
485
+ if (!parentIndex)
486
+ throw new Error("Parent index is missing");
487
+ const index = new FamilyIndex(family, data, parentIndex);
488
+ data.values.set(family, index.toArray());
489
+ } else {
490
+ const index = new FamilyIndex(family, data);
491
+ data.values.set(family, index.toArray());
492
+ }
431
493
  };
432
- var findInClosestStore = (state, data) => {
433
- const store = findClosestStoreWithAtomInitialized2(state, data);
434
- return store.values.get(state);
494
+ var findFamilyIndex = (family, data) => {
495
+ if (!data.values.has(family)) {
496
+ initFamilyIndex(family, data);
497
+ }
498
+ const value = data.values.get(family);
499
+ if (!value?.__index) {
500
+ console.log("value", value);
501
+ throw new Error("Family index is missing");
502
+ }
503
+ return value.__index;
504
+ };
505
+ var recursivlyUpdateIndexes = (data, family) => {
506
+ Object.keys(data.scopes).forEach((scopeKey) => {
507
+ const scopedData = data.scopes[scopeKey];
508
+ if (scopeKey) {
509
+ if (scopedData.values.has(family)) {
510
+ scopedData.values.set(family, scopedData.values.get(family).__index.toArray());
511
+ }
512
+ recursivlyUpdateIndexes(scopedData, family);
513
+ }
514
+ });
435
515
  };
436
516
  var addFamilyAtomsToSet = (family, familyAtoms, data) => {
437
- const currentAtoms = findInClosestStore(family, data) || [];
438
- const atomsToAdd = [];
439
- for (const familyAtom of familyAtoms) {
440
- if (!currentAtoms.includes(familyAtom)) {
441
- atomsToAdd.push(familyAtom);
517
+ const index = findFamilyIndex(family, data);
518
+ index.add(familyAtoms);
519
+ data.values.set(family, index.toArray());
520
+ recursivlyUpdateIndexes(data, family);
521
+ };
522
+ var propagateDeletedAtoms = (atoms, data, subscriptions = new Set, families = new Map) => {
523
+ const selectors = new Set;
524
+ for (const atom of atoms) {
525
+ addSetToSet(data.stateDependents.get(atom), selectors);
526
+ addSetToSet(data.subscriptions.get(atom), subscriptions);
527
+ if (isFamilyAtom(atom)) {
528
+ if (!families.has(atom.family)) {
529
+ families.set(atom.family, new Set);
530
+ }
531
+ families.get(atom.family).add(atom);
442
532
  }
443
533
  }
444
- if (atomsToAdd.length > 0) {
445
- data.values.set(family, [...currentAtoms, ...atomsToAdd]);
534
+ if (families.size > 0) {
535
+ for (const [family, familyAtoms] of families) {
536
+ addSetToSet(data.stateDependents.get(family), selectors);
537
+ addSetToSet(data.subscriptions.get(family), subscriptions);
538
+ if (familyAtoms.size === 0)
539
+ throw new Error("Should not be possible");
540
+ deleteFamilyAtomsFromSet(family, familyAtoms, data);
541
+ }
446
542
  }
543
+ propagateDirtySelectors(atoms, selectors, data, subscriptions, families);
447
544
  };
448
545
  var propagateUpdatedAtoms = (atoms, data, subscriptions = new Set, families = new Map, isRecursive = false) => {
449
546
  const selectors = new Set;
@@ -550,16 +647,8 @@ function createStoreData(id, parent) {
550
647
 
551
648
  // src/lib/deleteFamilyAtom.ts
552
649
  var deleteFamilyAtom = (atom, data) => {
553
- const array = data.values.get(atom.family);
554
- const index = array.indexOf(atom);
555
- const newArray = [
556
- ...array.slice(0, index),
557
- ...array.slice(index + 1)
558
- ];
559
650
  data.values.delete(atom);
560
- propagateUpdatedAtoms([atom], data);
561
- setValueInData(atom.family, newArray, data);
562
- propagateUpdatedAtoms([atom.family], data);
651
+ propagateDeletedAtoms([atom], data);
563
652
  };
564
653
 
565
654
  // src/lib/resetAtom.ts
@@ -802,21 +891,19 @@ class Transaction {
802
891
  this.dirty = true;
803
892
  if (isFamilyAtom(atom)) {
804
893
  const currentFamilyList = this.get(atom.family);
805
- if (!currentFamilyList.includes(atom)) {
806
- const newArr = [...currentFamilyList, atom];
807
- this.atomMap.set(atom.family, newArr);
894
+ if (currentFamilyList && !currentFamilyList.__index.has(atom)) {
895
+ const index = currentFamilyList ? currentFamilyList.__index.clone() : new FamilyIndex(atom.family, this.data);
896
+ index.add([atom]);
897
+ this.atomMap.set(atom.family, index.toArray());
808
898
  }
809
899
  }
810
900
  return value;
811
901
  };
812
902
  del = (atom) => {
813
- const array = this.get(atom.family);
814
- const index = array.indexOf(atom);
815
- const newArr = [
816
- ...array.slice(0, index),
817
- ...array.slice(index + 1)
818
- ];
819
- this.atomMap.set(atom.family, newArr);
903
+ const index = this.get(atom.family).__index;
904
+ const cloned = index.clone();
905
+ cloned.delete([atom]);
906
+ this.atomMap.set(atom.family, cloned.toArray());
820
907
  if (this.data.values.has(atom)) {
821
908
  this.deleteSet.add(atom);
822
909
  }
@@ -1298,9 +1385,9 @@ var isFamilySelector = (state) => isFamilyState(state) && isSelector(state);
1298
1385
 
1299
1386
  // src/index.ts
1300
1387
  if (globalThis.__valdres__) {
1301
- throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"0.2.0-pre.11"}`);
1388
+ throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"0.2.0-pre.14"}`);
1302
1389
  } else {
1303
- globalThis.__valdres__ = "0.2.0-pre.11";
1390
+ globalThis.__valdres__ = "0.2.0-pre.14";
1304
1391
  }
1305
1392
  export {
1306
1393
  store,
@@ -1,5 +1,5 @@
1
1
  import type { AtomFamily } from "./types/AtomFamily";
2
2
  import type { Selector } from "./types/Selector";
3
- export declare const index: <Term, Value extends unknown, FamilyArgs extends [any, ...any[]] = [any, ...any[]]>(family: AtomFamily<Value, FamilyArgs>, callback: (value: Value, term: Term) => boolean, options?: {
3
+ export declare const index: <Term, Value extends any, FamilyArgs extends [any, ...any[]] = [any, ...any[]]>(family: AtomFamily<Value, FamilyArgs>, callback: (value: Value, term: Term) => boolean, options?: {
4
4
  name?: string;
5
5
  }) => ((term: Term) => Selector<Term[]>);
@@ -1,4 +1,4 @@
1
1
  import type { AtomFamily } from "../types/AtomFamily";
2
2
  import type { AtomFamilyDefaultValue } from "../types/AtomFamilyDefaultValue";
3
3
  import type { AtomOptions } from "../types/AtomOptions";
4
- export declare const createAtomFamily: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(defaultValue: AtomFamilyDefaultValue<Value, Args>, options?: AtomOptions<Value>) => AtomFamily<Value, Args>;
4
+ export declare const createAtomFamily: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(defaultValue: AtomFamilyDefaultValue<Value, Args>, options?: AtomOptions<Value>) => AtomFamily<Value, Args>;
@@ -1,4 +1,4 @@
1
1
  import type { AtomFamily } from "../types/AtomFamily";
2
2
  import type { AtomFamilyDefaultValue } from "../types/AtomFamilyDefaultValue";
3
3
  import type { AtomOptions } from "../types/AtomOptions";
4
- export declare const createGlobalAtomFamily: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(defaultValue: AtomFamilyDefaultValue<Value, Args>, options: AtomOptions<Value>) => AtomFamily<Value, Args>;
4
+ export declare const createGlobalAtomFamily: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(defaultValue: AtomFamilyDefaultValue<Value, Args>, options: AtomOptions<Value>) => AtomFamily<Value, Args>;
@@ -1,8 +1,8 @@
1
1
  import type { Atom } from "../types/Atom";
2
2
  import type { AtomFamily } from "../types/AtomFamily";
3
+ import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
3
4
  import type { Selector } from "../types/Selector";
4
5
  import type { StoreData } from "../types/StoreData";
5
- import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
6
6
  export declare function getState<Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(atom: Atom<Value>, data: StoreData, initializedAtomsSet: Set<Atom>, circularDependencySet?: WeakSet<Selector>): Value;
7
7
  export declare function getState<Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(selector: Selector<Value>, data: StoreData, initializedAtomsSet: Set<Atom>, circularDependencySet?: WeakSet<Selector>): Value;
8
8
  export declare function getState<Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(family: AtomFamily<Value, Args>, data: StoreData, initializedAtomsSet: Set<Atom>, circularDependencySet?: WeakSet<Selector>): AtomFamilyAtom<Value, Args>[];
@@ -2,4 +2,4 @@ import type { Atom } from "../types/Atom";
2
2
  import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
3
3
  import type { StoreData } from "../types/StoreData";
4
4
  export declare const getAtomInitValue: <V = any>(atom: Atom<V>, data: StoreData, initializedAtomsSet: Set<Atom>) => any;
5
- export declare const initAtom: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(atom: Atom<Value> | AtomFamilyAtom<Value, Args>, data: StoreData, initializedAtomsSet: Set<Atom>) => void;
5
+ export declare const initAtom: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(atom: Atom<Value> | AtomFamilyAtom<Value, Args>, data: StoreData, initializedAtomsSet: Set<Atom>) => void;
@@ -5,6 +5,21 @@ import type { Family } from "../types/Family";
5
5
  import type { Selector } from "../types/Selector";
6
6
  import type { StoreData } from "../types/StoreData";
7
7
  import type { Subscription } from "../types/Subscription";
8
+ export declare class FamilyIndex {
9
+ map: Map<AtomFamilyAtom<any>, boolean>;
10
+ family: Family<any>;
11
+ data: StoreData;
12
+ parentIndex: FamilyIndex | undefined;
13
+ constructor(family: Family<any>, data: StoreData, parentIndex?: FamilyIndex, map?: Map<AtomFamilyAtom<any>, boolean>);
14
+ add(atoms: AtomFamilyAtom<any>[]): void;
15
+ delete(atoms: AtomFamilyAtom<any>[]): void;
16
+ has(atom: AtomFamilyAtom<any>): boolean;
17
+ clone(): FamilyIndex;
18
+ toSet(): Set<AtomFamilyAtom<any>>;
19
+ toArray(): AtomFamilyAtom<any>[];
20
+ }
21
+ export declare const deleteFamilyAtomsFromSet: (family: Family<any>, familyAtoms: Set<AtomFamilyAtom<any>>, data: StoreData) => void;
8
22
  export declare const addFamilyAtomsToSet: (family: Family<any>, familyAtoms: Set<AtomFamilyAtom<any>>, data: StoreData) => void;
23
+ export declare const propagateDeletedAtoms: (atoms: AtomFamilyAtom<any, any>[], data: StoreData, subscriptions?: Set<Subscription>, families?: Map<AtomFamily<any>, Set<AtomFamilyAtom<any>>>) => void;
9
24
  export declare const propagateUpdatedAtoms: (atoms: (Atom<any> | AtomFamilyAtom<any, any> | AtomFamily<any, any>)[], data: StoreData, subscriptions?: Set<Subscription>, families?: Map<AtomFamily<any>, Set<AtomFamilyAtom<any>>>, isRecursive?: boolean) => void;
10
25
  export declare const propagateDirtySelectors: (updatedAtoms: Atom[], selectors: Set<Selector>, data: StoreData, subscriptions: Set<Subscription>, families: Map<AtomFamily<any>, Set<AtomFamilyAtom<any>>>) => void;
@@ -1,4 +1,4 @@
1
1
  import type { GetValue } from "./types/GetValue";
2
2
  import type { Selector } from "./types/Selector";
3
3
  import type { SelectorOptions } from "./types/SelectorOptions";
4
- export declare const selector: <Value extends unknown, FamilyArgs extends [any, ...any[]] = [any, ...any[]]>(get: (get: GetValue, storeId: string) => Value | Promise<Value>, options?: SelectorOptions<Value>) => Selector<Value, FamilyArgs>;
4
+ export declare const selector: <Value extends any, FamilyArgs extends [any, ...any[]] = [any, ...any[]]>(get: (get: GetValue, storeId: string) => Value | Promise<Value>, options?: SelectorOptions<Value>) => Selector<Value, FamilyArgs>;
@@ -1,4 +1,4 @@
1
1
  import type { GetValue } from "./types/GetValue";
2
2
  import type { SelectorFamily } from "./types/SelectorFamily";
3
3
  import type { SelectorOptions } from "./types/SelectorOptions";
4
- export declare const selectorFamily: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(callback: (...args: Args) => (get: GetValue) => Value | Promise<Value>, options?: SelectorOptions<Value>) => SelectorFamily<Value, Args>;
4
+ export declare const selectorFamily: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(callback: (...args: Args) => (get: GetValue) => Value | Promise<Value>, options?: SelectorOptions<Value>) => SelectorFamily<Value, Args>;
@@ -1,2 +1,2 @@
1
1
  import type { AtomFamily } from "../types/AtomFamily";
2
- export declare const isAtomFamily: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamily<Value, Args>;
2
+ export declare const isAtomFamily: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamily<Value, Args>;
@@ -1,2 +1,2 @@
1
1
  import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
2
- export declare const isFamilyAtom: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamilyAtom<Value, Args>;
2
+ export declare const isFamilyAtom: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamilyAtom<Value, Args>;
@@ -1,2 +1,2 @@
1
1
  import type { AtomFamilySelector } from "../types/AtomFamilySelector";
2
- export declare const isFamilySelector: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamilySelector<Value, Args>;
2
+ export declare const isFamilySelector: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamilySelector<Value, Args>;
@@ -1,3 +1,3 @@
1
1
  import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
2
2
  import type { AtomFamilySelector } from "../types/AtomFamilySelector";
3
- export declare const isFamilyState: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamilyAtom<Value, Args> | AtomFamilySelector<Value, Args>;
3
+ export declare const isFamilyState: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is AtomFamilyAtom<Value, Args> | AtomFamilySelector<Value, Args>;
@@ -1,2 +1,2 @@
1
1
  import type { SelectorFamily } from "../types/SelectorFamily";
2
- export declare const isSelectorFamily: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is SelectorFamily<Value, Args>;
2
+ export declare const isSelectorFamily: <Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(state: any) => state is SelectorFamily<Value, Args>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-pre.11",
3
+ "version": "0.2.0-pre.14",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"