atom.io 0.1.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/README.md +11 -11
- package/dist/{index-3b5d305c.d.ts → index-9d9f5a05.d.ts} +66 -30
- package/dist/index.d.ts +1 -1
- package/dist/index.js +206 -100
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +205 -99
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.ts +11 -7
- package/dist/react/index.js +222 -93
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +221 -92
- package/dist/react/index.mjs.map +1 -1
- package/package.json +21 -7
- package/react/package.json +6 -0
- package/src/atom.ts +78 -0
- package/src/index.ts +91 -0
- package/src/internal/get.ts +109 -0
- package/src/internal/index.ts +23 -0
- package/src/internal/is-default.ts +19 -0
- package/src/internal/operation.ts +49 -0
- package/src/internal/selector-internal.ts +127 -0
- package/src/internal/set.ts +88 -0
- package/src/internal/store.ts +84 -0
- package/src/internal/subscribe-internal.ts +33 -0
- package/src/internal/transaction-internal.ts +34 -0
- package/src/react/index.ts +65 -0
- package/src/selector.ts +132 -0
- package/src/transaction.ts +53 -0
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
|
-
|
|
54
|
-
|
|
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
|
-
(
|
|
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;
|
|
@@ -531,12 +560,12 @@ var startAction = (store) => {
|
|
|
531
560
|
done: /* @__PURE__ */ new Set(),
|
|
532
561
|
prev: store.valueMap
|
|
533
562
|
};
|
|
534
|
-
(_a = store.config.logger) == null ? void 0 : _a.info(`\
|
|
563
|
+
(_a = store.config.logger) == null ? void 0 : _a.info(`\u2B55`, `operation start`);
|
|
535
564
|
};
|
|
536
565
|
var finishAction = (store) => {
|
|
537
566
|
var _a;
|
|
538
567
|
store.operation = { open: false };
|
|
539
|
-
(_a = store.config.logger) == null ? void 0 : _a.info(`\
|
|
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
|
|
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,87 +599,60 @@ var recall = (state, store = IMPLICIT.STORE) => {
|
|
|
570
599
|
};
|
|
571
600
|
|
|
572
601
|
// src/internal/set.ts
|
|
573
|
-
var
|
|
602
|
+
var evictDownStream = (state, store = IMPLICIT.STORE) => {
|
|
574
603
|
var _a, _b;
|
|
575
|
-
const
|
|
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
|
-
|
|
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(` ||`,
|
|
611
|
+
(_b = store.config.logger) == null ? void 0 : _b.info(` ||`, [...store.operation.done], `already done`);
|
|
585
612
|
}
|
|
586
|
-
|
|
587
|
-
var _a2, _b2, _c, _d
|
|
613
|
+
downstream.forEach(({ id: stateKey }) => {
|
|
614
|
+
var _a2, _b2, _c, _d;
|
|
588
615
|
if (isDone(stateKey, store)) {
|
|
589
|
-
(_a2 = store.config.logger) == null ? void 0 : _a2.info(`
|
|
616
|
+
(_a2 = store.config.logger) == null ? void 0 : _a2.info(` || ${stateKey} already done`);
|
|
590
617
|
return;
|
|
591
618
|
}
|
|
592
|
-
(_b2 = store.
|
|
593
|
-
const state2 = (_c = HAMT4.get(stateKey, store.selectors)) != null ? _c : HAMT4.get(stateKey, store.readonlySelectors);
|
|
619
|
+
const state2 = (_b2 = HAMT4.get(stateKey, store.selectors)) != null ? _b2 : HAMT4.get(stateKey, store.readonlySelectors);
|
|
594
620
|
if (!state2) {
|
|
595
|
-
(
|
|
596
|
-
`
|
|
597
|
-
stateKey,
|
|
598
|
-
`is an atom - no need to propagate down`
|
|
621
|
+
(_c = store.config.logger) == null ? void 0 : _c.info(
|
|
622
|
+
` || ${stateKey} is an atom, and can't be downstream`
|
|
599
623
|
);
|
|
600
624
|
return;
|
|
601
625
|
}
|
|
602
626
|
store.valueMap = HAMT4.remove(stateKey, store.valueMap);
|
|
603
|
-
|
|
604
|
-
(_e = store.config.logger) == null ? void 0 : _e.info(` <-`, stateKey, `became`, newValue);
|
|
605
|
-
const oldValue = recall(state2, store);
|
|
606
|
-
state2.subject.next({ newValue, oldValue });
|
|
627
|
+
(_d = store.config.logger) == null ? void 0 : _d.info(` xx evicted "${stateKey}"`);
|
|
607
628
|
markDone(stateKey, store);
|
|
608
|
-
if (`set` in state2)
|
|
609
|
-
propagateDown(state2, store);
|
|
610
629
|
});
|
|
611
630
|
};
|
|
612
631
|
var setAtomState = (atom2, next, store = IMPLICIT.STORE) => {
|
|
613
632
|
var _a, _b;
|
|
614
633
|
const oldValue = getState__INTERNAL(atom2, store);
|
|
615
634
|
const newValue = become(next)(oldValue);
|
|
616
|
-
(_a = store.config.logger) == null ? void 0 : _a.info(
|
|
617
|
-
`->`,
|
|
618
|
-
`setting atom`,
|
|
619
|
-
`"${atom2.key}"`,
|
|
620
|
-
`to`,
|
|
621
|
-
newValue
|
|
622
|
-
);
|
|
635
|
+
(_a = store.config.logger) == null ? void 0 : _a.info(`-> setting atom "${atom2.key}" to`, newValue);
|
|
623
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
|
+
}
|
|
624
640
|
markDone(atom2.key, store);
|
|
625
|
-
atom2.subject.next({ newValue, oldValue });
|
|
626
641
|
(_b = store.config.logger) == null ? void 0 : _b.info(
|
|
627
|
-
`
|
|
628
|
-
`propagating change made to`,
|
|
629
|
-
`"${atom2.key}"`
|
|
642
|
+
` || evicting caches downstream from "${atom2.key}"`
|
|
630
643
|
);
|
|
631
|
-
|
|
644
|
+
evictDownStream(atom2, store);
|
|
645
|
+
atom2.subject.next({ newValue, oldValue });
|
|
632
646
|
};
|
|
633
647
|
var setSelectorState = (selector2, next, store = IMPLICIT.STORE) => {
|
|
634
648
|
var _a, _b;
|
|
635
649
|
const oldValue = getState__INTERNAL(selector2, store);
|
|
636
650
|
const newValue = become(next)(oldValue);
|
|
637
|
-
(_a = store.config.logger) == null ? void 0 : _a.info(
|
|
638
|
-
|
|
639
|
-
`setting selector`,
|
|
640
|
-
`"${selector2.key}"`,
|
|
641
|
-
`to`,
|
|
642
|
-
newValue
|
|
643
|
-
);
|
|
644
|
-
(_b = store.config.logger) == null ? void 0 : _b.info(
|
|
645
|
-
` ||`,
|
|
646
|
-
`propagating change made to`,
|
|
647
|
-
`"${selector2.key}"`
|
|
648
|
-
);
|
|
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}"`);
|
|
649
653
|
selector2.set(newValue);
|
|
650
|
-
propagateDown(selector2, store);
|
|
651
654
|
};
|
|
652
|
-
var setState__INTERNAL = (
|
|
653
|
-
const state = withdraw(token, store);
|
|
655
|
+
var setState__INTERNAL = (state, value, store = IMPLICIT.STORE) => {
|
|
654
656
|
if (`set` in state) {
|
|
655
657
|
setSelectorState(state, value, store);
|
|
656
658
|
} else {
|
|
@@ -658,6 +660,114 @@ var setState__INTERNAL = (token, value, store = IMPLICIT.STORE) => {
|
|
|
658
660
|
}
|
|
659
661
|
};
|
|
660
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
|
+
|
|
661
771
|
// src/internal/transaction-internal.ts
|
|
662
772
|
var finishTransaction = (store) => {
|
|
663
773
|
var _a;
|
|
@@ -696,17 +806,17 @@ var abortTransaction = (store) => {
|
|
|
696
806
|
};
|
|
697
807
|
|
|
698
808
|
// src/atom.ts
|
|
699
|
-
import
|
|
809
|
+
import HAMT6 from "hamt_plus";
|
|
700
810
|
import * as Rx from "rxjs";
|
|
701
811
|
|
|
702
812
|
// ../anvl/src/json/index.ts
|
|
703
|
-
import { pipe as
|
|
813
|
+
import { pipe as pipe7 } from "fp-ts/function";
|
|
704
814
|
var stringifyJson = (json) => JSON.stringify(json);
|
|
705
815
|
|
|
706
816
|
// src/atom.ts
|
|
707
817
|
var atom = (options, store = IMPLICIT.STORE) => {
|
|
708
818
|
var _a, _b, _c;
|
|
709
|
-
if (
|
|
819
|
+
if (HAMT6.has(options.key, store.atoms)) {
|
|
710
820
|
(_b = (_a = store.config.logger) == null ? void 0 : _a.error) == null ? void 0 : _b.call(
|
|
711
821
|
_a,
|
|
712
822
|
`Key "${options.key}" already exists in the store.`
|
|
@@ -715,12 +825,13 @@ var atom = (options, store = IMPLICIT.STORE) => {
|
|
|
715
825
|
}
|
|
716
826
|
const subject = new Rx.Subject();
|
|
717
827
|
const newAtom = __spreadProps(__spreadValues({}, options), { subject });
|
|
718
|
-
|
|
719
|
-
store.
|
|
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);
|
|
720
832
|
const token = deposit(newAtom);
|
|
721
833
|
const setSelf = (next) => setState(token, next, store);
|
|
722
834
|
const onSet = (observe) => subscribe(token, observe, store);
|
|
723
|
-
setSelf(options.default);
|
|
724
835
|
(_c = options.effects) == null ? void 0 : _c.forEach((effect) => effect({ setSelf, onSet }));
|
|
725
836
|
return token;
|
|
726
837
|
};
|
|
@@ -742,18 +853,18 @@ var atomFamily = (options, store = IMPLICIT.STORE) => (key) => {
|
|
|
742
853
|
};
|
|
743
854
|
|
|
744
855
|
// src/selector.ts
|
|
745
|
-
import
|
|
856
|
+
import HAMT7 from "hamt_plus";
|
|
746
857
|
import * as Rx2 from "rxjs";
|
|
747
858
|
function selector(options, store = IMPLICIT.STORE) {
|
|
748
859
|
var _a, _b;
|
|
749
|
-
if (
|
|
860
|
+
if (HAMT7.has(options.key, store.selectors)) {
|
|
750
861
|
throw new Error(`Key "${options.key}" already exists in the store.`);
|
|
751
862
|
}
|
|
752
863
|
const subject = new Rx2.Subject();
|
|
753
864
|
const { get, set } = registerSelector(options.key, store);
|
|
754
865
|
const getSelf = () => {
|
|
755
866
|
const value = options.get({ get });
|
|
756
|
-
store.valueMap =
|
|
867
|
+
store.valueMap = HAMT7.set(options.key, value, store.valueMap);
|
|
757
868
|
return value;
|
|
758
869
|
};
|
|
759
870
|
if (!(`set` in options)) {
|
|
@@ -761,21 +872,21 @@ function selector(options, store = IMPLICIT.STORE) {
|
|
|
761
872
|
subject,
|
|
762
873
|
get: getSelf
|
|
763
874
|
});
|
|
764
|
-
store.readonlySelectors =
|
|
875
|
+
store.readonlySelectors = HAMT7.set(
|
|
765
876
|
options.key,
|
|
766
877
|
readonlySelector,
|
|
767
878
|
store.readonlySelectors
|
|
768
879
|
);
|
|
769
880
|
const initialValue2 = getSelf();
|
|
770
|
-
(_a = store.config.logger) == null ? void 0 : _a.info(` \u2728
|
|
881
|
+
(_a = store.config.logger) == null ? void 0 : _a.info(` \u2728 "${options.key}" =`, initialValue2);
|
|
771
882
|
return __spreadProps(__spreadValues({}, readonlySelector), { type: `readonly_selector` });
|
|
772
883
|
}
|
|
773
884
|
const setSelf = (next) => {
|
|
774
885
|
var _a2;
|
|
775
|
-
(_a2 = store.config.logger) == null ? void 0 : _a2.info(
|
|
886
|
+
(_a2 = store.config.logger) == null ? void 0 : _a2.info(` <- "${options.key}" became`, next);
|
|
776
887
|
const oldValue = getSelf();
|
|
777
888
|
const newValue = become(next)(oldValue);
|
|
778
|
-
store.valueMap =
|
|
889
|
+
store.valueMap = HAMT7.set(options.key, newValue, store.valueMap);
|
|
779
890
|
markDone(options.key, store);
|
|
780
891
|
subject.next({ newValue, oldValue });
|
|
781
892
|
options.set({ get, set }, newValue);
|
|
@@ -785,9 +896,9 @@ function selector(options, store = IMPLICIT.STORE) {
|
|
|
785
896
|
get: getSelf,
|
|
786
897
|
set: setSelf
|
|
787
898
|
});
|
|
788
|
-
store.selectors =
|
|
899
|
+
store.selectors = HAMT7.set(options.key, mySelector, store.selectors);
|
|
789
900
|
const initialValue = getSelf();
|
|
790
|
-
(_b = store.config.logger) == null ? void 0 : _b.info(` \u2728
|
|
901
|
+
(_b = store.config.logger) == null ? void 0 : _b.info(` \u2728 "${options.key}" =`, initialValue);
|
|
791
902
|
return __spreadProps(__spreadValues({}, mySelector), { type: `selector` });
|
|
792
903
|
}
|
|
793
904
|
function selectorFamily(options, store = IMPLICIT.STORE) {
|
|
@@ -816,30 +927,6 @@ function selectorFamily(options, store = IMPLICIT.STORE) {
|
|
|
816
927
|
);
|
|
817
928
|
};
|
|
818
929
|
}
|
|
819
|
-
var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
|
|
820
|
-
get: (state) => {
|
|
821
|
-
var _a, _b, _c;
|
|
822
|
-
const isRegistered = store.selectorGraph.getRelatedIds(selectorKey).includes(state.key);
|
|
823
|
-
if (isRegistered) {
|
|
824
|
-
(_a = store.config.logger) == null ? void 0 : _a.info(` ||`, selectorKey, `<-`, state.key);
|
|
825
|
-
} else {
|
|
826
|
-
(_b = store.config.logger) == null ? void 0 : _b.info(
|
|
827
|
-
`\u{1F50C} registerSelector`,
|
|
828
|
-
selectorKey,
|
|
829
|
-
`<-`,
|
|
830
|
-
state.key
|
|
831
|
-
);
|
|
832
|
-
store.selectorGraph = store.selectorGraph.set(selectorKey, state.key);
|
|
833
|
-
}
|
|
834
|
-
const currentValue = getState(state, store);
|
|
835
|
-
(_c = store.config.logger) == null ? void 0 : _c.info(` ||`, state.key, `=`, currentValue);
|
|
836
|
-
return currentValue;
|
|
837
|
-
},
|
|
838
|
-
set: (token, newValue) => {
|
|
839
|
-
store.selectorGraph.set(token.key, selectorKey);
|
|
840
|
-
setState__INTERNAL(token, newValue, store);
|
|
841
|
-
}
|
|
842
|
-
});
|
|
843
930
|
|
|
844
931
|
// src/transaction.ts
|
|
845
932
|
var transaction = (options, store = IMPLICIT.STORE) => Object.assign(
|
|
@@ -870,15 +957,34 @@ var getState = (token, store = IMPLICIT.STORE) => {
|
|
|
870
957
|
const state = withdraw(token, store);
|
|
871
958
|
return getState__INTERNAL(state, store);
|
|
872
959
|
};
|
|
873
|
-
var setState = (
|
|
960
|
+
var setState = (token, value, store = IMPLICIT.STORE) => {
|
|
874
961
|
startAction(store);
|
|
962
|
+
const state = withdraw(token, store);
|
|
875
963
|
setState__INTERNAL(state, value, store);
|
|
876
964
|
finishAction(store);
|
|
877
965
|
};
|
|
966
|
+
var isDefault = (token, store = IMPLICIT.STORE) => token.type === `atom` ? isAtomDefault(token.key, store) : isSelectorDefault(token.key, store);
|
|
878
967
|
var subscribe = (token, observe, store = IMPLICIT.STORE) => {
|
|
968
|
+
var _a;
|
|
879
969
|
const state = withdraw(token, store);
|
|
880
970
|
const subscription = state.subject.subscribe(observe);
|
|
881
|
-
|
|
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;
|
|
882
988
|
};
|
|
883
989
|
export {
|
|
884
990
|
internal_exports as __INTERNAL__,
|
|
@@ -886,7 +992,7 @@ export {
|
|
|
886
992
|
atomFamily,
|
|
887
993
|
configure,
|
|
888
994
|
getState,
|
|
889
|
-
|
|
995
|
+
isDefault,
|
|
890
996
|
selector,
|
|
891
997
|
selectorFamily,
|
|
892
998
|
setState,
|