atom.io 0.0.0 → 0.2.0

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
@@ -63,7 +63,7 @@ __export(src_exports, {
63
63
  atomFamily: () => atomFamily,
64
64
  configure: () => configure,
65
65
  getState: () => getState,
66
- registerSelector: () => registerSelector,
66
+ isDefault: () => isDefault,
67
67
  selector: () => selector,
68
68
  selectorFamily: () => selectorFamily,
69
69
  setState: () => setState,
@@ -81,24 +81,34 @@ __export(internal_exports, {
81
81
  configure: () => configure,
82
82
  createStore: () => createStore,
83
83
  deposit: () => deposit,
84
+ evictDownStream: () => evictDownStream,
84
85
  finishAction: () => finishAction,
85
86
  finishTransaction: () => finishTransaction,
86
87
  getCachedState: () => getCachedState,
87
88
  getSelectorState: () => getSelectorState,
88
89
  getState__INTERNAL: () => getState__INTERNAL,
90
+ isAtomDefault: () => isAtomDefault,
89
91
  isDone: () => isDone,
92
+ isSelectorDefault: () => isSelectorDefault,
93
+ lookup: () => lookup,
94
+ lookupSelectorSources: () => lookupSelectorSources,
90
95
  markDone: () => markDone,
91
- propagateChanges: () => propagateChanges,
92
- recall: () => recall,
96
+ recallState: () => recallState,
97
+ registerSelector: () => registerSelector,
93
98
  setAtomState: () => setAtomState,
94
99
  setSelectorState: () => setSelectorState,
95
100
  setState__INTERNAL: () => setState__INTERNAL,
96
101
  startAction: () => startAction,
97
102
  startTransaction: () => startTransaction,
103
+ subscribeToRootAtoms: () => subscribeToRootAtoms,
104
+ traceAllSelectorAtoms: () => traceAllSelectorAtoms,
105
+ traceSelectorAtoms: () => traceSelectorAtoms,
106
+ updateSelectorAtoms: () => updateSelectorAtoms,
98
107
  withdraw: () => withdraw
99
108
  });
100
109
 
101
110
  // src/internal/get.ts
111
+ var import_function7 = require("fp-ts/function");
102
112
  var import_hamt_plus2 = __toESM(require("hamt_plus"));
103
113
 
104
114
  // src/internal/store.ts
@@ -494,7 +504,9 @@ var Join = class {
494
504
  var createStore = (name) => ({
495
505
  valueMap: import_hamt_plus.default.make(),
496
506
  selectorGraph: new Join({ relationType: `n:n` }),
507
+ selectorAtoms: new Join({ relationType: `n:n` }),
497
508
  atoms: import_hamt_plus.default.make(),
509
+ atomsAreDefault: import_hamt_plus.default.make(),
498
510
  selectors: import_hamt_plus.default.make(),
499
511
  readonlySelectors: import_hamt_plus.default.make(),
500
512
  operation: {
@@ -526,10 +538,25 @@ var clearStore = (store = IMPLICIT.STORE) => {
526
538
 
527
539
  // src/internal/get.ts
528
540
  var getCachedState = (state, store = IMPLICIT.STORE) => {
541
+ const path = [];
542
+ if (`default` in state) {
543
+ const atomKey = state.key;
544
+ store.selectorAtoms = (0, import_function7.pipe)(store.selectorAtoms, (oldValue) => {
545
+ let newValue = oldValue;
546
+ for (const selectorKey of path) {
547
+ newValue = newValue.set(selectorKey, atomKey);
548
+ }
549
+ return newValue;
550
+ });
551
+ }
529
552
  const value = import_hamt_plus2.default.get(state.key, store.valueMap);
530
553
  return value;
531
554
  };
532
555
  var getSelectorState = (selector2) => selector2.get();
556
+ function lookup(key, store) {
557
+ const type = import_hamt_plus2.default.has(key, store.atoms) ? `atom` : import_hamt_plus2.default.has(key, store.selectors) ? `selector` : `readonly_selector`;
558
+ return { key, type };
559
+ }
533
560
  function withdraw(token, store) {
534
561
  var _a, _b;
535
562
  return (_b = (_a = import_hamt_plus2.default.get(token.key, store.atoms)) != null ? _a : import_hamt_plus2.default.get(token.key, store.selectors)) != null ? _b : import_hamt_plus2.default.get(token.key, store.readonlySelectors);
@@ -544,14 +571,16 @@ function deposit(state) {
544
571
  return { key: state.key, type: `atom` };
545
572
  }
546
573
  var getState__INTERNAL = (state, store = IMPLICIT.STORE) => {
547
- var _a;
574
+ var _a, _b, _c;
548
575
  if (import_hamt_plus2.default.has(state.key, store.valueMap)) {
576
+ (_a = store.config.logger) == null ? void 0 : _a.info(`>> read "${state.key}"`);
549
577
  return getCachedState(state, store);
550
578
  }
551
579
  if (`get` in state) {
580
+ (_b = store.config.logger) == null ? void 0 : _b.info(`-> calc "${state.key}"`);
552
581
  return getSelectorState(state);
553
582
  }
554
- (_a = store.config.logger) == null ? void 0 : _a.error(
583
+ (_c = store.config.logger) == null ? void 0 : _c.error(
555
584
  `Attempted to get atom "${state.key}", which was never initialized in store "${store.config.name}".`
556
585
  );
557
586
  return state.default;
@@ -562,11 +591,6 @@ var import_hamt_plus4 = __toESM(require("hamt_plus"));
562
591
 
563
592
  // src/internal/operation.ts
564
593
  var import_hamt_plus3 = __toESM(require("hamt_plus"));
565
- var finishAction = (store) => {
566
- var _a;
567
- store.operation = { open: false };
568
- (_a = store.config.logger) == null ? void 0 : _a.info(` \u2705`, `operation complete`);
569
- };
570
594
  var startAction = (store) => {
571
595
  var _a;
572
596
  store.operation = {
@@ -574,7 +598,12 @@ var startAction = (store) => {
574
598
  done: /* @__PURE__ */ new Set(),
575
599
  prev: store.valueMap
576
600
  };
577
- (_a = store.config.logger) == null ? void 0 : _a.info(` \u26A0\uFE0F`, `action started`);
601
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u2B55`, `operation start`);
602
+ };
603
+ var finishAction = (store) => {
604
+ var _a;
605
+ store.operation = { open: false };
606
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F534}`, `operation done`);
578
607
  };
579
608
  var isDone = (key, store = IMPLICIT.STORE) => {
580
609
  var _a;
@@ -596,7 +625,7 @@ var markDone = (key, store = IMPLICIT.STORE) => {
596
625
  }
597
626
  store.operation.done.add(key);
598
627
  };
599
- var recall = (state, store = IMPLICIT.STORE) => {
628
+ var recallState = (state, store = IMPLICIT.STORE) => {
600
629
  var _a;
601
630
  if (!store.operation.open) {
602
631
  (_a = store.config.logger) == null ? void 0 : _a.warn(
@@ -608,76 +637,60 @@ var recall = (state, store = IMPLICIT.STORE) => {
608
637
  };
609
638
 
610
639
  // src/internal/set.ts
611
- var propagateChanges = (state, store = IMPLICIT.STORE) => {
640
+ var evictDownStream = (state, store = IMPLICIT.STORE) => {
612
641
  var _a, _b;
613
- const relatedStateKeys = store.selectorGraph.getRelations(state.key);
642
+ const downstream = store.selectorAtoms.getRelations(state.key);
643
+ const downstreamKeys = downstream.map(({ id }) => id);
614
644
  (_a = store.config.logger) == null ? void 0 : _a.info(
615
- ` ||`,
616
- `bumping`,
617
- relatedStateKeys.length,
618
- `states:`,
619
- relatedStateKeys.map(({ id }) => id)
645
+ ` || ${downstreamKeys.length} downstream:`,
646
+ downstreamKeys
620
647
  );
621
648
  if (store.operation.open) {
622
- (_b = store.config.logger) == null ? void 0 : _b.info(` ||`, `done:`, store.operation.done);
649
+ (_b = store.config.logger) == null ? void 0 : _b.info(` ||`, [...store.operation.done], `already done`);
623
650
  }
624
- relatedStateKeys.forEach(({ id: stateKey }) => {
651
+ downstream.forEach(({ id: stateKey }) => {
625
652
  var _a2, _b2, _c, _d;
626
653
  if (isDone(stateKey, store)) {
627
- (_a2 = store.config.logger) == null ? void 0 : _a2.info(` ||`, stateKey, `already done`);
654
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(` || ${stateKey} already done`);
655
+ return;
656
+ }
657
+ const state2 = (_b2 = import_hamt_plus4.default.get(stateKey, store.selectors)) != null ? _b2 : import_hamt_plus4.default.get(stateKey, store.readonlySelectors);
658
+ if (!state2) {
659
+ (_c = store.config.logger) == null ? void 0 : _c.info(
660
+ ` || ${stateKey} is an atom, and can't be downstream`
661
+ );
628
662
  return;
629
663
  }
630
- (_b2 = store.config.logger) == null ? void 0 : _b2.info(`->`, `bumping`, stateKey);
631
664
  store.valueMap = import_hamt_plus4.default.remove(stateKey, store.valueMap);
632
- const state2 = (_c = import_hamt_plus4.default.get(stateKey, store.selectors)) != null ? _c : import_hamt_plus4.default.get(stateKey, store.readonlySelectors);
633
- const newValue = getState__INTERNAL(state2, store);
634
- (_d = store.config.logger) == null ? void 0 : _d.info(` <-`, stateKey, `became`, newValue);
635
- const oldValue = recall(state2, store);
636
- state2.subject.next({ newValue, oldValue });
665
+ (_d = store.config.logger) == null ? void 0 : _d.info(` xx evicted "${stateKey}"`);
637
666
  markDone(stateKey, store);
638
- if (`set` in state2)
639
- propagateChanges(state2, store);
640
667
  });
641
668
  };
642
669
  var setAtomState = (atom2, next, store = IMPLICIT.STORE) => {
643
670
  var _a, _b;
644
671
  const oldValue = getState__INTERNAL(atom2, store);
645
672
  const newValue = become(next)(oldValue);
646
- (_a = store.config.logger) == null ? void 0 : _a.info(
647
- `->`,
648
- `setting atom`,
649
- `"${atom2.key}"`,
650
- `to`,
651
- newValue
652
- );
673
+ (_a = store.config.logger) == null ? void 0 : _a.info(`-> setting atom "${atom2.key}" to`, newValue);
653
674
  store.valueMap = import_hamt_plus4.default.set(atom2.key, newValue, store.valueMap);
675
+ if (isAtomDefault(atom2.key)) {
676
+ store.atomsAreDefault = import_hamt_plus4.default.set(atom2.key, false, store.atomsAreDefault);
677
+ }
654
678
  markDone(atom2.key, store);
679
+ (_b = store.config.logger) == null ? void 0 : _b.info(
680
+ ` || evicting caches downstream from "${atom2.key}"`
681
+ );
682
+ evictDownStream(atom2, store);
655
683
  atom2.subject.next({ newValue, oldValue });
656
- (_b = store.config.logger) == null ? void 0 : _b.info(` ||`, `propagating change to`, `"${atom2.key}"`);
657
- propagateChanges(atom2, store);
658
684
  };
659
685
  var setSelectorState = (selector2, next, store = IMPLICIT.STORE) => {
660
686
  var _a, _b;
661
687
  const oldValue = getState__INTERNAL(selector2, store);
662
688
  const newValue = become(next)(oldValue);
663
- (_a = store.config.logger) == null ? void 0 : _a.info(
664
- `->`,
665
- `setting selector`,
666
- `"${selector2.key}"`,
667
- `to`,
668
- newValue
669
- );
670
- (_b = store.config.logger) == null ? void 0 : _b.info(
671
- ` ||`,
672
- `propagating change to`,
673
- `"${selector2.key}"`
674
- );
689
+ (_a = store.config.logger) == null ? void 0 : _a.info(`-> setting selector "${selector2.key}" to`, newValue);
690
+ (_b = store.config.logger) == null ? void 0 : _b.info(` || propagating change made to "${selector2.key}"`);
675
691
  selector2.set(newValue);
676
- markDone(selector2.key, store);
677
- propagateChanges(selector2, store);
678
692
  };
679
- var setState__INTERNAL = (token, value, store = IMPLICIT.STORE) => {
680
- const state = withdraw(token, store);
693
+ var setState__INTERNAL = (state, value, store = IMPLICIT.STORE) => {
681
694
  if (`set` in state) {
682
695
  setSelectorState(state, value, store);
683
696
  } else {
@@ -685,11 +698,119 @@ var setState__INTERNAL = (token, value, store = IMPLICIT.STORE) => {
685
698
  }
686
699
  };
687
700
 
701
+ // src/internal/is-default.ts
702
+ var import_hamt_plus5 = __toESM(require("hamt_plus"));
703
+ var isAtomDefault = (key, store = IMPLICIT.STORE) => {
704
+ return import_hamt_plus5.default.get(key, store.atomsAreDefault);
705
+ };
706
+ var isSelectorDefault = (key, store = IMPLICIT.STORE) => {
707
+ const roots = traceAllSelectorAtoms(key, store);
708
+ return roots.every((root) => isAtomDefault(root.key, store));
709
+ };
710
+
711
+ // src/internal/selector-internal.ts
712
+ var lookupSelectorSources = (key, store) => store.selectorGraph.getRelations(key).filter(({ source }) => source !== key).map(({ source }) => lookup(source, store));
713
+ var traceSelectorAtoms = (selectorKey, dependency, store) => {
714
+ const roots = [];
715
+ const sources = lookupSelectorSources(dependency.key, store);
716
+ let depth = 0;
717
+ while (sources.length > 0) {
718
+ const source = sources.shift();
719
+ ++depth;
720
+ if (depth > 999) {
721
+ throw new Error(
722
+ `Maximum selector dependency depth exceeded in selector "${selectorKey}".`
723
+ );
724
+ }
725
+ if (source.type !== `atom`) {
726
+ sources.push(...lookupSelectorSources(source.key, store));
727
+ } else {
728
+ roots.push(source);
729
+ }
730
+ }
731
+ return roots;
732
+ };
733
+ var traceAllSelectorAtoms = (selectorKey, store) => {
734
+ const sources = lookupSelectorSources(selectorKey, store);
735
+ return sources.flatMap(
736
+ (source) => source.type === `atom` ? source : traceSelectorAtoms(selectorKey, source, store)
737
+ );
738
+ };
739
+ var updateSelectorAtoms = (selectorKey, dependency, store) => {
740
+ var _a, _b;
741
+ if (dependency.type === `atom`) {
742
+ store.selectorAtoms = store.selectorAtoms.set(selectorKey, dependency.key);
743
+ (_a = store.config.logger) == null ? void 0 : _a.info(
744
+ ` || adding root for "${selectorKey}": ${dependency.key}`
745
+ );
746
+ return;
747
+ }
748
+ const roots = traceSelectorAtoms(selectorKey, dependency, store);
749
+ (_b = store.config.logger) == null ? void 0 : _b.info(` || adding roots for "${selectorKey}":`, roots);
750
+ for (const root of roots) {
751
+ store.selectorAtoms = store.selectorAtoms.set(selectorKey, root.key);
752
+ }
753
+ };
754
+ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
755
+ get: (dependency) => {
756
+ var _a, _b;
757
+ const alreadyRegistered = store.selectorGraph.getRelations(selectorKey).some(({ source }) => source === dependency.key);
758
+ const dependencyState = withdraw(dependency, store);
759
+ const dependencyValue = getState__INTERNAL(dependencyState, store);
760
+ if (alreadyRegistered) {
761
+ (_a = store.config.logger) == null ? void 0 : _a.info(
762
+ ` || ${selectorKey} <- ${dependency.key} =`,
763
+ dependencyValue
764
+ );
765
+ } else {
766
+ (_b = store.config.logger) == null ? void 0 : _b.info(
767
+ `\u{1F50C} registerSelector "${selectorKey}" <- "${dependency.key}" =`,
768
+ dependencyValue
769
+ );
770
+ store.selectorGraph = store.selectorGraph.set(
771
+ selectorKey,
772
+ dependency.key,
773
+ {
774
+ source: dependency.key
775
+ }
776
+ );
777
+ }
778
+ updateSelectorAtoms(selectorKey, dependency, store);
779
+ return dependencyValue;
780
+ },
781
+ set: (stateToken, newValue) => {
782
+ const state = withdraw(stateToken, store);
783
+ setState__INTERNAL(state, newValue, store);
784
+ }
785
+ });
786
+
787
+ // src/internal/subscribe-internal.ts
788
+ var subscribeToRootAtoms = (state, store = IMPLICIT.STORE) => {
789
+ const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomToken) => {
790
+ const atom2 = withdraw(atomToken, store);
791
+ return atom2.subject.subscribe((atomChange) => {
792
+ var _a, _b;
793
+ (_a = store.config.logger) == null ? void 0 : _a.info(
794
+ `\u{1F4E2} atom changed: "${atomToken.key}" (`,
795
+ atomChange.oldValue,
796
+ `->`,
797
+ atomChange.newValue,
798
+ `) re-evaluating "${state.key}"`
799
+ );
800
+ const oldValue = recallState(state, store);
801
+ const newValue = getState__INTERNAL(state, store);
802
+ (_b = store.config.logger) == null ? void 0 : _b.info(` <- ${state.key} became`, newValue);
803
+ state.subject.next({ newValue, oldValue });
804
+ });
805
+ });
806
+ return dependencySubscriptions;
807
+ };
808
+
688
809
  // src/internal/transaction-internal.ts
689
810
  var finishTransaction = (store) => {
690
811
  var _a;
691
812
  store.transaction = { open: false };
692
- (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F4B8}`, `transaction done`);
813
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F6EC}`, `transaction done`);
693
814
  };
694
815
  var startTransaction = (store) => {
695
816
  var _a;
@@ -703,7 +824,7 @@ var startTransaction = (store) => {
703
824
  valueMap: store.valueMap
704
825
  }
705
826
  };
706
- (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F3E6}`, `transaction start`);
827
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F6EB}`, `transaction start`);
707
828
  };
708
829
  var abortTransaction = (store) => {
709
830
  var _a, _b;
@@ -723,17 +844,17 @@ var abortTransaction = (store) => {
723
844
  };
724
845
 
725
846
  // src/atom.ts
726
- var import_hamt_plus5 = __toESM(require("hamt_plus"));
847
+ var import_hamt_plus6 = __toESM(require("hamt_plus"));
727
848
  var Rx = __toESM(require("rxjs"));
728
849
 
729
850
  // ../anvl/src/json/index.ts
730
- var import_function8 = require("fp-ts/function");
851
+ var import_function9 = require("fp-ts/function");
731
852
  var stringifyJson = (json) => JSON.stringify(json);
732
853
 
733
854
  // src/atom.ts
734
855
  var atom = (options, store = IMPLICIT.STORE) => {
735
856
  var _a, _b, _c;
736
- if (import_hamt_plus5.default.has(options.key, store.atoms)) {
857
+ if (import_hamt_plus6.default.has(options.key, store.atoms)) {
737
858
  (_b = (_a = store.config.logger) == null ? void 0 : _a.error) == null ? void 0 : _b.call(
738
859
  _a,
739
860
  `Key "${options.key}" already exists in the store.`
@@ -742,12 +863,13 @@ var atom = (options, store = IMPLICIT.STORE) => {
742
863
  }
743
864
  const subject = new Rx.Subject();
744
865
  const newAtom = __spreadProps(__spreadValues({}, options), { subject });
745
- store.atoms = import_hamt_plus5.default.set(options.key, newAtom, store.atoms);
746
- store.valueMap = import_hamt_plus5.default.set(options.key, options.default, store.valueMap);
866
+ const initialValue = options.default instanceof Function ? options.default() : options.default;
867
+ store.atoms = import_hamt_plus6.default.set(options.key, newAtom, store.atoms);
868
+ store.atomsAreDefault = import_hamt_plus6.default.set(options.key, true, store.atomsAreDefault);
869
+ store.valueMap = import_hamt_plus6.default.set(options.key, initialValue, store.valueMap);
747
870
  const token = deposit(newAtom);
748
871
  const setSelf = (next) => setState(token, next, store);
749
872
  const onSet = (observe) => subscribe(token, observe, store);
750
- setSelf(options.default);
751
873
  (_c = options.effects) == null ? void 0 : _c.forEach((effect) => effect({ setSelf, onSet }));
752
874
  return token;
753
875
  };
@@ -769,18 +891,18 @@ var atomFamily = (options, store = IMPLICIT.STORE) => (key) => {
769
891
  };
770
892
 
771
893
  // src/selector.ts
772
- var import_hamt_plus6 = __toESM(require("hamt_plus"));
894
+ var import_hamt_plus7 = __toESM(require("hamt_plus"));
773
895
  var Rx2 = __toESM(require("rxjs"));
774
896
  function selector(options, store = IMPLICIT.STORE) {
775
897
  var _a, _b;
776
- if (import_hamt_plus6.default.has(options.key, store.selectors)) {
898
+ if (import_hamt_plus7.default.has(options.key, store.selectors)) {
777
899
  throw new Error(`Key "${options.key}" already exists in the store.`);
778
900
  }
779
901
  const subject = new Rx2.Subject();
780
902
  const { get, set } = registerSelector(options.key, store);
781
903
  const getSelf = () => {
782
904
  const value = options.get({ get });
783
- store.valueMap = import_hamt_plus6.default.set(options.key, value, store.valueMap);
905
+ store.valueMap = import_hamt_plus7.default.set(options.key, value, store.valueMap);
784
906
  return value;
785
907
  };
786
908
  if (!(`set` in options)) {
@@ -788,22 +910,22 @@ function selector(options, store = IMPLICIT.STORE) {
788
910
  subject,
789
911
  get: getSelf
790
912
  });
791
- store.readonlySelectors = import_hamt_plus6.default.set(
913
+ store.readonlySelectors = import_hamt_plus7.default.set(
792
914
  options.key,
793
915
  readonlySelector,
794
916
  store.readonlySelectors
795
917
  );
796
918
  const initialValue2 = getSelf();
797
- (_a = store.config.logger) == null ? void 0 : _a.info(` \u2728`, options.key, `=`, initialValue2);
919
+ (_a = store.config.logger) == null ? void 0 : _a.info(` \u2728 "${options.key}" =`, initialValue2);
798
920
  return __spreadProps(__spreadValues({}, readonlySelector), { type: `readonly_selector` });
799
921
  }
800
922
  const setSelf = (next) => {
801
923
  var _a2;
802
- (_a2 = store.config.logger) == null ? void 0 : _a2.info(`${options.key}.set`, next);
924
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(` <- "${options.key}" became`, next);
803
925
  const oldValue = getSelf();
804
926
  const newValue = become(next)(oldValue);
805
- store.valueMap = import_hamt_plus6.default.set(options.key, newValue, store.valueMap);
806
- console.error({ oldValue });
927
+ store.valueMap = import_hamt_plus7.default.set(options.key, newValue, store.valueMap);
928
+ markDone(options.key, store);
807
929
  subject.next({ newValue, oldValue });
808
930
  options.set({ get, set }, newValue);
809
931
  };
@@ -812,9 +934,9 @@ function selector(options, store = IMPLICIT.STORE) {
812
934
  get: getSelf,
813
935
  set: setSelf
814
936
  });
815
- store.selectors = import_hamt_plus6.default.set(options.key, mySelector, store.selectors);
937
+ store.selectors = import_hamt_plus7.default.set(options.key, mySelector, store.selectors);
816
938
  const initialValue = getSelf();
817
- (_b = store.config.logger) == null ? void 0 : _b.info(` \u2728`, options.key, `=`, initialValue);
939
+ (_b = store.config.logger) == null ? void 0 : _b.info(` \u2728 "${options.key}" =`, initialValue);
818
940
  return __spreadProps(__spreadValues({}, mySelector), { type: `selector` });
819
941
  }
820
942
  function selectorFamily(options, store = IMPLICIT.STORE) {
@@ -843,30 +965,6 @@ function selectorFamily(options, store = IMPLICIT.STORE) {
843
965
  );
844
966
  };
845
967
  }
846
- var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
847
- get: (state) => {
848
- var _a, _b, _c;
849
- const isRegistered = store.selectorGraph.getRelatedIds(selectorKey).includes(state.key);
850
- if (isRegistered) {
851
- (_a = store.config.logger) == null ? void 0 : _a.info(` ||`, selectorKey, `<-`, state.key);
852
- } else {
853
- (_b = store.config.logger) == null ? void 0 : _b.info(
854
- `\u{1F50C} registerSelector`,
855
- state.key,
856
- `->`,
857
- selectorKey
858
- );
859
- store.selectorGraph = store.selectorGraph.set(selectorKey, state.key);
860
- }
861
- const currentValue = getState(state, store);
862
- (_c = store.config.logger) == null ? void 0 : _c.info(` ||`, state.key, `=`, currentValue);
863
- return currentValue;
864
- },
865
- set: (token, newValue) => {
866
- store.selectorGraph.set(token.key, selectorKey);
867
- setState__INTERNAL(token, newValue, store);
868
- }
869
- });
870
968
 
871
969
  // src/transaction.ts
872
970
  var transaction = (options, store = IMPLICIT.STORE) => Object.assign(
@@ -897,15 +995,34 @@ var getState = (token, store = IMPLICIT.STORE) => {
897
995
  const state = withdraw(token, store);
898
996
  return getState__INTERNAL(state, store);
899
997
  };
900
- var setState = (state, value, store = IMPLICIT.STORE) => {
998
+ var setState = (token, value, store = IMPLICIT.STORE) => {
901
999
  startAction(store);
1000
+ const state = withdraw(token, store);
902
1001
  setState__INTERNAL(state, value, store);
903
1002
  finishAction(store);
904
1003
  };
1004
+ var isDefault = (token, store = IMPLICIT.STORE) => token.type === `atom` ? isAtomDefault(token.key, store) : isSelectorDefault(token.key, store);
905
1005
  var subscribe = (token, observe, store = IMPLICIT.STORE) => {
1006
+ var _a;
906
1007
  const state = withdraw(token, store);
907
1008
  const subscription = state.subject.subscribe(observe);
908
- return () => subscription.unsubscribe();
1009
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F440} subscribe to "${state.key}"`);
1010
+ const dependencySubscriptions = subscribeToRootAtoms(state, store);
1011
+ const unsubscribe = dependencySubscriptions === null ? () => {
1012
+ var _a2;
1013
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(`\u{1F648} unsubscribe from "${state.key}"`);
1014
+ subscription.unsubscribe();
1015
+ } : () => {
1016
+ var _a2;
1017
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(
1018
+ `\u{1F648} unsubscribe from "${state.key}" and its dependencies`
1019
+ );
1020
+ subscription.unsubscribe();
1021
+ for (const dependencySubscription of dependencySubscriptions) {
1022
+ dependencySubscription.unsubscribe();
1023
+ }
1024
+ };
1025
+ return unsubscribe;
909
1026
  };
910
1027
  // Annotate the CommonJS export names for ESM import in node:
911
1028
  0 && (module.exports = {
@@ -914,10 +1031,11 @@ var subscribe = (token, observe, store = IMPLICIT.STORE) => {
914
1031
  atomFamily,
915
1032
  configure,
916
1033
  getState,
917
- registerSelector,
1034
+ isDefault,
918
1035
  selector,
919
1036
  selectorFamily,
920
1037
  setState,
921
1038
  subscribe,
922
1039
  transaction
923
1040
  });
1041
+ //# sourceMappingURL=index.js.map