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.mjs CHANGED
@@ -43,24 +43,34 @@ __export(internal_exports, {
43
43
  configure: () => configure,
44
44
  createStore: () => createStore,
45
45
  deposit: () => deposit,
46
+ evictDownStream: () => evictDownStream,
46
47
  finishAction: () => finishAction,
47
48
  finishTransaction: () => finishTransaction,
48
49
  getCachedState: () => getCachedState,
49
50
  getSelectorState: () => getSelectorState,
50
51
  getState__INTERNAL: () => getState__INTERNAL,
52
+ isAtomDefault: () => isAtomDefault,
51
53
  isDone: () => isDone,
54
+ isSelectorDefault: () => isSelectorDefault,
55
+ lookup: () => lookup,
56
+ lookupSelectorSources: () => lookupSelectorSources,
52
57
  markDone: () => markDone,
53
- propagateChanges: () => propagateChanges,
54
- recall: () => recall,
58
+ recallState: () => recallState,
59
+ registerSelector: () => registerSelector,
55
60
  setAtomState: () => setAtomState,
56
61
  setSelectorState: () => setSelectorState,
57
62
  setState__INTERNAL: () => setState__INTERNAL,
58
63
  startAction: () => startAction,
59
64
  startTransaction: () => startTransaction,
65
+ subscribeToRootAtoms: () => subscribeToRootAtoms,
66
+ traceAllSelectorAtoms: () => traceAllSelectorAtoms,
67
+ traceSelectorAtoms: () => traceSelectorAtoms,
68
+ updateSelectorAtoms: () => updateSelectorAtoms,
60
69
  withdraw: () => withdraw
61
70
  });
62
71
 
63
72
  // src/internal/get.ts
73
+ import { pipe as pipe6 } from "fp-ts/function";
64
74
  import HAMT2 from "hamt_plus";
65
75
 
66
76
  // src/internal/store.ts
@@ -456,7 +466,9 @@ var Join = class {
456
466
  var createStore = (name) => ({
457
467
  valueMap: HAMT.make(),
458
468
  selectorGraph: new Join({ relationType: `n:n` }),
469
+ selectorAtoms: new Join({ relationType: `n:n` }),
459
470
  atoms: HAMT.make(),
471
+ atomsAreDefault: HAMT.make(),
460
472
  selectors: HAMT.make(),
461
473
  readonlySelectors: HAMT.make(),
462
474
  operation: {
@@ -488,10 +500,25 @@ var clearStore = (store = IMPLICIT.STORE) => {
488
500
 
489
501
  // src/internal/get.ts
490
502
  var getCachedState = (state, store = IMPLICIT.STORE) => {
503
+ const path = [];
504
+ if (`default` in state) {
505
+ const atomKey = state.key;
506
+ store.selectorAtoms = pipe6(store.selectorAtoms, (oldValue) => {
507
+ let newValue = oldValue;
508
+ for (const selectorKey of path) {
509
+ newValue = newValue.set(selectorKey, atomKey);
510
+ }
511
+ return newValue;
512
+ });
513
+ }
491
514
  const value = HAMT2.get(state.key, store.valueMap);
492
515
  return value;
493
516
  };
494
517
  var getSelectorState = (selector2) => selector2.get();
518
+ function lookup(key, store) {
519
+ const type = HAMT2.has(key, store.atoms) ? `atom` : HAMT2.has(key, store.selectors) ? `selector` : `readonly_selector`;
520
+ return { key, type };
521
+ }
495
522
  function withdraw(token, store) {
496
523
  var _a, _b;
497
524
  return (_b = (_a = HAMT2.get(token.key, store.atoms)) != null ? _a : HAMT2.get(token.key, store.selectors)) != null ? _b : HAMT2.get(token.key, store.readonlySelectors);
@@ -506,14 +533,16 @@ function deposit(state) {
506
533
  return { key: state.key, type: `atom` };
507
534
  }
508
535
  var getState__INTERNAL = (state, store = IMPLICIT.STORE) => {
509
- var _a;
536
+ var _a, _b, _c;
510
537
  if (HAMT2.has(state.key, store.valueMap)) {
538
+ (_a = store.config.logger) == null ? void 0 : _a.info(`>> read "${state.key}"`);
511
539
  return getCachedState(state, store);
512
540
  }
513
541
  if (`get` in state) {
542
+ (_b = store.config.logger) == null ? void 0 : _b.info(`-> calc "${state.key}"`);
514
543
  return getSelectorState(state);
515
544
  }
516
- (_a = store.config.logger) == null ? void 0 : _a.error(
545
+ (_c = store.config.logger) == null ? void 0 : _c.error(
517
546
  `Attempted to get atom "${state.key}", which was never initialized in store "${store.config.name}".`
518
547
  );
519
548
  return state.default;
@@ -524,11 +553,6 @@ import HAMT4 from "hamt_plus";
524
553
 
525
554
  // src/internal/operation.ts
526
555
  import HAMT3 from "hamt_plus";
527
- var finishAction = (store) => {
528
- var _a;
529
- store.operation = { open: false };
530
- (_a = store.config.logger) == null ? void 0 : _a.info(` \u2705`, `operation complete`);
531
- };
532
556
  var startAction = (store) => {
533
557
  var _a;
534
558
  store.operation = {
@@ -536,7 +560,12 @@ var startAction = (store) => {
536
560
  done: /* @__PURE__ */ new Set(),
537
561
  prev: store.valueMap
538
562
  };
539
- (_a = store.config.logger) == null ? void 0 : _a.info(` \u26A0\uFE0F`, `action started`);
563
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u2B55`, `operation start`);
564
+ };
565
+ var finishAction = (store) => {
566
+ var _a;
567
+ store.operation = { open: false };
568
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F534}`, `operation done`);
540
569
  };
541
570
  var isDone = (key, store = IMPLICIT.STORE) => {
542
571
  var _a;
@@ -558,7 +587,7 @@ var markDone = (key, store = IMPLICIT.STORE) => {
558
587
  }
559
588
  store.operation.done.add(key);
560
589
  };
561
- var recall = (state, store = IMPLICIT.STORE) => {
590
+ var recallState = (state, store = IMPLICIT.STORE) => {
562
591
  var _a;
563
592
  if (!store.operation.open) {
564
593
  (_a = store.config.logger) == null ? void 0 : _a.warn(
@@ -570,76 +599,60 @@ var recall = (state, store = IMPLICIT.STORE) => {
570
599
  };
571
600
 
572
601
  // src/internal/set.ts
573
- var propagateChanges = (state, store = IMPLICIT.STORE) => {
602
+ var evictDownStream = (state, store = IMPLICIT.STORE) => {
574
603
  var _a, _b;
575
- const relatedStateKeys = store.selectorGraph.getRelations(state.key);
604
+ const downstream = store.selectorAtoms.getRelations(state.key);
605
+ const downstreamKeys = downstream.map(({ id }) => id);
576
606
  (_a = store.config.logger) == null ? void 0 : _a.info(
577
- ` ||`,
578
- `bumping`,
579
- relatedStateKeys.length,
580
- `states:`,
581
- relatedStateKeys.map(({ id }) => id)
607
+ ` || ${downstreamKeys.length} downstream:`,
608
+ downstreamKeys
582
609
  );
583
610
  if (store.operation.open) {
584
- (_b = store.config.logger) == null ? void 0 : _b.info(` ||`, `done:`, store.operation.done);
611
+ (_b = store.config.logger) == null ? void 0 : _b.info(` ||`, [...store.operation.done], `already done`);
585
612
  }
586
- relatedStateKeys.forEach(({ id: stateKey }) => {
613
+ downstream.forEach(({ id: stateKey }) => {
587
614
  var _a2, _b2, _c, _d;
588
615
  if (isDone(stateKey, store)) {
589
- (_a2 = store.config.logger) == null ? void 0 : _a2.info(` ||`, stateKey, `already done`);
616
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(` || ${stateKey} already done`);
617
+ return;
618
+ }
619
+ const state2 = (_b2 = HAMT4.get(stateKey, store.selectors)) != null ? _b2 : HAMT4.get(stateKey, store.readonlySelectors);
620
+ if (!state2) {
621
+ (_c = store.config.logger) == null ? void 0 : _c.info(
622
+ ` || ${stateKey} is an atom, and can't be downstream`
623
+ );
590
624
  return;
591
625
  }
592
- (_b2 = store.config.logger) == null ? void 0 : _b2.info(`->`, `bumping`, stateKey);
593
626
  store.valueMap = HAMT4.remove(stateKey, store.valueMap);
594
- const state2 = (_c = HAMT4.get(stateKey, store.selectors)) != null ? _c : HAMT4.get(stateKey, store.readonlySelectors);
595
- const newValue = getState__INTERNAL(state2, store);
596
- (_d = store.config.logger) == null ? void 0 : _d.info(` <-`, stateKey, `became`, newValue);
597
- const oldValue = recall(state2, store);
598
- state2.subject.next({ newValue, oldValue });
627
+ (_d = store.config.logger) == null ? void 0 : _d.info(` xx evicted "${stateKey}"`);
599
628
  markDone(stateKey, store);
600
- if (`set` in state2)
601
- propagateChanges(state2, store);
602
629
  });
603
630
  };
604
631
  var setAtomState = (atom2, next, store = IMPLICIT.STORE) => {
605
632
  var _a, _b;
606
633
  const oldValue = getState__INTERNAL(atom2, store);
607
634
  const newValue = become(next)(oldValue);
608
- (_a = store.config.logger) == null ? void 0 : _a.info(
609
- `->`,
610
- `setting atom`,
611
- `"${atom2.key}"`,
612
- `to`,
613
- newValue
614
- );
635
+ (_a = store.config.logger) == null ? void 0 : _a.info(`-> setting atom "${atom2.key}" to`, newValue);
615
636
  store.valueMap = HAMT4.set(atom2.key, newValue, store.valueMap);
637
+ if (isAtomDefault(atom2.key)) {
638
+ store.atomsAreDefault = HAMT4.set(atom2.key, false, store.atomsAreDefault);
639
+ }
616
640
  markDone(atom2.key, store);
641
+ (_b = store.config.logger) == null ? void 0 : _b.info(
642
+ ` || evicting caches downstream from "${atom2.key}"`
643
+ );
644
+ evictDownStream(atom2, store);
617
645
  atom2.subject.next({ newValue, oldValue });
618
- (_b = store.config.logger) == null ? void 0 : _b.info(` ||`, `propagating change to`, `"${atom2.key}"`);
619
- propagateChanges(atom2, store);
620
646
  };
621
647
  var setSelectorState = (selector2, next, store = IMPLICIT.STORE) => {
622
648
  var _a, _b;
623
649
  const oldValue = getState__INTERNAL(selector2, store);
624
650
  const newValue = become(next)(oldValue);
625
- (_a = store.config.logger) == null ? void 0 : _a.info(
626
- `->`,
627
- `setting selector`,
628
- `"${selector2.key}"`,
629
- `to`,
630
- newValue
631
- );
632
- (_b = store.config.logger) == null ? void 0 : _b.info(
633
- ` ||`,
634
- `propagating change to`,
635
- `"${selector2.key}"`
636
- );
651
+ (_a = store.config.logger) == null ? void 0 : _a.info(`-> setting selector "${selector2.key}" to`, newValue);
652
+ (_b = store.config.logger) == null ? void 0 : _b.info(` || propagating change made to "${selector2.key}"`);
637
653
  selector2.set(newValue);
638
- markDone(selector2.key, store);
639
- propagateChanges(selector2, store);
640
654
  };
641
- var setState__INTERNAL = (token, value, store = IMPLICIT.STORE) => {
642
- const state = withdraw(token, store);
655
+ var setState__INTERNAL = (state, value, store = IMPLICIT.STORE) => {
643
656
  if (`set` in state) {
644
657
  setSelectorState(state, value, store);
645
658
  } else {
@@ -647,11 +660,119 @@ var setState__INTERNAL = (token, value, store = IMPLICIT.STORE) => {
647
660
  }
648
661
  };
649
662
 
663
+ // src/internal/is-default.ts
664
+ import HAMT5 from "hamt_plus";
665
+ var isAtomDefault = (key, store = IMPLICIT.STORE) => {
666
+ return HAMT5.get(key, store.atomsAreDefault);
667
+ };
668
+ var isSelectorDefault = (key, store = IMPLICIT.STORE) => {
669
+ const roots = traceAllSelectorAtoms(key, store);
670
+ return roots.every((root) => isAtomDefault(root.key, store));
671
+ };
672
+
673
+ // src/internal/selector-internal.ts
674
+ var lookupSelectorSources = (key, store) => store.selectorGraph.getRelations(key).filter(({ source }) => source !== key).map(({ source }) => lookup(source, store));
675
+ var traceSelectorAtoms = (selectorKey, dependency, store) => {
676
+ const roots = [];
677
+ const sources = lookupSelectorSources(dependency.key, store);
678
+ let depth = 0;
679
+ while (sources.length > 0) {
680
+ const source = sources.shift();
681
+ ++depth;
682
+ if (depth > 999) {
683
+ throw new Error(
684
+ `Maximum selector dependency depth exceeded in selector "${selectorKey}".`
685
+ );
686
+ }
687
+ if (source.type !== `atom`) {
688
+ sources.push(...lookupSelectorSources(source.key, store));
689
+ } else {
690
+ roots.push(source);
691
+ }
692
+ }
693
+ return roots;
694
+ };
695
+ var traceAllSelectorAtoms = (selectorKey, store) => {
696
+ const sources = lookupSelectorSources(selectorKey, store);
697
+ return sources.flatMap(
698
+ (source) => source.type === `atom` ? source : traceSelectorAtoms(selectorKey, source, store)
699
+ );
700
+ };
701
+ var updateSelectorAtoms = (selectorKey, dependency, store) => {
702
+ var _a, _b;
703
+ if (dependency.type === `atom`) {
704
+ store.selectorAtoms = store.selectorAtoms.set(selectorKey, dependency.key);
705
+ (_a = store.config.logger) == null ? void 0 : _a.info(
706
+ ` || adding root for "${selectorKey}": ${dependency.key}`
707
+ );
708
+ return;
709
+ }
710
+ const roots = traceSelectorAtoms(selectorKey, dependency, store);
711
+ (_b = store.config.logger) == null ? void 0 : _b.info(` || adding roots for "${selectorKey}":`, roots);
712
+ for (const root of roots) {
713
+ store.selectorAtoms = store.selectorAtoms.set(selectorKey, root.key);
714
+ }
715
+ };
716
+ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
717
+ get: (dependency) => {
718
+ var _a, _b;
719
+ const alreadyRegistered = store.selectorGraph.getRelations(selectorKey).some(({ source }) => source === dependency.key);
720
+ const dependencyState = withdraw(dependency, store);
721
+ const dependencyValue = getState__INTERNAL(dependencyState, store);
722
+ if (alreadyRegistered) {
723
+ (_a = store.config.logger) == null ? void 0 : _a.info(
724
+ ` || ${selectorKey} <- ${dependency.key} =`,
725
+ dependencyValue
726
+ );
727
+ } else {
728
+ (_b = store.config.logger) == null ? void 0 : _b.info(
729
+ `\u{1F50C} registerSelector "${selectorKey}" <- "${dependency.key}" =`,
730
+ dependencyValue
731
+ );
732
+ store.selectorGraph = store.selectorGraph.set(
733
+ selectorKey,
734
+ dependency.key,
735
+ {
736
+ source: dependency.key
737
+ }
738
+ );
739
+ }
740
+ updateSelectorAtoms(selectorKey, dependency, store);
741
+ return dependencyValue;
742
+ },
743
+ set: (stateToken, newValue) => {
744
+ const state = withdraw(stateToken, store);
745
+ setState__INTERNAL(state, newValue, store);
746
+ }
747
+ });
748
+
749
+ // src/internal/subscribe-internal.ts
750
+ var subscribeToRootAtoms = (state, store = IMPLICIT.STORE) => {
751
+ const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomToken) => {
752
+ const atom2 = withdraw(atomToken, store);
753
+ return atom2.subject.subscribe((atomChange) => {
754
+ var _a, _b;
755
+ (_a = store.config.logger) == null ? void 0 : _a.info(
756
+ `\u{1F4E2} atom changed: "${atomToken.key}" (`,
757
+ atomChange.oldValue,
758
+ `->`,
759
+ atomChange.newValue,
760
+ `) re-evaluating "${state.key}"`
761
+ );
762
+ const oldValue = recallState(state, store);
763
+ const newValue = getState__INTERNAL(state, store);
764
+ (_b = store.config.logger) == null ? void 0 : _b.info(` <- ${state.key} became`, newValue);
765
+ state.subject.next({ newValue, oldValue });
766
+ });
767
+ });
768
+ return dependencySubscriptions;
769
+ };
770
+
650
771
  // src/internal/transaction-internal.ts
651
772
  var finishTransaction = (store) => {
652
773
  var _a;
653
774
  store.transaction = { open: false };
654
- (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F4B8}`, `transaction done`);
775
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F6EC}`, `transaction done`);
655
776
  };
656
777
  var startTransaction = (store) => {
657
778
  var _a;
@@ -665,7 +786,7 @@ var startTransaction = (store) => {
665
786
  valueMap: store.valueMap
666
787
  }
667
788
  };
668
- (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F3E6}`, `transaction start`);
789
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F6EB}`, `transaction start`);
669
790
  };
670
791
  var abortTransaction = (store) => {
671
792
  var _a, _b;
@@ -685,17 +806,17 @@ var abortTransaction = (store) => {
685
806
  };
686
807
 
687
808
  // src/atom.ts
688
- import HAMT5 from "hamt_plus";
809
+ import HAMT6 from "hamt_plus";
689
810
  import * as Rx from "rxjs";
690
811
 
691
812
  // ../anvl/src/json/index.ts
692
- import { pipe as pipe6 } from "fp-ts/function";
813
+ import { pipe as pipe7 } from "fp-ts/function";
693
814
  var stringifyJson = (json) => JSON.stringify(json);
694
815
 
695
816
  // src/atom.ts
696
817
  var atom = (options, store = IMPLICIT.STORE) => {
697
818
  var _a, _b, _c;
698
- if (HAMT5.has(options.key, store.atoms)) {
819
+ if (HAMT6.has(options.key, store.atoms)) {
699
820
  (_b = (_a = store.config.logger) == null ? void 0 : _a.error) == null ? void 0 : _b.call(
700
821
  _a,
701
822
  `Key "${options.key}" already exists in the store.`
@@ -704,12 +825,13 @@ var atom = (options, store = IMPLICIT.STORE) => {
704
825
  }
705
826
  const subject = new Rx.Subject();
706
827
  const newAtom = __spreadProps(__spreadValues({}, options), { subject });
707
- store.atoms = HAMT5.set(options.key, newAtom, store.atoms);
708
- store.valueMap = HAMT5.set(options.key, options.default, store.valueMap);
828
+ const initialValue = options.default instanceof Function ? options.default() : options.default;
829
+ store.atoms = HAMT6.set(options.key, newAtom, store.atoms);
830
+ store.atomsAreDefault = HAMT6.set(options.key, true, store.atomsAreDefault);
831
+ store.valueMap = HAMT6.set(options.key, initialValue, store.valueMap);
709
832
  const token = deposit(newAtom);
710
833
  const setSelf = (next) => setState(token, next, store);
711
834
  const onSet = (observe) => subscribe(token, observe, store);
712
- setSelf(options.default);
713
835
  (_c = options.effects) == null ? void 0 : _c.forEach((effect) => effect({ setSelf, onSet }));
714
836
  return token;
715
837
  };
@@ -731,18 +853,18 @@ var atomFamily = (options, store = IMPLICIT.STORE) => (key) => {
731
853
  };
732
854
 
733
855
  // src/selector.ts
734
- import HAMT6 from "hamt_plus";
856
+ import HAMT7 from "hamt_plus";
735
857
  import * as Rx2 from "rxjs";
736
858
  function selector(options, store = IMPLICIT.STORE) {
737
859
  var _a, _b;
738
- if (HAMT6.has(options.key, store.selectors)) {
860
+ if (HAMT7.has(options.key, store.selectors)) {
739
861
  throw new Error(`Key "${options.key}" already exists in the store.`);
740
862
  }
741
863
  const subject = new Rx2.Subject();
742
864
  const { get, set } = registerSelector(options.key, store);
743
865
  const getSelf = () => {
744
866
  const value = options.get({ get });
745
- store.valueMap = HAMT6.set(options.key, value, store.valueMap);
867
+ store.valueMap = HAMT7.set(options.key, value, store.valueMap);
746
868
  return value;
747
869
  };
748
870
  if (!(`set` in options)) {
@@ -750,22 +872,22 @@ function selector(options, store = IMPLICIT.STORE) {
750
872
  subject,
751
873
  get: getSelf
752
874
  });
753
- store.readonlySelectors = HAMT6.set(
875
+ store.readonlySelectors = HAMT7.set(
754
876
  options.key,
755
877
  readonlySelector,
756
878
  store.readonlySelectors
757
879
  );
758
880
  const initialValue2 = getSelf();
759
- (_a = store.config.logger) == null ? void 0 : _a.info(` \u2728`, options.key, `=`, initialValue2);
881
+ (_a = store.config.logger) == null ? void 0 : _a.info(` \u2728 "${options.key}" =`, initialValue2);
760
882
  return __spreadProps(__spreadValues({}, readonlySelector), { type: `readonly_selector` });
761
883
  }
762
884
  const setSelf = (next) => {
763
885
  var _a2;
764
- (_a2 = store.config.logger) == null ? void 0 : _a2.info(`${options.key}.set`, next);
886
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(` <- "${options.key}" became`, next);
765
887
  const oldValue = getSelf();
766
888
  const newValue = become(next)(oldValue);
767
- store.valueMap = HAMT6.set(options.key, newValue, store.valueMap);
768
- console.error({ oldValue });
889
+ store.valueMap = HAMT7.set(options.key, newValue, store.valueMap);
890
+ markDone(options.key, store);
769
891
  subject.next({ newValue, oldValue });
770
892
  options.set({ get, set }, newValue);
771
893
  };
@@ -774,9 +896,9 @@ function selector(options, store = IMPLICIT.STORE) {
774
896
  get: getSelf,
775
897
  set: setSelf
776
898
  });
777
- store.selectors = HAMT6.set(options.key, mySelector, store.selectors);
899
+ store.selectors = HAMT7.set(options.key, mySelector, store.selectors);
778
900
  const initialValue = getSelf();
779
- (_b = store.config.logger) == null ? void 0 : _b.info(` \u2728`, options.key, `=`, initialValue);
901
+ (_b = store.config.logger) == null ? void 0 : _b.info(` \u2728 "${options.key}" =`, initialValue);
780
902
  return __spreadProps(__spreadValues({}, mySelector), { type: `selector` });
781
903
  }
782
904
  function selectorFamily(options, store = IMPLICIT.STORE) {
@@ -805,30 +927,6 @@ function selectorFamily(options, store = IMPLICIT.STORE) {
805
927
  );
806
928
  };
807
929
  }
808
- var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
809
- get: (state) => {
810
- var _a, _b, _c;
811
- const isRegistered = store.selectorGraph.getRelatedIds(selectorKey).includes(state.key);
812
- if (isRegistered) {
813
- (_a = store.config.logger) == null ? void 0 : _a.info(` ||`, selectorKey, `<-`, state.key);
814
- } else {
815
- (_b = store.config.logger) == null ? void 0 : _b.info(
816
- `\u{1F50C} registerSelector`,
817
- state.key,
818
- `->`,
819
- selectorKey
820
- );
821
- store.selectorGraph = store.selectorGraph.set(selectorKey, state.key);
822
- }
823
- const currentValue = getState(state, store);
824
- (_c = store.config.logger) == null ? void 0 : _c.info(` ||`, state.key, `=`, currentValue);
825
- return currentValue;
826
- },
827
- set: (token, newValue) => {
828
- store.selectorGraph.set(token.key, selectorKey);
829
- setState__INTERNAL(token, newValue, store);
830
- }
831
- });
832
930
 
833
931
  // src/transaction.ts
834
932
  var transaction = (options, store = IMPLICIT.STORE) => Object.assign(
@@ -859,15 +957,34 @@ var getState = (token, store = IMPLICIT.STORE) => {
859
957
  const state = withdraw(token, store);
860
958
  return getState__INTERNAL(state, store);
861
959
  };
862
- var setState = (state, value, store = IMPLICIT.STORE) => {
960
+ var setState = (token, value, store = IMPLICIT.STORE) => {
863
961
  startAction(store);
962
+ const state = withdraw(token, store);
864
963
  setState__INTERNAL(state, value, store);
865
964
  finishAction(store);
866
965
  };
966
+ var isDefault = (token, store = IMPLICIT.STORE) => token.type === `atom` ? isAtomDefault(token.key, store) : isSelectorDefault(token.key, store);
867
967
  var subscribe = (token, observe, store = IMPLICIT.STORE) => {
968
+ var _a;
868
969
  const state = withdraw(token, store);
869
970
  const subscription = state.subject.subscribe(observe);
870
- return () => subscription.unsubscribe();
971
+ (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F440} subscribe to "${state.key}"`);
972
+ const dependencySubscriptions = subscribeToRootAtoms(state, store);
973
+ const unsubscribe = dependencySubscriptions === null ? () => {
974
+ var _a2;
975
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(`\u{1F648} unsubscribe from "${state.key}"`);
976
+ subscription.unsubscribe();
977
+ } : () => {
978
+ var _a2;
979
+ (_a2 = store.config.logger) == null ? void 0 : _a2.info(
980
+ `\u{1F648} unsubscribe from "${state.key}" and its dependencies`
981
+ );
982
+ subscription.unsubscribe();
983
+ for (const dependencySubscription of dependencySubscriptions) {
984
+ dependencySubscription.unsubscribe();
985
+ }
986
+ };
987
+ return unsubscribe;
871
988
  };
872
989
  export {
873
990
  internal_exports as __INTERNAL__,
@@ -875,10 +992,11 @@ export {
875
992
  atomFamily,
876
993
  configure,
877
994
  getState,
878
- registerSelector,
995
+ isDefault,
879
996
  selector,
880
997
  selectorFamily,
881
998
  setState,
882
999
  subscribe,
883
1000
  transaction
884
1001
  };
1002
+ //# sourceMappingURL=index.mjs.map