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,8 @@
1
1
  import { Junction } from '../../dist/chunk-NYTGCPHB.js';
2
+ import { isChildStore, isRootStore } from '../../dist/chunk-H4Q5FTPZ.js';
2
3
  import { __spreadValues, __spreadProps } from '../../dist/chunk-PZLG2HP3.js';
3
- import { AtomIOLogger, findInStore, getState, setState, runTransaction } from 'atom.io';
4
- import { selectJson, stringifyJson, parseJson, selectJsonFamily } from 'atom.io/json';
4
+ import { stringifyJson, selectJson, parseJson, selectJsonFamily } from 'atom.io/json';
5
+ import { AtomIOLogger, runTransaction } from 'atom.io';
5
6
  import { withdraw as withdraw$1 } from 'atom.io/internal';
6
7
 
7
8
  // internal/src/lineage.ts
@@ -91,11 +92,15 @@ var Store = class {
91
92
  selectorCreation: new Subject(),
92
93
  transactionCreation: new Subject(),
93
94
  timelineCreation: new Subject(),
94
- transactionApplying: new StatefulSubject(null),
95
+ transactionApplying: new StatefulSubject(
96
+ null
97
+ ),
95
98
  operationClose: new Subject()
96
99
  };
97
100
  this.operation = { open: false };
98
- this.transactionMeta = null;
101
+ this.transactionMeta = {
102
+ epoch: -1
103
+ };
99
104
  this.config = {
100
105
  name: `IMPLICIT_STORE`
101
106
  };
@@ -119,7 +124,7 @@ var Store = class {
119
124
  if (store !== null) {
120
125
  this.valueMap = new Map(store == null ? void 0 : store.valueMap);
121
126
  this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
122
- this.transactionMeta = null;
127
+ this.transactionMeta = __spreadValues({}, store == null ? void 0 : store.transactionMeta);
123
128
  this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
124
129
  name
125
130
  });
@@ -312,7 +317,7 @@ var evictCachedValue = (key, target) => {
312
317
  target.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
313
318
  };
314
319
 
315
- // internal/src/read-or-compute-value.ts
320
+ // internal/src/get-state/read-or-compute-value.ts
316
321
  var readOrComputeValue = (state, target) => {
317
322
  if (target.valueMap.has(state.key)) {
318
323
  target.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
@@ -332,6 +337,150 @@ var readOrComputeValue = (state, target) => {
332
337
  );
333
338
  return state.default instanceof Function ? state.default() : state.default;
334
339
  };
340
+ function createRegularAtomFamily(options, store) {
341
+ const subject = new Subject();
342
+ const atomFamily = Object.assign(
343
+ (key) => {
344
+ const subKey = stringifyJson(key);
345
+ const family = { key: options.key, subKey };
346
+ const fullKey = `${options.key}(${subKey})`;
347
+ const existing = withdraw({ key: fullKey, type: `atom` }, store);
348
+ let token;
349
+ if (existing) {
350
+ token = deposit(existing);
351
+ } else {
352
+ const individualOptions = {
353
+ key: fullKey,
354
+ default: options.default instanceof Function ? options.default(key) : options.default
355
+ };
356
+ if (options.effects) {
357
+ individualOptions.effects = options.effects(key);
358
+ }
359
+ token = createRegularAtom(individualOptions, family, store);
360
+ subject.next(token);
361
+ }
362
+ return token;
363
+ },
364
+ {
365
+ key: options.key,
366
+ type: `atom_family`,
367
+ subject,
368
+ install: (store2) => createRegularAtomFamily(options, store2)
369
+ }
370
+ );
371
+ const target = newest(store);
372
+ target.families.set(options.key, atomFamily);
373
+ return atomFamily;
374
+ }
375
+
376
+ // internal/src/families/create-atom-family.ts
377
+ function createAtomFamily(options, store) {
378
+ const isMutable2 = `mutable` in options;
379
+ if (isMutable2) {
380
+ return createMutableAtomFamily(options, store);
381
+ }
382
+ return createRegularAtomFamily(options, store);
383
+ }
384
+ function createReadonlySelectorFamily(options, store) {
385
+ const subject = new Subject();
386
+ const readonlySelectorFamily = Object.assign(
387
+ (key) => {
388
+ const target = newest(store);
389
+ const subKey = stringifyJson(key);
390
+ const family = { key: options.key, subKey };
391
+ const fullKey = `${options.key}(${subKey})`;
392
+ const existing = target.readonlySelectors.get(fullKey);
393
+ if (existing) {
394
+ return deposit(existing);
395
+ }
396
+ return createReadonlySelector(
397
+ {
398
+ key: fullKey,
399
+ get: options.get(key)
400
+ },
401
+ family,
402
+ store
403
+ );
404
+ },
405
+ {
406
+ key: options.key,
407
+ type: `readonly_selector_family`,
408
+ subject,
409
+ install: (store2) => createReadonlySelectorFamily(options, store2)
410
+ }
411
+ );
412
+ store.families.set(options.key, readonlySelectorFamily);
413
+ return readonlySelectorFamily;
414
+ }
415
+ function createWritableSelectorFamily(options, store) {
416
+ const subject = new Subject();
417
+ const selectorFamily = Object.assign(
418
+ (key) => {
419
+ const subKey = stringifyJson(key);
420
+ const family = { key: options.key, subKey };
421
+ const fullKey = `${options.key}(${subKey})`;
422
+ const existing = store.selectors.get(fullKey);
423
+ if (existing) {
424
+ return deposit(existing);
425
+ }
426
+ const token = createWritableSelector(
427
+ {
428
+ key: fullKey,
429
+ get: options.get(key),
430
+ set: options.set(key)
431
+ },
432
+ family,
433
+ store
434
+ );
435
+ subject.next(token);
436
+ return token;
437
+ },
438
+ {
439
+ key: options.key,
440
+ type: `selector_family`,
441
+ subject,
442
+ install: (store2) => createWritableSelectorFamily(options, store2)
443
+ }
444
+ );
445
+ store.families.set(options.key, selectorFamily);
446
+ return selectorFamily;
447
+ }
448
+
449
+ // internal/src/families/create-selector-family.ts
450
+ function createSelectorFamily(options, store) {
451
+ const isWritable = `set` in options;
452
+ if (isWritable) {
453
+ return createWritableSelectorFamily(options, store);
454
+ }
455
+ return createReadonlySelectorFamily(options, store);
456
+ }
457
+
458
+ // internal/src/not-found-error.ts
459
+ var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
460
+ function prettyPrintTokenType(token) {
461
+ if (token.type === `readonly_selector`) {
462
+ return `Readonly Selector`;
463
+ }
464
+ return capitalize(token.type);
465
+ }
466
+ var NotFoundError = class extends Error {
467
+ constructor(token, store) {
468
+ super(
469
+ `${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
470
+ );
471
+ }
472
+ };
473
+
474
+ // internal/src/families/find-in-store.ts
475
+ function findInStore(token, key, store) {
476
+ const familyKey = token.key;
477
+ const family = store.families.get(familyKey);
478
+ if (family === void 0) {
479
+ throw new NotFoundError(token, store);
480
+ }
481
+ const state = family(key);
482
+ return state;
483
+ }
335
484
 
336
485
  // internal/src/set-state/become.ts
337
486
  var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
@@ -360,7 +509,7 @@ var openOperation = (token, store) => {
360
509
  `\u2B55`,
361
510
  token.type,
362
511
  token.key,
363
- `operation start in store "${store.config.name}"${store.transactionMeta === null ? `` : ` ${store.transactionMeta.phase} "${store.transactionMeta.update.key}"`}`
512
+ `operation start in store "${store.config.name}"${!isChildStore(store) ? `` : ` ${store.transactionMeta.phase} "${store.transactionMeta.update.key}"`}`
364
513
  );
365
514
  };
366
515
  var closeOperation = (store) => {
@@ -458,7 +607,7 @@ function shouldUpdateBeStowed(key, update) {
458
607
  var stowUpdate = (state, update, store) => {
459
608
  const { key } = state;
460
609
  const target = newest(store);
461
- if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
610
+ if (!isChildStore(target) || target.transactionMeta.phase !== `building`) {
462
611
  store.logger.error(
463
612
  `\u{1F41E}`,
464
613
  `atom`,
@@ -501,7 +650,7 @@ var setAtom = (atom, next, target) => {
501
650
  markDone(atom.key, target);
502
651
  evictDownStream(atom, target);
503
652
  const update = { oldValue, newValue };
504
- if (target.transactionMeta === null) {
653
+ if (isRootStore(target)) {
505
654
  emitUpdate(atom, update, target);
506
655
  } else if (target.parent) {
507
656
  if (target.on.transactionApplying.state === null) {
@@ -511,7 +660,16 @@ var setAtom = (atom, next, target) => {
511
660
  const mutableAtom = target.atoms.get(mutableKey);
512
661
  let mutable = target.valueMap.get(mutableKey);
513
662
  mutable = copyMutableIfWithinTransaction(mutable, mutableAtom, target);
514
- mutable.do(update.newValue);
663
+ const output = mutable.do(update.newValue);
664
+ if (output !== null) {
665
+ target.logger.warn(
666
+ `\u274C`,
667
+ `mutable_atom`,
668
+ mutableKey,
669
+ `could not be updated.`,
670
+ typeof output === `number` ? `Expected update number ${mutable.cacheUpdateNumber + 1}, but got ${output}` : output
671
+ );
672
+ }
515
673
  }
516
674
  }
517
675
  };
@@ -529,6 +687,21 @@ var setAtomOrSelector = (state, value, store) => {
529
687
  }
530
688
  };
531
689
 
690
+ // internal/src/set-state/set-into-store.ts
691
+ function setIntoStore(token, value, store) {
692
+ var _a;
693
+ const rejection = openOperation(token, store);
694
+ if (rejection) {
695
+ return;
696
+ }
697
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
698
+ if (state === void 0) {
699
+ throw new NotFoundError(token, store);
700
+ }
701
+ setAtomOrSelector(state, value, store);
702
+ closeOperation(store);
703
+ }
704
+
532
705
  // internal/src/keys.ts
533
706
  var isAtomKey = (key, store) => newest(store).atoms.has(key);
534
707
  var isSelectorKey = (key, store) => newest(store).selectors.has(key);
@@ -609,11 +782,12 @@ var updateSelectorAtoms = (selectorKey, dependency, store) => {
609
782
  // internal/src/selector/register-selector.ts
610
783
  var registerSelector = (selectorKey, store) => ({
611
784
  get: (dependency) => {
785
+ var _a;
612
786
  const target = newest(store);
613
- const dependencyState = withdraw(dependency, store);
787
+ const dependencyState = (_a = withdraw(dependency, store)) != null ? _a : withdrawNewFamilyMember(dependency, store);
614
788
  if (dependencyState === void 0) {
615
789
  throw new Error(
616
- `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
790
+ `State "${dependency.key}" not found in store "${store.config.name}".`
617
791
  );
618
792
  }
619
793
  const dependencyValue = readOrComputeValue(dependencyState, store);
@@ -712,7 +886,7 @@ var createWritableSelector = (options, family, store) => {
712
886
  );
713
887
  cacheValue(options.key, newValue, subject, store);
714
888
  markDone(options.key, store);
715
- if (target.transactionMeta === null) {
889
+ if (isRootStore(target)) {
716
890
  subject.next({ newValue, oldValue });
717
891
  }
718
892
  options.set(transactors, newValue);
@@ -906,6 +1080,8 @@ var subscribeToTransaction = (token, handleUpdate, key, store) => {
906
1080
  unsubscribe();
907
1081
  };
908
1082
  };
1083
+
1084
+ // internal/src/mutable/tracker.ts
909
1085
  var Tracker = class {
910
1086
  initializeState(mutableState, store) {
911
1087
  var _a;
@@ -930,24 +1106,23 @@ var Tracker = class {
930
1106
  }
931
1107
  return latestUpdateState;
932
1108
  }
933
- observeCore(mutableState, latestUpdateState, store) {
934
- const subscriptionKey = `tracker:${store.config.name}:${store.transactionMeta === null ? `main` : store.transactionMeta.update.key}:${mutableState.key}`;
935
- const originalInnerValue = getState(mutableState, store);
936
- const target = newest(store);
1109
+ observeCore(mutableState, latestUpdateState, target) {
1110
+ const subscriptionKey = `tracker:${target.config.name}:${isChildStore(target) ? target.transactionMeta.update.key : `main`}:${mutableState.key}`;
1111
+ const originalInnerValue = getFromStore(mutableState, target);
937
1112
  this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
938
- `tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
1113
+ subscriptionKey,
939
1114
  (update) => {
940
1115
  if (target.operation.open) {
941
1116
  const unsubscribe = target.on.operationClose.subscribe(
942
1117
  subscriptionKey,
943
1118
  () => {
944
1119
  unsubscribe();
945
- setState(latestUpdateState, update, target);
1120
+ setIntoStore(latestUpdateState, update, target);
946
1121
  }
947
1122
  );
948
1123
  } else {
949
- setState(mutableState, (current) => current, target);
950
- setState(latestUpdateState, update, target);
1124
+ setIntoStore(mutableState, (current) => current, target);
1125
+ setIntoStore(latestUpdateState, update, target);
951
1126
  }
952
1127
  }
953
1128
  );
@@ -956,46 +1131,45 @@ var Tracker = class {
956
1131
  (update) => {
957
1132
  if (update.newValue !== update.oldValue) {
958
1133
  this.unsubscribeFromInnerValue();
959
- const target2 = newest(store);
960
1134
  this.unsubscribeFromInnerValue = update.newValue.subscribe(
961
1135
  subscriptionKey,
962
1136
  (update2) => {
963
- if (target2.operation.open) {
964
- const unsubscribe = target2.on.operationClose.subscribe(
1137
+ if (target.operation.open) {
1138
+ const unsubscribe = target.on.operationClose.subscribe(
965
1139
  subscriptionKey,
966
1140
  () => {
967
1141
  unsubscribe();
968
- setState(latestUpdateState, update2, target2);
1142
+ setIntoStore(latestUpdateState, update2, target);
969
1143
  }
970
1144
  );
971
1145
  } else {
972
- setState(mutableState, (current) => current, target2);
973
- setState(latestUpdateState, update2, target2);
1146
+ setIntoStore(mutableState, (current) => current, target);
1147
+ setIntoStore(latestUpdateState, update2, target);
974
1148
  }
975
1149
  }
976
1150
  );
977
1151
  }
978
1152
  },
979
1153
  subscriptionKey,
980
- store
1154
+ target
981
1155
  );
982
1156
  }
983
- updateCore(mutableState, latestUpdateState, store) {
984
- const subscriptionKey = `tracker:${store.config.name}:${store.transactionMeta === null ? `main` : store.transactionMeta.update.key}:${mutableState.key}`;
1157
+ updateCore(mutableState, latestUpdateState, target) {
1158
+ const subscriptionKey = `tracker:${target.config.name}:${isChildStore(target) ? target.transactionMeta.update.key : `main`}:${mutableState.key}`;
985
1159
  subscribeToState(
986
1160
  latestUpdateState,
987
1161
  ({ newValue, oldValue }) => {
988
- const timelineId = store.timelineAtoms.getRelatedKey(
1162
+ const timelineId = target.timelineAtoms.getRelatedKey(
989
1163
  latestUpdateState.key
990
1164
  );
991
1165
  if (timelineId) {
992
- const timelineData = store.timelines.get(timelineId);
1166
+ const timelineData = target.timelines.get(timelineId);
993
1167
  if (timelineData == null ? void 0 : timelineData.timeTraveling) {
994
1168
  const unsubscribe2 = subscribeToTimeline(
995
1169
  { key: timelineId, type: `timeline` },
996
1170
  (update) => {
997
1171
  unsubscribe2();
998
- setState(
1172
+ setIntoStore(
999
1173
  mutableState,
1000
1174
  (transceiver) => {
1001
1175
  if (update === `redo` && newValue) {
@@ -1005,34 +1179,41 @@ var Tracker = class {
1005
1179
  }
1006
1180
  return transceiver;
1007
1181
  },
1008
- store
1182
+ target
1009
1183
  );
1010
1184
  },
1011
1185
  subscriptionKey,
1012
- store
1186
+ target
1013
1187
  );
1014
1188
  return;
1015
1189
  }
1016
1190
  }
1017
- const unsubscribe = store.on.operationClose.subscribe(
1191
+ const unsubscribe = target.on.operationClose.subscribe(
1018
1192
  subscriptionKey,
1019
1193
  () => {
1020
1194
  unsubscribe();
1021
- const mutable = getState(mutableState, store);
1195
+ const mutable = getFromStore(mutableState, target);
1022
1196
  const updateNumber = newValue === null ? -1 : mutable.getUpdateNumber(newValue);
1023
1197
  const eventOffset = updateNumber - mutable.cacheUpdateNumber;
1024
1198
  if (newValue && eventOffset === 1) {
1025
- setState(
1199
+ setIntoStore(
1026
1200
  mutableState,
1027
1201
  (transceiver) => (transceiver.do(newValue), transceiver),
1028
- store
1202
+ target
1203
+ );
1204
+ } else {
1205
+ target.logger.info(
1206
+ `\u274C`,
1207
+ `mutable_atom`,
1208
+ mutableState.key,
1209
+ `could not be updated. Expected update number ${mutable.cacheUpdateNumber + 1}, but got ${updateNumber}`
1029
1210
  );
1030
1211
  }
1031
1212
  }
1032
1213
  );
1033
1214
  },
1034
1215
  subscriptionKey,
1035
- store
1216
+ target
1036
1217
  );
1037
1218
  }
1038
1219
  constructor(mutableState, store) {
@@ -1096,7 +1277,7 @@ function createMutableAtom(options, family, store) {
1096
1277
  const cleanupFunctions = [];
1097
1278
  for (const effect of options.effects) {
1098
1279
  const cleanup = effect({
1099
- setSelf: (next) => setState(token, next, store),
1280
+ setSelf: (next) => setIntoStore(token, next, store),
1100
1281
  onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1101
1282
  });
1102
1283
  if (cleanup) {
@@ -1115,125 +1296,6 @@ function createMutableAtom(options, family, store) {
1115
1296
  store.on.atomCreation.next(token);
1116
1297
  return token;
1117
1298
  }
1118
- function createRegularAtomFamily(options, store) {
1119
- const subject = new Subject();
1120
- const atomFamily = Object.assign(
1121
- (key) => {
1122
- const subKey = stringifyJson(key);
1123
- const family = { key: options.key, subKey };
1124
- const fullKey = `${options.key}(${subKey})`;
1125
- const existing = withdraw({ key: fullKey, type: `atom` }, store);
1126
- let token;
1127
- if (existing) {
1128
- token = deposit(existing);
1129
- } else {
1130
- const individualOptions = {
1131
- key: fullKey,
1132
- default: options.default instanceof Function ? options.default(key) : options.default
1133
- };
1134
- if (options.effects) {
1135
- individualOptions.effects = options.effects(key);
1136
- }
1137
- token = createRegularAtom(individualOptions, family, store);
1138
- subject.next(token);
1139
- }
1140
- return token;
1141
- },
1142
- {
1143
- key: options.key,
1144
- type: `atom_family`,
1145
- subject,
1146
- install: (store2) => createRegularAtomFamily(options, store2)
1147
- }
1148
- );
1149
- const target = newest(store);
1150
- target.families.set(options.key, atomFamily);
1151
- return atomFamily;
1152
- }
1153
-
1154
- // internal/src/families/create-atom-family.ts
1155
- function createAtomFamily(options, store) {
1156
- const isMutable2 = `mutable` in options;
1157
- if (isMutable2) {
1158
- return createMutableAtomFamily(options, store);
1159
- }
1160
- return createRegularAtomFamily(options, store);
1161
- }
1162
- function createReadonlySelectorFamily(options, store) {
1163
- const subject = new Subject();
1164
- const readonlySelectorFamily = Object.assign(
1165
- (key) => {
1166
- const target = newest(store);
1167
- const subKey = stringifyJson(key);
1168
- const family = { key: options.key, subKey };
1169
- const fullKey = `${options.key}(${subKey})`;
1170
- const existing = target.readonlySelectors.get(fullKey);
1171
- if (existing) {
1172
- return deposit(existing);
1173
- }
1174
- return createReadonlySelector(
1175
- {
1176
- key: fullKey,
1177
- get: options.get(key)
1178
- },
1179
- family,
1180
- store
1181
- );
1182
- },
1183
- {
1184
- key: options.key,
1185
- type: `readonly_selector_family`,
1186
- subject,
1187
- install: (store2) => createReadonlySelectorFamily(options, store2)
1188
- }
1189
- );
1190
- store.families.set(options.key, readonlySelectorFamily);
1191
- return readonlySelectorFamily;
1192
- }
1193
- function createWritableSelectorFamily(options, store) {
1194
- const subject = new Subject();
1195
- const selectorFamily = Object.assign(
1196
- (key) => {
1197
- const subKey = stringifyJson(key);
1198
- const family = { key: options.key, subKey };
1199
- const fullKey = `${options.key}(${subKey})`;
1200
- const existing = store.selectors.get(fullKey);
1201
- if (existing) {
1202
- return deposit(existing);
1203
- }
1204
- const token = createWritableSelector(
1205
- {
1206
- key: fullKey,
1207
- get: options.get(key),
1208
- set: options.set(key)
1209
- },
1210
- family,
1211
- store
1212
- );
1213
- subject.next(token);
1214
- return token;
1215
- },
1216
- {
1217
- key: options.key,
1218
- type: `selector_family`,
1219
- subject,
1220
- install: (store2) => createWritableSelectorFamily(options, store2)
1221
- }
1222
- );
1223
- store.families.set(options.key, selectorFamily);
1224
- return selectorFamily;
1225
- }
1226
-
1227
- // internal/src/families/create-selector-family.ts
1228
- function createSelectorFamily(options, store) {
1229
- const isWritable = `set` in options;
1230
- if (isWritable) {
1231
- return createWritableSelectorFamily(options, store);
1232
- }
1233
- return createReadonlySelectorFamily(options, store);
1234
- }
1235
-
1236
- // internal/src/mutable/tracker-family.ts
1237
1299
  var FamilyTracker = class {
1238
1300
  constructor(findMutableState, store) {
1239
1301
  this.findLatestUpdateState = createRegularAtomFamily(
@@ -1422,7 +1484,7 @@ function createRegularAtom(options, family, store) {
1422
1484
  const cleanupFunctions = [];
1423
1485
  for (const effect of options.effects) {
1424
1486
  const cleanup = effect({
1425
- setSelf: (next) => setState(token, next, store),
1487
+ setSelf: (next) => setIntoStore(token, next, store),
1426
1488
  onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1427
1489
  });
1428
1490
  if (cleanup) {
@@ -1488,10 +1550,22 @@ function getEnvironmentData(store) {
1488
1550
  store
1489
1551
  };
1490
1552
  }
1553
+
1554
+ // internal/src/get-state/get-from-store.ts
1555
+ function getFromStore(token, store) {
1556
+ var _a;
1557
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1558
+ if (state === void 0) {
1559
+ throw new NotFoundError(token, store);
1560
+ }
1561
+ return readOrComputeValue(state, store);
1562
+ }
1563
+
1564
+ // internal/src/ingest-updates/ingest-atom-update.ts
1491
1565
  function ingestAtomUpdate(applying, atomUpdate, store) {
1492
1566
  const { key, newValue, oldValue } = atomUpdate;
1493
1567
  const value = applying === `newValue` ? newValue : oldValue;
1494
- setState({ key, type: `atom` }, value, store);
1568
+ setIntoStore({ key, type: `atom` }, value, store);
1495
1569
  }
1496
1570
 
1497
1571
  // internal/src/ingest-updates/ingest-selector-update.ts
@@ -1548,22 +1622,6 @@ var LazyMap = class extends Map {
1548
1622
  }
1549
1623
  };
1550
1624
 
1551
- // internal/src/not-found-error.ts
1552
- var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
1553
- function prettyPrintTokenType(token) {
1554
- if (token.type === `readonly_selector`) {
1555
- return `Readonly Selector`;
1556
- }
1557
- return capitalize(token.type);
1558
- }
1559
- var NotFoundError = class extends Error {
1560
- constructor(token, store) {
1561
- super(
1562
- `${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
1563
- );
1564
- }
1565
- };
1566
-
1567
1625
  // internal/src/timeline/add-atom-to-timeline.ts
1568
1626
  var addAtomToTimeline = (atomToken, tl, store) => {
1569
1627
  let maybeAtom = withdraw(atomToken, store);
@@ -1583,8 +1641,9 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1583
1641
  const target = newest(store);
1584
1642
  const currentSelectorKey = store.operation.open && store.operation.token.type === `selector` ? store.operation.token.key : null;
1585
1643
  const currentSelectorTime = store.operation.open && store.operation.token.type === `selector` ? store.operation.time : null;
1586
- const currentTransactionKey = (_a = target.on.transactionApplying.state) == null ? void 0 : _a.update.key;
1587
- const currentTransactionTime = (_b = target.on.transactionApplying.state) == null ? void 0 : _b.time;
1644
+ const { transactionApplying } = target.on;
1645
+ const currentTransactionKey = (_a = transactionApplying.state) == null ? void 0 : _a.update.key;
1646
+ const currentTransactionInstanceId = (_b = transactionApplying.state) == null ? void 0 : _b.update.id;
1588
1647
  store.logger.info(
1589
1648
  `\u23F3`,
1590
1649
  `timeline`,
@@ -1631,7 +1690,7 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1631
1690
  (update2) => {
1632
1691
  var _a2, _b2;
1633
1692
  unsubscribe();
1634
- if (tl.timeTraveling === null && currentTransactionTime) {
1693
+ if (tl.timeTraveling === null && currentTransactionInstanceId) {
1635
1694
  if (tl.at !== tl.history.length) {
1636
1695
  tl.history.splice(tl.at);
1637
1696
  }
@@ -1658,7 +1717,7 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1658
1717
  const updates = filterUpdates(update2.updates);
1659
1718
  const timelineTransactionUpdate = __spreadProps(__spreadValues({
1660
1719
  type: `transaction_update`,
1661
- timestamp: currentTransactionTime
1720
+ timestamp: Date.now()
1662
1721
  }, update2), {
1663
1722
  updates
1664
1723
  });
@@ -1920,7 +1979,7 @@ var timeTravel = (action, token, store) => {
1920
1979
  // internal/src/transaction/abort-transaction.ts
1921
1980
  var abortTransaction = (store) => {
1922
1981
  const target = newest(store);
1923
- if (target.transactionMeta === null || target.parent === null) {
1982
+ if (!isChildStore(target)) {
1924
1983
  store.logger.warn(
1925
1984
  `\u{1F41E}`,
1926
1985
  `transaction`,
@@ -1943,7 +2002,7 @@ var applyTransaction = (output, store) => {
1943
2002
  var _a;
1944
2003
  const child = newest(store);
1945
2004
  const { parent } = child;
1946
- if (parent === null || child.transactionMeta === null || ((_a = child.transactionMeta) == null ? void 0 : _a.phase) !== `building`) {
2005
+ if (parent === null || !isChildStore(child) || ((_a = child.transactionMeta) == null ? void 0 : _a.phase) !== `building`) {
1947
2006
  store.logger.warn(
1948
2007
  `\u{1F41E}`,
1949
2008
  `transaction`,
@@ -1985,7 +2044,8 @@ var applyTransaction = (output, store) => {
1985
2044
  }
1986
2045
  }
1987
2046
  ingestTransactionUpdate(`newValue`, child.transactionMeta.update, parent);
1988
- if (parent.transactionMeta === null) {
2047
+ if (isRootStore(parent)) {
2048
+ parent.transactionMeta.epoch = child.transactionMeta.update.epoch;
1989
2049
  const myTransaction = withdraw(
1990
2050
  { key: child.transactionMeta.update.key, type: `transaction` },
1991
2051
  store
@@ -1997,21 +2057,20 @@ var applyTransaction = (output, store) => {
1997
2057
  child.transactionMeta.update.key,
1998
2058
  `Finished applying transaction.`
1999
2059
  );
2000
- } else {
2060
+ } else if (isChildStore(parent)) {
2001
2061
  parent.transactionMeta.update.updates.push(child.transactionMeta.update);
2002
2062
  }
2003
2063
  parent.on.transactionApplying.next(null);
2004
2064
  };
2005
2065
  var buildTransaction = (key, params, store, id) => {
2006
2066
  const parent = newest(store);
2007
- const child = {
2067
+ const childBase = {
2008
2068
  parent,
2009
2069
  child: null,
2010
2070
  on: parent.on,
2011
2071
  loggers: parent.loggers,
2012
2072
  logger: parent.logger,
2013
2073
  config: parent.config,
2014
- transactionMeta: null,
2015
2074
  atoms: new LazyMap(parent.atoms),
2016
2075
  atomsThatAreDefault: new Set(parent.atomsThatAreDefault),
2017
2076
  families: new LazyMap(parent.families),
@@ -2028,24 +2087,27 @@ var buildTransaction = (key, params, store, id) => {
2028
2087
  selectors: new LazyMap(parent.selectors),
2029
2088
  valueMap: new LazyMap(parent.valueMap)
2030
2089
  };
2031
- child.transactionMeta = {
2090
+ const transactionMeta = {
2032
2091
  phase: `building`,
2033
- time: Date.now(),
2034
2092
  update: {
2035
2093
  key,
2036
2094
  id: id != null ? id : Math.random().toString(36).slice(2),
2095
+ epoch: isRootStore(parent) ? parent.transactionMeta.epoch + 1 : NaN,
2037
2096
  updates: [],
2038
2097
  params,
2039
2098
  output: void 0
2040
2099
  },
2041
2100
  transactors: {
2042
- get: (token) => getState(token, child),
2043
- set: (token, value) => setState(token, value, child),
2101
+ get: (token) => getFromStore(token, child),
2102
+ set: (token, value) => setIntoStore(token, value, child),
2044
2103
  run: (token, id2) => runTransaction(token, id2, child),
2045
2104
  find: (token, key2) => findInStore(token, key2, child),
2046
2105
  env: () => getEnvironmentData(child)
2047
2106
  }
2048
2107
  };
2108
+ const child = Object.assign(childBase, {
2109
+ transactionMeta
2110
+ });
2049
2111
  parent.child = child;
2050
2112
  store.logger.info(
2051
2113
  `\u{1F6EB}`,
@@ -2054,6 +2116,7 @@ var buildTransaction = (key, params, store, id) => {
2054
2116
  `Building transaction with params:`,
2055
2117
  params
2056
2118
  );
2119
+ return child;
2057
2120
  };
2058
2121
 
2059
2122
  // internal/src/transaction/create-transaction.ts
@@ -2062,10 +2125,10 @@ function createTransaction(options, store) {
2062
2125
  key: options.key,
2063
2126
  type: `transaction`,
2064
2127
  run: (params, id) => {
2065
- buildTransaction(options.key, params, store, id);
2128
+ const childStore = buildTransaction(options.key, params, store, id);
2066
2129
  try {
2067
2130
  const target2 = newest(store);
2068
- const { transactors } = target2.transactionMeta;
2131
+ const { transactors } = childStore.transactionMeta;
2069
2132
  const output = options.do(transactors, ...params);
2070
2133
  applyTransaction(output, target2);
2071
2134
  return output;
@@ -2088,6 +2151,6 @@ function createTransaction(options, store) {
2088
2151
  // internal/src/transaction/index.ts
2089
2152
  var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
2090
2153
 
2091
- export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, StatefulSubject, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, addAtomToTimeline, applyTransaction, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, getEnvironmentData, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isDone, isMutable, isReadonlySelectorKey, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
2154
+ export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, StatefulSubject, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, addAtomToTimeline, applyTransaction, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, findInStore, getEnvironmentData, getFromStore, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isDone, isMutable, isReadonlySelectorKey, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, setIntoStore, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
2092
2155
  //# sourceMappingURL=out.js.map
2093
2156
  //# sourceMappingURL=index.js.map