atom.io 0.16.3 → 0.18.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 (162) hide show
  1. package/data/dist/index.cjs +62 -40
  2. package/data/dist/index.cjs.map +1 -1
  3. package/data/dist/index.d.ts +8 -2
  4. package/data/dist/index.js +64 -42
  5. package/data/dist/index.js.map +1 -1
  6. package/data/src/dict.ts +8 -4
  7. package/data/src/join.ts +74 -33
  8. package/data/src/struct-family.ts +18 -17
  9. package/dist/chunk-OEVFAUPE.js +289 -0
  10. package/dist/chunk-OEVFAUPE.js.map +1 -0
  11. package/dist/index.cjs +36 -57
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.ts +64 -53
  14. package/dist/index.js +15 -36
  15. package/dist/index.js.map +1 -1
  16. package/internal/dist/index.cjs +211 -81
  17. package/internal/dist/index.cjs.map +1 -1
  18. package/internal/dist/index.d.ts +100 -72
  19. package/internal/dist/index.js +200 -75
  20. package/internal/dist/index.js.map +1 -1
  21. package/internal/src/arbitrary.ts +3 -0
  22. package/internal/src/atom/create-regular-atom.ts +2 -3
  23. package/internal/src/caching.ts +8 -6
  24. package/internal/src/families/find-in-store.ts +16 -0
  25. package/internal/src/get-environment-data.ts +4 -7
  26. package/internal/src/get-state/get-from-store.ts +14 -0
  27. package/internal/src/get-state/index.ts +2 -0
  28. package/internal/src/{read-or-compute-value.ts → get-state/read-or-compute-value.ts} +3 -3
  29. package/internal/src/index.ts +7 -6
  30. package/internal/src/ingest-updates/ingest-atom-update.ts +2 -2
  31. package/internal/src/ingest-updates/ingest-transaction-update.ts +0 -1
  32. package/internal/src/mutable/create-mutable-atom.ts +3 -4
  33. package/internal/src/mutable/tracker.ts +18 -13
  34. package/internal/src/selector/create-standalone-selector.ts +0 -2
  35. package/internal/src/selector/register-selector.ts +1 -1
  36. package/internal/src/set-state/index.ts +1 -0
  37. package/internal/src/set-state/set-atom.ts +15 -19
  38. package/internal/src/set-state/set-into-store.ts +24 -0
  39. package/internal/src/store/store.ts +14 -2
  40. package/internal/src/store/withdraw.ts +72 -2
  41. package/internal/src/subscribe/subscribe-to-root-atoms.ts +1 -1
  42. package/internal/src/subscribe/subscribe-to-timeline.ts +2 -2
  43. package/internal/src/subscribe/subscribe-to-transaction.ts +2 -2
  44. package/internal/src/timeline/create-timeline.ts +12 -1
  45. package/internal/src/transaction/act-upon-store.ts +19 -0
  46. package/internal/src/transaction/apply-transaction.ts +7 -1
  47. package/internal/src/transaction/assign-transaction-to-continuity.ts +18 -0
  48. package/internal/src/transaction/build-transaction.ts +11 -8
  49. package/internal/src/transaction/create-transaction.ts +1 -1
  50. package/internal/src/transaction/get-epoch-number.ts +40 -0
  51. package/internal/src/transaction/index.ts +10 -1
  52. package/internal/src/transaction/set-epoch-number.ts +31 -0
  53. package/introspection/dist/index.cjs.map +1 -1
  54. package/introspection/dist/index.d.ts +3 -3
  55. package/introspection/dist/index.js.map +1 -1
  56. package/introspection/src/attach-introspection-states.ts +6 -2
  57. package/introspection/src/attach-timeline-family.ts +5 -2
  58. package/introspection/src/attach-transaction-logs.ts +2 -2
  59. package/json/dist/index.d.ts +3 -1
  60. package/json/src/index.ts +6 -2
  61. package/package.json +24 -13
  62. package/react/dist/index.cjs +3 -3
  63. package/react/dist/index.cjs.map +1 -1
  64. package/react/dist/index.d.ts +1 -1
  65. package/react/dist/index.js +5 -5
  66. package/react/dist/index.js.map +1 -1
  67. package/react/src/use-i.ts +2 -3
  68. package/react/src/use-json.ts +1 -1
  69. package/react/src/use-o.ts +3 -4
  70. package/react-devtools/dist/index.cjs +131 -134
  71. package/react-devtools/dist/index.cjs.map +1 -1
  72. package/react-devtools/dist/index.css +2 -2
  73. package/react-devtools/dist/index.css.map +1 -1
  74. package/react-devtools/dist/index.d.ts +3 -3
  75. package/react-devtools/dist/index.js +103 -106
  76. package/react-devtools/dist/index.js.map +1 -1
  77. package/react-devtools/src/StateEditor.tsx +6 -6
  78. package/react-devtools/src/StateIndex.tsx +2 -5
  79. package/react-devtools/src/TimelineIndex.tsx +3 -3
  80. package/react-devtools/src/TransactionIndex.tsx +9 -8
  81. package/react-devtools/src/Updates.tsx +1 -1
  82. package/react-devtools/src/index.ts +4 -4
  83. package/realtime/dist/index.cjs +72 -0
  84. package/realtime/dist/index.cjs.map +1 -0
  85. package/realtime/dist/index.d.ts +39 -0
  86. package/realtime/dist/index.js +68 -0
  87. package/realtime/dist/index.js.map +1 -0
  88. package/realtime/package.json +16 -0
  89. package/realtime/src/index.ts +1 -0
  90. package/realtime/src/realtime-continuity.ts +152 -0
  91. package/realtime-client/dist/index.cjs +403 -59
  92. package/realtime-client/dist/index.cjs.map +1 -1
  93. package/realtime-client/dist/index.d.ts +16 -9
  94. package/realtime-client/dist/index.js +114 -48
  95. package/realtime-client/dist/index.js.map +1 -1
  96. package/realtime-client/src/index.ts +8 -5
  97. package/realtime-client/src/{pull-family-member.ts → pull-atom-family-member.ts} +5 -5
  98. package/realtime-client/src/{pull-state.ts → pull-atom.ts} +5 -5
  99. package/realtime-client/src/{pull-mutable-family-member.ts → pull-mutable-atom-family-member.ts} +5 -5
  100. package/realtime-client/src/{pull-mutable.ts → pull-mutable-atom.ts} +5 -5
  101. package/realtime-client/src/pull-selector-family-member.ts +42 -0
  102. package/realtime-client/src/pull-selector.ts +38 -0
  103. package/realtime-client/src/realtime-client-stores/client-main-store.ts +2 -2
  104. package/realtime-client/src/realtime-client-stores/client-sync-store.ts +7 -7
  105. package/realtime-client/src/sync-continuity.ts +321 -0
  106. package/realtime-client/src/sync-server-action.ts +22 -21
  107. package/realtime-client/src/sync-state.ts +3 -3
  108. package/realtime-react/dist/index.cjs +330 -15
  109. package/realtime-react/dist/index.cjs.map +1 -1
  110. package/realtime-react/dist/index.d.ts +26 -6
  111. package/realtime-react/dist/index.js +43 -12
  112. package/realtime-react/dist/index.js.map +1 -1
  113. package/realtime-react/src/index.ts +6 -3
  114. package/realtime-react/src/use-pull-atom-family-member.ts +21 -0
  115. package/realtime-react/src/{use-pull.ts → use-pull-atom.ts} +6 -5
  116. package/realtime-react/src/{use-pull-mutable.ts → use-pull-mutable-atom.ts} +4 -3
  117. package/realtime-react/src/use-pull-mutable-family-member.ts +9 -4
  118. package/realtime-react/src/use-pull-selector-family-member.ts +21 -0
  119. package/realtime-react/src/{use-pull-family-member.ts → use-pull-selector.ts} +7 -5
  120. package/realtime-react/src/use-push.ts +3 -2
  121. package/realtime-react/src/use-server-action.ts +3 -2
  122. package/realtime-react/src/use-sync-continuity.ts +12 -0
  123. package/realtime-react/src/use-sync-server-action.ts +3 -2
  124. package/realtime-server/dist/index.cjs +582 -256
  125. package/realtime-server/dist/index.cjs.map +1 -1
  126. package/realtime-server/dist/index.d.ts +124 -49
  127. package/realtime-server/dist/index.js +566 -249
  128. package/realtime-server/dist/index.js.map +1 -1
  129. package/realtime-server/src/index.ts +18 -2
  130. package/realtime-server/src/ipc-socket.ts +230 -0
  131. package/realtime-server/src/realtime-action-receiver.ts +8 -5
  132. package/realtime-server/src/realtime-action-synchronizer.ts +53 -35
  133. package/realtime-server/src/realtime-continuity-synchronizer.ts +247 -0
  134. package/realtime-server/src/realtime-family-provider.ts +37 -73
  135. package/realtime-server/src/realtime-mutable-family-provider.ts +26 -87
  136. package/realtime-server/src/realtime-mutable-provider.ts +3 -2
  137. package/realtime-server/src/realtime-server-stores/index.ts +3 -1
  138. package/realtime-server/src/realtime-server-stores/realtime-continuity-store.ts +90 -0
  139. package/realtime-server/src/realtime-server-stores/server-room-store.ts +97 -0
  140. package/realtime-server/src/realtime-server-stores/server-sync-store.ts +2 -72
  141. package/realtime-server/src/realtime-server-stores/server-user-store.ts +14 -29
  142. package/realtime-server/src/realtime-state-provider.ts +3 -3
  143. package/realtime-server/src/realtime-state-receiver.ts +2 -3
  144. package/realtime-server/src/realtime-state-synchronizer.ts +3 -3
  145. package/realtime-testing/dist/index.cjs +28 -28
  146. package/realtime-testing/dist/index.cjs.map +1 -1
  147. package/realtime-testing/dist/index.js +28 -27
  148. package/realtime-testing/dist/index.js.map +1 -1
  149. package/realtime-testing/src/setup-realtime-test.tsx +38 -28
  150. package/src/atom.ts +49 -31
  151. package/src/get-state.ts +2 -11
  152. package/src/logger.ts +10 -5
  153. package/src/selector.ts +44 -25
  154. package/src/set-state.ts +1 -13
  155. package/src/silo.ts +7 -3
  156. package/src/subscribe.ts +2 -1
  157. package/src/timeline.ts +4 -4
  158. package/src/transaction.ts +13 -17
  159. package/src/validators.ts +15 -9
  160. package/dist/chunk-H4Q5FTPZ.js +0 -11
  161. package/dist/chunk-H4Q5FTPZ.js.map +0 -1
  162. package/internal/src/set-state/copy-mutable-in-transaction.ts +0 -19
@@ -1,9 +1,7 @@
1
1
  import { Junction } from '../../dist/chunk-NYTGCPHB.js';
2
- import { isChildStore, isRootStore } from '../../dist/chunk-H4Q5FTPZ.js';
3
2
  import { __spreadValues, __spreadProps } from '../../dist/chunk-PZLG2HP3.js';
4
- import { AtomIOLogger, getState, setState, runTransaction } from 'atom.io';
5
3
  import { stringifyJson, selectJson, parseJson, selectJsonFamily } from 'atom.io/json';
6
- import { withdraw as withdraw$1 } from 'atom.io/internal';
4
+ import { AtomIOLogger } from 'atom.io';
7
5
 
8
6
  // internal/src/lineage.ts
9
7
  function newest(scion) {
@@ -56,6 +54,14 @@ var StatefulSubject = class extends Subject {
56
54
  }
57
55
  };
58
56
 
57
+ // internal/src/transaction/is-root-store.ts
58
+ function isRootStore(store) {
59
+ return `epoch` in store.transactionMeta;
60
+ }
61
+ function isChildStore(store) {
62
+ return `phase` in store.transactionMeta;
63
+ }
64
+
59
65
  // internal/src/store/store.ts
60
66
  var Store = class {
61
67
  constructor(name, store = null) {
@@ -99,7 +105,11 @@ var Store = class {
99
105
  };
100
106
  this.operation = { open: false };
101
107
  this.transactionMeta = {
102
- epoch: -1
108
+ epoch: /* @__PURE__ */ new Map(),
109
+ actionContinuities: new Junction({
110
+ between: [`continuity`, `action`],
111
+ cardinality: `1:n`
112
+ })
103
113
  };
104
114
  this.config = {
105
115
  name: `IMPLICIT_STORE`
@@ -124,7 +134,14 @@ var Store = class {
124
134
  if (store !== null) {
125
135
  this.valueMap = new Map(store == null ? void 0 : store.valueMap);
126
136
  this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
127
- this.transactionMeta = __spreadValues({}, store == null ? void 0 : store.transactionMeta);
137
+ if (isRootStore(store)) {
138
+ this.transactionMeta = {
139
+ epoch: new Map(store == null ? void 0 : store.transactionMeta.epoch),
140
+ actionContinuities: new Junction(
141
+ store == null ? void 0 : store.transactionMeta.actionContinuities.toJSON()
142
+ )
143
+ };
144
+ }
128
145
  this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
129
146
  name
130
147
  });
@@ -191,6 +208,12 @@ function withdraw(token, store) {
191
208
  case `readonly_selector`:
192
209
  withdrawn = target.readonlySelectors.get(token.key);
193
210
  break;
211
+ case `atom_family`:
212
+ case `mutable_atom_family`:
213
+ case `selector_family`:
214
+ case `readonly_selector_family`:
215
+ withdrawn = target.families.get(token.key);
216
+ break;
194
217
  case `timeline`:
195
218
  withdrawn = target.timelines.get(token.key);
196
219
  break;
@@ -245,34 +268,6 @@ var Future = class extends Promise {
245
268
  }
246
269
  };
247
270
 
248
- // internal/src/set-state/copy-mutable-if-needed.ts
249
- function copyMutableIfNeeded(atom, origin, target) {
250
- const originValue = origin.valueMap.get(atom.key);
251
- const targetValue = target.valueMap.get(atom.key);
252
- if (originValue === targetValue) {
253
- origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
254
- const jsonValue = atom.toJson(originValue);
255
- const copiedValue = atom.fromJson(jsonValue);
256
- target.valueMap.set(atom.key, copiedValue);
257
- new Tracker(atom, origin);
258
- return copiedValue;
259
- }
260
- return targetValue;
261
- }
262
-
263
- // internal/src/set-state/copy-mutable-in-transaction.ts
264
- function copyMutableIfWithinTransaction(oldValue, atom, store) {
265
- const target = newest(store);
266
- const parent = target.parent;
267
- if (parent !== null) {
268
- if (atom.type === `mutable_atom`) {
269
- const copiedValue = copyMutableIfNeeded(atom, parent, target);
270
- return copiedValue;
271
- }
272
- }
273
- return oldValue;
274
- }
275
-
276
271
  // internal/src/caching.ts
277
272
  function cacheValue(key, value, subject, target) {
278
273
  const currentValue = target.valueMap.get(key);
@@ -299,10 +294,7 @@ function cacheValue(key, value, subject, target) {
299
294
  return value;
300
295
  }
301
296
  var readCachedValue = (token, target) => {
302
- let value = target.valueMap.get(token.key);
303
- if (token.type === `atom`) {
304
- value = copyMutableIfWithinTransaction(value, token, target);
305
- }
297
+ const value = target.valueMap.get(token.key);
306
298
  return value;
307
299
  };
308
300
  var evictCachedValue = (key, target) => {
@@ -317,7 +309,7 @@ var evictCachedValue = (key, target) => {
317
309
  target.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
318
310
  };
319
311
 
320
- // internal/src/read-or-compute-value.ts
312
+ // internal/src/get-state/read-or-compute-value.ts
321
313
  var readOrComputeValue = (state, target) => {
322
314
  if (target.valueMap.has(state.key)) {
323
315
  target.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
@@ -549,6 +541,21 @@ var markDone = (key, store) => {
549
541
  store.operation.done.add(key);
550
542
  };
551
543
 
544
+ // internal/src/set-state/copy-mutable-if-needed.ts
545
+ function copyMutableIfNeeded(atom, origin, target) {
546
+ const originValue = origin.valueMap.get(atom.key);
547
+ const targetValue = target.valueMap.get(atom.key);
548
+ if (originValue === targetValue) {
549
+ origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
550
+ const jsonValue = atom.toJson(originValue);
551
+ const copiedValue = atom.fromJson(jsonValue);
552
+ target.valueMap.set(atom.key, copiedValue);
553
+ new Tracker(atom, origin);
554
+ return copiedValue;
555
+ }
556
+ return targetValue;
557
+ }
558
+
552
559
  // internal/src/set-state/emit-update.ts
553
560
  var emitUpdate = (state, update, store) => {
554
561
  store.logger.info(
@@ -640,7 +647,12 @@ var stowUpdate = (state, update, store) => {
640
647
  // internal/src/set-state/set-atom.ts
641
648
  var setAtom = (atom, next, target) => {
642
649
  const oldValue = readOrComputeValue(atom, target);
643
- let newValue = copyMutableIfWithinTransaction(oldValue, atom, target);
650
+ let newValue = oldValue;
651
+ if (atom.type === `mutable_atom` && isChildStore(target)) {
652
+ const { parent } = target;
653
+ const copiedValue = copyMutableIfNeeded(atom, parent, target);
654
+ newValue = copiedValue;
655
+ }
644
656
  newValue = become(next)(newValue);
645
657
  target.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
646
658
  newValue = cacheValue(atom.key, newValue, atom.subject, target);
@@ -657,19 +669,14 @@ var setAtom = (atom, next, target) => {
657
669
  stowUpdate(atom, update, target);
658
670
  } else if (atom.key.startsWith(`*`)) {
659
671
  const mutableKey = atom.key.slice(1);
660
- const mutableAtom = target.atoms.get(mutableKey);
661
- let mutable = target.valueMap.get(mutableKey);
662
- mutable = copyMutableIfWithinTransaction(mutable, mutableAtom, target);
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
+ target.atoms.get(mutableKey);
673
+ let transceiver = target.valueMap.get(mutableKey);
674
+ if (atom.type === `mutable_atom` && isChildStore(target)) {
675
+ const { parent } = target;
676
+ const copiedValue = copyMutableIfNeeded(atom, parent, target);
677
+ transceiver = copiedValue;
672
678
  }
679
+ transceiver.do(update.newValue);
673
680
  }
674
681
  }
675
682
  };
@@ -687,6 +694,21 @@ var setAtomOrSelector = (state, value, store) => {
687
694
  }
688
695
  };
689
696
 
697
+ // internal/src/set-state/set-into-store.ts
698
+ function setIntoStore(token, value, store) {
699
+ var _a;
700
+ const rejection = openOperation(token, store);
701
+ if (rejection) {
702
+ return;
703
+ }
704
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
705
+ if (state === void 0) {
706
+ throw new NotFoundError(token, store);
707
+ }
708
+ setAtomOrSelector(state, value, store);
709
+ closeOperation(store);
710
+ }
711
+
690
712
  // internal/src/keys.ts
691
713
  var isAtomKey = (key, store) => newest(store).atoms.has(key);
692
714
  var isSelectorKey = (key, store) => newest(store).selectors.has(key);
@@ -1022,8 +1044,10 @@ function subscribeToState(token, handleUpdate, key, store) {
1022
1044
  };
1023
1045
  return unsubscribe;
1024
1046
  }
1047
+
1048
+ // internal/src/subscribe/subscribe-to-timeline.ts
1025
1049
  var subscribeToTimeline = (token, handleUpdate, key, store) => {
1026
- const tl = withdraw$1(token, store);
1050
+ const tl = withdraw(token, store);
1027
1051
  if (tl === void 0) {
1028
1052
  throw new Error(
1029
1053
  `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
@@ -1041,8 +1065,10 @@ var subscribeToTimeline = (token, handleUpdate, key, store) => {
1041
1065
  unsubscribe();
1042
1066
  };
1043
1067
  };
1068
+
1069
+ // internal/src/subscribe/subscribe-to-transaction.ts
1044
1070
  var subscribeToTransaction = (token, handleUpdate, key, store) => {
1045
- const tx = withdraw$1(token, store);
1071
+ const tx = withdraw(token, store);
1046
1072
  if (tx === void 0) {
1047
1073
  throw new Error(
1048
1074
  `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
@@ -1065,6 +1091,8 @@ var subscribeToTransaction = (token, handleUpdate, key, store) => {
1065
1091
  unsubscribe();
1066
1092
  };
1067
1093
  };
1094
+
1095
+ // internal/src/mutable/tracker.ts
1068
1096
  var Tracker = class {
1069
1097
  initializeState(mutableState, store) {
1070
1098
  var _a;
@@ -1091,7 +1119,7 @@ var Tracker = class {
1091
1119
  }
1092
1120
  observeCore(mutableState, latestUpdateState, target) {
1093
1121
  const subscriptionKey = `tracker:${target.config.name}:${isChildStore(target) ? target.transactionMeta.update.key : `main`}:${mutableState.key}`;
1094
- const originalInnerValue = getState(mutableState, target);
1122
+ const originalInnerValue = getFromStore(mutableState, target);
1095
1123
  this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
1096
1124
  subscriptionKey,
1097
1125
  (update) => {
@@ -1100,12 +1128,12 @@ var Tracker = class {
1100
1128
  subscriptionKey,
1101
1129
  () => {
1102
1130
  unsubscribe();
1103
- setState(latestUpdateState, update, target);
1131
+ setIntoStore(latestUpdateState, update, target);
1104
1132
  }
1105
1133
  );
1106
1134
  } else {
1107
- setState(mutableState, (current) => current, target);
1108
- setState(latestUpdateState, update, target);
1135
+ setIntoStore(mutableState, (current) => current, target);
1136
+ setIntoStore(latestUpdateState, update, target);
1109
1137
  }
1110
1138
  }
1111
1139
  );
@@ -1122,12 +1150,12 @@ var Tracker = class {
1122
1150
  subscriptionKey,
1123
1151
  () => {
1124
1152
  unsubscribe();
1125
- setState(latestUpdateState, update2, target);
1153
+ setIntoStore(latestUpdateState, update2, target);
1126
1154
  }
1127
1155
  );
1128
1156
  } else {
1129
- setState(mutableState, (current) => current, target);
1130
- setState(latestUpdateState, update2, target);
1157
+ setIntoStore(mutableState, (current) => current, target);
1158
+ setIntoStore(latestUpdateState, update2, target);
1131
1159
  }
1132
1160
  }
1133
1161
  );
@@ -1152,7 +1180,7 @@ var Tracker = class {
1152
1180
  { key: timelineId, type: `timeline` },
1153
1181
  (update) => {
1154
1182
  unsubscribe2();
1155
- setState(
1183
+ setIntoStore(
1156
1184
  mutableState,
1157
1185
  (transceiver) => {
1158
1186
  if (update === `redo` && newValue) {
@@ -1175,11 +1203,11 @@ var Tracker = class {
1175
1203
  subscriptionKey,
1176
1204
  () => {
1177
1205
  unsubscribe();
1178
- const mutable = getState(mutableState, target);
1206
+ const mutable = getFromStore(mutableState, target);
1179
1207
  const updateNumber = newValue === null ? -1 : mutable.getUpdateNumber(newValue);
1180
1208
  const eventOffset = updateNumber - mutable.cacheUpdateNumber;
1181
1209
  if (newValue && eventOffset === 1) {
1182
- setState(
1210
+ setIntoStore(
1183
1211
  mutableState,
1184
1212
  (transceiver) => (transceiver.do(newValue), transceiver),
1185
1213
  target
@@ -1260,7 +1288,7 @@ function createMutableAtom(options, family, store) {
1260
1288
  const cleanupFunctions = [];
1261
1289
  for (const effect of options.effects) {
1262
1290
  const cleanup = effect({
1263
- setSelf: (next) => setState(token, next, store),
1291
+ setSelf: (next) => setIntoStore(token, next, store),
1264
1292
  onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1265
1293
  });
1266
1294
  if (cleanup) {
@@ -1467,7 +1495,7 @@ function createRegularAtom(options, family, store) {
1467
1495
  const cleanupFunctions = [];
1468
1496
  for (const effect of options.effects) {
1469
1497
  const cleanup = effect({
1470
- setSelf: (next) => setState(token, next, store),
1498
+ setSelf: (next) => setIntoStore(token, next, store),
1471
1499
  onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1472
1500
  });
1473
1501
  if (cleanup) {
@@ -1526,17 +1554,35 @@ function deleteAtom(atomToken, store) {
1526
1554
  store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
1527
1555
  }
1528
1556
 
1557
+ // internal/src/arbitrary.ts
1558
+ function arbitrary(random = Math.random) {
1559
+ return random().toString(36).slice(2);
1560
+ }
1561
+
1529
1562
  // internal/src/get-environment-data.ts
1530
1563
  function getEnvironmentData(store) {
1531
1564
  return {
1532
- runtime: typeof window === `undefined` ? typeof global === `object` ? `node` : `unknown` : `browser`,
1565
+ window: typeof window === `undefined` ? void 0 : window,
1566
+ global: typeof global === `undefined` ? void 0 : global,
1533
1567
  store
1534
1568
  };
1535
1569
  }
1570
+
1571
+ // internal/src/get-state/get-from-store.ts
1572
+ function getFromStore(token, store) {
1573
+ var _a;
1574
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1575
+ if (state === void 0) {
1576
+ throw new NotFoundError(token, store);
1577
+ }
1578
+ return readOrComputeValue(state, store);
1579
+ }
1580
+
1581
+ // internal/src/ingest-updates/ingest-atom-update.ts
1536
1582
  function ingestAtomUpdate(applying, atomUpdate, store) {
1537
1583
  const { key, newValue, oldValue } = atomUpdate;
1538
1584
  const value = applying === `newValue` ? newValue : oldValue;
1539
- setState({ key, type: `atom` }, value, store);
1585
+ setIntoStore({ key, type: `atom` }, value, store);
1540
1586
  }
1541
1587
 
1542
1588
  // internal/src/ingest-updates/ingest-selector-update.ts
@@ -1815,7 +1861,17 @@ function createTimeline(options, store, data) {
1815
1861
  for (const tokenOrFamily of options.atoms) {
1816
1862
  let atomKey = tokenOrFamily.key;
1817
1863
  if (tokenOrFamily.type === `atom_family` || tokenOrFamily.type === `mutable_atom_family`) {
1818
- const family = tokenOrFamily;
1864
+ const familyToken = tokenOrFamily;
1865
+ const family = withdraw(familyToken, store);
1866
+ if (family === void 0) {
1867
+ store.logger.error(
1868
+ `\u274C`,
1869
+ `timeline`,
1870
+ options.key,
1871
+ `Failed to add family "${familyToken.key}" because it does not exist in the store`
1872
+ );
1873
+ continue;
1874
+ }
1819
1875
  const familyKey = family.key;
1820
1876
  target.timelineAtoms.set({ atomKey: familyKey, timelineKey });
1821
1877
  family.subject.subscribe(`timeline:${options.key}`, (token2) => {
@@ -1968,6 +2024,36 @@ var abortTransaction = (store) => {
1968
2024
  target.parent.child = null;
1969
2025
  };
1970
2026
 
2027
+ // internal/src/transaction/act-upon-store.ts
2028
+ function actUponStore(token, id, store) {
2029
+ return (...parameters) => {
2030
+ const tx = withdraw(token, store);
2031
+ if (tx) {
2032
+ return tx.run(parameters, id);
2033
+ }
2034
+ throw new NotFoundError(token, store);
2035
+ };
2036
+ }
2037
+
2038
+ // internal/src/transaction/set-epoch-number.ts
2039
+ function setEpochNumberOfContinuity(continuityKey, newEpoch, store) {
2040
+ const isRoot = isRootStore(store);
2041
+ if (isRoot && continuityKey) {
2042
+ store.transactionMeta.epoch.set(continuityKey, newEpoch);
2043
+ }
2044
+ }
2045
+ function setEpochNumberOfAction(transactionKey, newEpoch, store) {
2046
+ const isRoot = isRootStore(store);
2047
+ if (!isRoot) {
2048
+ return;
2049
+ }
2050
+ const continuityKey = store.transactionMeta.actionContinuities.getRelatedKey(transactionKey);
2051
+ if (continuityKey !== void 0) {
2052
+ store.transactionMeta.epoch.set(continuityKey, newEpoch);
2053
+ console.log(`epoch is now`, newEpoch);
2054
+ }
2055
+ }
2056
+
1971
2057
  // internal/src/transaction/apply-transaction.ts
1972
2058
  var applyTransaction = (output, store) => {
1973
2059
  var _a;
@@ -2016,7 +2102,12 @@ var applyTransaction = (output, store) => {
2016
2102
  }
2017
2103
  ingestTransactionUpdate(`newValue`, child.transactionMeta.update, parent);
2018
2104
  if (isRootStore(parent)) {
2019
- parent.transactionMeta.epoch = child.transactionMeta.update.epoch;
2105
+ console.log(child.transactionMeta.update);
2106
+ setEpochNumberOfAction(
2107
+ child.transactionMeta.update.key,
2108
+ child.transactionMeta.update.epoch,
2109
+ parent
2110
+ );
2020
2111
  const myTransaction = withdraw(
2021
2112
  { key: child.transactionMeta.update.key, type: `transaction` },
2022
2113
  store
@@ -2033,6 +2124,21 @@ var applyTransaction = (output, store) => {
2033
2124
  }
2034
2125
  parent.on.transactionApplying.next(null);
2035
2126
  };
2127
+
2128
+ // internal/src/transaction/assign-transaction-to-continuity.ts
2129
+ function assignTransactionToContinuity(continuityKey, transactionKey, store) {
2130
+ const isRoot = isRootStore(store);
2131
+ if (!isRoot) {
2132
+ return;
2133
+ }
2134
+ const { epoch, actionContinuities } = store.transactionMeta;
2135
+ actionContinuities.set(continuityKey, transactionKey);
2136
+ if (!epoch.has(continuityKey)) {
2137
+ epoch.set(continuityKey, -1);
2138
+ }
2139
+ }
2140
+
2141
+ // internal/src/transaction/build-transaction.ts
2036
2142
  var buildTransaction = (key, params, store, id) => {
2037
2143
  const parent = newest(store);
2038
2144
  const childBase = {
@@ -2058,20 +2164,21 @@ var buildTransaction = (key, params, store, id) => {
2058
2164
  selectors: new LazyMap(parent.selectors),
2059
2165
  valueMap: new LazyMap(parent.valueMap)
2060
2166
  };
2167
+ const epoch = getEpochNumberOfAction(key, store);
2061
2168
  const transactionMeta = {
2062
2169
  phase: `building`,
2063
2170
  update: {
2064
2171
  key,
2065
- id: id != null ? id : Math.random().toString(36).slice(2),
2066
- epoch: isRootStore(parent) ? parent.transactionMeta.epoch + 1 : NaN,
2172
+ id,
2173
+ epoch: epoch === void 0 ? NaN : epoch + 1,
2067
2174
  updates: [],
2068
2175
  params,
2069
2176
  output: void 0
2070
2177
  },
2071
2178
  transactors: {
2072
- get: (token) => getState(token, child),
2073
- set: (token, value) => setState(token, value, child),
2074
- run: (token, id2) => runTransaction(token, id2, child),
2179
+ get: (token) => getFromStore(token, child),
2180
+ set: (token, value) => setIntoStore(token, value, child),
2181
+ run: (token, id2 = arbitrary()) => actUponStore(token, id2, child),
2075
2182
  find: (token, key2) => findInStore(token, key2, child),
2076
2183
  env: () => getEnvironmentData(child)
2077
2184
  }
@@ -2119,9 +2226,27 @@ function createTransaction(options, store) {
2119
2226
  return token;
2120
2227
  }
2121
2228
 
2229
+ // internal/src/transaction/get-epoch-number.ts
2230
+ function getContinuityKey(transactionKey, store) {
2231
+ const isRoot = isRootStore(store);
2232
+ const continuity = isRoot ? store.transactionMeta.actionContinuities.getRelatedKey(transactionKey) : void 0;
2233
+ return continuity;
2234
+ }
2235
+ function getEpochNumberOfContinuity(continuityKey, store) {
2236
+ const isRoot = isRootStore(store);
2237
+ const epoch = isRoot && continuityKey ? store.transactionMeta.epoch.get(continuityKey) : void 0;
2238
+ return epoch;
2239
+ }
2240
+ function getEpochNumberOfAction(transactionKey, store) {
2241
+ const isRoot = isRootStore(store);
2242
+ const continuity = isRoot ? store.transactionMeta.actionContinuities.getRelatedKey(transactionKey) : void 0;
2243
+ const epoch = isRoot && continuity !== void 0 ? store.transactionMeta.epoch.get(continuity) : void 0;
2244
+ return epoch;
2245
+ }
2246
+
2122
2247
  // internal/src/transaction/index.ts
2123
2248
  var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
2124
2249
 
2125
- 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, 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 };
2250
+ export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, StatefulSubject, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, actUponStore, addAtomToTimeline, applyTransaction, arbitrary, assignTransactionToContinuity, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, findInStore, getContinuityKey, getEnvironmentData, getEpochNumberOfAction, getEpochNumberOfContinuity, getFromStore, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isChildStore, isDone, isMutable, isReadonlySelectorKey, isRootStore, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, setEpochNumberOfAction, setEpochNumberOfContinuity, setIntoStore, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
2126
2251
  //# sourceMappingURL=out.js.map
2127
2252
  //# sourceMappingURL=index.js.map