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