atom.io 0.11.0 → 0.12.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 (174) hide show
  1. package/data/dist/index.cjs +614 -0
  2. package/data/dist/index.cjs.map +1 -0
  3. package/data/dist/index.d.cts +158 -0
  4. package/data/dist/index.d.ts +118 -1
  5. package/data/dist/index.js +551 -30
  6. package/data/dist/index.js.map +1 -1
  7. package/data/dist/metafile-cjs.json +1 -0
  8. package/data/dist/metafile-esm.json +1 -0
  9. package/data/package.json +4 -3
  10. package/data/src/index.ts +1 -0
  11. package/data/src/join.ts +450 -0
  12. package/data/src/struct-family.ts +34 -24
  13. package/data/src/struct.ts +6 -8
  14. package/dist/index.cjs +257 -0
  15. package/dist/index.cjs.map +1 -0
  16. package/dist/{index.d.mts → index.d.cts} +10 -11
  17. package/dist/index.d.ts +10 -11
  18. package/dist/index.js +63 -104
  19. package/dist/index.js.map +1 -1
  20. package/dist/metafile-cjs.json +1 -0
  21. package/dist/metafile-esm.json +1 -0
  22. package/internal/dist/{index.mjs → index.cjs} +716 -495
  23. package/internal/dist/index.cjs.map +1 -0
  24. package/internal/dist/{index.d.mts → index.d.cts} +124 -105
  25. package/internal/dist/index.d.ts +124 -105
  26. package/internal/dist/index.js +651 -555
  27. package/internal/dist/index.js.map +1 -1
  28. package/internal/dist/metafile-cjs.json +1 -0
  29. package/internal/dist/metafile-esm.json +1 -0
  30. package/internal/package.json +4 -3
  31. package/internal/src/atom/create-atom.ts +29 -16
  32. package/internal/src/atom/delete-atom.ts +25 -6
  33. package/internal/src/atom/is-default.ts +4 -17
  34. package/internal/src/caching.ts +28 -23
  35. package/internal/src/families/create-atom-family.ts +3 -2
  36. package/internal/src/families/create-readonly-selector-family.ts +1 -1
  37. package/internal/src/families/create-selector-family.ts +4 -4
  38. package/internal/src/index.ts +4 -2
  39. package/internal/src/lazy-map.ts +33 -0
  40. package/internal/src/mutable/create-mutable-atom-family.ts +2 -2
  41. package/internal/src/mutable/create-mutable-atom.ts +1 -2
  42. package/internal/src/mutable/get-json-family.ts +22 -0
  43. package/internal/src/mutable/get-json-token.ts +1 -0
  44. package/internal/src/mutable/index.ts +1 -0
  45. package/internal/src/mutable/tracker-family.ts +1 -2
  46. package/internal/src/mutable/tracker.ts +8 -6
  47. package/internal/src/mutable/transceiver.ts +2 -0
  48. package/internal/src/not-found-error.ts +27 -0
  49. package/internal/src/operation.ts +2 -3
  50. package/internal/src/{get-state-internal.ts → read-or-compute-value.ts} +13 -6
  51. package/internal/src/selector/create-selector.ts +6 -7
  52. package/internal/src/selector/delete-selector.ts +37 -0
  53. package/internal/src/selector/index.ts +2 -1
  54. package/internal/src/selector/register-selector.ts +7 -7
  55. package/internal/src/set-state/copy-mutable-in-transaction.ts +3 -2
  56. package/internal/src/set-state/emit-update.ts +1 -3
  57. package/internal/src/set-state/evict-downstream.ts +6 -8
  58. package/internal/src/set-state/index.ts +1 -1
  59. package/internal/src/set-state/{set-state-internal.ts → set-atom-or-selector.ts} +2 -3
  60. package/internal/src/set-state/set-atom.ts +5 -6
  61. package/internal/src/store/store.ts +1 -1
  62. package/internal/src/store/withdraw-new-family-member.ts +6 -6
  63. package/internal/src/subscribe/recall-state.ts +1 -2
  64. package/internal/src/subscribe/subscribe-to-root-atoms.ts +2 -2
  65. package/internal/src/timeline/add-atom-to-timeline.ts +5 -16
  66. package/internal/src/timeline/{timeline-internal.ts → create-timeline.ts} +4 -5
  67. package/internal/src/timeline/index.ts +2 -2
  68. package/internal/src/timeline/time-travel.ts +89 -0
  69. package/internal/src/transaction/build-transaction.ts +8 -7
  70. package/internal/src/transaction/{transaction-internal.ts → create-transaction.ts} +6 -5
  71. package/internal/src/transaction/index.ts +2 -3
  72. package/introspection/dist/{index.mjs → index.cjs} +54 -29
  73. package/introspection/dist/index.cjs.map +1 -0
  74. package/introspection/dist/{index.d.mts → index.d.cts} +2 -2
  75. package/introspection/dist/index.d.ts +2 -2
  76. package/introspection/dist/index.js +32 -49
  77. package/introspection/dist/index.js.map +1 -1
  78. package/introspection/dist/metafile-cjs.json +1 -0
  79. package/introspection/dist/metafile-esm.json +1 -0
  80. package/introspection/package.json +4 -3
  81. package/introspection/src/attach-introspection-states.ts +2 -2
  82. package/introspection/src/attach-selector-index.ts +8 -4
  83. package/json/dist/{index.mjs → index.cjs} +20 -7
  84. package/json/dist/{index.mjs.map → index.cjs.map} +1 -1
  85. package/json/dist/{index.d.mts → index.d.cts} +1 -1
  86. package/json/dist/index.d.ts +1 -1
  87. package/json/dist/index.js +6 -19
  88. package/json/dist/index.js.map +1 -1
  89. package/json/dist/metafile-cjs.json +1 -0
  90. package/json/dist/metafile-esm.json +1 -0
  91. package/json/package.json +4 -3
  92. package/package.json +48 -47
  93. package/react/dist/index.cjs +59 -0
  94. package/react/dist/index.cjs.map +1 -0
  95. package/react/dist/index.js +20 -42
  96. package/react/dist/index.js.map +1 -1
  97. package/react/dist/metafile-cjs.json +1 -0
  98. package/react/dist/metafile-esm.json +1 -0
  99. package/react/package.json +4 -3
  100. package/react/src/store-hooks.ts +8 -2
  101. package/react-devtools/dist/{index.mjs → index.cjs} +286 -240
  102. package/react-devtools/dist/index.cjs.map +1 -0
  103. package/react-devtools/dist/{index.d.mts → index.d.cts} +29 -17
  104. package/react-devtools/dist/index.d.ts +29 -17
  105. package/react-devtools/dist/index.js +251 -273
  106. package/react-devtools/dist/index.js.map +1 -1
  107. package/react-devtools/dist/metafile-cjs.json +1 -0
  108. package/react-devtools/dist/metafile-esm.json +1 -0
  109. package/react-devtools/package.json +4 -3
  110. package/react-devtools/src/StateEditor.tsx +8 -8
  111. package/realtime-client/dist/{index.mjs → index.cjs} +50 -21
  112. package/realtime-client/dist/index.js +20 -49
  113. package/realtime-client/dist/metafile-cjs.json +1 -0
  114. package/realtime-client/dist/metafile-esm.json +1 -0
  115. package/realtime-client/package.json +4 -3
  116. package/realtime-react/dist/index.cjs +99 -0
  117. package/realtime-react/dist/index.js +35 -66
  118. package/realtime-react/dist/metafile-cjs.json +1 -0
  119. package/realtime-react/dist/metafile-esm.json +1 -0
  120. package/realtime-react/package.json +4 -3
  121. package/realtime-server/dist/{index.mjs → index.cjs} +67 -40
  122. package/realtime-server/dist/index.js +39 -66
  123. package/realtime-server/dist/metafile-cjs.json +1 -0
  124. package/realtime-server/dist/metafile-esm.json +1 -0
  125. package/realtime-server/package.json +4 -3
  126. package/realtime-testing/dist/{index.mjs → index.cjs} +54 -23
  127. package/realtime-testing/dist/index.js +22 -53
  128. package/realtime-testing/dist/metafile-cjs.json +1 -0
  129. package/realtime-testing/dist/metafile-esm.json +1 -0
  130. package/realtime-testing/package.json +4 -3
  131. package/src/atom.ts +6 -8
  132. package/src/dispose.ts +18 -0
  133. package/src/get-state.ts +16 -0
  134. package/src/index.ts +3 -1
  135. package/src/logger.ts +1 -1
  136. package/src/selector.ts +3 -3
  137. package/src/set-state.ts +22 -0
  138. package/src/silo.ts +7 -8
  139. package/src/timeline.ts +6 -11
  140. package/src/transaction.ts +2 -2
  141. package/transceivers/set-rtx/dist/{index.mjs → index.cjs} +40 -36
  142. package/transceivers/set-rtx/dist/index.cjs.map +1 -0
  143. package/transceivers/set-rtx/dist/{index.d.mts → index.d.cts} +2 -1
  144. package/transceivers/set-rtx/dist/index.d.ts +2 -1
  145. package/transceivers/set-rtx/dist/index.js +37 -37
  146. package/transceivers/set-rtx/dist/index.js.map +1 -1
  147. package/transceivers/set-rtx/dist/metafile-cjs.json +1 -0
  148. package/transceivers/set-rtx/dist/metafile-esm.json +1 -0
  149. package/transceivers/set-rtx/package.json +4 -3
  150. package/transceivers/set-rtx/src/set-rtx.ts +29 -26
  151. package/data/dist/index.d.mts +0 -41
  152. package/data/dist/index.mjs +0 -82
  153. package/data/dist/index.mjs.map +0 -1
  154. package/dist/index.mjs +0 -215
  155. package/dist/index.mjs.map +0 -1
  156. package/internal/dist/index.mjs.map +0 -1
  157. package/internal/src/set-state/set-selector-state.ts +0 -8
  158. package/internal/src/timeline/time-travel-internal.ts +0 -109
  159. package/introspection/dist/index.mjs.map +0 -1
  160. package/react/dist/index.mjs +0 -29
  161. package/react/dist/index.mjs.map +0 -1
  162. package/react-devtools/dist/index.mjs.map +0 -1
  163. package/realtime-react/dist/index.mjs +0 -68
  164. package/src/get-set.ts +0 -48
  165. package/transceivers/set-rtx/dist/index.mjs.map +0 -1
  166. /package/react/dist/{index.d.mts → index.d.cts} +0 -0
  167. /package/realtime-client/dist/{index.mjs.map → index.cjs.map} +0 -0
  168. /package/realtime-client/dist/{index.d.mts → index.d.cts} +0 -0
  169. /package/realtime-react/dist/{index.mjs.map → index.cjs.map} +0 -0
  170. /package/realtime-react/dist/{index.d.mts → index.d.cts} +0 -0
  171. /package/realtime-server/dist/{index.mjs.map → index.cjs.map} +0 -0
  172. /package/realtime-server/dist/{index.d.mts → index.d.cts} +0 -0
  173. /package/realtime-testing/dist/{index.mjs.map → index.cjs.map} +0 -0
  174. /package/realtime-testing/dist/{index.d.mts → index.d.cts} +0 -0
@@ -1,7 +1,5 @@
1
- 'use strict';
2
-
3
- var atom_io = require('atom.io');
4
- var json = require('atom.io/json');
1
+ import { AtomIOLogger, setState, getState, subscribe, subscribeToTimeline } from 'atom.io';
2
+ import { selectJson, stringifyJson, parseJson, selectJsonFamily } from 'atom.io/json';
5
3
 
6
4
  var __defProp = Object.defineProperty;
7
5
  var __defProps = Object.defineProperties;
@@ -42,6 +40,26 @@ var Future = class extends Promise {
42
40
  }
43
41
  };
44
42
 
43
+ // src/transaction/abort-transaction.ts
44
+ var abortTransaction = (store) => {
45
+ if (store.transactionStatus.phase === `idle`) {
46
+ store.logger.warn(
47
+ `\u{1F41E}`,
48
+ `transaction`,
49
+ `???`,
50
+ `abortTransaction called outside of a transaction. This is probably a bug in AtomIO.`
51
+ );
52
+ return;
53
+ }
54
+ store.logger.info(
55
+ `\u{1FA82}`,
56
+ `transaction`,
57
+ store.transactionStatus.key,
58
+ `Aborting transaction`
59
+ );
60
+ store.transactionStatus = { phase: `idle` };
61
+ };
62
+
45
63
  // src/store/deposit.ts
46
64
  function deposit(state) {
47
65
  const token = {
@@ -60,12 +78,14 @@ var Junction = class {
60
78
  this.relations = /* @__PURE__ */ new Map();
61
79
  this.contents = /* @__PURE__ */ new Map();
62
80
  this.makeContentKey = (a, b) => `${a}:${b}`;
63
- var _a, _b;
81
+ var _a, _b, _c, _d;
64
82
  this.a = data.between[0];
65
83
  this.b = data.between[1];
66
84
  this.cardinality = data.cardinality;
67
- this.relations = new Map((_a = data.relations) == null ? void 0 : _a.map(([a, b]) => [a, new Set(b)]));
68
- this.contents = new Map(data.contents);
85
+ if (!(config == null ? void 0 : config.externalStore)) {
86
+ this.relations = new Map((_a = data.relations) == null ? void 0 : _a.map(([a, b]) => [a, new Set(b)]));
87
+ this.contents = new Map(data.contents);
88
+ }
69
89
  this.isContent = (_b = config == null ? void 0 : config.isContent) != null ? _b : null;
70
90
  if (config == null ? void 0 : config.makeContentKey) {
71
91
  this.makeContentKey = config.makeContentKey;
@@ -75,11 +95,15 @@ var Junction = class {
75
95
  this.has = (a, b) => externalStore.has(a, b);
76
96
  this.addRelation = (a, b) => {
77
97
  externalStore.addRelation(a, b);
78
- return this;
79
98
  };
80
99
  this.deleteRelation = (a, b) => {
81
100
  externalStore.deleteRelation(a, b);
82
- return this;
101
+ };
102
+ this.replaceRelationsSafely = (a, bs) => {
103
+ externalStore.replaceRelationsSafely(a, bs);
104
+ };
105
+ this.replaceRelationsUnsafely = (a, bs) => {
106
+ externalStore.replaceRelationsUnsafely(a, bs);
83
107
  };
84
108
  this.getRelatedKeys = (key) => externalStore.getRelatedKeys(key);
85
109
  if (externalStore.getContent) {
@@ -88,30 +112,37 @@ var Junction = class {
88
112
  };
89
113
  this.setContent = (contentKey, content) => {
90
114
  externalStore.setContent(contentKey, content);
91
- return this;
92
115
  };
93
116
  this.deleteContent = (contentKey) => {
94
117
  externalStore.deleteContent(contentKey);
95
- return this;
96
118
  };
97
119
  }
120
+ for (const [x, ys] of (_c = data.relations) != null ? _c : []) {
121
+ for (const y of ys)
122
+ this.addRelation(x, y);
123
+ }
124
+ for (const [contentKey, content] of (_d = data.contents) != null ? _d : []) {
125
+ this.setContent(contentKey, content);
126
+ }
98
127
  }
99
128
  }
100
129
  getRelatedKeys(key) {
101
130
  return this.relations.get(key);
102
131
  }
103
132
  addRelation(a, b) {
104
- const aRelations = this.relations.get(a);
105
- const bRelations = this.relations.get(b);
133
+ let aRelations = this.relations.get(a);
134
+ let bRelations = this.relations.get(b);
106
135
  if (aRelations) {
107
136
  aRelations.add(b);
108
137
  } else {
109
- this.relations.set(a, /* @__PURE__ */ new Set([b]));
138
+ aRelations = /* @__PURE__ */ new Set([b]);
139
+ this.relations.set(a, aRelations);
110
140
  }
111
141
  if (bRelations) {
112
142
  bRelations.add(a);
113
143
  } else {
114
- this.relations.set(b, /* @__PURE__ */ new Set([a]));
144
+ bRelations = /* @__PURE__ */ new Set([a]);
145
+ this.relations.set(b, bRelations);
115
146
  }
116
147
  }
117
148
  deleteRelation(a, b) {
@@ -130,6 +161,39 @@ var Junction = class {
130
161
  }
131
162
  }
132
163
  }
164
+ replaceRelationsUnsafely(a, bs) {
165
+ this.relations.set(a, new Set(bs));
166
+ for (const b of bs) {
167
+ const bRelations = /* @__PURE__ */ new Set([a]);
168
+ this.relations.set(b, bRelations);
169
+ }
170
+ }
171
+ replaceRelationsSafely(a, bs) {
172
+ const aRelationsPrev = this.relations.get(a);
173
+ if (aRelationsPrev) {
174
+ for (const b of aRelationsPrev) {
175
+ const bRelations = this.relations.get(b);
176
+ if (bRelations) {
177
+ if (bRelations.size === 1) {
178
+ this.relations.delete(b);
179
+ } else {
180
+ bRelations.delete(a);
181
+ }
182
+ this.contents.delete(this.makeContentKey(a, b));
183
+ }
184
+ }
185
+ }
186
+ this.relations.set(a, new Set(bs));
187
+ for (const b of bs) {
188
+ let bRelations = this.relations.get(b);
189
+ if (bRelations) {
190
+ bRelations.add(a);
191
+ } else {
192
+ bRelations = /* @__PURE__ */ new Set([a]);
193
+ this.relations.set(b, bRelations);
194
+ }
195
+ }
196
+ }
133
197
  getContentInternal(contentKey) {
134
198
  return this.contents.get(contentKey);
135
199
  }
@@ -149,10 +213,7 @@ var Junction = class {
149
213
  }
150
214
  set(a, ...rest) {
151
215
  var _a;
152
- const b = (
153
- // @ts-expect-error we deduce hereby that this.b may index a
154
- typeof rest[0] === `string` ? rest[0] : a[this.b]
155
- );
216
+ const b = typeof rest[0] === `string` ? rest[0] : a[this.b];
156
217
  const content = ((_a = rest[1]) != null ? _a : typeof rest[0] === `string`) ? void 0 : rest[0];
157
218
  a = typeof a === `string` ? a : a[this.a];
158
219
  switch (this.cardinality) {
@@ -167,11 +228,11 @@ var Junction = class {
167
228
  this.delete(aPrev, b);
168
229
  }
169
230
  }
170
- this.addRelation(a, b);
171
231
  if (content) {
172
232
  const contentKey = this.makeContentKey(a, b);
173
233
  this.setContent(contentKey, content);
174
234
  }
235
+ this.addRelation(a, b);
175
236
  return this;
176
237
  }
177
238
  delete(x, b) {
@@ -215,6 +276,23 @@ var Junction = class {
215
276
  }
216
277
  }
217
278
  }
279
+ replaceRelations(a, relations, config) {
280
+ const hasContent = !Array.isArray(relations);
281
+ const bs = hasContent ? Object.keys(relations) : relations;
282
+ if (config == null ? void 0 : config.reckless) {
283
+ this.replaceRelationsUnsafely(a, bs);
284
+ } else {
285
+ this.replaceRelationsSafely(a, bs);
286
+ }
287
+ if (hasContent) {
288
+ for (const b of bs) {
289
+ const contentKey = this.makeContentKey(a, b);
290
+ const content = relations[b];
291
+ this.setContent(contentKey, content);
292
+ }
293
+ }
294
+ return this;
295
+ }
218
296
  getContent(a, b) {
219
297
  const contentKey = this.makeContentKey(a, b);
220
298
  return this.getContentInternal(contentKey);
@@ -314,7 +392,7 @@ var Store = class {
314
392
  name: `IMPLICIT_STORE`
315
393
  };
316
394
  this.loggers = [
317
- new atom_io.AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
395
+ new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
318
396
  ];
319
397
  this.logger = {
320
398
  error: (...messages) => {
@@ -362,209 +440,14 @@ var IMPLICIT = {
362
440
  return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
363
441
  }
364
442
  };
365
- var clearStore = (store = IMPLICIT.STORE) => {
443
+ var clearStore = (store) => {
366
444
  const { config } = store;
367
445
  Object.assign(store, new Store(config.name));
368
446
  store.config = config;
369
447
  };
370
448
 
371
- // src/transaction/abort-transaction.ts
372
- var abortTransaction = (store) => {
373
- if (store.transactionStatus.phase === `idle`) {
374
- store.logger.warn(
375
- `\u{1F41E}`,
376
- `transaction`,
377
- `???`,
378
- `abortTransaction called outside of a transaction. This is probably a bug in AtomIO.`
379
- );
380
- return;
381
- }
382
- store.logger.info(
383
- `\u{1FA82}`,
384
- `transaction`,
385
- store.transactionStatus.key,
386
- `Aborting transaction`
387
- );
388
- store.transactionStatus = { phase: `idle` };
389
- };
390
- var applyTransaction = (output, store) => {
391
- if (store.transactionStatus.phase !== `building`) {
392
- store.logger.warn(
393
- `\u{1F41E}`,
394
- `transaction`,
395
- `???`,
396
- `applyTransaction called outside of a transaction. This is probably a bug in AtomIO.`
397
- );
398
- return;
399
- }
400
- store.transactionStatus.phase = `applying`;
401
- store.transactionStatus.output = output;
402
- const { atomUpdates } = store.transactionStatus;
403
- store.logger.info(
404
- `\u{1F6C4}`,
405
- `transaction`,
406
- store.transactionStatus.key,
407
- `Applying transaction with ${atomUpdates.length} updates:`,
408
- atomUpdates
409
- );
410
- for (const { key, newValue } of atomUpdates) {
411
- const token = { key, type: `atom` };
412
- if (!store.valueMap.has(token.key)) {
413
- if (token.family) {
414
- const family = store.families.get(token.family.key);
415
- if (family) {
416
- family(token.family.subKey);
417
- }
418
- } else {
419
- const newAtom = store.transactionStatus.core.atoms.get(token.key);
420
- if (!newAtom) {
421
- throw new Error(
422
- `Absurd Error: Atom "${token.key}" not found while copying updates from transaction "${store.transactionStatus.key}" to store "${store.config.name}"`
423
- );
424
- }
425
- store.atoms.set(newAtom.key, newAtom);
426
- store.valueMap.set(newAtom.key, newAtom.default);
427
- store.logger.info(
428
- `\u{1F528}`,
429
- `transaction`,
430
- store.transactionStatus.key,
431
- `Adding atom "${newAtom.key}"`
432
- );
433
- }
434
- }
435
- atom_io.setState(token, newValue, store);
436
- }
437
- const myTransaction = withdraw(
438
- { key: store.transactionStatus.key, type: `transaction` },
439
- store
440
- );
441
- if (myTransaction === void 0) {
442
- throw new Error(
443
- `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`
444
- );
445
- }
446
- myTransaction.subject.next({
447
- key: store.transactionStatus.key,
448
- atomUpdates,
449
- output,
450
- params: store.transactionStatus.params
451
- });
452
- store.logger.info(
453
- `\u{1F6EC}`,
454
- `transaction`,
455
- store.transactionStatus.key,
456
- `Finished applying transaction.`
457
- );
458
- store.transactionStatus = { phase: `idle` };
459
- };
460
-
461
- // src/transaction/build-transaction.ts
462
- var buildTransaction = (key, params, store) => {
463
- store.transactionStatus = {
464
- key,
465
- phase: `building`,
466
- time: Date.now(),
467
- core: {
468
- atoms: new Map(store.atoms),
469
- atomsThatAreDefault: new Set(store.atomsThatAreDefault),
470
- families: new Map(store.families),
471
- operation: { open: false },
472
- readonlySelectors: new Map(store.readonlySelectors),
473
- timelines: new Map(store.timelines),
474
- timelineAtoms: new Junction(store.timelineAtoms.toJSON()),
475
- trackers: /* @__PURE__ */ new Map(),
476
- transactions: new Map(store.transactions),
477
- selectorAtoms: new Junction(store.selectorAtoms.toJSON()),
478
- selectorGraph: new Junction(store.selectorGraph.toJSON(), {
479
- makeContentKey: (...keys) => keys.sort().join(`:`)
480
- }),
481
- selectors: new Map(store.selectors),
482
- valueMap: new Map(store.valueMap)
483
- },
484
- atomUpdates: [],
485
- params,
486
- output: void 0
487
- };
488
- store.logger.info(
489
- `\u{1F6EB}`,
490
- `transaction`,
491
- key,
492
- `Building transaction with params:`,
493
- params
494
- );
495
- };
496
-
497
- // src/transaction/transaction-internal.ts
498
- function transaction__INTERNAL(options, store = IMPLICIT.STORE) {
499
- const newTransaction = {
500
- key: options.key,
501
- type: `transaction`,
502
- run: (...params) => {
503
- buildTransaction(options.key, params, store);
504
- try {
505
- const output = options.do(
506
- {
507
- get: (token2) => atom_io.getState(token2, store),
508
- set: (token2, value) => atom_io.setState(token2, value, store)
509
- },
510
- ...params
511
- );
512
- applyTransaction(output, store);
513
- return output;
514
- } catch (thrown) {
515
- abortTransaction(store);
516
- store.logger.warn(`\u{1F4A5}`, `transaction`, options.key, `caught:`, thrown);
517
- throw thrown;
518
- }
519
- },
520
- install: (store2) => transaction__INTERNAL(options, store2),
521
- subject: new Subject()
522
- };
523
- const core = target(store);
524
- core.transactions.set(newTransaction.key, newTransaction);
525
- const token = deposit(newTransaction);
526
- store.subject.transactionCreation.next(token);
527
- return token;
528
- }
529
- var target = (store = IMPLICIT.STORE) => store.transactionStatus.phase === `building` ? store.transactionStatus.core : store;
530
- var redoTransactionUpdate = (update, store) => {
531
- store.logger.info(`\u23ED\uFE0F`, `transaction`, update.key, `Redo`);
532
- for (const { key, newValue } of update.atomUpdates) {
533
- const token = { key, type: `atom` };
534
- const state = withdraw(token, store);
535
- if (state === void 0) {
536
- throw new Error(
537
- `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
538
- );
539
- }
540
- atom_io.setState(state, newValue, store);
541
- }
542
- };
543
- var undoTransactionUpdate = (update, store) => {
544
- store.logger.info(
545
- `\u23EE\uFE0F`,
546
- `transaction`,
547
- update.key,
548
- `Undoing transaction update`,
549
- update
550
- );
551
- for (const { key, oldValue } of update.atomUpdates) {
552
- const token = { key, type: `atom` };
553
- const state = withdraw(token, store);
554
- if (state === void 0) {
555
- throw new Error(
556
- `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
557
- );
558
- }
559
- atom_io.setState(state, oldValue, store);
560
- }
561
- };
562
-
563
- // src/transaction/index.ts
564
- var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
565
-
566
449
  // src/timeline/add-atom-to-timeline.ts
567
- var addAtomToTimeline = (atomToken, tl, store = IMPLICIT.STORE) => {
450
+ var addAtomToTimeline = (atomToken, tl, store) => {
568
451
  const atom = withdraw(atomToken, store);
569
452
  if (atom === void 0) {
570
453
  throw new Error(
@@ -744,105 +627,9 @@ var addAtomToTimeline = (atomToken, tl, store = IMPLICIT.STORE) => {
744
627
  }
745
628
  });
746
629
  };
747
- var redo__INTERNAL = (token, store = IMPLICIT.STORE) => {
748
- store.logger.info(`\u23E9`, `timeline`, token.key, `redo`);
749
- const timelineData = store.timelines.get(token.key);
750
- if (!timelineData) {
751
- store.logger.error(
752
- `\u{1F41E}`,
753
- `timeline`,
754
- token.key,
755
- `Failed to redo. This timeline has not been initialized.`
756
- );
757
- return;
758
- }
759
- if (timelineData.at === timelineData.history.length) {
760
- store.logger.warn(
761
- `\u{1F481}`,
762
- `timeline`,
763
- token.key,
764
- `Failed to redo at the end of timeline "${token.key}". There is nothing to redo.`
765
- );
766
- return;
767
- }
768
- timelineData.timeTraveling = `into_future`;
769
- const update = timelineData.history[timelineData.at];
770
- switch (update.type) {
771
- case `atom_update`: {
772
- const { key, newValue } = update;
773
- atom_io.setState({ key, type: `atom` }, newValue, store);
774
- break;
775
- }
776
- case `selector_update`:
777
- case `transaction_update`: {
778
- for (const atomUpdate of update.atomUpdates) {
779
- const { key, newValue } = atomUpdate;
780
- atom_io.setState({ key, type: `atom` }, newValue, store);
781
- }
782
- break;
783
- }
784
- }
785
- ++timelineData.at;
786
- timelineData.subject.next(`redo`);
787
- timelineData.timeTraveling = null;
788
- store.logger.info(
789
- `\u23F9\uFE0F`,
790
- `timeline`,
791
- token.key,
792
- `"${token.key}" is now at ${timelineData.at} / ${timelineData.history.length}`
793
- );
794
- };
795
- var undo__INTERNAL = (token, store = IMPLICIT.STORE) => {
796
- store.logger.info(`\u23EA`, `timeline`, token.key, `undo`);
797
- const timelineData = store.timelines.get(token.key);
798
- if (!timelineData) {
799
- store.logger.error(
800
- `\u{1F41E}`,
801
- `timeline`,
802
- token.key,
803
- `Failed to undo. This timeline has not been initialized.`
804
- );
805
- return;
806
- }
807
- if (timelineData.at === 0) {
808
- store.logger.warn(
809
- `\u{1F481}`,
810
- `timeline`,
811
- token.key,
812
- `Failed to undo at the beginning of timeline "${token.key}". There is nothing to undo.`
813
- );
814
- return;
815
- }
816
- timelineData.timeTraveling = `into_past`;
817
- --timelineData.at;
818
- const update = timelineData.history[timelineData.at];
819
- switch (update.type) {
820
- case `atom_update`: {
821
- const { key, oldValue } = update;
822
- atom_io.setState({ key, type: `atom` }, oldValue, store);
823
- break;
824
- }
825
- case `selector_update`:
826
- case `transaction_update`: {
827
- for (const atomUpdate of [...update.atomUpdates].reverse()) {
828
- const { key, oldValue } = atomUpdate;
829
- atom_io.setState({ key, type: `atom` }, oldValue, store);
830
- }
831
- break;
832
- }
833
- }
834
- timelineData.subject.next(`undo`);
835
- timelineData.timeTraveling = null;
836
- store.logger.info(
837
- `\u23F9\uFE0F`,
838
- `timeline`,
839
- token.key,
840
- `"${token.key}" is now at ${timelineData.at} / ${timelineData.history.length}`
841
- );
842
- };
843
630
 
844
- // src/timeline/timeline-internal.ts
845
- function timeline__INTERNAL(options, store = IMPLICIT.STORE, data = null) {
631
+ // src/timeline/create-timeline.ts
632
+ function createTimeline(options, store, data) {
846
633
  var _a, _b;
847
634
  const tl = __spreadProps(__spreadValues({
848
635
  type: `timeline`,
@@ -853,7 +640,7 @@ function timeline__INTERNAL(options, store = IMPLICIT.STORE, data = null) {
853
640
  transactionKey: null
854
641
  }, data), {
855
642
  history: (_a = data == null ? void 0 : data.history.map((update) => __spreadValues({}, update))) != null ? _a : [],
856
- install: (store2) => timeline__INTERNAL(options, store2, tl),
643
+ install: (store2) => createTimeline(options, store2, tl),
857
644
  subject: new Subject()
858
645
  });
859
646
  if (options.shouldCapture) {
@@ -899,96 +686,365 @@ function timeline__INTERNAL(options, store = IMPLICIT.STORE, data = null) {
899
686
  }
900
687
  addAtomToTimeline(token2, tl, store);
901
688
  }
902
- core.timelineAtoms = core.timelineAtoms.set({
903
- atomKey: tokenOrFamily.key,
904
- timelineKey: options.key
905
- });
689
+ core.timelineAtoms = core.timelineAtoms.set({
690
+ atomKey: tokenOrFamily.key,
691
+ timelineKey: options.key
692
+ });
693
+ }
694
+ store.timelines.set(options.key, tl);
695
+ const token = {
696
+ key: options.key,
697
+ type: `timeline`
698
+ };
699
+ store.subject.timelineCreation.next(token);
700
+ return token;
701
+ }
702
+ var timeTravel = (direction, token, store) => {
703
+ const action = direction === `forward` ? `redo` : `undo`;
704
+ store.logger.info(
705
+ direction === `forward` ? `\u23E9` : `\u23EA`,
706
+ `timeline`,
707
+ token.key,
708
+ action
709
+ );
710
+ const timelineData = store.timelines.get(token.key);
711
+ if (!timelineData) {
712
+ store.logger.error(
713
+ `\u{1F41E}`,
714
+ `timeline`,
715
+ token.key,
716
+ `Failed to ${action}. This timeline has not been initialized.`
717
+ );
718
+ return;
719
+ }
720
+ if (direction === `forward` && timelineData.at === timelineData.history.length || direction === `backward` && timelineData.at === 0) {
721
+ store.logger.warn(
722
+ `\u{1F481}`,
723
+ `timeline`,
724
+ token.key,
725
+ `Failed to ${action} at the ${direction === `forward` ? `end` : `beginning`} of timeline "${token.key}". There is nothing to ${action}.`
726
+ );
727
+ return;
728
+ }
729
+ timelineData.timeTraveling = direction === `forward` ? `into_future` : `into_past`;
730
+ if (direction === `backward`) {
731
+ --timelineData.at;
732
+ }
733
+ const update = timelineData.history[timelineData.at];
734
+ const updateValues = (atomUpdate) => {
735
+ const { key, newValue, oldValue } = atomUpdate;
736
+ const value = direction === `forward` ? newValue : oldValue;
737
+ setState({ key, type: `atom` }, value, store);
738
+ };
739
+ switch (update.type) {
740
+ case `atom_update`: {
741
+ updateValues(update);
742
+ break;
743
+ }
744
+ case `selector_update`:
745
+ case `transaction_update`: {
746
+ const updates = direction === `forward` ? update.atomUpdates : [...update.atomUpdates].reverse();
747
+ for (const atomUpdate of updates) {
748
+ updateValues(atomUpdate);
749
+ }
750
+ break;
751
+ }
752
+ }
753
+ if (direction === `forward`) {
754
+ ++timelineData.at;
755
+ }
756
+ timelineData.subject.next(action);
757
+ timelineData.timeTraveling = null;
758
+ store.logger.info(
759
+ `\u23F9\uFE0F`,
760
+ `timeline`,
761
+ token.key,
762
+ `"${token.key}" is now at ${timelineData.at} / ${timelineData.history.length}`
763
+ );
764
+ };
765
+
766
+ // src/store/withdraw.ts
767
+ function withdraw(token, store) {
768
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
769
+ let core = target(store);
770
+ let state = (_d = (_c = (_b = (_a = core.atoms.get(token.key)) != null ? _a : core.selectors.get(token.key)) != null ? _b : core.readonlySelectors.get(token.key)) != null ? _c : core.transactions.get(token.key)) != null ? _d : core.timelines.get(token.key);
771
+ if (state) {
772
+ return state;
773
+ }
774
+ if (store.transactionStatus.phase === `applying`) {
775
+ core = store.transactionStatus.core;
776
+ state = (_h = (_g = (_f = (_e = core.atoms.get(token.key)) != null ? _e : core.selectors.get(token.key)) != null ? _f : core.readonlySelectors.get(token.key)) != null ? _g : core.transactions.get(token.key)) != null ? _h : core.timelines.get(token.key);
777
+ if (state) {
778
+ store.logger.info(
779
+ `\u{1F6E0}\uFE0F`,
780
+ token.type,
781
+ token.key,
782
+ `add ${token.type} "${token.key}"`
783
+ );
784
+ switch (state.type) {
785
+ case `atom`: {
786
+ store.atoms.set(token.key, state);
787
+ store.valueMap.set(token.key, state.default);
788
+ const stateKey = state.key;
789
+ const familyKey = (_i = state.family) == null ? void 0 : _i.key;
790
+ let timelineKey = core.timelineAtoms.getRelatedKey(stateKey);
791
+ if (timelineKey === void 0 && typeof familyKey === `string`) {
792
+ timelineKey = core.timelineAtoms.getRelatedKey(familyKey);
793
+ }
794
+ const timeline = typeof timelineKey === `string` ? store.timelines.get(timelineKey) : void 0;
795
+ if (timeline) {
796
+ addAtomToTimeline(state, timeline, store);
797
+ }
798
+ break;
799
+ }
800
+ case `selector`:
801
+ core.selectors.set(token.key, state);
802
+ break;
803
+ case `readonly_selector`:
804
+ core.readonlySelectors.set(token.key, state);
805
+ break;
806
+ case `transaction`:
807
+ core.transactions.set(token.key, state);
808
+ break;
809
+ case `timeline`:
810
+ core.timelines.set(token.key, state);
811
+ break;
812
+ }
813
+ return state;
814
+ }
815
+ }
816
+ return void 0;
817
+ }
818
+
819
+ // src/store/withdraw-new-family-member.ts
820
+ function withdrawNewFamilyMember(token, store) {
821
+ if (token.family) {
822
+ store.logger.info(
823
+ `\u{1F46A}`,
824
+ token.type,
825
+ token.key,
826
+ `creating new family member in store "${store.config.name}"`
827
+ );
828
+ const core = target(store);
829
+ const family = core.families.get(token.family.key);
830
+ if (family) {
831
+ const jsonSubKey = JSON.parse(token.family.subKey);
832
+ family(jsonSubKey);
833
+ const state = withdraw(token, store);
834
+ return state;
835
+ }
836
+ }
837
+ return void 0;
838
+ }
839
+
840
+ // src/transaction/apply-transaction.ts
841
+ var applyTransaction = (output, store) => {
842
+ if (store.transactionStatus.phase !== `building`) {
843
+ store.logger.warn(
844
+ `\u{1F41E}`,
845
+ `transaction`,
846
+ `???`,
847
+ `applyTransaction called outside of a transaction. This is probably a bug in AtomIO.`
848
+ );
849
+ return;
850
+ }
851
+ store.transactionStatus.phase = `applying`;
852
+ store.transactionStatus.output = output;
853
+ const { atomUpdates } = store.transactionStatus;
854
+ store.logger.info(
855
+ `\u{1F6C4}`,
856
+ `transaction`,
857
+ store.transactionStatus.key,
858
+ `Applying transaction with ${atomUpdates.length} updates:`,
859
+ atomUpdates
860
+ );
861
+ for (const { key, newValue } of atomUpdates) {
862
+ const token = { key, type: `atom` };
863
+ if (!store.valueMap.has(token.key)) {
864
+ if (token.family) {
865
+ const family = store.families.get(token.family.key);
866
+ if (family) {
867
+ family(token.family.subKey);
868
+ }
869
+ } else {
870
+ const newAtom = store.transactionStatus.core.atoms.get(token.key);
871
+ if (!newAtom) {
872
+ throw new Error(
873
+ `Absurd Error: Atom "${token.key}" not found while copying updates from transaction "${store.transactionStatus.key}" to store "${store.config.name}"`
874
+ );
875
+ }
876
+ store.atoms.set(newAtom.key, newAtom);
877
+ store.valueMap.set(newAtom.key, newAtom.default);
878
+ store.logger.info(
879
+ `\u{1F528}`,
880
+ `transaction`,
881
+ store.transactionStatus.key,
882
+ `Adding atom "${newAtom.key}"`
883
+ );
884
+ }
885
+ }
886
+ setState(token, newValue, store);
887
+ }
888
+ const myTransaction = withdraw(
889
+ { key: store.transactionStatus.key, type: `transaction` },
890
+ store
891
+ );
892
+ if (myTransaction === void 0) {
893
+ throw new Error(
894
+ `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`
895
+ );
896
+ }
897
+ myTransaction.subject.next({
898
+ key: store.transactionStatus.key,
899
+ atomUpdates,
900
+ output,
901
+ params: store.transactionStatus.params
902
+ });
903
+ store.logger.info(
904
+ `\u{1F6EC}`,
905
+ `transaction`,
906
+ store.transactionStatus.key,
907
+ `Finished applying transaction.`
908
+ );
909
+ store.transactionStatus = { phase: `idle` };
910
+ };
911
+
912
+ // src/lazy-map.ts
913
+ var LazyMap = class extends Map {
914
+ constructor(source) {
915
+ super();
916
+ this.source = source;
917
+ this.deleted = /* @__PURE__ */ new Set();
918
+ }
919
+ get(key) {
920
+ const has = super.has(key);
921
+ if (has) {
922
+ return super.get(key);
923
+ }
924
+ if (!this.deleted.has(key) && this.source.has(key)) {
925
+ const value = this.source.get(key);
926
+ super.set(key, value);
927
+ return value;
928
+ }
929
+ return void 0;
906
930
  }
907
- store.timelines.set(options.key, tl);
908
- const token = {
931
+ _has(key) {
932
+ return super.has(key);
933
+ }
934
+ has(key) {
935
+ return !this.deleted.has(key) && (super.has(key) || this.source.has(key));
936
+ }
937
+ delete(key) {
938
+ this.deleted.add(key);
939
+ return super.delete(key);
940
+ }
941
+ };
942
+
943
+ // src/transaction/build-transaction.ts
944
+ var buildTransaction = (key, params, store) => {
945
+ store.transactionStatus = {
946
+ key,
947
+ phase: `building`,
948
+ time: Date.now(),
949
+ core: {
950
+ atoms: new LazyMap(store.atoms),
951
+ atomsThatAreDefault: new Set(store.atomsThatAreDefault),
952
+ families: new LazyMap(store.families),
953
+ operation: { open: false },
954
+ readonlySelectors: new LazyMap(store.readonlySelectors),
955
+ timelines: new LazyMap(store.timelines),
956
+ timelineAtoms: new Junction(store.timelineAtoms.toJSON()),
957
+ trackers: /* @__PURE__ */ new Map(),
958
+ transactions: new LazyMap(store.transactions),
959
+ selectorAtoms: new Junction(store.selectorAtoms.toJSON()),
960
+ selectorGraph: new Junction(store.selectorGraph.toJSON(), {
961
+ makeContentKey: (...keys) => keys.sort().join(`:`)
962
+ }),
963
+ selectors: new LazyMap(store.selectors),
964
+ valueMap: new LazyMap(store.valueMap)
965
+ },
966
+ atomUpdates: [],
967
+ params,
968
+ output: void 0
969
+ };
970
+ store.logger.info(
971
+ `\u{1F6EB}`,
972
+ `transaction`,
973
+ key,
974
+ `Building transaction with params:`,
975
+ params
976
+ );
977
+ };
978
+ function createTransaction(options, store) {
979
+ const newTransaction = {
909
980
  key: options.key,
910
- type: `timeline`
981
+ type: `transaction`,
982
+ run: (...params) => {
983
+ buildTransaction(options.key, params, store);
984
+ try {
985
+ const output = options.do(
986
+ {
987
+ get: (token2) => getState(token2, store),
988
+ set: (token2, value) => setState(token2, value, store)
989
+ },
990
+ ...params
991
+ );
992
+ applyTransaction(output, store);
993
+ return output;
994
+ } catch (thrown) {
995
+ abortTransaction(store);
996
+ store.logger.warn(`\u{1F4A5}`, `transaction`, options.key, `caught:`, thrown);
997
+ throw thrown;
998
+ }
999
+ },
1000
+ install: (store2) => createTransaction(options, store2),
1001
+ subject: new Subject()
911
1002
  };
912
- store.subject.timelineCreation.next(token);
1003
+ const core = target(store);
1004
+ core.transactions.set(newTransaction.key, newTransaction);
1005
+ const token = deposit(newTransaction);
1006
+ store.subject.transactionCreation.next(token);
913
1007
  return token;
914
1008
  }
915
-
916
- // src/store/withdraw.ts
917
- function withdraw(token, store) {
918
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
919
- let core = target(store);
920
- let state = (_d = (_c = (_b = (_a = core.atoms.get(token.key)) != null ? _a : core.selectors.get(token.key)) != null ? _b : core.readonlySelectors.get(token.key)) != null ? _c : core.transactions.get(token.key)) != null ? _d : core.timelines.get(token.key);
921
- if (state) {
922
- return state;
923
- }
924
- if (store.transactionStatus.phase === `applying`) {
925
- core = store.transactionStatus.core;
926
- state = (_h = (_g = (_f = (_e = core.atoms.get(token.key)) != null ? _e : core.selectors.get(token.key)) != null ? _f : core.readonlySelectors.get(token.key)) != null ? _g : core.transactions.get(token.key)) != null ? _h : core.timelines.get(token.key);
927
- if (state) {
928
- store.logger.info(
929
- `\u{1F6E0}\uFE0F`,
930
- token.type,
931
- token.key,
932
- `add ${token.type} "${token.key}"`
1009
+ var target = (store) => store.transactionStatus.phase === `building` ? store.transactionStatus.core : store;
1010
+ var redoTransactionUpdate = (update, store) => {
1011
+ store.logger.info(`\u23ED\uFE0F`, `transaction`, update.key, `Redo`);
1012
+ for (const { key, newValue } of update.atomUpdates) {
1013
+ const token = { key, type: `atom` };
1014
+ const state = withdraw(token, store);
1015
+ if (state === void 0) {
1016
+ throw new Error(
1017
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
933
1018
  );
934
- switch (state.type) {
935
- case `atom`: {
936
- store.atoms.set(token.key, state);
937
- store.valueMap.set(token.key, state.default);
938
- const stateKey = state.key;
939
- const familyKey = (_i = state.family) == null ? void 0 : _i.key;
940
- let timelineKey = core.timelineAtoms.getRelatedKey(stateKey);
941
- if (timelineKey === void 0 && typeof familyKey === `string`) {
942
- timelineKey = core.timelineAtoms.getRelatedKey(familyKey);
943
- }
944
- const timeline = typeof timelineKey === `string` ? store.timelines.get(timelineKey) : void 0;
945
- if (timeline) {
946
- addAtomToTimeline(state, timeline, store);
947
- }
948
- break;
949
- }
950
- case `selector`:
951
- core.selectors.set(token.key, state);
952
- break;
953
- case `readonly_selector`:
954
- core.readonlySelectors.set(token.key, state);
955
- break;
956
- case `transaction`:
957
- core.transactions.set(token.key, state);
958
- break;
959
- case `timeline`:
960
- core.timelines.set(token.key, state);
961
- break;
962
- }
963
- return state;
964
1019
  }
1020
+ setState(state, newValue, store);
965
1021
  }
966
- return void 0;
967
- }
968
-
969
- // src/store/withdraw-new-family-member.ts
970
- function withdrawNewFamilyMember(token, store) {
1022
+ };
1023
+ var undoTransactionUpdate = (update, store) => {
971
1024
  store.logger.info(
972
- `\u{1F46A}`,
973
- token.type,
974
- token.key,
975
- `creating new family member in store "${store.config.name}"`
1025
+ `\u23EE\uFE0F`,
1026
+ `transaction`,
1027
+ update.key,
1028
+ `Undoing transaction update`,
1029
+ update
976
1030
  );
977
- if (token.family) {
978
- const core = target(store);
979
- const family = core.families.get(token.family.key);
980
- if (family) {
981
- const jsonSubKey = JSON.parse(token.family.subKey);
982
- family(jsonSubKey);
983
- const state = withdraw(token, store);
984
- return state;
1031
+ for (const { key, oldValue } of update.atomUpdates) {
1032
+ const token = { key, type: `atom` };
1033
+ const state = withdraw(token, store);
1034
+ if (state === void 0) {
1035
+ throw new Error(
1036
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
1037
+ );
985
1038
  }
1039
+ setState(state, oldValue, store);
986
1040
  }
987
- return void 0;
988
- }
1041
+ };
1042
+
1043
+ // src/transaction/index.ts
1044
+ var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
989
1045
 
990
1046
  // src/caching.ts
991
- var cacheValue = (key, value, subject, store = IMPLICIT.STORE) => {
1047
+ function cacheValue(key, value, subject, store) {
992
1048
  const currentValue = target(store).valueMap.get(key);
993
1049
  if (currentValue instanceof Future) {
994
1050
  currentValue.cancel();
@@ -996,24 +1052,25 @@ var cacheValue = (key, value, subject, store = IMPLICIT.STORE) => {
996
1052
  if (value instanceof Promise) {
997
1053
  const future = new Future(value);
998
1054
  target(store).valueMap.set(key, future);
999
- future.then((value2) => {
1055
+ future.then((resolved) => {
1000
1056
  if (future.isCanceled) {
1001
1057
  return;
1002
1058
  }
1003
- cacheValue(key, value2, subject, store);
1004
- subject.next({ newValue: value2, oldValue: value2 });
1059
+ cacheValue(key, resolved, subject, store);
1060
+ subject.next({ newValue: resolved, oldValue: future });
1005
1061
  }).catch((thrown) => {
1006
1062
  if (thrown !== `canceled`) {
1007
1063
  store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
1008
1064
  }
1009
1065
  });
1010
- } else {
1011
- target(store).valueMap.set(key, value);
1066
+ return future;
1012
1067
  }
1013
- };
1014
- var readCachedValue = (key, store = IMPLICIT.STORE) => target(store).valueMap.get(key);
1015
- var isValueCached = (key, store = IMPLICIT.STORE) => target(store).valueMap.has(key);
1016
- var evictCachedValue = (key, store = IMPLICIT.STORE) => {
1068
+ target(store).valueMap.set(key, value);
1069
+ return value;
1070
+ }
1071
+ var readCachedValue = (key, store) => target(store).valueMap.get(key);
1072
+ var isValueCached = (key, store) => target(store).valueMap.has(key);
1073
+ var evictCachedValue = (key, store) => {
1017
1074
  const core = target(store);
1018
1075
  const currentValue = core.valueMap.get(key);
1019
1076
  if (currentValue instanceof Future) {
@@ -1026,7 +1083,7 @@ var evictCachedValue = (key, store = IMPLICIT.STORE) => {
1026
1083
  store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
1027
1084
  };
1028
1085
  var Tracker = class {
1029
- constructor(mutableState, store = IMPLICIT.STORE) {
1086
+ constructor(mutableState, store) {
1030
1087
  this.unsubscribeFromInnerValue = null;
1031
1088
  this.mutableState = mutableState;
1032
1089
  this.latestUpdateState = this.initializeState(mutableState, store);
@@ -1035,7 +1092,7 @@ var Tracker = class {
1035
1092
  const core = target(store);
1036
1093
  core.trackers.set(mutableState.key, this);
1037
1094
  }
1038
- initializeState(mutableState, store = IMPLICIT.STORE) {
1095
+ initializeState(mutableState, store) {
1039
1096
  const latestUpdateStateKey = `*${mutableState.key}`;
1040
1097
  deleteAtom({ type: `atom`, key: latestUpdateStateKey }, store);
1041
1098
  const familyMetaData = mutableState.family ? {
@@ -1052,8 +1109,8 @@ var Tracker = class {
1052
1109
  );
1053
1110
  return latestUpdateState;
1054
1111
  }
1055
- observeCore(mutableState, latestUpdateState, store = IMPLICIT.STORE) {
1056
- const originalInnerValue = atom_io.getState(mutableState, store);
1112
+ observeCore(mutableState, latestUpdateState, store) {
1113
+ const originalInnerValue = getState(mutableState, store);
1057
1114
  this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
1058
1115
  `tracker:${store.config.name}:${store.transactionStatus.phase === `idle` ? `main` : store.transactionStatus.key}`,
1059
1116
  (update) => {
@@ -1061,12 +1118,12 @@ var Tracker = class {
1061
1118
  mutableState.key,
1062
1119
  () => {
1063
1120
  unsubscribe();
1064
- atom_io.setState(latestUpdateState, update, store);
1121
+ setState(latestUpdateState, update, store);
1065
1122
  }
1066
1123
  );
1067
1124
  }
1068
1125
  );
1069
- atom_io.subscribe(
1126
+ subscribe(
1070
1127
  mutableState,
1071
1128
  (update) => {
1072
1129
  var _a;
@@ -1079,7 +1136,7 @@ var Tracker = class {
1079
1136
  mutableState.key,
1080
1137
  () => {
1081
1138
  unsubscribe();
1082
- atom_io.setState(latestUpdateState, update2, store);
1139
+ setState(latestUpdateState, update2, store);
1083
1140
  }
1084
1141
  );
1085
1142
  }
@@ -1090,8 +1147,8 @@ var Tracker = class {
1090
1147
  store
1091
1148
  );
1092
1149
  }
1093
- updateCore(mutableState, latestUpdateState, store = IMPLICIT.STORE) {
1094
- atom_io.subscribe(
1150
+ updateCore(mutableState, latestUpdateState, store) {
1151
+ subscribe(
1095
1152
  latestUpdateState,
1096
1153
  ({ newValue, oldValue }) => {
1097
1154
  const timelineId = store.timelineAtoms.getRelatedKey(
@@ -1100,11 +1157,11 @@ var Tracker = class {
1100
1157
  if (timelineId) {
1101
1158
  const timelineData = store.timelines.get(timelineId);
1102
1159
  if (timelineData == null ? void 0 : timelineData.timeTraveling) {
1103
- const unsubscribe2 = atom_io.subscribeToTimeline(
1160
+ const unsubscribe2 = subscribeToTimeline(
1104
1161
  { key: timelineId, type: `timeline` },
1105
1162
  (update) => {
1106
1163
  unsubscribe2();
1107
- atom_io.setState(
1164
+ setState(
1108
1165
  mutableState,
1109
1166
  (transceiver) => {
1110
1167
  if (update === `redo` && newValue) {
@@ -1125,8 +1182,11 @@ var Tracker = class {
1125
1182
  latestUpdateState.key,
1126
1183
  () => {
1127
1184
  unsubscribe();
1128
- if (newValue) {
1129
- atom_io.setState(
1185
+ const mutable = getState(mutableState, store);
1186
+ const updateNumber = mutable.getUpdateNumber(newValue);
1187
+ const eventOffset = updateNumber - mutable.cacheUpdateNumber;
1188
+ if (newValue && eventOffset === 1) {
1189
+ setState(
1130
1190
  mutableState,
1131
1191
  (transceiver) => (transceiver.do(newValue), transceiver),
1132
1192
  store
@@ -1142,7 +1202,7 @@ var Tracker = class {
1142
1202
  };
1143
1203
 
1144
1204
  // src/mutable/create-mutable-atom.ts
1145
- function createMutableAtom(options, store = IMPLICIT.STORE) {
1205
+ function createMutableAtom(options, store) {
1146
1206
  store.logger.info(
1147
1207
  `\u{1F527}`,
1148
1208
  `atom`,
@@ -1151,8 +1211,8 @@ function createMutableAtom(options, store = IMPLICIT.STORE) {
1151
1211
  );
1152
1212
  const coreState = createAtom(options, void 0, store);
1153
1213
  new Tracker(coreState, store);
1154
- const jsonState = json.selectJson(coreState, options, store);
1155
- atom_io.subscribe(
1214
+ const jsonState = selectJson(coreState, options, store);
1215
+ subscribe(
1156
1216
  jsonState,
1157
1217
  () => {
1158
1218
  const trackerHasBeenInitialized = target(store).trackers.has(coreState.key);
@@ -1164,11 +1224,11 @@ function createMutableAtom(options, store = IMPLICIT.STORE) {
1164
1224
  );
1165
1225
  return coreState;
1166
1226
  }
1167
- function createAtomFamily(options, store = IMPLICIT.STORE) {
1227
+ function createAtomFamily(options, store) {
1168
1228
  const subject = new Subject();
1169
1229
  const atomFamily = Object.assign(
1170
1230
  (key) => {
1171
- const subKey = json.stringifyJson(key);
1231
+ const subKey = stringifyJson(key);
1172
1232
  const family = { key: options.key, subKey };
1173
1233
  const fullKey = `${options.key}(${subKey})`;
1174
1234
  const existing = withdraw({ key: fullKey, type: `atom` }, store);
@@ -1199,37 +1259,6 @@ function createAtomFamily(options, store = IMPLICIT.STORE) {
1199
1259
  return atomFamily;
1200
1260
  }
1201
1261
 
1202
- // src/keys.ts
1203
- var isAtomKey = (key, store) => target(store).atoms.has(key);
1204
- var isSelectorKey = (key, store) => target(store).selectors.has(key);
1205
- var isReadonlySelectorKey = (key, store) => target(store).readonlySelectors.has(key);
1206
- var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
1207
-
1208
- // src/selector/get-selector-dependency-keys.ts
1209
- var getSelectorDependencyKeys = (key, store) => {
1210
- const sources = target(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
1211
- return sources;
1212
- };
1213
-
1214
- // src/get-state-internal.ts
1215
- var getState__INTERNAL = (state, store = IMPLICIT.STORE) => {
1216
- if (isValueCached(state.key, store)) {
1217
- store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
1218
- return readCachedValue(state.key, store);
1219
- }
1220
- if (state.type !== `atom`) {
1221
- store.logger.info(`\u{1F9EE}`, state.type, state.key, `calculating value`);
1222
- return state.get();
1223
- }
1224
- store.logger.error(`\u{1F41E}`, `atom`, state.key, `could not find cached value`);
1225
- return state.default;
1226
- };
1227
-
1228
- // src/set-state/become.ts
1229
- var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
1230
- originalThing instanceof Function ? originalThing() : originalThing
1231
- ) : nextVersionOfThing;
1232
-
1233
1262
  // src/operation.ts
1234
1263
  var openOperation = (token, store) => {
1235
1264
  const core = target(store);
@@ -1269,7 +1298,7 @@ var closeOperation = (store) => {
1269
1298
  core.operation = { open: false };
1270
1299
  store.subject.operationStatus.next(core.operation);
1271
1300
  };
1272
- var isDone = (key, store = IMPLICIT.STORE) => {
1301
+ var isDone = (key, store) => {
1273
1302
  const core = target(store);
1274
1303
  if (!core.operation.open) {
1275
1304
  store.logger.warn(
@@ -1282,7 +1311,7 @@ var isDone = (key, store = IMPLICIT.STORE) => {
1282
1311
  }
1283
1312
  return core.operation.done.has(key);
1284
1313
  };
1285
- var markDone = (key, store = IMPLICIT.STORE) => {
1314
+ var markDone = (key, store) => {
1286
1315
  const core = target(store);
1287
1316
  if (!core.operation.open) {
1288
1317
  store.logger.warn(
@@ -1296,6 +1325,32 @@ var markDone = (key, store = IMPLICIT.STORE) => {
1296
1325
  core.operation.done.add(key);
1297
1326
  };
1298
1327
 
1328
+ // src/set-state/become.ts
1329
+ var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
1330
+ originalThing instanceof Function ? originalThing() : originalThing
1331
+ ) : nextVersionOfThing;
1332
+
1333
+ // src/read-or-compute-value.ts
1334
+ var readOrComputeValue = (state, store) => {
1335
+ if (isValueCached(state.key, store)) {
1336
+ store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
1337
+ return readCachedValue(state.key, store);
1338
+ }
1339
+ if (state.type !== `atom`) {
1340
+ store.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
1341
+ return state.get();
1342
+ }
1343
+ const fallback = state.default instanceof Function ? state.default() : state.default;
1344
+ store.logger.info(
1345
+ `\u{1F481}`,
1346
+ `atom`,
1347
+ state.key,
1348
+ `could not find cached value; using default`,
1349
+ fallback
1350
+ );
1351
+ return state.default instanceof Function ? state.default() : state.default;
1352
+ };
1353
+
1299
1354
  // src/set-state/copy-mutable-if-needed.ts
1300
1355
  function copyMutableIfNeeded(atom, transform, origin, target2) {
1301
1356
  const originValue = origin.valueMap.get(atom.key);
@@ -1311,7 +1366,7 @@ function copyMutableIfNeeded(atom, transform, origin, target2) {
1311
1366
  }
1312
1367
 
1313
1368
  // src/set-state/copy-mutable-in-transaction.ts
1314
- function copyMutableIfWithinTransaction(atom, store) {
1369
+ function copyMutableIfWithinTransaction(oldValue, atom, store) {
1315
1370
  if (store.transactionStatus.phase === `building` || store.transactionStatus.phase === `applying`) {
1316
1371
  if (`toJson` in atom && `fromJson` in atom) {
1317
1372
  const copiedValue = copyMutableIfNeeded(
@@ -1337,7 +1392,7 @@ function copyMutableIfWithinTransaction(atom, store) {
1337
1392
  }
1338
1393
  }
1339
1394
  }
1340
- return getState__INTERNAL(atom, store);
1395
+ return oldValue;
1341
1396
  }
1342
1397
  function copyMutableFamilyMemberWithinTransaction(atom, family, origin, target2) {
1343
1398
  if (`toJson` in family && `fromJson` in family) {
@@ -1349,8 +1404,7 @@ function copyMutableFamilyMemberWithinTransaction(atom, family, origin, target2)
1349
1404
 
1350
1405
  // src/set-state/emit-update.ts
1351
1406
  var emitUpdate = (state, update, store) => {
1352
- const { logger } = store;
1353
- logger.info(
1407
+ store.logger.info(
1354
1408
  `\u{1F4E2}`,
1355
1409
  state.type,
1356
1410
  state.key,
@@ -1365,18 +1419,17 @@ var emitUpdate = (state, update, store) => {
1365
1419
  };
1366
1420
 
1367
1421
  // src/set-state/evict-downstream.ts
1368
- var evictDownStream = (atom, store = IMPLICIT.STORE) => {
1369
- var _a;
1422
+ var evictDownStream = (atom, store) => {
1370
1423
  const core = target(store);
1371
1424
  const downstreamKeys = core.selectorAtoms.getRelatedKeys(atom.key);
1372
1425
  store.logger.info(
1373
1426
  `\u{1F9F9}`,
1374
1427
  atom.type,
1375
1428
  atom.key,
1376
- `evicting ${(_a = downstreamKeys == null ? void 0 : downstreamKeys.size) != null ? _a : 0} states downstream:`,
1377
- downstreamKeys
1429
+ downstreamKeys ? `evicting ${downstreamKeys.size} states downstream:` : `no downstream states`,
1430
+ downstreamKeys != null ? downstreamKeys : `to evict`
1378
1431
  );
1379
- if (downstreamKeys !== void 0) {
1432
+ if (downstreamKeys) {
1380
1433
  if (core.operation.open) {
1381
1434
  store.logger.info(
1382
1435
  `\u{1F9F9}`,
@@ -1438,12 +1491,12 @@ var stowUpdate = (state, update, store) => {
1438
1491
  };
1439
1492
 
1440
1493
  // src/set-state/set-atom.ts
1441
- var setAtom = (atom, next, store = IMPLICIT.STORE) => {
1442
- const oldValue = getState__INTERNAL(atom, store);
1443
- let newValue = copyMutableIfWithinTransaction(atom, store);
1494
+ var setAtom = (atom, next, store) => {
1495
+ const oldValue = readOrComputeValue(atom, store);
1496
+ let newValue = copyMutableIfWithinTransaction(oldValue, atom, store);
1444
1497
  newValue = become(next)(newValue);
1445
1498
  store.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
1446
- cacheValue(atom.key, newValue, atom.subject, store);
1499
+ newValue = cacheValue(atom.key, newValue, atom.subject, store);
1447
1500
  if (isAtomDefault(atom.key, store)) {
1448
1501
  markAtomAsNotDefault(atom.key, store);
1449
1502
  }
@@ -1457,8 +1510,8 @@ var setAtom = (atom, next, store = IMPLICIT.STORE) => {
1457
1510
  }
1458
1511
  };
1459
1512
 
1460
- // src/set-state/set-state-internal.ts
1461
- var setState__INTERNAL = (state, value, store = IMPLICIT.STORE) => {
1513
+ // src/set-state/set-atom-or-selector.ts
1514
+ var setAtomOrSelector = (state, value, store) => {
1462
1515
  if (state.type === `selector`) {
1463
1516
  state.set(value);
1464
1517
  } else {
@@ -1466,6 +1519,18 @@ var setState__INTERNAL = (state, value, store = IMPLICIT.STORE) => {
1466
1519
  }
1467
1520
  };
1468
1521
 
1522
+ // src/keys.ts
1523
+ var isAtomKey = (key, store) => target(store).atoms.has(key);
1524
+ var isSelectorKey = (key, store) => target(store).selectors.has(key);
1525
+ var isReadonlySelectorKey = (key, store) => target(store).readonlySelectors.has(key);
1526
+ var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
1527
+
1528
+ // src/selector/get-selector-dependency-keys.ts
1529
+ var getSelectorDependencyKeys = (key, store) => {
1530
+ const sources = target(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
1531
+ return sources;
1532
+ };
1533
+
1469
1534
  // src/selector/trace-selector-atoms.ts
1470
1535
  var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
1471
1536
  const rootKeys = [];
@@ -1531,7 +1596,7 @@ var updateSelectorAtoms = (selectorKey, dependency, store) => {
1531
1596
  };
1532
1597
 
1533
1598
  // src/selector/register-selector.ts
1534
- var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1599
+ var registerSelector = (selectorKey, store) => ({
1535
1600
  get: (dependency) => {
1536
1601
  const core = target(store);
1537
1602
  const alreadyRegistered = core.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
@@ -1541,7 +1606,7 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1541
1606
  `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1542
1607
  );
1543
1608
  }
1544
- const dependencyValue = getState__INTERNAL(dependencyState, store);
1609
+ const dependencyValue = readOrComputeValue(dependencyState, store);
1545
1610
  store.logger.info(
1546
1611
  `\u{1F50C}`,
1547
1612
  `selector`,
@@ -1571,7 +1636,7 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1571
1636
  `State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1572
1637
  );
1573
1638
  }
1574
- setState__INTERNAL(state, newValue, store);
1639
+ setAtomOrSelector(state, newValue, store);
1575
1640
  }
1576
1641
  });
1577
1642
 
@@ -1661,7 +1726,7 @@ var createReadonlySelector = (options, family, store, core) => {
1661
1726
  };
1662
1727
 
1663
1728
  // src/selector/create-selector.ts
1664
- function createSelector(options, family, store = IMPLICIT.STORE) {
1729
+ function createSelector(options, family, store) {
1665
1730
  const core = target(store);
1666
1731
  const existingWritable = core.selectors.get(options.key);
1667
1732
  const existingReadonly = core.readonlySelectors.get(options.key);
@@ -1679,13 +1744,42 @@ function createSelector(options, family, store = IMPLICIT.STORE) {
1679
1744
  return createReadonlySelector(options, family, store, core);
1680
1745
  }
1681
1746
 
1747
+ // src/selector/delete-selector.ts
1748
+ function deleteSelector(selectorToken, store) {
1749
+ const core = target(store);
1750
+ const { key } = selectorToken;
1751
+ switch (selectorToken.type) {
1752
+ case `selector`:
1753
+ core.selectors.delete(key);
1754
+ break;
1755
+ case `readonly_selector`:
1756
+ core.readonlySelectors.delete(key);
1757
+ break;
1758
+ }
1759
+ core.valueMap.delete(key);
1760
+ core.selectorAtoms.delete(key);
1761
+ const downstreamTokens = core.selectorGraph.getRelationEntries({ upstreamSelectorKey: key }).filter(([_, { source }]) => source === key).map(
1762
+ ([downstreamSelectorKey]) => {
1763
+ var _a;
1764
+ return (_a = core.selectors.get(downstreamSelectorKey)) != null ? _a : core.readonlySelectors.get(downstreamSelectorKey);
1765
+ }
1766
+ );
1767
+ for (const downstreamToken of downstreamTokens) {
1768
+ if (downstreamToken) {
1769
+ deleteSelector(downstreamToken, store);
1770
+ }
1771
+ }
1772
+ core.selectorGraph.delete(key);
1773
+ store.logger.info(`\u{1F525}`, selectorToken.type, `${key}`, `deleted`);
1774
+ }
1775
+
1682
1776
  // src/families/create-readonly-selector-family.ts
1683
1777
  function createReadonlySelectorFamily(options, store) {
1684
1778
  const core = target(store);
1685
1779
  const subject = new Subject();
1686
1780
  return Object.assign(
1687
1781
  (key) => {
1688
- const subKey = json.stringifyJson(key);
1782
+ const subKey = stringifyJson(key);
1689
1783
  const family = { key: options.key, subKey };
1690
1784
  const fullKey = `${options.key}(${subKey})`;
1691
1785
  const existing = core.readonlySelectors.get(fullKey);
@@ -1708,7 +1802,7 @@ function createReadonlySelectorFamily(options, store) {
1708
1802
  }
1709
1803
  );
1710
1804
  }
1711
- function createSelectorFamily(options, store = IMPLICIT.STORE) {
1805
+ function createSelectorFamily(options, store) {
1712
1806
  const isReadonly = !(`set` in options);
1713
1807
  if (isReadonly) {
1714
1808
  return createReadonlySelectorFamily(options, store);
@@ -1717,7 +1811,7 @@ function createSelectorFamily(options, store = IMPLICIT.STORE) {
1717
1811
  const subject = new Subject();
1718
1812
  const selectorFamily = Object.assign(
1719
1813
  (key) => {
1720
- const subKey = json.stringifyJson(key);
1814
+ const subKey = stringifyJson(key);
1721
1815
  const family = { key: options.key, subKey };
1722
1816
  const fullKey = `${options.key}(${subKey})`;
1723
1817
  const existing = core.selectors.get(fullKey);
@@ -1747,7 +1841,7 @@ function createSelectorFamily(options, store = IMPLICIT.STORE) {
1747
1841
 
1748
1842
  // src/mutable/tracker-family.ts
1749
1843
  var FamilyTracker = class {
1750
- constructor(findMutableState, store = IMPLICIT.STORE) {
1844
+ constructor(findMutableState, store) {
1751
1845
  this.findLatestUpdateState = createAtomFamily(
1752
1846
  {
1753
1847
  key: `*${findMutableState.key}`,
@@ -1760,7 +1854,7 @@ var FamilyTracker = class {
1760
1854
  `store=${store.config.name}::tracker-atom-family`,
1761
1855
  (atomToken) => {
1762
1856
  if (atomToken.family) {
1763
- const key = json.parseJson(atomToken.family.subKey);
1857
+ const key = parseJson(atomToken.family.subKey);
1764
1858
  this.findLatestUpdateState(key);
1765
1859
  new Tracker(atomToken, store);
1766
1860
  }
@@ -1770,7 +1864,7 @@ var FamilyTracker = class {
1770
1864
  `store=${store.config.name}::tracker-atom-family`,
1771
1865
  (atomToken) => {
1772
1866
  if (atomToken.family) {
1773
- const key = json.parseJson(atomToken.family.subKey);
1867
+ const key = parseJson(atomToken.family.subKey);
1774
1868
  const mutableAtomToken = this.findMutableState(key);
1775
1869
  new Tracker(mutableAtomToken, store);
1776
1870
  }
@@ -1780,16 +1874,26 @@ var FamilyTracker = class {
1780
1874
  };
1781
1875
 
1782
1876
  // src/mutable/create-mutable-atom-family.ts
1783
- function createMutableAtomFamily(options, store = IMPLICIT.STORE) {
1877
+ function createMutableAtomFamily(options, store) {
1784
1878
  const coreFamily = Object.assign(
1785
1879
  createAtomFamily(options, store),
1786
1880
  options
1787
1881
  );
1788
- json.selectJsonFamily(coreFamily, options);
1882
+ selectJsonFamily(coreFamily, options);
1789
1883
  new FamilyTracker(coreFamily, store);
1790
1884
  return coreFamily;
1791
1885
  }
1792
1886
 
1887
+ // src/mutable/get-json-family.ts
1888
+ var getJsonFamily = (mutableAtomFamily, store) => {
1889
+ const core = target(store);
1890
+ const key = `${mutableAtomFamily.key}:JSON`;
1891
+ const jsonFamily = core.families.get(
1892
+ key
1893
+ );
1894
+ return jsonFamily;
1895
+ };
1896
+
1793
1897
  // src/mutable/get-json-token.ts
1794
1898
  var getJsonToken = (mutableAtomToken) => {
1795
1899
  const key = mutableAtomToken.family ? `${mutableAtomToken.family.key}:JSON(${mutableAtomToken.family.subKey})` : `${mutableAtomToken.key}:JSON`;
@@ -1830,27 +1934,26 @@ function isTransceiver(value) {
1830
1934
  var isAtomMutable = (atom) => `isMutable` in atom;
1831
1935
 
1832
1936
  // src/atom/is-default.ts
1833
- var isAtomDefault = (key, store = IMPLICIT.STORE) => {
1937
+ var isAtomDefault = (key, store) => {
1834
1938
  const core = target(store);
1835
1939
  return core.atomsThatAreDefault.has(key);
1836
1940
  };
1837
- var markAtomAsDefault = (key, store = IMPLICIT.STORE) => {
1941
+ var markAtomAsDefault = (key, store) => {
1838
1942
  const core = target(store);
1839
1943
  core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key);
1840
1944
  };
1841
- var markAtomAsNotDefault = (key, store = IMPLICIT.STORE) => {
1945
+ var markAtomAsNotDefault = (key, store) => {
1842
1946
  const core = target(store);
1843
1947
  core.atomsThatAreDefault = new Set(target(store).atomsThatAreDefault);
1844
1948
  core.atomsThatAreDefault.delete(key);
1845
1949
  };
1846
- var isSelectorDefault = (key, store = IMPLICIT.STORE) => {
1950
+ var isSelectorDefault = (key, store) => {
1847
1951
  const rootKeys = traceAllSelectorAtoms(key, store);
1848
1952
  return rootKeys.every((rootKey) => isAtomDefault(rootKey, store));
1849
1953
  };
1850
1954
 
1851
1955
  // src/atom/create-atom.ts
1852
- function createAtom(options, family, store = IMPLICIT.STORE) {
1853
- var _a;
1956
+ function createAtom(options, family, store) {
1854
1957
  store.logger.info(
1855
1958
  `\u{1F528}`,
1856
1959
  `atom`,
@@ -1883,35 +1986,87 @@ function createAtom(options, family, store = IMPLICIT.STORE) {
1883
1986
  },
1884
1987
  subject
1885
1988
  }), family && { family });
1886
- const initialValue = options.default instanceof Function ? options.default() : options.default;
1989
+ let initialValue = options.default;
1990
+ if (options.default instanceof Function) {
1991
+ initialValue = options.default();
1992
+ }
1887
1993
  core.atoms.set(newAtom.key, newAtom);
1888
1994
  markAtomAsDefault(options.key, store);
1889
1995
  cacheValue(options.key, initialValue, subject, store);
1890
1996
  const token = deposit(newAtom);
1891
- for (const effect of (_a = options.effects) != null ? _a : []) {
1892
- effect({
1893
- setSelf: (next) => atom_io.setState(token, next, store),
1894
- onSet: (handle) => atom_io.subscribe(token, handle, `effect[${subject.subscribers.size}]`, store)
1895
- });
1997
+ if (options.effects) {
1998
+ let effectIndex = 0;
1999
+ const cleanupFunctions = [];
2000
+ for (const effect of options.effects) {
2001
+ const cleanup = effect({
2002
+ setSelf: (next) => setState(token, next, store),
2003
+ onSet: (handle) => subscribe(token, handle, `effect[${effectIndex}]`, store)
2004
+ });
2005
+ if (cleanup) {
2006
+ cleanupFunctions.push(cleanup);
2007
+ }
2008
+ ++effectIndex;
2009
+ }
2010
+ newAtom.cleanup = () => {
2011
+ for (const cleanup of cleanupFunctions) {
2012
+ cleanup();
2013
+ }
2014
+ };
1896
2015
  }
1897
2016
  store.subject.atomCreation.next(token);
1898
2017
  return token;
1899
2018
  }
1900
2019
 
1901
2020
  // src/atom/delete-atom.ts
1902
- function deleteAtom(atomToken, store = IMPLICIT.STORE) {
2021
+ function deleteAtom(atomToken, store) {
2022
+ var _a, _b;
1903
2023
  const core = target(store);
1904
2024
  const { key } = atomToken;
2025
+ const atom = core.atoms.get(key);
2026
+ if (!atom) {
2027
+ store.logger.error(
2028
+ `\u274C`,
2029
+ `atom`,
2030
+ `${key}`,
2031
+ `Tried to delete atom, but it does not exist in the store.`
2032
+ );
2033
+ }
2034
+ (_a = atom == null ? void 0 : atom.cleanup) == null ? void 0 : _a.call(atom);
1905
2035
  core.atoms.delete(key);
1906
2036
  core.valueMap.delete(key);
2037
+ const selectorKeys = core.selectorAtoms.getRelatedKeys(key);
2038
+ if (selectorKeys) {
2039
+ for (const selectorKey of selectorKeys) {
2040
+ const token = (_b = core.selectors.get(selectorKey)) != null ? _b : core.readonlySelectors.get(selectorKey);
2041
+ if (token) {
2042
+ deleteSelector(token, store);
2043
+ }
2044
+ }
2045
+ }
1907
2046
  core.selectorAtoms.delete(key);
1908
2047
  core.atomsThatAreDefault.delete(key);
1909
2048
  core.timelineAtoms.delete(key);
1910
2049
  store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
1911
2050
  }
1912
2051
 
2052
+ // src/not-found-error.ts
2053
+ var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
2054
+ function prettyPrintTokenType(token) {
2055
+ if (token.type === `readonly_selector`) {
2056
+ return `Readonly Selector`;
2057
+ }
2058
+ return capitalize(token.type);
2059
+ }
2060
+ var NotFoundError = class extends Error {
2061
+ constructor(token, store) {
2062
+ super(
2063
+ `${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
2064
+ );
2065
+ }
2066
+ };
2067
+
1913
2068
  // src/subscribe/recall-state.ts
1914
- var recallState = (state, store = IMPLICIT.STORE) => {
2069
+ var recallState = (state, store) => {
1915
2070
  const core = target(store);
1916
2071
  if (!core.operation.open) {
1917
2072
  store.logger.warn(
@@ -1949,7 +2104,7 @@ var subscribeToRootAtoms = (state, store) => {
1949
2104
  atomChange.newValue
1950
2105
  );
1951
2106
  const oldValue = recallState(state, store);
1952
- const newValue = getState__INTERNAL(state, store);
2107
+ const newValue = readOrComputeValue(state, store);
1953
2108
  store.logger.info(
1954
2109
  `\u2728`,
1955
2110
  state.type,
@@ -1966,65 +2121,6 @@ var subscribeToRootAtoms = (state, store) => {
1966
2121
  return dependencySubscriptions;
1967
2122
  };
1968
2123
 
1969
- exports.FamilyTracker = FamilyTracker;
1970
- exports.Future = Future;
1971
- exports.IMPLICIT = IMPLICIT;
1972
- exports.Store = Store;
1973
- exports.Subject = Subject;
1974
- exports.TRANSACTION_PHASES = TRANSACTION_PHASES;
1975
- exports.Tracker = Tracker;
1976
- exports.abortTransaction = abortTransaction;
1977
- exports.addAtomToTimeline = addAtomToTimeline;
1978
- exports.applyTransaction = applyTransaction;
1979
- exports.become = become;
1980
- exports.buildTransaction = buildTransaction;
1981
- exports.cacheValue = cacheValue;
1982
- exports.clearStore = clearStore;
1983
- exports.closeOperation = closeOperation;
1984
- exports.createAtom = createAtom;
1985
- exports.createAtomFamily = createAtomFamily;
1986
- exports.createMutableAtom = createMutableAtom;
1987
- exports.createMutableAtomFamily = createMutableAtomFamily;
1988
- exports.createReadonlySelectorFamily = createReadonlySelectorFamily;
1989
- exports.createSelector = createSelector;
1990
- exports.createSelectorFamily = createSelectorFamily;
1991
- exports.deleteAtom = deleteAtom;
1992
- exports.deposit = deposit;
1993
- exports.evictCachedValue = evictCachedValue;
1994
- exports.getJsonToken = getJsonToken;
1995
- exports.getSelectorDependencyKeys = getSelectorDependencyKeys;
1996
- exports.getState__INTERNAL = getState__INTERNAL;
1997
- exports.getUpdateToken = getUpdateToken;
1998
- exports.isAtomDefault = isAtomDefault;
1999
- exports.isAtomKey = isAtomKey;
2000
- exports.isAtomMutable = isAtomMutable;
2001
- exports.isAtomTokenMutable = isAtomTokenMutable;
2002
- exports.isDone = isDone;
2003
- exports.isReadonlySelectorKey = isReadonlySelectorKey;
2004
- exports.isSelectorDefault = isSelectorDefault;
2005
- exports.isSelectorKey = isSelectorKey;
2006
- exports.isStateKey = isStateKey;
2007
- exports.isTransceiver = isTransceiver;
2008
- exports.isValueCached = isValueCached;
2009
- exports.markAtomAsDefault = markAtomAsDefault;
2010
- exports.markAtomAsNotDefault = markAtomAsNotDefault;
2011
- exports.markDone = markDone;
2012
- exports.openOperation = openOperation;
2013
- exports.readCachedValue = readCachedValue;
2014
- exports.redoTransactionUpdate = redoTransactionUpdate;
2015
- exports.redo__INTERNAL = redo__INTERNAL;
2016
- exports.registerSelector = registerSelector;
2017
- exports.setState__INTERNAL = setState__INTERNAL;
2018
- exports.subscribeToRootAtoms = subscribeToRootAtoms;
2019
- exports.target = target;
2020
- exports.timeline__INTERNAL = timeline__INTERNAL;
2021
- exports.traceAllSelectorAtoms = traceAllSelectorAtoms;
2022
- exports.traceSelectorAtoms = traceSelectorAtoms;
2023
- exports.transaction__INTERNAL = transaction__INTERNAL;
2024
- exports.undoTransactionUpdate = undoTransactionUpdate;
2025
- exports.undo__INTERNAL = undo__INTERNAL;
2026
- exports.updateSelectorAtoms = updateSelectorAtoms;
2027
- exports.withdraw = withdraw;
2028
- exports.withdrawNewFamilyMember = withdrawNewFamilyMember;
2124
+ export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, addAtomToTimeline, applyTransaction, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtom, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelectorFamily, createSelector, createSelectorFamily, createTimeline, createTransaction, deleteAtom, deleteSelector, deposit, evictCachedValue, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, isAtomDefault, isAtomKey, isAtomMutable, isAtomTokenMutable, isDone, isReadonlySelectorKey, isSelectorDefault, isSelectorKey, isStateKey, isTransceiver, isValueCached, markAtomAsDefault, markAtomAsNotDefault, markDone, openOperation, readCachedValue, readOrComputeValue, redoTransactionUpdate, registerSelector, setAtomOrSelector, subscribeToRootAtoms, target, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, undoTransactionUpdate, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
2029
2125
  //# sourceMappingURL=out.js.map
2030
2126
  //# sourceMappingURL=index.js.map