atom.io 0.16.2 → 0.17.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.
Files changed (108) hide show
  1. package/dist/chunk-H4Q5FTPZ.js +11 -0
  2. package/dist/chunk-H4Q5FTPZ.js.map +1 -0
  3. package/dist/index.cjs +35 -60
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +8 -8
  6. package/dist/index.js +12 -36
  7. package/dist/index.js.map +1 -1
  8. package/internal/dist/index.cjs +268 -195
  9. package/internal/dist/index.cjs.map +1 -1
  10. package/internal/dist/index.d.ts +36 -11
  11. package/internal/dist/index.js +258 -195
  12. package/internal/dist/index.js.map +1 -1
  13. package/internal/src/atom/create-regular-atom.ts +2 -3
  14. package/internal/src/families/find-in-store.ts +74 -0
  15. package/internal/src/families/index.ts +1 -0
  16. package/internal/src/get-state/get-from-store.ts +14 -0
  17. package/internal/src/get-state/index.ts +2 -0
  18. package/internal/src/{read-or-compute-value.ts → get-state/read-or-compute-value.ts} +3 -3
  19. package/internal/src/index.ts +1 -1
  20. package/internal/src/ingest-updates/ingest-atom-update.ts +2 -2
  21. package/internal/src/ingest-updates/ingest-transaction-update.ts +1 -0
  22. package/internal/src/mutable/create-mutable-atom.ts +3 -4
  23. package/internal/src/mutable/tracker.ts +43 -35
  24. package/internal/src/mutable/transceiver.ts +1 -1
  25. package/internal/src/not-found-error.ts +14 -3
  26. package/internal/src/operation.ts +2 -1
  27. package/internal/src/selector/create-writable-selector.ts +2 -1
  28. package/internal/src/selector/register-selector.ts +6 -5
  29. package/internal/src/set-state/index.ts +1 -0
  30. package/internal/src/set-state/set-atom.ts +17 -3
  31. package/internal/src/set-state/set-into-store.ts +24 -0
  32. package/internal/src/set-state/stow-update.ts +2 -4
  33. package/internal/src/store/store.ts +13 -4
  34. package/internal/src/subscribe/subscribe-to-root-atoms.ts +1 -1
  35. package/internal/src/timeline/add-atom-to-timeline.ts +5 -5
  36. package/internal/src/transaction/abort-transaction.ts +2 -1
  37. package/internal/src/transaction/apply-transaction.ts +5 -3
  38. package/internal/src/transaction/build-transaction.ts +20 -11
  39. package/internal/src/transaction/create-transaction.ts +2 -3
  40. package/internal/src/transaction/index.ts +3 -2
  41. package/internal/src/transaction/is-root-store.ts +23 -0
  42. package/package.json +10 -10
  43. package/react/dist/index.cjs +27 -21
  44. package/react/dist/index.cjs.map +1 -1
  45. package/react/dist/index.d.ts +8 -2
  46. package/react/dist/index.js +28 -22
  47. package/react/dist/index.js.map +1 -1
  48. package/react/src/index.ts +4 -1
  49. package/react/src/use-i.ts +35 -0
  50. package/react/src/use-json.ts +38 -0
  51. package/react/src/use-o.ts +33 -0
  52. package/react/src/use-tl.ts +45 -0
  53. package/realtime-client/dist/index.cjs +167 -64
  54. package/realtime-client/dist/index.cjs.map +1 -1
  55. package/realtime-client/dist/index.d.ts +10 -6
  56. package/realtime-client/dist/index.js +158 -63
  57. package/realtime-client/dist/index.js.map +1 -1
  58. package/realtime-client/src/index.ts +2 -1
  59. package/realtime-client/src/pull-family-member.ts +3 -3
  60. package/realtime-client/src/pull-mutable-family-member.ts +4 -4
  61. package/realtime-client/src/pull-mutable.ts +4 -4
  62. package/realtime-client/src/pull-state.ts +7 -6
  63. package/realtime-client/src/{realtime-client-store.ts → realtime-client-stores/client-main-store.ts} +0 -8
  64. package/realtime-client/src/realtime-client-stores/client-sync-store.ts +15 -0
  65. package/realtime-client/src/realtime-client-stores/index.ts +2 -0
  66. package/realtime-client/src/sync-server-action.ts +134 -40
  67. package/realtime-client/src/sync-state.ts +19 -0
  68. package/realtime-react/dist/index.cjs +43 -26
  69. package/realtime-react/dist/index.cjs.map +1 -1
  70. package/realtime-react/dist/index.d.ts +3 -1
  71. package/realtime-react/dist/index.js +41 -25
  72. package/realtime-react/dist/index.js.map +1 -1
  73. package/realtime-react/src/index.ts +1 -0
  74. package/realtime-react/src/on-mount.ts +3 -21
  75. package/realtime-react/src/use-realtime-service.ts +1 -1
  76. package/realtime-react/src/use-single-effect.ts +29 -0
  77. package/realtime-react/src/use-sync-server-action.ts +4 -7
  78. package/realtime-react/src/use-sync.ts +17 -0
  79. package/realtime-server/dist/index.cjs +239 -56
  80. package/realtime-server/dist/index.cjs.map +1 -1
  81. package/realtime-server/dist/index.d.ts +140 -9
  82. package/realtime-server/dist/index.js +228 -58
  83. package/realtime-server/dist/index.js.map +1 -1
  84. package/realtime-server/src/index.ts +2 -0
  85. package/realtime-server/src/realtime-action-synchronizer.ts +95 -14
  86. package/realtime-server/src/realtime-family-provider.ts +11 -6
  87. package/realtime-server/src/realtime-mutable-family-provider.ts +8 -6
  88. package/realtime-server/src/realtime-mutable-provider.ts +3 -2
  89. package/realtime-server/src/realtime-server-stores/index.ts +2 -0
  90. package/realtime-server/src/realtime-server-stores/server-sync-store.ts +115 -0
  91. package/realtime-server/src/realtime-server-stores/server-user-store.ts +45 -0
  92. package/realtime-server/src/realtime-state-provider.ts +18 -11
  93. package/realtime-server/src/realtime-state-receiver.ts +2 -2
  94. package/realtime-server/src/realtime-state-synchronizer.ts +23 -0
  95. package/realtime-testing/dist/index.cjs +65 -26
  96. package/realtime-testing/dist/index.cjs.map +1 -1
  97. package/realtime-testing/dist/index.d.ts +11 -7
  98. package/realtime-testing/dist/index.js +64 -26
  99. package/realtime-testing/dist/index.js.map +1 -1
  100. package/realtime-testing/src/setup-realtime-test.tsx +83 -43
  101. package/src/find-state.ts +8 -16
  102. package/src/get-state.ts +2 -11
  103. package/src/logger.ts +1 -0
  104. package/src/set-state.ts +1 -13
  105. package/src/silo.ts +7 -3
  106. package/src/transaction.ts +3 -3
  107. package/react/src/store-hooks.ts +0 -87
  108. package/realtime-server/src/realtime-server-store.ts +0 -39
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var atom_io = require('atom.io');
4
3
  var json = require('atom.io/json');
4
+ var atom_io = require('atom.io');
5
5
  var internal = require('atom.io/internal');
6
6
 
7
7
  var __defProp = Object.defineProperty;
@@ -369,11 +369,15 @@ var Store = class {
369
369
  selectorCreation: new Subject(),
370
370
  transactionCreation: new Subject(),
371
371
  timelineCreation: new Subject(),
372
- transactionApplying: new StatefulSubject(null),
372
+ transactionApplying: new StatefulSubject(
373
+ null
374
+ ),
373
375
  operationClose: new Subject()
374
376
  };
375
377
  this.operation = { open: false };
376
- this.transactionMeta = null;
378
+ this.transactionMeta = {
379
+ epoch: -1
380
+ };
377
381
  this.config = {
378
382
  name: `IMPLICIT_STORE`
379
383
  };
@@ -397,7 +401,7 @@ var Store = class {
397
401
  if (store !== null) {
398
402
  this.valueMap = new Map(store == null ? void 0 : store.valueMap);
399
403
  this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
400
- this.transactionMeta = null;
404
+ this.transactionMeta = __spreadValues({}, store == null ? void 0 : store.transactionMeta);
401
405
  this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
402
406
  name
403
407
  });
@@ -590,7 +594,7 @@ var evictCachedValue = (key, target) => {
590
594
  target.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
591
595
  };
592
596
 
593
- // internal/src/read-or-compute-value.ts
597
+ // internal/src/get-state/read-or-compute-value.ts
594
598
  var readOrComputeValue = (state, target) => {
595
599
  if (target.valueMap.has(state.key)) {
596
600
  target.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
@@ -610,12 +614,164 @@ var readOrComputeValue = (state, target) => {
610
614
  );
611
615
  return state.default instanceof Function ? state.default() : state.default;
612
616
  };
617
+ function createRegularAtomFamily(options, store) {
618
+ const subject = new Subject();
619
+ const atomFamily = Object.assign(
620
+ (key) => {
621
+ const subKey = json.stringifyJson(key);
622
+ const family = { key: options.key, subKey };
623
+ const fullKey = `${options.key}(${subKey})`;
624
+ const existing = withdraw({ key: fullKey, type: `atom` }, store);
625
+ let token;
626
+ if (existing) {
627
+ token = deposit(existing);
628
+ } else {
629
+ const individualOptions = {
630
+ key: fullKey,
631
+ default: options.default instanceof Function ? options.default(key) : options.default
632
+ };
633
+ if (options.effects) {
634
+ individualOptions.effects = options.effects(key);
635
+ }
636
+ token = createRegularAtom(individualOptions, family, store);
637
+ subject.next(token);
638
+ }
639
+ return token;
640
+ },
641
+ {
642
+ key: options.key,
643
+ type: `atom_family`,
644
+ subject,
645
+ install: (store2) => createRegularAtomFamily(options, store2)
646
+ }
647
+ );
648
+ const target = newest(store);
649
+ target.families.set(options.key, atomFamily);
650
+ return atomFamily;
651
+ }
652
+
653
+ // internal/src/families/create-atom-family.ts
654
+ function createAtomFamily(options, store) {
655
+ const isMutable2 = `mutable` in options;
656
+ if (isMutable2) {
657
+ return createMutableAtomFamily(options, store);
658
+ }
659
+ return createRegularAtomFamily(options, store);
660
+ }
661
+ function createReadonlySelectorFamily(options, store) {
662
+ const subject = new Subject();
663
+ const readonlySelectorFamily = Object.assign(
664
+ (key) => {
665
+ const target = newest(store);
666
+ const subKey = json.stringifyJson(key);
667
+ const family = { key: options.key, subKey };
668
+ const fullKey = `${options.key}(${subKey})`;
669
+ const existing = target.readonlySelectors.get(fullKey);
670
+ if (existing) {
671
+ return deposit(existing);
672
+ }
673
+ return createReadonlySelector(
674
+ {
675
+ key: fullKey,
676
+ get: options.get(key)
677
+ },
678
+ family,
679
+ store
680
+ );
681
+ },
682
+ {
683
+ key: options.key,
684
+ type: `readonly_selector_family`,
685
+ subject,
686
+ install: (store2) => createReadonlySelectorFamily(options, store2)
687
+ }
688
+ );
689
+ store.families.set(options.key, readonlySelectorFamily);
690
+ return readonlySelectorFamily;
691
+ }
692
+ function createWritableSelectorFamily(options, store) {
693
+ const subject = new Subject();
694
+ const selectorFamily = Object.assign(
695
+ (key) => {
696
+ const subKey = json.stringifyJson(key);
697
+ const family = { key: options.key, subKey };
698
+ const fullKey = `${options.key}(${subKey})`;
699
+ const existing = store.selectors.get(fullKey);
700
+ if (existing) {
701
+ return deposit(existing);
702
+ }
703
+ const token = createWritableSelector(
704
+ {
705
+ key: fullKey,
706
+ get: options.get(key),
707
+ set: options.set(key)
708
+ },
709
+ family,
710
+ store
711
+ );
712
+ subject.next(token);
713
+ return token;
714
+ },
715
+ {
716
+ key: options.key,
717
+ type: `selector_family`,
718
+ subject,
719
+ install: (store2) => createWritableSelectorFamily(options, store2)
720
+ }
721
+ );
722
+ store.families.set(options.key, selectorFamily);
723
+ return selectorFamily;
724
+ }
725
+
726
+ // internal/src/families/create-selector-family.ts
727
+ function createSelectorFamily(options, store) {
728
+ const isWritable = `set` in options;
729
+ if (isWritable) {
730
+ return createWritableSelectorFamily(options, store);
731
+ }
732
+ return createReadonlySelectorFamily(options, store);
733
+ }
734
+
735
+ // internal/src/not-found-error.ts
736
+ var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
737
+ function prettyPrintTokenType(token) {
738
+ if (token.type === `readonly_selector`) {
739
+ return `Readonly Selector`;
740
+ }
741
+ return capitalize(token.type);
742
+ }
743
+ var NotFoundError = class extends Error {
744
+ constructor(token, store) {
745
+ super(
746
+ `${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
747
+ );
748
+ }
749
+ };
750
+
751
+ // internal/src/families/find-in-store.ts
752
+ function findInStore(token, key, store) {
753
+ const familyKey = token.key;
754
+ const family = store.families.get(familyKey);
755
+ if (family === void 0) {
756
+ throw new NotFoundError(token, store);
757
+ }
758
+ const state = family(key);
759
+ return state;
760
+ }
613
761
 
614
762
  // internal/src/set-state/become.ts
615
763
  var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
616
764
  originalThing instanceof Function ? originalThing() : originalThing
617
765
  ) : nextVersionOfThing;
618
766
 
767
+ // internal/src/transaction/is-root-store.ts
768
+ function isRootStore(store) {
769
+ return `epoch` in store.transactionMeta;
770
+ }
771
+ function isChildStore(store) {
772
+ return `phase` in store.transactionMeta;
773
+ }
774
+
619
775
  // internal/src/operation.ts
620
776
  var openOperation = (token, store) => {
621
777
  if (store.operation.open) {
@@ -638,7 +794,7 @@ var openOperation = (token, store) => {
638
794
  `\u2B55`,
639
795
  token.type,
640
796
  token.key,
641
- `operation start in store "${store.config.name}"${store.transactionMeta === null ? `` : ` ${store.transactionMeta.phase} "${store.transactionMeta.update.key}"`}`
797
+ `operation start in store "${store.config.name}"${!isChildStore(store) ? `` : ` ${store.transactionMeta.phase} "${store.transactionMeta.update.key}"`}`
642
798
  );
643
799
  };
644
800
  var closeOperation = (store) => {
@@ -736,7 +892,7 @@ function shouldUpdateBeStowed(key, update) {
736
892
  var stowUpdate = (state, update, store) => {
737
893
  const { key } = state;
738
894
  const target = newest(store);
739
- if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
895
+ if (!isChildStore(target) || target.transactionMeta.phase !== `building`) {
740
896
  store.logger.error(
741
897
  `\u{1F41E}`,
742
898
  `atom`,
@@ -779,7 +935,7 @@ var setAtom = (atom, next, target) => {
779
935
  markDone(atom.key, target);
780
936
  evictDownStream(atom, target);
781
937
  const update = { oldValue, newValue };
782
- if (target.transactionMeta === null) {
938
+ if (isRootStore(target)) {
783
939
  emitUpdate(atom, update, target);
784
940
  } else if (target.parent) {
785
941
  if (target.on.transactionApplying.state === null) {
@@ -789,7 +945,16 @@ var setAtom = (atom, next, target) => {
789
945
  const mutableAtom = target.atoms.get(mutableKey);
790
946
  let mutable = target.valueMap.get(mutableKey);
791
947
  mutable = copyMutableIfWithinTransaction(mutable, mutableAtom, target);
792
- mutable.do(update.newValue);
948
+ const output = mutable.do(update.newValue);
949
+ if (output !== null) {
950
+ target.logger.warn(
951
+ `\u274C`,
952
+ `mutable_atom`,
953
+ mutableKey,
954
+ `could not be updated.`,
955
+ typeof output === `number` ? `Expected update number ${mutable.cacheUpdateNumber + 1}, but got ${output}` : output
956
+ );
957
+ }
793
958
  }
794
959
  }
795
960
  };
@@ -807,6 +972,21 @@ var setAtomOrSelector = (state, value, store) => {
807
972
  }
808
973
  };
809
974
 
975
+ // internal/src/set-state/set-into-store.ts
976
+ function setIntoStore(token, value, store) {
977
+ var _a;
978
+ const rejection = openOperation(token, store);
979
+ if (rejection) {
980
+ return;
981
+ }
982
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
983
+ if (state === void 0) {
984
+ throw new NotFoundError(token, store);
985
+ }
986
+ setAtomOrSelector(state, value, store);
987
+ closeOperation(store);
988
+ }
989
+
810
990
  // internal/src/keys.ts
811
991
  var isAtomKey = (key, store) => newest(store).atoms.has(key);
812
992
  var isSelectorKey = (key, store) => newest(store).selectors.has(key);
@@ -887,11 +1067,12 @@ var updateSelectorAtoms = (selectorKey, dependency, store) => {
887
1067
  // internal/src/selector/register-selector.ts
888
1068
  var registerSelector = (selectorKey, store) => ({
889
1069
  get: (dependency) => {
1070
+ var _a;
890
1071
  const target = newest(store);
891
- const dependencyState = withdraw(dependency, store);
1072
+ const dependencyState = (_a = withdraw(dependency, store)) != null ? _a : withdrawNewFamilyMember(dependency, store);
892
1073
  if (dependencyState === void 0) {
893
1074
  throw new Error(
894
- `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1075
+ `State "${dependency.key}" not found in store "${store.config.name}".`
895
1076
  );
896
1077
  }
897
1078
  const dependencyValue = readOrComputeValue(dependencyState, store);
@@ -924,7 +1105,7 @@ var registerSelector = (selectorKey, store) => ({
924
1105
  }
925
1106
  setAtomOrSelector(state, newValue, store);
926
1107
  },
927
- find: (token, key) => atom_io.findInStore(token, key, store)
1108
+ find: (token, key) => findInStore(token, key, store)
928
1109
  });
929
1110
 
930
1111
  // internal/src/selector/create-readonly-selector.ts
@@ -990,7 +1171,7 @@ var createWritableSelector = (options, family, store) => {
990
1171
  );
991
1172
  cacheValue(options.key, newValue, subject, store);
992
1173
  markDone(options.key, store);
993
- if (target.transactionMeta === null) {
1174
+ if (isRootStore(target)) {
994
1175
  subject.next({ newValue, oldValue });
995
1176
  }
996
1177
  options.set(transactors, newValue);
@@ -1184,6 +1365,8 @@ var subscribeToTransaction = (token, handleUpdate, key, store) => {
1184
1365
  unsubscribe();
1185
1366
  };
1186
1367
  };
1368
+
1369
+ // internal/src/mutable/tracker.ts
1187
1370
  var Tracker = class {
1188
1371
  initializeState(mutableState, store) {
1189
1372
  var _a;
@@ -1208,24 +1391,23 @@ var Tracker = class {
1208
1391
  }
1209
1392
  return latestUpdateState;
1210
1393
  }
1211
- observeCore(mutableState, latestUpdateState, store) {
1212
- const subscriptionKey = `tracker:${store.config.name}:${store.transactionMeta === null ? `main` : store.transactionMeta.update.key}:${mutableState.key}`;
1213
- const originalInnerValue = atom_io.getState(mutableState, store);
1214
- const target = newest(store);
1394
+ observeCore(mutableState, latestUpdateState, target) {
1395
+ const subscriptionKey = `tracker:${target.config.name}:${isChildStore(target) ? target.transactionMeta.update.key : `main`}:${mutableState.key}`;
1396
+ const originalInnerValue = getFromStore(mutableState, target);
1215
1397
  this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
1216
- `tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
1398
+ subscriptionKey,
1217
1399
  (update) => {
1218
1400
  if (target.operation.open) {
1219
1401
  const unsubscribe = target.on.operationClose.subscribe(
1220
1402
  subscriptionKey,
1221
1403
  () => {
1222
1404
  unsubscribe();
1223
- atom_io.setState(latestUpdateState, update, target);
1405
+ setIntoStore(latestUpdateState, update, target);
1224
1406
  }
1225
1407
  );
1226
1408
  } else {
1227
- atom_io.setState(mutableState, (current) => current, target);
1228
- atom_io.setState(latestUpdateState, update, target);
1409
+ setIntoStore(mutableState, (current) => current, target);
1410
+ setIntoStore(latestUpdateState, update, target);
1229
1411
  }
1230
1412
  }
1231
1413
  );
@@ -1234,46 +1416,45 @@ var Tracker = class {
1234
1416
  (update) => {
1235
1417
  if (update.newValue !== update.oldValue) {
1236
1418
  this.unsubscribeFromInnerValue();
1237
- const target2 = newest(store);
1238
1419
  this.unsubscribeFromInnerValue = update.newValue.subscribe(
1239
1420
  subscriptionKey,
1240
1421
  (update2) => {
1241
- if (target2.operation.open) {
1242
- const unsubscribe = target2.on.operationClose.subscribe(
1422
+ if (target.operation.open) {
1423
+ const unsubscribe = target.on.operationClose.subscribe(
1243
1424
  subscriptionKey,
1244
1425
  () => {
1245
1426
  unsubscribe();
1246
- atom_io.setState(latestUpdateState, update2, target2);
1427
+ setIntoStore(latestUpdateState, update2, target);
1247
1428
  }
1248
1429
  );
1249
1430
  } else {
1250
- atom_io.setState(mutableState, (current) => current, target2);
1251
- atom_io.setState(latestUpdateState, update2, target2);
1431
+ setIntoStore(mutableState, (current) => current, target);
1432
+ setIntoStore(latestUpdateState, update2, target);
1252
1433
  }
1253
1434
  }
1254
1435
  );
1255
1436
  }
1256
1437
  },
1257
1438
  subscriptionKey,
1258
- store
1439
+ target
1259
1440
  );
1260
1441
  }
1261
- updateCore(mutableState, latestUpdateState, store) {
1262
- const subscriptionKey = `tracker:${store.config.name}:${store.transactionMeta === null ? `main` : store.transactionMeta.update.key}:${mutableState.key}`;
1442
+ updateCore(mutableState, latestUpdateState, target) {
1443
+ const subscriptionKey = `tracker:${target.config.name}:${isChildStore(target) ? target.transactionMeta.update.key : `main`}:${mutableState.key}`;
1263
1444
  subscribeToState(
1264
1445
  latestUpdateState,
1265
1446
  ({ newValue, oldValue }) => {
1266
- const timelineId = store.timelineAtoms.getRelatedKey(
1447
+ const timelineId = target.timelineAtoms.getRelatedKey(
1267
1448
  latestUpdateState.key
1268
1449
  );
1269
1450
  if (timelineId) {
1270
- const timelineData = store.timelines.get(timelineId);
1451
+ const timelineData = target.timelines.get(timelineId);
1271
1452
  if (timelineData == null ? void 0 : timelineData.timeTraveling) {
1272
1453
  const unsubscribe2 = subscribeToTimeline(
1273
1454
  { key: timelineId, type: `timeline` },
1274
1455
  (update) => {
1275
1456
  unsubscribe2();
1276
- atom_io.setState(
1457
+ setIntoStore(
1277
1458
  mutableState,
1278
1459
  (transceiver) => {
1279
1460
  if (update === `redo` && newValue) {
@@ -1283,34 +1464,41 @@ var Tracker = class {
1283
1464
  }
1284
1465
  return transceiver;
1285
1466
  },
1286
- store
1467
+ target
1287
1468
  );
1288
1469
  },
1289
1470
  subscriptionKey,
1290
- store
1471
+ target
1291
1472
  );
1292
1473
  return;
1293
1474
  }
1294
1475
  }
1295
- const unsubscribe = store.on.operationClose.subscribe(
1476
+ const unsubscribe = target.on.operationClose.subscribe(
1296
1477
  subscriptionKey,
1297
1478
  () => {
1298
1479
  unsubscribe();
1299
- const mutable = atom_io.getState(mutableState, store);
1480
+ const mutable = getFromStore(mutableState, target);
1300
1481
  const updateNumber = newValue === null ? -1 : mutable.getUpdateNumber(newValue);
1301
1482
  const eventOffset = updateNumber - mutable.cacheUpdateNumber;
1302
1483
  if (newValue && eventOffset === 1) {
1303
- atom_io.setState(
1484
+ setIntoStore(
1304
1485
  mutableState,
1305
1486
  (transceiver) => (transceiver.do(newValue), transceiver),
1306
- store
1487
+ target
1488
+ );
1489
+ } else {
1490
+ target.logger.info(
1491
+ `\u274C`,
1492
+ `mutable_atom`,
1493
+ mutableState.key,
1494
+ `could not be updated. Expected update number ${mutable.cacheUpdateNumber + 1}, but got ${updateNumber}`
1307
1495
  );
1308
1496
  }
1309
1497
  }
1310
1498
  );
1311
1499
  },
1312
1500
  subscriptionKey,
1313
- store
1501
+ target
1314
1502
  );
1315
1503
  }
1316
1504
  constructor(mutableState, store) {
@@ -1374,7 +1562,7 @@ function createMutableAtom(options, family, store) {
1374
1562
  const cleanupFunctions = [];
1375
1563
  for (const effect of options.effects) {
1376
1564
  const cleanup = effect({
1377
- setSelf: (next) => atom_io.setState(token, next, store),
1565
+ setSelf: (next) => setIntoStore(token, next, store),
1378
1566
  onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1379
1567
  });
1380
1568
  if (cleanup) {
@@ -1393,125 +1581,6 @@ function createMutableAtom(options, family, store) {
1393
1581
  store.on.atomCreation.next(token);
1394
1582
  return token;
1395
1583
  }
1396
- function createRegularAtomFamily(options, store) {
1397
- const subject = new Subject();
1398
- const atomFamily = Object.assign(
1399
- (key) => {
1400
- const subKey = json.stringifyJson(key);
1401
- const family = { key: options.key, subKey };
1402
- const fullKey = `${options.key}(${subKey})`;
1403
- const existing = withdraw({ key: fullKey, type: `atom` }, store);
1404
- let token;
1405
- if (existing) {
1406
- token = deposit(existing);
1407
- } else {
1408
- const individualOptions = {
1409
- key: fullKey,
1410
- default: options.default instanceof Function ? options.default(key) : options.default
1411
- };
1412
- if (options.effects) {
1413
- individualOptions.effects = options.effects(key);
1414
- }
1415
- token = createRegularAtom(individualOptions, family, store);
1416
- subject.next(token);
1417
- }
1418
- return token;
1419
- },
1420
- {
1421
- key: options.key,
1422
- type: `atom_family`,
1423
- subject,
1424
- install: (store2) => createRegularAtomFamily(options, store2)
1425
- }
1426
- );
1427
- const target = newest(store);
1428
- target.families.set(options.key, atomFamily);
1429
- return atomFamily;
1430
- }
1431
-
1432
- // internal/src/families/create-atom-family.ts
1433
- function createAtomFamily(options, store) {
1434
- const isMutable2 = `mutable` in options;
1435
- if (isMutable2) {
1436
- return createMutableAtomFamily(options, store);
1437
- }
1438
- return createRegularAtomFamily(options, store);
1439
- }
1440
- function createReadonlySelectorFamily(options, store) {
1441
- const subject = new Subject();
1442
- const readonlySelectorFamily = Object.assign(
1443
- (key) => {
1444
- const target = newest(store);
1445
- const subKey = json.stringifyJson(key);
1446
- const family = { key: options.key, subKey };
1447
- const fullKey = `${options.key}(${subKey})`;
1448
- const existing = target.readonlySelectors.get(fullKey);
1449
- if (existing) {
1450
- return deposit(existing);
1451
- }
1452
- return createReadonlySelector(
1453
- {
1454
- key: fullKey,
1455
- get: options.get(key)
1456
- },
1457
- family,
1458
- store
1459
- );
1460
- },
1461
- {
1462
- key: options.key,
1463
- type: `readonly_selector_family`,
1464
- subject,
1465
- install: (store2) => createReadonlySelectorFamily(options, store2)
1466
- }
1467
- );
1468
- store.families.set(options.key, readonlySelectorFamily);
1469
- return readonlySelectorFamily;
1470
- }
1471
- function createWritableSelectorFamily(options, store) {
1472
- const subject = new Subject();
1473
- const selectorFamily = Object.assign(
1474
- (key) => {
1475
- const subKey = json.stringifyJson(key);
1476
- const family = { key: options.key, subKey };
1477
- const fullKey = `${options.key}(${subKey})`;
1478
- const existing = store.selectors.get(fullKey);
1479
- if (existing) {
1480
- return deposit(existing);
1481
- }
1482
- const token = createWritableSelector(
1483
- {
1484
- key: fullKey,
1485
- get: options.get(key),
1486
- set: options.set(key)
1487
- },
1488
- family,
1489
- store
1490
- );
1491
- subject.next(token);
1492
- return token;
1493
- },
1494
- {
1495
- key: options.key,
1496
- type: `selector_family`,
1497
- subject,
1498
- install: (store2) => createWritableSelectorFamily(options, store2)
1499
- }
1500
- );
1501
- store.families.set(options.key, selectorFamily);
1502
- return selectorFamily;
1503
- }
1504
-
1505
- // internal/src/families/create-selector-family.ts
1506
- function createSelectorFamily(options, store) {
1507
- const isWritable = `set` in options;
1508
- if (isWritable) {
1509
- return createWritableSelectorFamily(options, store);
1510
- }
1511
- return createReadonlySelectorFamily(options, store);
1512
- }
1513
-
1514
- // internal/src/mutable/tracker-family.ts
1515
1584
  var FamilyTracker = class {
1516
1585
  constructor(findMutableState, store) {
1517
1586
  this.findLatestUpdateState = createRegularAtomFamily(
@@ -1700,7 +1769,7 @@ function createRegularAtom(options, family, store) {
1700
1769
  const cleanupFunctions = [];
1701
1770
  for (const effect of options.effects) {
1702
1771
  const cleanup = effect({
1703
- setSelf: (next) => atom_io.setState(token, next, store),
1772
+ setSelf: (next) => setIntoStore(token, next, store),
1704
1773
  onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1705
1774
  });
1706
1775
  if (cleanup) {
@@ -1766,10 +1835,22 @@ function getEnvironmentData(store) {
1766
1835
  store
1767
1836
  };
1768
1837
  }
1838
+
1839
+ // internal/src/get-state/get-from-store.ts
1840
+ function getFromStore(token, store) {
1841
+ var _a;
1842
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1843
+ if (state === void 0) {
1844
+ throw new NotFoundError(token, store);
1845
+ }
1846
+ return readOrComputeValue(state, store);
1847
+ }
1848
+
1849
+ // internal/src/ingest-updates/ingest-atom-update.ts
1769
1850
  function ingestAtomUpdate(applying, atomUpdate, store) {
1770
1851
  const { key, newValue, oldValue } = atomUpdate;
1771
1852
  const value = applying === `newValue` ? newValue : oldValue;
1772
- atom_io.setState({ key, type: `atom` }, value, store);
1853
+ setIntoStore({ key, type: `atom` }, value, store);
1773
1854
  }
1774
1855
 
1775
1856
  // internal/src/ingest-updates/ingest-selector-update.ts
@@ -1826,22 +1907,6 @@ var LazyMap = class extends Map {
1826
1907
  }
1827
1908
  };
1828
1909
 
1829
- // internal/src/not-found-error.ts
1830
- var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
1831
- function prettyPrintTokenType(token) {
1832
- if (token.type === `readonly_selector`) {
1833
- return `Readonly Selector`;
1834
- }
1835
- return capitalize(token.type);
1836
- }
1837
- var NotFoundError = class extends Error {
1838
- constructor(token, store) {
1839
- super(
1840
- `${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
1841
- );
1842
- }
1843
- };
1844
-
1845
1910
  // internal/src/timeline/add-atom-to-timeline.ts
1846
1911
  var addAtomToTimeline = (atomToken, tl, store) => {
1847
1912
  let maybeAtom = withdraw(atomToken, store);
@@ -1861,8 +1926,9 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1861
1926
  const target = newest(store);
1862
1927
  const currentSelectorKey = store.operation.open && store.operation.token.type === `selector` ? store.operation.token.key : null;
1863
1928
  const currentSelectorTime = store.operation.open && store.operation.token.type === `selector` ? store.operation.time : null;
1864
- const currentTransactionKey = (_a = target.on.transactionApplying.state) == null ? void 0 : _a.update.key;
1865
- const currentTransactionTime = (_b = target.on.transactionApplying.state) == null ? void 0 : _b.time;
1929
+ const { transactionApplying } = target.on;
1930
+ const currentTransactionKey = (_a = transactionApplying.state) == null ? void 0 : _a.update.key;
1931
+ const currentTransactionInstanceId = (_b = transactionApplying.state) == null ? void 0 : _b.update.id;
1866
1932
  store.logger.info(
1867
1933
  `\u23F3`,
1868
1934
  `timeline`,
@@ -1909,7 +1975,7 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1909
1975
  (update2) => {
1910
1976
  var _a2, _b2;
1911
1977
  unsubscribe();
1912
- if (tl.timeTraveling === null && currentTransactionTime) {
1978
+ if (tl.timeTraveling === null && currentTransactionInstanceId) {
1913
1979
  if (tl.at !== tl.history.length) {
1914
1980
  tl.history.splice(tl.at);
1915
1981
  }
@@ -1936,7 +2002,7 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1936
2002
  const updates = filterUpdates(update2.updates);
1937
2003
  const timelineTransactionUpdate = __spreadProps(__spreadValues({
1938
2004
  type: `transaction_update`,
1939
- timestamp: currentTransactionTime
2005
+ timestamp: Date.now()
1940
2006
  }, update2), {
1941
2007
  updates
1942
2008
  });
@@ -2198,7 +2264,7 @@ var timeTravel = (action, token, store) => {
2198
2264
  // internal/src/transaction/abort-transaction.ts
2199
2265
  var abortTransaction = (store) => {
2200
2266
  const target = newest(store);
2201
- if (target.transactionMeta === null || target.parent === null) {
2267
+ if (!isChildStore(target)) {
2202
2268
  store.logger.warn(
2203
2269
  `\u{1F41E}`,
2204
2270
  `transaction`,
@@ -2221,7 +2287,7 @@ var applyTransaction = (output, store) => {
2221
2287
  var _a;
2222
2288
  const child = newest(store);
2223
2289
  const { parent } = child;
2224
- if (parent === null || child.transactionMeta === null || ((_a = child.transactionMeta) == null ? void 0 : _a.phase) !== `building`) {
2290
+ if (parent === null || !isChildStore(child) || ((_a = child.transactionMeta) == null ? void 0 : _a.phase) !== `building`) {
2225
2291
  store.logger.warn(
2226
2292
  `\u{1F41E}`,
2227
2293
  `transaction`,
@@ -2263,7 +2329,8 @@ var applyTransaction = (output, store) => {
2263
2329
  }
2264
2330
  }
2265
2331
  ingestTransactionUpdate(`newValue`, child.transactionMeta.update, parent);
2266
- if (parent.transactionMeta === null) {
2332
+ if (isRootStore(parent)) {
2333
+ parent.transactionMeta.epoch = child.transactionMeta.update.epoch;
2267
2334
  const myTransaction = withdraw(
2268
2335
  { key: child.transactionMeta.update.key, type: `transaction` },
2269
2336
  store
@@ -2275,21 +2342,20 @@ var applyTransaction = (output, store) => {
2275
2342
  child.transactionMeta.update.key,
2276
2343
  `Finished applying transaction.`
2277
2344
  );
2278
- } else {
2345
+ } else if (isChildStore(parent)) {
2279
2346
  parent.transactionMeta.update.updates.push(child.transactionMeta.update);
2280
2347
  }
2281
2348
  parent.on.transactionApplying.next(null);
2282
2349
  };
2283
2350
  var buildTransaction = (key, params, store, id) => {
2284
2351
  const parent = newest(store);
2285
- const child = {
2352
+ const childBase = {
2286
2353
  parent,
2287
2354
  child: null,
2288
2355
  on: parent.on,
2289
2356
  loggers: parent.loggers,
2290
2357
  logger: parent.logger,
2291
2358
  config: parent.config,
2292
- transactionMeta: null,
2293
2359
  atoms: new LazyMap(parent.atoms),
2294
2360
  atomsThatAreDefault: new Set(parent.atomsThatAreDefault),
2295
2361
  families: new LazyMap(parent.families),
@@ -2306,24 +2372,27 @@ var buildTransaction = (key, params, store, id) => {
2306
2372
  selectors: new LazyMap(parent.selectors),
2307
2373
  valueMap: new LazyMap(parent.valueMap)
2308
2374
  };
2309
- child.transactionMeta = {
2375
+ const transactionMeta = {
2310
2376
  phase: `building`,
2311
- time: Date.now(),
2312
2377
  update: {
2313
2378
  key,
2314
2379
  id: id != null ? id : Math.random().toString(36).slice(2),
2380
+ epoch: isRootStore(parent) ? parent.transactionMeta.epoch + 1 : NaN,
2315
2381
  updates: [],
2316
2382
  params,
2317
2383
  output: void 0
2318
2384
  },
2319
2385
  transactors: {
2320
- get: (token) => atom_io.getState(token, child),
2321
- set: (token, value) => atom_io.setState(token, value, child),
2386
+ get: (token) => getFromStore(token, child),
2387
+ set: (token, value) => setIntoStore(token, value, child),
2322
2388
  run: (token, id2) => atom_io.runTransaction(token, id2, child),
2323
- find: (token, key2) => atom_io.findInStore(token, key2, child),
2389
+ find: (token, key2) => findInStore(token, key2, child),
2324
2390
  env: () => getEnvironmentData(child)
2325
2391
  }
2326
2392
  };
2393
+ const child = Object.assign(childBase, {
2394
+ transactionMeta
2395
+ });
2327
2396
  parent.child = child;
2328
2397
  store.logger.info(
2329
2398
  `\u{1F6EB}`,
@@ -2332,6 +2401,7 @@ var buildTransaction = (key, params, store, id) => {
2332
2401
  `Building transaction with params:`,
2333
2402
  params
2334
2403
  );
2404
+ return child;
2335
2405
  };
2336
2406
 
2337
2407
  // internal/src/transaction/create-transaction.ts
@@ -2340,10 +2410,10 @@ function createTransaction(options, store) {
2340
2410
  key: options.key,
2341
2411
  type: `transaction`,
2342
2412
  run: (params, id) => {
2343
- buildTransaction(options.key, params, store, id);
2413
+ const childStore = buildTransaction(options.key, params, store, id);
2344
2414
  try {
2345
2415
  const target2 = newest(store);
2346
- const { transactors } = target2.transactionMeta;
2416
+ const { transactors } = childStore.transactionMeta;
2347
2417
  const output = options.do(transactors, ...params);
2348
2418
  applyTransaction(output, target2);
2349
2419
  return output;
@@ -2401,7 +2471,9 @@ exports.deleteAtom = deleteAtom;
2401
2471
  exports.deleteSelector = deleteSelector;
2402
2472
  exports.deposit = deposit;
2403
2473
  exports.evictCachedValue = evictCachedValue;
2474
+ exports.findInStore = findInStore;
2404
2475
  exports.getEnvironmentData = getEnvironmentData;
2476
+ exports.getFromStore = getFromStore;
2405
2477
  exports.getJsonFamily = getJsonFamily;
2406
2478
  exports.getJsonToken = getJsonToken;
2407
2479
  exports.getSelectorDependencyKeys = getSelectorDependencyKeys;
@@ -2426,6 +2498,7 @@ exports.readCachedValue = readCachedValue;
2426
2498
  exports.readOrComputeValue = readOrComputeValue;
2427
2499
  exports.registerSelector = registerSelector;
2428
2500
  exports.setAtomOrSelector = setAtomOrSelector;
2501
+ exports.setIntoStore = setIntoStore;
2429
2502
  exports.subscribeToRootAtoms = subscribeToRootAtoms;
2430
2503
  exports.subscribeToState = subscribeToState;
2431
2504
  exports.subscribeToTimeline = subscribeToTimeline;