atom.io 0.14.8 → 0.15.1

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 (100) hide show
  1. package/README.md +5 -0
  2. package/data/dist/index.cjs +216 -218
  3. package/data/dist/index.cjs.map +1 -1
  4. package/data/dist/index.d.ts +30 -11
  5. package/data/dist/index.js +218 -221
  6. package/data/dist/index.js.map +1 -1
  7. package/data/src/join.ts +337 -296
  8. package/data/src/struct-family.ts +2 -2
  9. package/data/src/struct.ts +2 -2
  10. package/dist/chunk-S7R5MU6A.js +137 -0
  11. package/dist/chunk-S7R5MU6A.js.map +1 -0
  12. package/dist/index.cjs +3 -20
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.ts +15 -8
  15. package/dist/index.js +1 -151
  16. package/dist/index.js.map +1 -1
  17. package/internal/dist/index.cjs +278 -201
  18. package/internal/dist/index.cjs.map +1 -1
  19. package/internal/dist/index.d.ts +43 -36
  20. package/internal/dist/index.js +224 -194
  21. package/internal/dist/index.js.map +1 -1
  22. package/internal/src/atom/create-atom.ts +5 -86
  23. package/internal/src/atom/create-regular-atom.ts +92 -0
  24. package/internal/src/atom/index.ts +16 -0
  25. package/internal/src/atom/is-default.ts +0 -5
  26. package/internal/src/caching.ts +14 -16
  27. package/internal/src/families/create-atom-family.ts +20 -46
  28. package/internal/src/families/create-readonly-selector-family.ts +4 -1
  29. package/internal/src/families/create-regular-atom-family.ts +72 -0
  30. package/internal/src/families/create-selector-family.ts +2 -0
  31. package/internal/src/families/index.ts +1 -0
  32. package/internal/src/mutable/create-mutable-atom-family.ts +2 -2
  33. package/internal/src/mutable/create-mutable-atom.ts +8 -3
  34. package/internal/src/mutable/get-update-family.ts +1 -1
  35. package/internal/src/mutable/is-mutable.ts +3 -30
  36. package/internal/src/mutable/tracker-family.ts +2 -2
  37. package/internal/src/mutable/tracker.ts +5 -5
  38. package/internal/src/operation.ts +14 -18
  39. package/internal/src/selector/create-read-write-selector.ts +2 -3
  40. package/internal/src/selector/create-selector.ts +1 -1
  41. package/internal/src/selector/register-selector.ts +9 -14
  42. package/internal/src/set-state/evict-downstream.ts +3 -5
  43. package/internal/src/set-state/set-atom.ts +14 -17
  44. package/internal/src/store/store.ts +23 -19
  45. package/internal/src/store/withdraw.ts +32 -70
  46. package/internal/src/subscribe/subscribe-to-root-atoms.ts +5 -3
  47. package/internal/src/subscribe/subscribe-to-state.ts +5 -3
  48. package/internal/src/transaction/apply-transaction.ts +20 -2
  49. package/internal/src/transaction/build-transaction.ts +19 -11
  50. package/internal/src/transaction/create-transaction.ts +6 -11
  51. package/internal/src/transaction/index.ts +2 -3
  52. package/introspection/dist/index.cjs +6 -6
  53. package/introspection/dist/index.cjs.map +1 -1
  54. package/introspection/dist/index.d.ts +3 -3
  55. package/introspection/dist/index.js +7 -7
  56. package/introspection/dist/index.js.map +1 -1
  57. package/introspection/src/attach-atom-index.ts +7 -2
  58. package/introspection/src/attach-selector-index.ts +7 -2
  59. package/introspection/src/attach-timeline-family.ts +5 -2
  60. package/introspection/src/attach-timeline-index.ts +2 -2
  61. package/introspection/src/attach-transaction-index.ts +2 -2
  62. package/introspection/src/attach-transaction-logs.ts +2 -2
  63. package/package.json +10 -8
  64. package/react-devtools/dist/index.d.ts +17 -11
  65. package/realtime-client/dist/index.cjs +3 -0
  66. package/realtime-client/dist/index.cjs.map +1 -1
  67. package/realtime-client/dist/index.d.ts +3 -3
  68. package/realtime-client/dist/index.js +3 -0
  69. package/realtime-client/dist/index.js.map +1 -1
  70. package/realtime-client/src/index.ts +6 -6
  71. package/realtime-client/src/{use-pull-family-member.ts → pull-family-member.ts} +1 -1
  72. package/realtime-client/src/{use-pull-mutable-family-member.ts → pull-mutable-family-member.ts} +1 -1
  73. package/realtime-client/src/{use-pull-mutable.ts → pull-mutable.ts} +1 -1
  74. package/realtime-client/src/{use-server-action.ts → server-action.ts} +3 -0
  75. package/realtime-react/dist/index.cjs +31 -17
  76. package/realtime-react/dist/index.cjs.map +1 -1
  77. package/realtime-react/dist/index.d.ts +3 -3
  78. package/realtime-react/dist/index.js +31 -17
  79. package/realtime-react/dist/index.js.map +1 -1
  80. package/realtime-react/src/realtime-context.tsx +2 -3
  81. package/realtime-react/src/use-pull-family-member.ts +6 -2
  82. package/realtime-react/src/use-pull-mutable-family-member.ts +5 -4
  83. package/realtime-react/src/use-pull-mutable.ts +6 -2
  84. package/realtime-react/src/use-pull.ts +5 -1
  85. package/realtime-react/src/use-push.ts +5 -4
  86. package/realtime-react/src/use-server-action.ts +5 -4
  87. package/src/atom.ts +8 -17
  88. package/src/selector.ts +3 -1
  89. package/src/set-state.ts +1 -3
  90. package/src/silo.ts +2 -14
  91. package/src/transaction.ts +17 -6
  92. package/transceivers/set-rtx/dist/index.cjs +2 -1
  93. package/transceivers/set-rtx/dist/index.cjs.map +1 -1
  94. package/transceivers/set-rtx/dist/index.js +2 -1
  95. package/transceivers/set-rtx/dist/index.js.map +1 -1
  96. package/transceivers/set-rtx/src/set-rtx.ts +2 -1
  97. package/internal/src/transaction/redo-transaction.ts +0 -27
  98. package/internal/src/transaction/undo-transaction.ts +0 -27
  99. /package/realtime-client/src/{use-pull.ts → pull.ts} +0 -0
  100. /package/realtime-client/src/{use-push.ts → push.ts} +0 -0
@@ -1,8 +1,28 @@
1
1
  'use strict';
2
2
 
3
- var atom_io = require('atom.io');
4
3
  var json = require('atom.io/json');
5
- var internal = require('atom.io/internal');
4
+ var atom_io = require('atom.io');
5
+ var Internal2 = require('atom.io/internal');
6
+
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var Internal2__namespace = /*#__PURE__*/_interopNamespace(Internal2);
6
26
 
7
27
  var __defProp = Object.defineProperty;
8
28
  var __defProps = Object.defineProperties;
@@ -24,6 +44,20 @@ var __spreadValues = (a, b) => {
24
44
  };
25
45
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
26
46
 
47
+ // internal/src/lineage.ts
48
+ function newest(scion) {
49
+ while (scion.child !== null) {
50
+ scion = scion.child;
51
+ }
52
+ return scion;
53
+ }
54
+ function eldest(scion) {
55
+ while (scion.parent !== null) {
56
+ scion = scion.parent;
57
+ }
58
+ return scion;
59
+ }
60
+
27
61
  // internal/src/future.ts
28
62
  var Future = class extends Promise {
29
63
  constructor(executor) {
@@ -43,39 +77,24 @@ var Future = class extends Promise {
43
77
  }
44
78
  };
45
79
 
46
- // internal/src/lineage.ts
47
- function newest(scion) {
48
- while (scion.child !== null) {
49
- scion = scion.child;
50
- }
51
- return scion;
52
- }
53
- function eldest(scion) {
54
- while (scion.parent !== null) {
55
- scion = scion.parent;
56
- }
57
- return scion;
58
- }
59
-
60
80
  // internal/src/caching.ts
61
- function cacheValue(key, value, subject, store) {
62
- const target = newest(store);
81
+ function cacheValue(key, value, subject, target) {
63
82
  const currentValue = target.valueMap.get(key);
64
83
  if (currentValue instanceof Future) {
65
84
  currentValue.cancel();
66
85
  }
67
86
  if (value instanceof Promise) {
68
87
  const future = new Future(value);
69
- newest(store).valueMap.set(key, future);
88
+ target.valueMap.set(key, future);
70
89
  future.then((resolved) => {
71
90
  if (future.isCanceled) {
72
91
  return;
73
92
  }
74
- cacheValue(key, resolved, subject, store);
93
+ cacheValue(key, resolved, subject, target);
75
94
  subject.next({ newValue: resolved, oldValue: future });
76
95
  }).catch((thrown) => {
77
96
  if (thrown !== `canceled`) {
78
- store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
97
+ target.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
79
98
  }
80
99
  });
81
100
  return future;
@@ -83,23 +102,22 @@ function cacheValue(key, value, subject, store) {
83
102
  target.valueMap.set(key, value);
84
103
  return value;
85
104
  }
86
- var readCachedValue = (key, store) => {
87
- return newest(store).valueMap.get(key);
105
+ var readCachedValue = (key, target) => {
106
+ return target.valueMap.get(key);
88
107
  };
89
- var isValueCached = (key, store) => {
90
- return newest(store).valueMap.has(key);
108
+ var isValueCached = (key, target) => {
109
+ return target.valueMap.has(key);
91
110
  };
92
- var evictCachedValue = (key, store) => {
93
- const core = newest(store);
94
- const currentValue = core.valueMap.get(key);
111
+ var evictCachedValue = (key, target) => {
112
+ const currentValue = target.valueMap.get(key);
95
113
  if (currentValue instanceof Future) {
96
114
  currentValue.cancel();
97
115
  }
98
- if (core.operation.open) {
99
- core.operation.prev.set(key, currentValue);
116
+ if (target.operation.open) {
117
+ target.operation.prev.set(key, currentValue);
100
118
  }
101
- core.valueMap.delete(key);
102
- store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
119
+ target.valueMap.delete(key);
120
+ target.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
103
121
  };
104
122
 
105
123
  // internal/src/read-or-compute-value.ts
@@ -125,17 +143,16 @@ var readOrComputeValue = (state, store) => {
125
143
 
126
144
  // internal/src/operation.ts
127
145
  var openOperation = (token, store) => {
128
- const target = newest(store);
129
- if (target.operation.open) {
146
+ if (store.operation.open) {
130
147
  store.logger.error(
131
148
  `\u274C`,
132
149
  token.type,
133
150
  token.key,
134
- `failed to setState during a setState for "${target.operation.token.key}"`
151
+ `failed to setState during a setState for "${store.operation.token.key}"`
135
152
  );
136
153
  return `rejection`;
137
154
  }
138
- target.operation = {
155
+ store.operation = {
139
156
  open: true,
140
157
  done: /* @__PURE__ */ new Set(),
141
158
  prev: /* @__PURE__ */ new Map(),
@@ -146,25 +163,23 @@ var openOperation = (token, store) => {
146
163
  `\u2B55`,
147
164
  token.type,
148
165
  token.key,
149
- `operation start in store "${store.config.name}"${target.transactionMeta === null ? `` : ` ${target.transactionMeta.phase} "${target.transactionMeta.update.key}"`}`
166
+ `operation start in store "${store.config.name}"${store.transactionMeta === null ? `` : ` ${store.transactionMeta.phase} "${store.transactionMeta.update.key}"`}`
150
167
  );
151
168
  };
152
169
  var closeOperation = (store) => {
153
- const target = newest(store);
154
- if (target.operation.open) {
170
+ if (store.operation.open) {
155
171
  store.logger.info(
156
172
  `\u{1F534}`,
157
- target.operation.token.type,
158
- target.operation.token.key,
173
+ store.operation.token.type,
174
+ store.operation.token.key,
159
175
  `operation done in store "${store.config.name}"`
160
176
  );
161
177
  }
162
- target.operation = { open: false };
163
- store.subject.operationStatus.next(target.operation);
178
+ store.operation = { open: false };
179
+ store.subject.operationStatus.next(store.operation);
164
180
  };
165
181
  var isDone = (key, store) => {
166
- const target = newest(store);
167
- if (!target.operation.open) {
182
+ if (!store.operation.open) {
168
183
  store.logger.warn(
169
184
  `\u{1F41E}`,
170
185
  `unknown`,
@@ -173,11 +188,10 @@ var isDone = (key, store) => {
173
188
  );
174
189
  return true;
175
190
  }
176
- return target.operation.done.has(key);
191
+ return store.operation.done.has(key);
177
192
  };
178
193
  var markDone = (key, store) => {
179
- const target = newest(store);
180
- if (!target.operation.open) {
194
+ if (!store.operation.open) {
181
195
  store.logger.warn(
182
196
  `\u{1F41E}`,
183
197
  `unknown`,
@@ -186,7 +200,7 @@ var markDone = (key, store) => {
186
200
  );
187
201
  return;
188
202
  }
189
- target.operation.done.add(key);
203
+ store.operation.done.add(key);
190
204
  };
191
205
 
192
206
  // internal/src/set-state/become.ts
@@ -292,8 +306,7 @@ var emitUpdate = (state, update, store) => {
292
306
 
293
307
  // internal/src/set-state/evict-downstream.ts
294
308
  var evictDownStream = (atom, store) => {
295
- const target = newest(store);
296
- const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom.key);
309
+ const downstreamKeys = store.selectorAtoms.getRelatedKeys(atom.key);
297
310
  store.logger.info(
298
311
  `\u{1F9F9}`,
299
312
  atom.type,
@@ -302,12 +315,12 @@ var evictDownStream = (atom, store) => {
302
315
  downstreamKeys != null ? downstreamKeys : `to evict`
303
316
  );
304
317
  if (downstreamKeys) {
305
- if (target.operation.open) {
318
+ if (store.operation.open) {
306
319
  store.logger.info(
307
320
  `\u{1F9F9}`,
308
321
  atom.type,
309
322
  atom.key,
310
- `[ ${[...target.operation.done].join(`, `)} ] already done`
323
+ `[ ${[...store.operation.done].join(`, `)} ] already done`
311
324
  );
312
325
  }
313
326
  for (const key of downstreamKeys) {
@@ -364,23 +377,24 @@ var stowUpdate = (state, update, store) => {
364
377
  };
365
378
 
366
379
  // internal/src/set-state/set-atom.ts
367
- var setAtom = (atom, next, store) => {
368
- const target = newest(store);
369
- const oldValue = readOrComputeValue(atom, store);
370
- let newValue = copyMutableIfWithinTransaction(oldValue, atom, store);
380
+ var setAtom = (atom, next, target) => {
381
+ const oldValue = readOrComputeValue(atom, target);
382
+ let newValue = copyMutableIfWithinTransaction(oldValue, atom, target);
371
383
  newValue = become(next)(newValue);
372
- store.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
373
- newValue = cacheValue(atom.key, newValue, atom.subject, store);
374
- if (isAtomDefault(atom.key, store)) {
375
- markAtomAsNotDefault(atom.key, store);
384
+ target.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
385
+ newValue = cacheValue(atom.key, newValue, atom.subject, target);
386
+ if (isAtomDefault(atom.key, target)) {
387
+ markAtomAsNotDefault(atom.key, target);
376
388
  }
377
- markDone(atom.key, store);
378
- evictDownStream(atom, store);
389
+ markDone(atom.key, target);
390
+ evictDownStream(atom, target);
379
391
  const update = { oldValue, newValue };
380
- if (target.transactionMeta === null || target.transactionMeta.phase === `applying`) {
381
- emitUpdate(atom, update, store);
392
+ if (target.transactionMeta === null) {
393
+ emitUpdate(atom, update, target);
394
+ } else if (target.transactionMeta.phase === `applying` && target.parent) {
395
+ emitUpdate(atom, update, target.parent);
382
396
  } else {
383
- stowUpdate(atom, update, store);
397
+ stowUpdate(atom, update, target);
384
398
  }
385
399
  };
386
400
 
@@ -731,13 +745,29 @@ var Store = class {
731
745
  this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
732
746
  name
733
747
  });
748
+ for (const [, family] of store.families) {
749
+ family.install(this);
750
+ }
751
+ const mutableHelpers = /* @__PURE__ */ new Set();
734
752
  for (const [, atom] of store.atoms) {
753
+ if (mutableHelpers.has(atom.key)) {
754
+ continue;
755
+ }
735
756
  atom.install(this);
757
+ if (`mutable` in atom) {
758
+ const originalJsonToken = getJsonToken(atom);
759
+ const originalUpdateToken = getUpdateToken(atom);
760
+ mutableHelpers.add(originalJsonToken.key);
761
+ mutableHelpers.add(originalUpdateToken.key);
762
+ }
736
763
  }
737
764
  for (const [, selector] of store.readonlySelectors) {
738
765
  selector.install(this);
739
766
  }
740
767
  for (const [, selector] of store.selectors) {
768
+ if (mutableHelpers.has(selector.key)) {
769
+ continue;
770
+ }
741
771
  selector.install(this);
742
772
  }
743
773
  for (const [, tx] of store.transactions) {
@@ -764,13 +794,31 @@ var clearStore = (store) => {
764
794
 
765
795
  // internal/src/store/withdraw.ts
766
796
  function withdraw(token, store) {
767
- var _a, _b, _c, _d;
768
- const target = newest(store);
769
- const state = (_d = (_c = (_b = (_a = target.atoms.get(token.key)) != null ? _a : target.selectors.get(token.key)) != null ? _b : target.readonlySelectors.get(token.key)) != null ? _c : target.transactions.get(token.key)) != null ? _d : target.timelines.get(token.key);
770
- if (state) {
771
- return state;
797
+ let withdrawn;
798
+ let target = store;
799
+ while (target !== null) {
800
+ switch (token.type) {
801
+ case `atom`:
802
+ withdrawn = target.atoms.get(token.key);
803
+ break;
804
+ case `selector`:
805
+ withdrawn = target.selectors.get(token.key);
806
+ break;
807
+ case `readonly_selector`:
808
+ withdrawn = target.readonlySelectors.get(token.key);
809
+ break;
810
+ case `timeline`:
811
+ withdrawn = target.timelines.get(token.key);
812
+ break;
813
+ case `transaction`:
814
+ withdrawn = target.transactions.get(token.key);
815
+ break;
816
+ }
817
+ if (withdrawn) {
818
+ return withdrawn;
819
+ }
820
+ target = target.child;
772
821
  }
773
- return void 0;
774
822
  }
775
823
 
776
824
  // internal/src/store/withdraw-new-family-member.ts
@@ -874,7 +922,6 @@ var updateSelectorAtoms = (selectorKey, dependency, store) => {
874
922
  var registerSelector = (selectorKey, store) => ({
875
923
  get: (dependency) => {
876
924
  const target = newest(store);
877
- const alreadyRegistered = target.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
878
925
  const dependencyState = withdraw(dependency, store);
879
926
  if (dependencyState === void 0) {
880
927
  throw new Error(
@@ -890,17 +937,15 @@ var registerSelector = (selectorKey, store) => ({
890
937
  dependencyValue,
891
938
  `)`
892
939
  );
893
- if (!alreadyRegistered) {
894
- target.selectorGraph.set(
895
- {
896
- upstreamSelectorKey: dependency.key,
897
- downstreamSelectorKey: selectorKey
898
- },
899
- {
900
- source: dependency.key
901
- }
902
- );
903
- }
940
+ target.selectorGraph.set(
941
+ {
942
+ upstreamSelectorKey: dependency.key,
943
+ downstreamSelectorKey: selectorKey
944
+ },
945
+ {
946
+ source: dependency.key
947
+ }
948
+ );
904
949
  updateSelectorAtoms(selectorKey, dependency, store);
905
950
  return dependencyValue;
906
951
  },
@@ -922,7 +967,7 @@ var createReadWriteSelector = (options, family, store) => {
922
967
  const { get, set } = registerSelector(options.key, store);
923
968
  const getSelf = () => {
924
969
  const value = options.get({ get });
925
- cacheValue(options.key, value, subject, store);
970
+ cacheValue(options.key, value, subject, newest(store));
926
971
  return value;
927
972
  };
928
973
  const setSelf = (next) => {
@@ -1012,7 +1057,7 @@ function createSelector(options, family, store) {
1012
1057
  `\u274C`,
1013
1058
  existingReadonly ? `readonly_selector` : `selector`,
1014
1059
  options.key,
1015
- `Tried to create selector, but it already exists in the store. (Ignore if you are in development using hot module replacement.)`
1060
+ `Tried to create selector, but it already exists in the store.`
1016
1061
  );
1017
1062
  }
1018
1063
  if (`set` in options) {
@@ -1061,8 +1106,9 @@ var recallState = (state, store) => {
1061
1106
 
1062
1107
  // internal/src/subscribe/subscribe-to-root-atoms.ts
1063
1108
  var subscribeToRootAtoms = (state, store) => {
1109
+ const target = newest(store);
1064
1110
  const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
1065
- const atom = store.atoms.get(atomKey);
1111
+ const atom = target.atoms.get(atomKey);
1066
1112
  if (atom === void 0) {
1067
1113
  throw new Error(
1068
1114
  `Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
@@ -1082,8 +1128,8 @@ var subscribeToRootAtoms = (state, store) => {
1082
1128
  `->`,
1083
1129
  atomChange.newValue
1084
1130
  );
1085
- const oldValue = recallState(state, store);
1086
- const newValue = readOrComputeValue(state, store);
1131
+ const oldValue = recallState(state, target);
1132
+ const newValue = readOrComputeValue(state, target);
1087
1133
  store.logger.info(
1088
1134
  `\u2728`,
1089
1135
  state.type,
@@ -1099,8 +1145,11 @@ var subscribeToRootAtoms = (state, store) => {
1099
1145
  });
1100
1146
  return dependencySubscriptions;
1101
1147
  };
1148
+
1149
+ // internal/src/subscribe/subscribe-to-state.ts
1102
1150
  function subscribeToState(token, handleUpdate, key, store) {
1103
- const state = internal.withdraw(token, store);
1151
+ var _a;
1152
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1104
1153
  if (state === void 0) {
1105
1154
  throw new Error(
1106
1155
  `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
@@ -1108,7 +1157,7 @@ function subscribeToState(token, handleUpdate, key, store) {
1108
1157
  }
1109
1158
  const unsubFunction = state.subject.subscribe(key, handleUpdate);
1110
1159
  store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
1111
- const dependencyUnsubFunctions = state.type !== `atom` ? internal.subscribeToRootAtoms(state, store) : null;
1160
+ const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
1112
1161
  const unsubscribe = dependencyUnsubFunctions === null ? () => {
1113
1162
  store.logger.info(
1114
1163
  `\u{1F648}`,
@@ -1132,7 +1181,7 @@ function subscribeToState(token, handleUpdate, key, store) {
1132
1181
  return unsubscribe;
1133
1182
  }
1134
1183
  var subscribeToTimeline = (token, handleUpdate, key, store) => {
1135
- const tl = internal.withdraw(token, store);
1184
+ const tl = Internal2.withdraw(token, store);
1136
1185
  if (tl === void 0) {
1137
1186
  throw new Error(
1138
1187
  `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
@@ -1151,7 +1200,7 @@ var subscribeToTimeline = (token, handleUpdate, key, store) => {
1151
1200
  };
1152
1201
  };
1153
1202
  var subscribeToTransaction = (token, handleUpdate, key, store) => {
1154
- const tx = internal.withdraw(token, store);
1203
+ const tx = Internal2.withdraw(token, store);
1155
1204
  if (tx === void 0) {
1156
1205
  throw new Error(
1157
1206
  `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
@@ -1185,6 +1234,7 @@ var Tracker = class {
1185
1234
  target.trackers.set(mutableState.key, this);
1186
1235
  }
1187
1236
  initializeState(mutableState, store) {
1237
+ var _a;
1188
1238
  const latestUpdateStateKey = `*${mutableState.key}`;
1189
1239
  store.atoms.delete(latestUpdateStateKey);
1190
1240
  store.valueMap.delete(latestUpdateStateKey);
@@ -1192,7 +1242,7 @@ var Tracker = class {
1192
1242
  key: `*${mutableState.family.key}`,
1193
1243
  subKey: mutableState.family.subKey
1194
1244
  } : void 0;
1195
- const latestUpdateState = createAtom(
1245
+ const latestUpdateState = createRegularAtom(
1196
1246
  {
1197
1247
  key: latestUpdateStateKey,
1198
1248
  default: null
@@ -1200,7 +1250,7 @@ var Tracker = class {
1200
1250
  familyMetaData,
1201
1251
  store
1202
1252
  );
1203
- if (store.parent) {
1253
+ if ((_a = store.parent) == null ? void 0 : _a.valueMap.has(latestUpdateStateKey)) {
1204
1254
  const parentValue = store.parent.valueMap.get(latestUpdateStateKey);
1205
1255
  store.valueMap.set(latestUpdateStateKey, parentValue);
1206
1256
  }
@@ -1212,11 +1262,11 @@ var Tracker = class {
1212
1262
  this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
1213
1263
  `tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
1214
1264
  (update) => {
1215
- const unsubscribe = store.subject.operationStatus.subscribe(
1265
+ const unsubscribe = target.subject.operationStatus.subscribe(
1216
1266
  mutableState.key,
1217
1267
  () => {
1218
1268
  unsubscribe();
1219
- atom_io.setState(latestUpdateState, update, store);
1269
+ atom_io.setState(latestUpdateState, update, target);
1220
1270
  }
1221
1271
  );
1222
1272
  }
@@ -1303,14 +1353,14 @@ var Tracker = class {
1303
1353
  };
1304
1354
 
1305
1355
  // internal/src/mutable/create-mutable-atom.ts
1306
- function createMutableAtom(options, store) {
1356
+ function createMutableAtom(options, family, store) {
1307
1357
  store.logger.info(
1308
1358
  `\u{1F527}`,
1309
1359
  `atom`,
1310
1360
  options.key,
1311
1361
  `creating in store "${store.config.name}"`
1312
1362
  );
1313
- const coreState = createAtom(options, void 0, store);
1363
+ const coreState = createRegularAtom(options, family, store);
1314
1364
  new Tracker(coreState, store);
1315
1365
  const jsonState = json.selectJson(coreState, options, store);
1316
1366
  const target = newest(store);
@@ -1327,7 +1377,7 @@ function createMutableAtom(options, store) {
1327
1377
  );
1328
1378
  return coreState;
1329
1379
  }
1330
- function createAtomFamily(options, store) {
1380
+ function createRegularAtomFamily(options, store) {
1331
1381
  const subject = new Subject();
1332
1382
  const atomFamily = Object.assign(
1333
1383
  (key) => {
@@ -1346,7 +1396,16 @@ function createAtomFamily(options, store) {
1346
1396
  if (options.effects) {
1347
1397
  individualOptions.effects = options.effects(key);
1348
1398
  }
1349
- token = createAtom(individualOptions, family, store);
1399
+ if (`mutable` in options) {
1400
+ const mutableOptions = __spreadProps(__spreadValues({}, individualOptions), {
1401
+ mutable: true,
1402
+ toJson: options.toJson,
1403
+ fromJson: options.fromJson
1404
+ });
1405
+ token = createMutableAtom(mutableOptions, family, store);
1406
+ } else {
1407
+ token = createRegularAtom(individualOptions, family, store);
1408
+ }
1350
1409
  subject.next(token);
1351
1410
  }
1352
1411
  return token;
@@ -1354,7 +1413,8 @@ function createAtomFamily(options, store) {
1354
1413
  {
1355
1414
  key: options.key,
1356
1415
  type: `atom_family`,
1357
- subject
1416
+ subject,
1417
+ install: (store2) => createRegularAtomFamily(options, store2)
1358
1418
  }
1359
1419
  );
1360
1420
  if (`mutable` in options && typeof options.mutable === `boolean`) {
@@ -1364,9 +1424,17 @@ function createAtomFamily(options, store) {
1364
1424
  target.families.set(options.key, atomFamily);
1365
1425
  return atomFamily;
1366
1426
  }
1427
+
1428
+ // internal/src/families/create-atom-family.ts
1429
+ function createAtomFamily(options, store) {
1430
+ if (`mutable` in options) {
1431
+ return createMutableAtomFamily(options, store);
1432
+ }
1433
+ return createRegularAtomFamily(options, store);
1434
+ }
1367
1435
  function createReadonlySelectorFamily(options, store) {
1368
1436
  const subject = new Subject();
1369
- return Object.assign(
1437
+ const readonlySelectorFamily = Object.assign(
1370
1438
  (key) => {
1371
1439
  const target = newest(store);
1372
1440
  const subKey = json.stringifyJson(key);
@@ -1388,9 +1456,12 @@ function createReadonlySelectorFamily(options, store) {
1388
1456
  {
1389
1457
  key: options.key,
1390
1458
  type: `readonly_selector_family`,
1391
- subject
1459
+ subject,
1460
+ install: (store2) => createReadonlySelectorFamily(options, store2)
1392
1461
  }
1393
1462
  );
1463
+ store.families.set(options.key, readonlySelectorFamily);
1464
+ return readonlySelectorFamily;
1394
1465
  }
1395
1466
  function createSelectorFamily(options, store) {
1396
1467
  const isReadonly = !(`set` in options);
@@ -1422,7 +1493,9 @@ function createSelectorFamily(options, store) {
1422
1493
  },
1423
1494
  {
1424
1495
  key: options.key,
1425
- type: `selector_family`
1496
+ type: `selector_family`,
1497
+ subject,
1498
+ install: (store2) => createSelectorFamily(options, store2)
1426
1499
  }
1427
1500
  );
1428
1501
  target.families.set(options.key, selectorFamily);
@@ -1432,7 +1505,7 @@ function createSelectorFamily(options, store) {
1432
1505
  // internal/src/mutable/tracker-family.ts
1433
1506
  var FamilyTracker = class {
1434
1507
  constructor(findMutableState, store) {
1435
- this.findLatestUpdateState = createAtomFamily(
1508
+ this.findLatestUpdateState = createRegularAtomFamily(
1436
1509
  {
1437
1510
  key: `*${findMutableState.key}`,
1438
1511
  default: null
@@ -1466,7 +1539,7 @@ var FamilyTracker = class {
1466
1539
  // internal/src/mutable/create-mutable-atom-family.ts
1467
1540
  function createMutableAtomFamily(options, store) {
1468
1541
  const coreFamily = Object.assign(
1469
- createAtomFamily(options, store),
1542
+ createRegularAtomFamily(options, store),
1470
1543
  options
1471
1544
  );
1472
1545
  json.selectJsonFamily(coreFamily, options);
@@ -1537,13 +1610,9 @@ var markAtomAsNotDefault = (key, store) => {
1537
1610
  core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault);
1538
1611
  core.atomsThatAreDefault.delete(key);
1539
1612
  };
1540
- var isSelectorDefault = (key, store) => {
1541
- const rootKeys = traceAllSelectorAtoms(key, store);
1542
- return rootKeys.every((rootKey) => isAtomDefault(rootKey, store));
1543
- };
1544
1613
 
1545
- // internal/src/atom/create-atom.ts
1546
- function createAtom(options, family, store) {
1614
+ // internal/src/atom/create-regular-atom.ts
1615
+ function createRegularAtom(options, family, store) {
1547
1616
  store.logger.info(
1548
1617
  `\u{1F528}`,
1549
1618
  `atom`,
@@ -1557,13 +1626,12 @@ function createAtom(options, family, store) {
1557
1626
  `\u274C`,
1558
1627
  `atom`,
1559
1628
  options.key,
1560
- `Tried to create atom, but it already exists in the store.`,
1561
- `(Ignore if you are in development using hot module replacement.)`
1629
+ `Tried to create atom, but it already exists in the store.`
1562
1630
  );
1563
1631
  return deposit(existing);
1564
1632
  }
1565
1633
  const subject = new Subject();
1566
- const newAtom = __spreadValues(__spreadProps(__spreadValues({}, options), {
1634
+ const newAtom = __spreadProps(__spreadValues({}, options), {
1567
1635
  type: `atom`,
1568
1636
  install: (store2) => {
1569
1637
  store2.logger.info(
@@ -1572,17 +1640,20 @@ function createAtom(options, family, store) {
1572
1640
  options.key,
1573
1641
  `installing in store "${store2.config.name}"`
1574
1642
  );
1575
- return `mutable` in options ? createMutableAtom(options, store2) : createAtom(options, void 0, store2);
1643
+ return `mutable` in options ? createMutableAtom(options, family, store2) : createRegularAtom(options, family, store2);
1576
1644
  },
1577
1645
  subject
1578
- }), family && { family });
1646
+ });
1647
+ if (family) {
1648
+ newAtom.family = family;
1649
+ }
1579
1650
  let initialValue = options.default;
1580
1651
  if (options.default instanceof Function) {
1581
1652
  initialValue = options.default();
1582
1653
  }
1583
1654
  target.atoms.set(newAtom.key, newAtom);
1584
1655
  markAtomAsDefault(options.key, store);
1585
- cacheValue(options.key, initialValue, subject, store);
1656
+ cacheValue(options.key, initialValue, subject, target);
1586
1657
  const token = deposit(newAtom);
1587
1658
  if (options.effects) {
1588
1659
  let effectIndex = 0;
@@ -1607,6 +1678,14 @@ function createAtom(options, family, store) {
1607
1678
  return token;
1608
1679
  }
1609
1680
 
1681
+ // internal/src/atom/create-atom.ts
1682
+ function createAtom(options, family, store) {
1683
+ if (`mutable` in options) {
1684
+ return createMutableAtom(options, family, store);
1685
+ }
1686
+ return createRegularAtom(options, family, store);
1687
+ }
1688
+
1610
1689
  // internal/src/atom/delete-atom.ts
1611
1690
  function deleteAtom2(atomToken, store) {
1612
1691
  var _a, _b;
@@ -1888,26 +1967,10 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1888
1967
  };
1889
1968
 
1890
1969
  // internal/src/mutable/is-mutable.ts
1891
- function isMutable(atomOrTokenOrFamily, store) {
1970
+ function isMutable(atomOrTokenOrFamily) {
1892
1971
  if (`mutable` in atomOrTokenOrFamily) {
1893
1972
  return atomOrTokenOrFamily.mutable;
1894
1973
  }
1895
- if (atomOrTokenOrFamily.type === `atom_family`) {
1896
- return false;
1897
- }
1898
- if (`default` in atomOrTokenOrFamily) {
1899
- return false;
1900
- }
1901
- if (!store) {
1902
- throw new Error(`Cannot check mutability without a store`);
1903
- }
1904
- const atom = withdraw(atomOrTokenOrFamily, store);
1905
- if (!atom) {
1906
- throw new Error(`Cannot check mutability without an atom`);
1907
- }
1908
- if (`mutable` in atom) {
1909
- return atom.mutable;
1910
- }
1911
1974
  return false;
1912
1975
  }
1913
1976
 
@@ -2168,8 +2231,27 @@ var applyTransaction = (output, store) => {
2168
2231
  `Applying transaction with ${updates.length} updates:`,
2169
2232
  updates
2170
2233
  );
2234
+ for (const tracker of child.trackers.values()) {
2235
+ const mutableKey = tracker.mutableState.key;
2236
+ if (!parent.atoms.has(mutableKey)) {
2237
+ const atom = child.atoms.get(mutableKey);
2238
+ atom == null ? void 0 : atom.install(parent);
2239
+ }
2240
+ }
2241
+ for (const atom of child.atoms.values()) {
2242
+ if (!parent.atoms.has(atom.key)) {
2243
+ parent.atoms.set(atom.key, atom);
2244
+ parent.valueMap.set(atom.key, atom.default);
2245
+ parent.logger.info(
2246
+ `\u{1F528}`,
2247
+ `transaction`,
2248
+ child.transactionMeta.update.key,
2249
+ `Adding atom "${atom.key}"`
2250
+ );
2251
+ }
2252
+ }
2253
+ ingestTransactionUpdate(child.transactionMeta.update, parent, child);
2171
2254
  if (parent.transactionMeta === null) {
2172
- ingestTransactionUpdate(child.transactionMeta.update, parent, child);
2173
2255
  const myTransaction = withdraw(
2174
2256
  { key: child.transactionMeta.update.key, type: `transaction` },
2175
2257
  store
@@ -2182,32 +2264,51 @@ var applyTransaction = (output, store) => {
2182
2264
  `Finished applying transaction.`
2183
2265
  );
2184
2266
  } else {
2185
- ingestTransactionUpdate(child.transactionMeta.update, parent, child);
2186
2267
  parent.transactionMeta.update.updates.push(child.transactionMeta.update);
2187
2268
  }
2188
2269
  parent.subject.transactionApplying.next(null);
2189
2270
  };
2271
+ function getState2(token, store = Internal2__namespace.IMPLICIT.STORE) {
2272
+ var _a;
2273
+ const state = (_a = Internal2__namespace.withdraw(token, store)) != null ? _a : Internal2__namespace.withdrawNewFamilyMember(token, store);
2274
+ if (state === void 0) {
2275
+ throw new Internal2__namespace.NotFoundError(token, store);
2276
+ }
2277
+ return Internal2__namespace.readOrComputeValue(state, store);
2278
+ }
2279
+ function setState5(token, value, store = Internal2__namespace.IMPLICIT.STORE) {
2280
+ const rejection = Internal2__namespace.openOperation(token, store);
2281
+ if (rejection) {
2282
+ return;
2283
+ }
2284
+ const state = Internal2__namespace.withdraw(token, store);
2285
+ if (state === void 0) {
2286
+ throw new Internal2__namespace.NotFoundError(token, store);
2287
+ }
2288
+ Internal2__namespace.setAtomOrSelector(state, value, store);
2289
+ Internal2__namespace.closeOperation(store);
2290
+ }
2291
+ var runTransaction = (token, store = Internal2.IMPLICIT.STORE) => (...parameters) => {
2292
+ const tx = Internal2.withdraw(token, store);
2293
+ if (tx) {
2294
+ return tx.run(...parameters);
2295
+ }
2296
+ throw new Error(
2297
+ `Cannot run transaction "${token.key}": transaction not found in store "${store.config.name}".`
2298
+ );
2299
+ };
2190
2300
 
2191
2301
  // internal/src/transaction/build-transaction.ts
2192
2302
  var buildTransaction = (key, params, store) => {
2193
2303
  const parent = newest(store);
2194
- parent.child = {
2304
+ const child = {
2195
2305
  parent,
2196
2306
  child: null,
2197
2307
  subject: parent.subject,
2198
2308
  loggers: parent.loggers,
2199
2309
  logger: parent.logger,
2200
2310
  config: parent.config,
2201
- transactionMeta: {
2202
- phase: `building`,
2203
- time: Date.now(),
2204
- update: {
2205
- key,
2206
- updates: [],
2207
- params,
2208
- output: void 0
2209
- }
2210
- },
2311
+ transactionMeta: null,
2211
2312
  atoms: new LazyMap(parent.atoms),
2212
2313
  atomsThatAreDefault: new Set(parent.atomsThatAreDefault),
2213
2314
  families: new LazyMap(parent.families),
@@ -2224,6 +2325,22 @@ var buildTransaction = (key, params, store) => {
2224
2325
  selectors: new LazyMap(parent.selectors),
2225
2326
  valueMap: new LazyMap(parent.valueMap)
2226
2327
  };
2328
+ child.transactionMeta = {
2329
+ phase: `building`,
2330
+ time: Date.now(),
2331
+ update: {
2332
+ key,
2333
+ updates: [],
2334
+ params,
2335
+ output: void 0
2336
+ },
2337
+ transactors: {
2338
+ get: (token) => getState2(token, child),
2339
+ set: (token, value) => setState5(token, value, child),
2340
+ run: (token) => runTransaction(token, child)
2341
+ }
2342
+ };
2343
+ parent.child = child;
2227
2344
  store.logger.info(
2228
2345
  `\u{1F6EB}`,
2229
2346
  `transaction`,
@@ -2232,30 +2349,27 @@ var buildTransaction = (key, params, store) => {
2232
2349
  params
2233
2350
  );
2234
2351
  };
2235
- function createTransaction(options, store) {
2352
+
2353
+ // internal/src/transaction/create-transaction.ts
2354
+ function createTransaction2(options, store) {
2236
2355
  const newTransaction = {
2237
2356
  key: options.key,
2238
2357
  type: `transaction`,
2239
2358
  run: (...params) => {
2240
2359
  buildTransaction(options.key, params, store);
2241
2360
  try {
2242
- const output = options.do(
2243
- {
2244
- get: (token2) => atom_io.getState(token2, store),
2245
- set: (token2, value) => atom_io.setState(token2, value, store),
2246
- run: (token2) => atom_io.runTransaction(token2, store)
2247
- },
2248
- ...params
2249
- );
2250
- applyTransaction(output, store);
2361
+ const target2 = newest(store);
2362
+ const { transactors } = target2.transactionMeta;
2363
+ const output = options.do(transactors, ...params);
2364
+ applyTransaction(output, target2);
2251
2365
  return output;
2252
2366
  } catch (thrown) {
2253
- abortTransaction(store);
2367
+ abortTransaction(target);
2254
2368
  store.logger.warn(`\u{1F4A5}`, `transaction`, options.key, `caught:`, thrown);
2255
2369
  throw thrown;
2256
2370
  }
2257
2371
  },
2258
- install: (store2) => createTransaction(options, store2),
2372
+ install: (store2) => createTransaction2(options, store2),
2259
2373
  subject: new Subject()
2260
2374
  };
2261
2375
  const target = newest(store);
@@ -2264,42 +2378,6 @@ function createTransaction(options, store) {
2264
2378
  store.subject.transactionCreation.next(token);
2265
2379
  return token;
2266
2380
  }
2267
- var redoTransactionUpdate = (transactionUpdate, store) => {
2268
- store.logger.info(`\u23ED\uFE0F`, `transaction`, transactionUpdate.key, `Redo`);
2269
- for (const update of transactionUpdate.updates) {
2270
- if (`newValue` in update) {
2271
- const { key, newValue } = update;
2272
- const token = { key, type: `atom` };
2273
- const state = withdraw(token, store);
2274
- if (state === void 0) {
2275
- throw new Error(
2276
- `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
2277
- );
2278
- }
2279
- atom_io.setState(state, newValue, store);
2280
- } else {
2281
- redoTransactionUpdate(update, store);
2282
- }
2283
- }
2284
- };
2285
- var undoTransactionUpdate = (transactionUpdate, store) => {
2286
- store.logger.info(`\u23EE\uFE0F`, `transaction`, transactionUpdate.key, `Undo`);
2287
- for (const update of transactionUpdate.updates.reverse()) {
2288
- if (`newValue` in update) {
2289
- const { key, newValue } = update;
2290
- const token = { key, type: `atom` };
2291
- const state = withdraw(token, store);
2292
- if (state === void 0) {
2293
- throw new Error(
2294
- `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
2295
- );
2296
- }
2297
- atom_io.setState(state, newValue, store);
2298
- } else {
2299
- undoTransactionUpdate(update, store);
2300
- }
2301
- }
2302
- };
2303
2381
 
2304
2382
  // internal/src/transaction/index.ts
2305
2383
  var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
@@ -2327,10 +2405,12 @@ exports.createAtomFamily = createAtomFamily;
2327
2405
  exports.createMutableAtom = createMutableAtom;
2328
2406
  exports.createMutableAtomFamily = createMutableAtomFamily;
2329
2407
  exports.createReadonlySelectorFamily = createReadonlySelectorFamily;
2408
+ exports.createRegularAtom = createRegularAtom;
2409
+ exports.createRegularAtomFamily = createRegularAtomFamily;
2330
2410
  exports.createSelector = createSelector;
2331
2411
  exports.createSelectorFamily = createSelectorFamily;
2332
2412
  exports.createTimeline = createTimeline;
2333
- exports.createTransaction = createTransaction;
2413
+ exports.createTransaction = createTransaction2;
2334
2414
  exports.deleteAtom = deleteAtom2;
2335
2415
  exports.deleteSelector = deleteSelector;
2336
2416
  exports.deposit = deposit;
@@ -2345,7 +2425,6 @@ exports.isAtomDefault = isAtomDefault;
2345
2425
  exports.isAtomKey = isAtomKey;
2346
2426
  exports.isDone = isDone;
2347
2427
  exports.isReadonlySelectorKey = isReadonlySelectorKey;
2348
- exports.isSelectorDefault = isSelectorDefault;
2349
2428
  exports.isSelectorKey = isSelectorKey;
2350
2429
  exports.isStateKey = isStateKey;
2351
2430
  exports.isTransceiver = isTransceiver;
@@ -2357,7 +2436,6 @@ exports.newest = newest;
2357
2436
  exports.openOperation = openOperation;
2358
2437
  exports.readCachedValue = readCachedValue;
2359
2438
  exports.readOrComputeValue = readOrComputeValue;
2360
- exports.redoTransactionUpdate = redoTransactionUpdate;
2361
2439
  exports.registerSelector = registerSelector;
2362
2440
  exports.setAtomOrSelector = setAtomOrSelector;
2363
2441
  exports.subscribeToRootAtoms = subscribeToRootAtoms;
@@ -2367,7 +2445,6 @@ exports.subscribeToTransaction = subscribeToTransaction;
2367
2445
  exports.timeTravel = timeTravel;
2368
2446
  exports.traceAllSelectorAtoms = traceAllSelectorAtoms;
2369
2447
  exports.traceSelectorAtoms = traceSelectorAtoms;
2370
- exports.undoTransactionUpdate = undoTransactionUpdate;
2371
2448
  exports.updateSelectorAtoms = updateSelectorAtoms;
2372
2449
  exports.withdraw = withdraw;
2373
2450
  exports.withdrawNewFamilyMember = withdrawNewFamilyMember;