atom.io 0.14.0 → 0.14.2

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 (133) hide show
  1. package/data/dist/index.cjs +1421 -52
  2. package/data/dist/index.cjs.map +1 -1
  3. package/data/dist/index.d.cts +317 -16
  4. package/data/dist/index.js +10 -267
  5. package/data/dist/index.js.map +1 -1
  6. package/data/package.json +5 -5
  7. package/data/src/join.ts +3 -3
  8. package/dist/chunk-C4YZZNRH.js +67 -0
  9. package/dist/chunk-C4YZZNRH.js.map +1 -0
  10. package/dist/chunk-CWKKQKVQ.js +68 -0
  11. package/dist/chunk-CWKKQKVQ.js.map +1 -0
  12. package/dist/chunk-GMN5KH6A.js +309 -0
  13. package/dist/chunk-GMN5KH6A.js.map +1 -0
  14. package/dist/chunk-N7ADBQJG.js +176 -0
  15. package/dist/chunk-N7ADBQJG.js.map +1 -0
  16. package/dist/chunk-NYCVSXQB.js +181 -0
  17. package/dist/chunk-NYCVSXQB.js.map +1 -0
  18. package/dist/chunk-PURABO5G.js +2581 -0
  19. package/dist/chunk-PURABO5G.js.map +1 -0
  20. package/dist/index.cjs +2330 -55
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +272 -22
  23. package/dist/index.js +1 -152
  24. package/dist/index.js.map +1 -1
  25. package/dist/metafile-cjs.json +1 -1
  26. package/internal/dist/index.cjs +299 -106
  27. package/internal/dist/index.cjs.map +1 -1
  28. package/internal/dist/index.d.cts +231 -30
  29. package/internal/dist/index.js +1 -2240
  30. package/internal/dist/index.js.map +1 -1
  31. package/internal/package.json +5 -5
  32. package/internal/src/atom/create-atom.ts +3 -1
  33. package/internal/src/families/create-atom-family.ts +3 -4
  34. package/internal/src/mutable/get-update-family.ts +23 -0
  35. package/internal/src/mutable/index.ts +1 -4
  36. package/internal/src/mutable/is-mutable.ts +44 -0
  37. package/internal/src/mutable/tracker.ts +9 -2
  38. package/internal/src/subject.ts +2 -1
  39. package/internal/src/timeline/add-atom-to-timeline.ts +10 -1
  40. package/internal/src/timeline/create-timeline.ts +44 -16
  41. package/introspection/dist/index.cjs +1566 -46
  42. package/introspection/dist/index.cjs.map +1 -1
  43. package/introspection/dist/index.d.cts +376 -4
  44. package/introspection/dist/index.js +2 -316
  45. package/introspection/dist/index.js.map +1 -1
  46. package/introspection/package.json +5 -5
  47. package/json/dist/index.cjs +1505 -8
  48. package/json/dist/index.cjs.map +1 -1
  49. package/json/dist/index.d.cts +373 -7
  50. package/json/dist/index.js +1 -76
  51. package/json/dist/index.js.map +1 -1
  52. package/json/package.json +5 -5
  53. package/package.json +5 -4
  54. package/react/dist/index.cjs +1549 -14
  55. package/react/dist/index.cjs.map +1 -1
  56. package/react/dist/index.d.cts +377 -4
  57. package/react/dist/index.js +2 -68
  58. package/react/dist/index.js.map +1 -1
  59. package/react/package.json +5 -5
  60. package/react-devtools/dist/index.cjs +2216 -198
  61. package/react-devtools/dist/index.cjs.map +1 -1
  62. package/react-devtools/dist/index.css +2 -2
  63. package/react-devtools/dist/index.d.cts +150 -24
  64. package/react-devtools/dist/index.js +57 -297
  65. package/react-devtools/dist/index.js.map +1 -1
  66. package/react-devtools/package.json +5 -5
  67. package/realtime-client/dist/index.cjs +1530 -35
  68. package/realtime-client/dist/index.cjs.map +1 -1
  69. package/realtime-client/dist/index.d.cts +384 -12
  70. package/realtime-client/dist/index.js +2 -165
  71. package/realtime-client/dist/index.js.map +1 -1
  72. package/realtime-client/package.json +5 -5
  73. package/realtime-react/dist/index.cjs +1727 -35
  74. package/realtime-react/dist/index.cjs.map +1 -1
  75. package/realtime-react/dist/index.d.cts +49 -9
  76. package/realtime-react/dist/index.js +4 -66
  77. package/realtime-react/dist/index.js.map +1 -1
  78. package/realtime-react/package.json +5 -5
  79. package/realtime-server/dist/index.cjs +1549 -58
  80. package/realtime-server/dist/index.cjs.map +1 -1
  81. package/realtime-server/dist/index.d.cts +394 -11
  82. package/realtime-server/dist/index.js +27 -21
  83. package/realtime-server/dist/index.js.map +1 -1
  84. package/realtime-server/package.json +6 -6
  85. package/realtime-testing/dist/index.cjs +2377 -16
  86. package/realtime-testing/dist/index.cjs.map +1 -1
  87. package/realtime-testing/dist/index.d.cts +489 -3
  88. package/realtime-testing/dist/index.js +10 -31
  89. package/realtime-testing/dist/index.js.map +1 -1
  90. package/realtime-testing/package.json +5 -5
  91. package/src/atom.ts +3 -1
  92. package/transceivers/set-rtx/dist/index.cjs +32 -10
  93. package/transceivers/set-rtx/dist/index.cjs.map +1 -1
  94. package/transceivers/set-rtx/dist/index.d.cts +29 -3
  95. package/transceivers/set-rtx/dist/index.js +2 -3
  96. package/transceivers/set-rtx/dist/index.js.map +1 -1
  97. package/transceivers/set-rtx/package.json +6 -6
  98. package/data/dist/index.d.ts +0 -158
  99. package/data/dist/metafile-cjs.json +0 -1
  100. package/data/dist/metafile-esm.json +0 -1
  101. package/dist/index.d.ts +0 -243
  102. package/dist/metafile-esm.json +0 -1
  103. package/internal/dist/index.d.ts +0 -421
  104. package/internal/dist/metafile-cjs.json +0 -1
  105. package/internal/dist/metafile-esm.json +0 -1
  106. package/internal/src/mutable/is-atom-token-mutable.ts +0 -7
  107. package/introspection/dist/index.d.ts +0 -24
  108. package/introspection/dist/metafile-cjs.json +0 -1
  109. package/introspection/dist/metafile-esm.json +0 -1
  110. package/json/dist/index.d.ts +0 -51
  111. package/json/dist/metafile-cjs.json +0 -1
  112. package/json/dist/metafile-esm.json +0 -1
  113. package/react/dist/index.d.ts +0 -23
  114. package/react/dist/metafile-cjs.json +0 -1
  115. package/react/dist/metafile-esm.json +0 -1
  116. package/react-devtools/dist/index.d.ts +0 -341
  117. package/react-devtools/dist/metafile-cjs.json +0 -1
  118. package/react-devtools/dist/metafile-esm.json +0 -1
  119. package/realtime-client/dist/index.d.ts +0 -22
  120. package/realtime-client/dist/metafile-cjs.json +0 -1
  121. package/realtime-client/dist/metafile-esm.json +0 -1
  122. package/realtime-react/dist/index.d.ts +0 -27
  123. package/realtime-react/dist/metafile-cjs.json +0 -1
  124. package/realtime-react/dist/metafile-esm.json +0 -1
  125. package/realtime-server/dist/index.d.ts +0 -25
  126. package/realtime-server/dist/metafile-cjs.json +0 -1
  127. package/realtime-server/dist/metafile-esm.json +0 -1
  128. package/realtime-testing/dist/index.d.ts +0 -49
  129. package/realtime-testing/dist/metafile-cjs.json +0 -1
  130. package/realtime-testing/dist/metafile-esm.json +0 -1
  131. package/transceivers/set-rtx/dist/index.d.ts +0 -40
  132. package/transceivers/set-rtx/dist/metafile-cjs.json +0 -1
  133. package/transceivers/set-rtx/dist/metafile-esm.json +0 -1
package/dist/index.cjs CHANGED
@@ -1,58 +1,2321 @@
1
1
  'use strict';
2
2
 
3
- var Internal3 = require('atom.io/internal');
4
-
5
- function _interopNamespace(e) {
6
- if (e && e.__esModule) return e;
7
- var n = Object.create(null);
8
- if (e) {
9
- Object.keys(e).forEach(function (k) {
10
- if (k !== 'default') {
11
- var d = Object.getOwnPropertyDescriptor(e, k);
12
- Object.defineProperty(n, k, d.get ? d : {
13
- enumerable: true,
14
- get: function () { return e[k]; }
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
+
23
+ // internal/src/future.ts
24
+ var Future = class extends Promise {
25
+ constructor(executor) {
26
+ super((resolve, reject) => {
27
+ const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
28
+ const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
29
+ if (typeof executor === `function`) {
30
+ executor(pass, fail);
31
+ } else {
32
+ executor.then(pass, fail);
33
+ }
34
+ });
35
+ this.isCanceled = false;
36
+ }
37
+ cancel() {
38
+ this.isCanceled = true;
39
+ }
40
+ };
41
+
42
+ // internal/src/lineage.ts
43
+ function newest(scion) {
44
+ while (scion.child !== null) {
45
+ scion = scion.child;
46
+ }
47
+ return scion;
48
+ }
49
+
50
+ // internal/src/caching.ts
51
+ function cacheValue(key, value, subject, store) {
52
+ const target = newest(store);
53
+ const currentValue = target.valueMap.get(key);
54
+ if (currentValue instanceof Future) {
55
+ currentValue.cancel();
56
+ }
57
+ if (value instanceof Promise) {
58
+ const future = new Future(value);
59
+ newest(store).valueMap.set(key, future);
60
+ future.then((resolved) => {
61
+ if (future.isCanceled) {
62
+ return;
63
+ }
64
+ cacheValue(key, resolved, subject, store);
65
+ subject.next({ newValue: resolved, oldValue: future });
66
+ }).catch((thrown) => {
67
+ if (thrown !== `canceled`) {
68
+ store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
69
+ }
70
+ });
71
+ return future;
72
+ }
73
+ target.valueMap.set(key, value);
74
+ return value;
75
+ }
76
+ var readCachedValue = (key, store) => {
77
+ return newest(store).valueMap.get(key);
78
+ };
79
+ var isValueCached = (key, store) => {
80
+ return newest(store).valueMap.has(key);
81
+ };
82
+ var evictCachedValue = (key, store) => {
83
+ const core = newest(store);
84
+ const currentValue = core.valueMap.get(key);
85
+ if (currentValue instanceof Future) {
86
+ currentValue.cancel();
87
+ }
88
+ if (core.operation.open) {
89
+ core.operation.prev.set(key, currentValue);
90
+ }
91
+ core.valueMap.delete(key);
92
+ store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
93
+ };
94
+
95
+ // ../anvl/src/json/index.ts
96
+ var parseJson = (str) => JSON.parse(str);
97
+ var stringifyJson = (json) => JSON.stringify(json);
98
+
99
+ // json/src/select-json.ts
100
+ var selectJson = (atom2, transform, store = IMPLICIT.STORE) => {
101
+ return createSelector(
102
+ {
103
+ key: `${atom2.key}:JSON`,
104
+ get: ({ get }) => transform.toJson(get(atom2)),
105
+ set: ({ set }, newValue) => set(atom2, transform.fromJson(newValue))
106
+ },
107
+ void 0,
108
+ store
109
+ );
110
+ };
111
+
112
+ // json/src/select-json-family.ts
113
+ var selectJsonFamily = (atomFamily2, transform, store = IMPLICIT.STORE) => {
114
+ const jsonFamily = createSelectorFamily(
115
+ {
116
+ key: `${atomFamily2.key}:JSON`,
117
+ get: (key) => ({ get }) => transform.toJson(get(atomFamily2(key))),
118
+ set: (key) => ({ set }, newValue) => set(atomFamily2(key), transform.fromJson(newValue))
119
+ },
120
+ store
121
+ );
122
+ atomFamily2.subject.subscribe(
123
+ `store=${store.config.name}::json-selector-family`,
124
+ (token) => {
125
+ if (token.family) {
126
+ jsonFamily(parseJson(token.family.subKey));
127
+ }
128
+ }
129
+ );
130
+ return jsonFamily;
131
+ };
132
+
133
+ // internal/src/read-or-compute-value.ts
134
+ var readOrComputeValue = (state, store) => {
135
+ if (isValueCached(state.key, store)) {
136
+ store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
137
+ return readCachedValue(state.key, store);
138
+ }
139
+ if (state.type !== `atom`) {
140
+ store.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
141
+ return state.get();
142
+ }
143
+ const fallback = state.default instanceof Function ? state.default() : state.default;
144
+ store.logger.info(
145
+ `\u{1F481}`,
146
+ `atom`,
147
+ state.key,
148
+ `could not find cached value; using default`,
149
+ fallback
150
+ );
151
+ return state.default instanceof Function ? state.default() : state.default;
152
+ };
153
+
154
+ // internal/src/operation.ts
155
+ var openOperation = (token, store) => {
156
+ const target = newest(store);
157
+ if (target.operation.open) {
158
+ store.logger.error(
159
+ `\u274C`,
160
+ token.type,
161
+ token.key,
162
+ `failed to setState during a setState for "${target.operation.token.key}"`
163
+ );
164
+ return `rejection`;
165
+ }
166
+ target.operation = {
167
+ open: true,
168
+ done: /* @__PURE__ */ new Set(),
169
+ prev: /* @__PURE__ */ new Map(),
170
+ time: Date.now(),
171
+ token
172
+ };
173
+ store.logger.info(
174
+ `\u2B55`,
175
+ token.type,
176
+ token.key,
177
+ `operation start in store "${store.config.name}"${target.transactionMeta === null ? `` : ` ${target.transactionMeta.phase} "${target.transactionMeta.update.key}"`}`
178
+ );
179
+ };
180
+ var closeOperation = (store) => {
181
+ const target = newest(store);
182
+ if (target.operation.open) {
183
+ store.logger.info(
184
+ `\u{1F534}`,
185
+ target.operation.token.type,
186
+ target.operation.token.key,
187
+ `operation done in store "${store.config.name}"`
188
+ );
189
+ }
190
+ target.operation = { open: false };
191
+ store.subject.operationStatus.next(target.operation);
192
+ };
193
+ var isDone = (key, store) => {
194
+ const target = newest(store);
195
+ if (!target.operation.open) {
196
+ store.logger.warn(
197
+ `\u{1F41E}`,
198
+ `unknown`,
199
+ key,
200
+ `isDone called outside of an operation. This is probably a bug.`
201
+ );
202
+ return true;
203
+ }
204
+ return target.operation.done.has(key);
205
+ };
206
+ var markDone = (key, store) => {
207
+ const target = newest(store);
208
+ if (!target.operation.open) {
209
+ store.logger.warn(
210
+ `\u{1F41E}`,
211
+ `unknown`,
212
+ key,
213
+ `markDone called outside of an operation. This is probably a bug.`
214
+ );
215
+ return;
216
+ }
217
+ target.operation.done.add(key);
218
+ };
219
+
220
+ // internal/src/set-state/become.ts
221
+ var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
222
+ originalThing instanceof Function ? originalThing() : originalThing
223
+ ) : nextVersionOfThing;
224
+
225
+ // internal/src/subject.ts
226
+ var Subject = class {
227
+ constructor() {
228
+ this.subscribers = /* @__PURE__ */ new Map();
229
+ }
230
+ subscribe(key, subscriber) {
231
+ this.subscribers.set(key, subscriber);
232
+ const unsubscribe = () => this.unsubscribe(key);
233
+ return unsubscribe;
234
+ }
235
+ unsubscribe(key) {
236
+ this.subscribers.delete(key);
237
+ }
238
+ next(value) {
239
+ const subscribers = this.subscribers.values();
240
+ for (const subscriber of subscribers) {
241
+ subscriber(value);
242
+ }
243
+ }
244
+ };
245
+ var StatefulSubject = class extends Subject {
246
+ constructor(initialState) {
247
+ super();
248
+ this.state = initialState;
249
+ }
250
+ next(value) {
251
+ this.state = value;
252
+ super.next(value);
253
+ }
254
+ };
255
+
256
+ // internal/src/set-state/copy-mutable-if-needed.ts
257
+ function copyMutableIfNeeded(atom2, transform, origin, target) {
258
+ const originValue = origin.valueMap.get(atom2.key);
259
+ const targetValue = target.valueMap.get(atom2.key);
260
+ if (originValue === targetValue) {
261
+ origin.logger.info(`\u{1F4C3}`, `atom`, `${atom2.key}`, `copying`);
262
+ const jsonValue = transform.toJson(originValue);
263
+ const copiedValue = transform.fromJson(jsonValue);
264
+ target.valueMap.set(atom2.key, copiedValue);
265
+ new Tracker(atom2, origin);
266
+ return copiedValue;
267
+ }
268
+ return targetValue;
269
+ }
270
+
271
+ // internal/src/set-state/copy-mutable-in-transaction.ts
272
+ function copyMutableIfWithinTransaction(oldValue, atom2, store) {
273
+ const target = newest(store);
274
+ const parent = target.parent;
275
+ if (parent !== null) {
276
+ if (`toJson` in atom2 && `fromJson` in atom2) {
277
+ const copiedValue = copyMutableIfNeeded(atom2, atom2, parent, target);
278
+ return copiedValue;
279
+ }
280
+ if (`family` in atom2) {
281
+ const family = parent.families.get(atom2.family.key);
282
+ if (family && family.type === `atom_family`) {
283
+ const result = copyMutableFamilyMemberWithinTransaction(
284
+ atom2,
285
+ family,
286
+ parent,
287
+ target
288
+ );
289
+ if (result) {
290
+ return result;
291
+ }
292
+ }
293
+ }
294
+ }
295
+ return oldValue;
296
+ }
297
+ function copyMutableFamilyMemberWithinTransaction(atom2, family, origin, target) {
298
+ if (`toJson` in family && `fromJson` in family) {
299
+ const copyCreated = copyMutableIfNeeded(atom2, family, origin, target);
300
+ return copyCreated;
301
+ }
302
+ return null;
303
+ }
304
+
305
+ // internal/src/set-state/emit-update.ts
306
+ var emitUpdate = (state, update, store) => {
307
+ store.logger.info(
308
+ `\u{1F4E2}`,
309
+ state.type,
310
+ state.key,
311
+ `went (`,
312
+ update.oldValue,
313
+ `->`,
314
+ update.newValue,
315
+ `) subscribers:`,
316
+ state.subject.subscribers
317
+ );
318
+ state.subject.next(update);
319
+ };
320
+
321
+ // internal/src/set-state/evict-downstream.ts
322
+ var evictDownStream = (atom2, store) => {
323
+ const target = newest(store);
324
+ const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom2.key);
325
+ store.logger.info(
326
+ `\u{1F9F9}`,
327
+ atom2.type,
328
+ atom2.key,
329
+ downstreamKeys ? `evicting ${downstreamKeys.size} states downstream:` : `no downstream states`,
330
+ downstreamKeys != null ? downstreamKeys : `to evict`
331
+ );
332
+ if (downstreamKeys) {
333
+ if (target.operation.open) {
334
+ store.logger.info(
335
+ `\u{1F9F9}`,
336
+ atom2.type,
337
+ atom2.key,
338
+ `[ ${[...target.operation.done].join(`, `)} ] already done`
339
+ );
340
+ }
341
+ for (const key of downstreamKeys) {
342
+ if (isDone(key, store)) {
343
+ continue;
344
+ }
345
+ evictCachedValue(key, store);
346
+ markDone(key, store);
347
+ }
348
+ }
349
+ };
350
+
351
+ // internal/src/set-state/stow-update.ts
352
+ function shouldUpdateBeStowed(key, update) {
353
+ if (isTransceiver(update.newValue)) {
354
+ return false;
355
+ }
356
+ if (key.includes(`\u{1F441}\u200D\u{1F5E8}`)) {
357
+ return false;
358
+ }
359
+ return true;
360
+ }
361
+ var stowUpdate = (state, update, store) => {
362
+ const { key } = state;
363
+ const target = newest(store);
364
+ if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
365
+ store.logger.error(
366
+ `\u{1F41E}`,
367
+ `atom`,
368
+ key,
369
+ `stowUpdate called outside of a transaction. This is probably a bug.`
370
+ );
371
+ return;
372
+ }
373
+ const shouldStow = shouldUpdateBeStowed(key, update);
374
+ if (!shouldStow) {
375
+ return;
376
+ }
377
+ const atomUpdate = __spreadValues({ key }, update);
378
+ if (state.family) {
379
+ atomUpdate.family = state.family;
380
+ }
381
+ target.transactionMeta.update.updates.push(atomUpdate);
382
+ store.logger.info(
383
+ `\u{1F4C1}`,
384
+ `atom`,
385
+ key,
386
+ `stowed (`,
387
+ update.oldValue,
388
+ `->`,
389
+ update.newValue,
390
+ `)`
391
+ );
392
+ };
393
+
394
+ // internal/src/set-state/set-atom.ts
395
+ var setAtom = (atom2, next, store) => {
396
+ const target = newest(store);
397
+ const oldValue = readOrComputeValue(atom2, store);
398
+ let newValue = copyMutableIfWithinTransaction(oldValue, atom2, store);
399
+ newValue = become(next)(newValue);
400
+ store.logger.info(`\u{1F4DD}`, `atom`, atom2.key, `set to`, newValue);
401
+ newValue = cacheValue(atom2.key, newValue, atom2.subject, store);
402
+ if (isAtomDefault(atom2.key, store)) {
403
+ markAtomAsNotDefault(atom2.key, store);
404
+ }
405
+ markDone(atom2.key, store);
406
+ evictDownStream(atom2, store);
407
+ const update = { oldValue, newValue };
408
+ if (target.transactionMeta === null || target.transactionMeta.phase === `applying`) {
409
+ emitUpdate(atom2, update, store);
410
+ } else {
411
+ stowUpdate(atom2, update, store);
412
+ }
413
+ };
414
+
415
+ // internal/src/set-state/set-atom-or-selector.ts
416
+ var setAtomOrSelector = (state, value, store) => {
417
+ if (state.type === `selector`) {
418
+ state.set(value);
419
+ } else {
420
+ setAtom(state, value, store);
421
+ }
422
+ };
423
+
424
+ // internal/src/store/deposit.ts
425
+ function deposit(state) {
426
+ const token = {
427
+ key: state.key,
428
+ type: state.type
429
+ };
430
+ if (`family` in state) {
431
+ token.family = state.family;
432
+ }
433
+ return token;
434
+ }
435
+
436
+ // ../rel8/junction/src/junction.ts
437
+ var Junction = class {
438
+ constructor(data, config) {
439
+ this.relations = /* @__PURE__ */ new Map();
440
+ this.contents = /* @__PURE__ */ new Map();
441
+ this.makeContentKey = (a, b) => `${a}:${b}`;
442
+ var _a, _b, _c, _d;
443
+ this.a = data.between[0];
444
+ this.b = data.between[1];
445
+ this.cardinality = data.cardinality;
446
+ if (!(config == null ? void 0 : config.externalStore)) {
447
+ this.relations = new Map((_a = data.relations) == null ? void 0 : _a.map(([a, b]) => [a, new Set(b)]));
448
+ this.contents = new Map(data.contents);
449
+ }
450
+ this.isContent = (_b = config == null ? void 0 : config.isContent) != null ? _b : null;
451
+ if (config == null ? void 0 : config.makeContentKey) {
452
+ this.makeContentKey = config.makeContentKey;
453
+ }
454
+ if (config == null ? void 0 : config.externalStore) {
455
+ const externalStore = config.externalStore;
456
+ this.has = (a, b) => externalStore.has(a, b);
457
+ this.addRelation = (a, b) => {
458
+ externalStore.addRelation(a, b);
459
+ };
460
+ this.deleteRelation = (a, b) => {
461
+ externalStore.deleteRelation(a, b);
462
+ };
463
+ this.replaceRelationsSafely = (a, bs) => {
464
+ externalStore.replaceRelationsSafely(a, bs);
465
+ };
466
+ this.replaceRelationsUnsafely = (a, bs) => {
467
+ externalStore.replaceRelationsUnsafely(a, bs);
468
+ };
469
+ this.getRelatedKeys = (key) => externalStore.getRelatedKeys(key);
470
+ if (externalStore.getContent) {
471
+ this.getContentInternal = (contentKey) => {
472
+ return externalStore.getContent(contentKey);
473
+ };
474
+ this.setContent = (contentKey, content) => {
475
+ externalStore.setContent(contentKey, content);
476
+ };
477
+ this.deleteContent = (contentKey) => {
478
+ externalStore.deleteContent(contentKey);
479
+ };
480
+ }
481
+ for (const [x, ys] of (_c = data.relations) != null ? _c : []) {
482
+ for (const y of ys)
483
+ this.addRelation(x, y);
484
+ }
485
+ for (const [contentKey, content] of (_d = data.contents) != null ? _d : []) {
486
+ this.setContent(contentKey, content);
487
+ }
488
+ }
489
+ }
490
+ getRelatedKeys(key) {
491
+ return this.relations.get(key);
492
+ }
493
+ addRelation(a, b) {
494
+ let aRelations = this.relations.get(a);
495
+ let bRelations = this.relations.get(b);
496
+ if (aRelations) {
497
+ aRelations.add(b);
498
+ } else {
499
+ aRelations = /* @__PURE__ */ new Set([b]);
500
+ this.relations.set(a, aRelations);
501
+ }
502
+ if (bRelations) {
503
+ bRelations.add(a);
504
+ } else {
505
+ bRelations = /* @__PURE__ */ new Set([a]);
506
+ this.relations.set(b, bRelations);
507
+ }
508
+ }
509
+ deleteRelation(a, b) {
510
+ const aRelations = this.relations.get(a);
511
+ if (aRelations) {
512
+ aRelations.delete(b);
513
+ if (aRelations.size === 0) {
514
+ this.relations.delete(a);
515
+ }
516
+ const bRelations = this.relations.get(b);
517
+ if (bRelations) {
518
+ bRelations.delete(a);
519
+ if (bRelations.size === 0) {
520
+ this.relations.delete(b);
521
+ }
522
+ }
523
+ }
524
+ }
525
+ replaceRelationsUnsafely(a, bs) {
526
+ this.relations.set(a, new Set(bs));
527
+ for (const b of bs) {
528
+ const bRelations = /* @__PURE__ */ new Set([a]);
529
+ this.relations.set(b, bRelations);
530
+ }
531
+ }
532
+ replaceRelationsSafely(a, bs) {
533
+ const aRelationsPrev = this.relations.get(a);
534
+ if (aRelationsPrev) {
535
+ for (const b of aRelationsPrev) {
536
+ const bRelations = this.relations.get(b);
537
+ if (bRelations) {
538
+ if (bRelations.size === 1) {
539
+ this.relations.delete(b);
540
+ } else {
541
+ bRelations.delete(a);
542
+ }
543
+ this.contents.delete(this.makeContentKey(a, b));
544
+ }
545
+ }
546
+ }
547
+ this.relations.set(a, new Set(bs));
548
+ for (const b of bs) {
549
+ let bRelations = this.relations.get(b);
550
+ if (bRelations) {
551
+ bRelations.add(a);
552
+ } else {
553
+ bRelations = /* @__PURE__ */ new Set([a]);
554
+ this.relations.set(b, bRelations);
555
+ }
556
+ }
557
+ }
558
+ getContentInternal(contentKey) {
559
+ return this.contents.get(contentKey);
560
+ }
561
+ setContent(contentKey, content) {
562
+ this.contents.set(contentKey, content);
563
+ }
564
+ deleteContent(contentKey) {
565
+ this.contents.delete(contentKey);
566
+ }
567
+ toJSON() {
568
+ return {
569
+ between: [this.a, this.b],
570
+ cardinality: this.cardinality,
571
+ relations: [...this.relations.entries()].map(([a, b]) => [a, [...b]]),
572
+ contents: [...this.contents.entries()]
573
+ };
574
+ }
575
+ set(a, ...rest) {
576
+ var _a;
577
+ const b = typeof rest[0] === `string` ? rest[0] : a[this.b];
578
+ const content = ((_a = rest[1]) != null ? _a : typeof rest[0] === `string`) ? void 0 : rest[0];
579
+ a = typeof a === `string` ? a : a[this.a];
580
+ switch (this.cardinality) {
581
+ case `1:1`: {
582
+ const bPrev = this.getRelatedKey(a);
583
+ if (bPrev && bPrev !== b)
584
+ this.delete(bPrev, a);
585
+ }
586
+ case `1:n`: {
587
+ const aPrev = this.getRelatedKey(b);
588
+ if (aPrev && aPrev !== a)
589
+ this.delete(aPrev, b);
590
+ }
591
+ }
592
+ if (content) {
593
+ const contentKey = this.makeContentKey(a, b);
594
+ this.setContent(contentKey, content);
595
+ }
596
+ this.addRelation(a, b);
597
+ return this;
598
+ }
599
+ delete(x, b) {
600
+ b = typeof b === `string` ? b : x[this.b];
601
+ const a = typeof x === `string` ? x : x[this.a];
602
+ if (a === void 0 && typeof b === `string`) {
603
+ const bRelations = this.getRelatedKeys(b);
604
+ if (bRelations) {
605
+ for (const a2 of bRelations) {
606
+ this.delete(a2, b);
607
+ }
608
+ }
609
+ }
610
+ if (typeof a === `string` && b === void 0) {
611
+ const aRelations = this.getRelatedKeys(a);
612
+ if (aRelations) {
613
+ for (const b2 of aRelations) {
614
+ this.delete(a, b2);
615
+ }
616
+ }
617
+ }
618
+ if (typeof a === `string` && typeof b === `string`) {
619
+ this.deleteRelation(a, b);
620
+ const contentKey = this.makeContentKey(a, b);
621
+ this.deleteContent(contentKey);
622
+ }
623
+ return this;
624
+ }
625
+ getRelatedKey(key) {
626
+ const relations = this.getRelatedKeys(key);
627
+ if (relations) {
628
+ if (relations.size > 1) {
629
+ console.warn(
630
+ `${relations.size} related keys were found for key "${key}": (${[
631
+ ...relations
632
+ ].map((k) => `"${k}"`).join(`, `)}). Only one related key was expected.`
633
+ );
634
+ }
635
+ for (const relation of relations) {
636
+ return relation;
637
+ }
638
+ }
639
+ }
640
+ replaceRelations(a, relations, config) {
641
+ const hasContent = !Array.isArray(relations);
642
+ const bs = hasContent ? Object.keys(relations) : relations;
643
+ if (config == null ? void 0 : config.reckless) {
644
+ this.replaceRelationsUnsafely(a, bs);
645
+ } else {
646
+ this.replaceRelationsSafely(a, bs);
647
+ }
648
+ if (hasContent) {
649
+ for (const b of bs) {
650
+ const contentKey = this.makeContentKey(a, b);
651
+ const content = relations[b];
652
+ this.setContent(contentKey, content);
653
+ }
654
+ }
655
+ return this;
656
+ }
657
+ getContent(a, b) {
658
+ const contentKey = this.makeContentKey(a, b);
659
+ return this.getContentInternal(contentKey);
660
+ }
661
+ getRelationEntries(input) {
662
+ const a = input[this.a];
663
+ const b = input[this.b];
664
+ if (a !== void 0 && b === void 0) {
665
+ const aRelations = this.getRelatedKeys(a);
666
+ if (aRelations) {
667
+ return [...aRelations].map((b2) => {
668
+ var _a;
669
+ return [b2, (_a = this.getContent(a, b2)) != null ? _a : null];
15
670
  });
16
671
  }
672
+ }
673
+ if (a === void 0 && b !== void 0) {
674
+ const bRelations = this.getRelatedKeys(b);
675
+ if (bRelations) {
676
+ return [...bRelations].map((a2) => {
677
+ var _a;
678
+ return [a2, (_a = this.getContent(a2, b)) != null ? _a : null];
679
+ });
680
+ }
681
+ }
682
+ return [];
683
+ }
684
+ has(a, b) {
685
+ var _a;
686
+ if (b) {
687
+ const setA = this.getRelatedKeys(a);
688
+ return (_a = setA == null ? void 0 : setA.has(b)) != null ? _a : false;
689
+ }
690
+ return this.relations.has(a);
691
+ }
692
+ };
693
+
694
+ // internal/src/store/store.ts
695
+ var Store = class {
696
+ constructor(name, store = null) {
697
+ this.parent = null;
698
+ this.child = null;
699
+ this.valueMap = /* @__PURE__ */ new Map();
700
+ this.atoms = /* @__PURE__ */ new Map();
701
+ this.selectors = /* @__PURE__ */ new Map();
702
+ this.readonlySelectors = /* @__PURE__ */ new Map();
703
+ this.trackers = /* @__PURE__ */ new Map();
704
+ this.families = /* @__PURE__ */ new Map();
705
+ this.timelines = /* @__PURE__ */ new Map();
706
+ this.transactions = /* @__PURE__ */ new Map();
707
+ this.atomsThatAreDefault = /* @__PURE__ */ new Set();
708
+ this.timelineAtoms = new Junction({
709
+ between: [`timelineKey`, `atomKey`],
710
+ cardinality: `1:n`
711
+ });
712
+ this.selectorAtoms = new Junction({
713
+ between: [`selectorKey`, `atomKey`],
714
+ cardinality: `n:n`
17
715
  });
716
+ this.selectorGraph = new Junction(
717
+ {
718
+ between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
719
+ cardinality: `n:n`
720
+ },
721
+ {
722
+ makeContentKey: (...keys) => keys.sort().join(`:`)
723
+ }
724
+ );
725
+ this.subject = {
726
+ atomCreation: new Subject(),
727
+ selectorCreation: new Subject(),
728
+ transactionCreation: new Subject(),
729
+ timelineCreation: new Subject(),
730
+ transactionApplying: new StatefulSubject(null),
731
+ operationStatus: new Subject()
732
+ };
733
+ this.operation = { open: false };
734
+ this.transactionMeta = null;
735
+ this.config = {
736
+ name: `IMPLICIT_STORE`
737
+ };
738
+ this.loggers = [
739
+ new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
740
+ ];
741
+ this.logger = {
742
+ error: (...messages) => {
743
+ for (const logger of this.loggers)
744
+ logger.error(...messages);
745
+ },
746
+ info: (...messages) => {
747
+ for (const logger of this.loggers)
748
+ logger.info(...messages);
749
+ },
750
+ warn: (...messages) => {
751
+ for (const logger of this.loggers)
752
+ logger.warn(...messages);
753
+ }
754
+ };
755
+ if (store !== null) {
756
+ this.valueMap = new Map(store == null ? void 0 : store.valueMap);
757
+ this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
758
+ this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
759
+ this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
760
+ name
761
+ });
762
+ for (const [, atom2] of store.atoms) {
763
+ atom2.install(this);
764
+ }
765
+ for (const [, selector2] of store.readonlySelectors) {
766
+ selector2.install(this);
767
+ }
768
+ for (const [, selector2] of store.selectors) {
769
+ selector2.install(this);
770
+ }
771
+ for (const [, tx] of store.transactions) {
772
+ tx.install(this);
773
+ }
774
+ for (const [, timeline2] of store.timelines) {
775
+ timeline2.install(this);
776
+ }
777
+ }
778
+ }
779
+ };
780
+ var IMPLICIT = {
781
+ STORE_INTERNAL: void 0,
782
+ get STORE() {
783
+ var _a;
784
+ return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
785
+ }
786
+ };
787
+
788
+ // internal/src/store/withdraw.ts
789
+ function withdraw(token, store) {
790
+ var _a, _b, _c, _d;
791
+ const target = newest(store);
792
+ const state = (_d = (_c = (_b = (_a = target.atoms.get(token.key)) != null ? _a : target.selectors.get(token.key)) != null ? _b : target.readonlySelectors.get(token.key)) != null ? _c : target.transactions.get(token.key)) != null ? _d : target.timelines.get(token.key);
793
+ if (state) {
794
+ return state;
18
795
  }
19
- n.default = e;
20
- return Object.freeze(n);
796
+ return void 0;
21
797
  }
22
798
 
23
- var Internal3__namespace = /*#__PURE__*/_interopNamespace(Internal3);
799
+ // internal/src/store/withdraw-new-family-member.ts
800
+ function withdrawNewFamilyMember(token, store) {
801
+ if (token.family) {
802
+ store.logger.info(
803
+ `\u{1F46A}`,
804
+ token.type,
805
+ token.key,
806
+ `creating new family member in store "${store.config.name}"`
807
+ );
808
+ const target = newest(store);
809
+ const family = target.families.get(token.family.key);
810
+ if (family) {
811
+ const jsonSubKey = JSON.parse(token.family.subKey);
812
+ family(jsonSubKey);
813
+ const state = withdraw(token, store);
814
+ return state;
815
+ }
816
+ }
817
+ return void 0;
818
+ }
819
+
820
+ // internal/src/keys.ts
821
+ var isAtomKey = (key, store) => newest(store).atoms.has(key);
822
+ var isSelectorKey = (key, store) => newest(store).selectors.has(key);
823
+ var isReadonlySelectorKey = (key, store) => newest(store).readonlySelectors.has(key);
824
+ var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
825
+
826
+ // internal/src/selector/get-selector-dependency-keys.ts
827
+ var getSelectorDependencyKeys = (key, store) => {
828
+ const sources = newest(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
829
+ return sources;
830
+ };
831
+
832
+ // internal/src/selector/trace-selector-atoms.ts
833
+ var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
834
+ const rootKeys = [];
835
+ const indirectDependencyKeys = getSelectorDependencyKeys(
836
+ directDependencyKey,
837
+ store
838
+ );
839
+ let depth = 0;
840
+ while (indirectDependencyKeys.length > 0) {
841
+ const indirectDependencyKey = indirectDependencyKeys.shift();
842
+ ++depth;
843
+ if (depth > 99999) {
844
+ throw new Error(
845
+ `Maximum selector dependency depth exceeded (> 99999) in selector "${selectorKey}". This is likely due to a circular dependency.`
846
+ );
847
+ }
848
+ if (!isAtomKey(indirectDependencyKey, store)) {
849
+ indirectDependencyKeys.push(
850
+ ...getSelectorDependencyKeys(indirectDependencyKey, store)
851
+ );
852
+ } else if (!rootKeys.includes(indirectDependencyKey)) {
853
+ rootKeys.push(indirectDependencyKey);
854
+ }
855
+ }
856
+ return rootKeys;
857
+ };
858
+ var traceAllSelectorAtoms = (selectorKey, store) => {
859
+ const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
860
+ return directDependencyKeys.flatMap(
861
+ (depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
862
+ );
863
+ };
864
+
865
+ // internal/src/selector/update-selector-atoms.ts
866
+ var updateSelectorAtoms = (selectorKey, dependency, store) => {
867
+ const target = newest(store);
868
+ if (dependency.type === `atom`) {
869
+ target.selectorAtoms.set({
870
+ selectorKey,
871
+ atomKey: dependency.key
872
+ });
873
+ store.logger.info(
874
+ `\u{1F50D}`,
875
+ `selector`,
876
+ selectorKey,
877
+ `discovers root atom "${dependency.key}"`
878
+ );
879
+ } else {
880
+ const rootKeys = traceSelectorAtoms(selectorKey, dependency.key, store);
881
+ store.logger.info(
882
+ `\u{1F50D}`,
883
+ `selector`,
884
+ selectorKey,
885
+ `discovers root atoms: [ ${rootKeys.map((key) => `"${key}"`).join(`, `)} ]`
886
+ );
887
+ for (const atomKey of rootKeys) {
888
+ target.selectorAtoms = target.selectorAtoms.set({
889
+ selectorKey,
890
+ atomKey
891
+ });
892
+ }
893
+ }
894
+ };
895
+
896
+ // internal/src/selector/register-selector.ts
897
+ var registerSelector = (selectorKey, store) => ({
898
+ get: (dependency) => {
899
+ const target = newest(store);
900
+ const alreadyRegistered = target.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
901
+ const dependencyState = withdraw(dependency, store);
902
+ if (dependencyState === void 0) {
903
+ throw new Error(
904
+ `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
905
+ );
906
+ }
907
+ const dependencyValue = readOrComputeValue(dependencyState, store);
908
+ store.logger.info(
909
+ `\u{1F50C}`,
910
+ `selector`,
911
+ selectorKey,
912
+ `registers dependency ( "${dependency.key}" =`,
913
+ dependencyValue,
914
+ `)`
915
+ );
916
+ if (!alreadyRegistered) {
917
+ target.selectorGraph.set(
918
+ {
919
+ upstreamSelectorKey: dependency.key,
920
+ downstreamSelectorKey: selectorKey
921
+ },
922
+ {
923
+ source: dependency.key
924
+ }
925
+ );
926
+ }
927
+ updateSelectorAtoms(selectorKey, dependency, store);
928
+ return dependencyValue;
929
+ },
930
+ set: (stateToken, newValue) => {
931
+ const state = withdraw(stateToken, store);
932
+ if (state === void 0) {
933
+ throw new Error(
934
+ `State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
935
+ );
936
+ }
937
+ setAtomOrSelector(state, newValue, store);
938
+ }
939
+ });
940
+
941
+ // internal/src/selector/create-read-write-selector.ts
942
+ var createReadWriteSelector = (options, family, store) => {
943
+ const target = newest(store);
944
+ const subject = new Subject();
945
+ const { get, set } = registerSelector(options.key, store);
946
+ const getSelf = () => {
947
+ const value = options.get({ get });
948
+ cacheValue(options.key, value, subject, store);
949
+ return value;
950
+ };
951
+ const setSelf = (next) => {
952
+ const oldValue = getSelf();
953
+ const newValue = become(next)(oldValue);
954
+ store.logger.info(
955
+ `\u{1F4DD}`,
956
+ `selector`,
957
+ options.key,
958
+ `set (`,
959
+ oldValue,
960
+ `->`,
961
+ newValue,
962
+ `)`
963
+ );
964
+ cacheValue(options.key, newValue, subject, store);
965
+ markDone(options.key, store);
966
+ if (target.transactionMeta === null) {
967
+ subject.next({ newValue, oldValue });
968
+ }
969
+ options.set({ get, set }, newValue);
970
+ };
971
+ const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
972
+ subject,
973
+ install: (s) => createSelector(options, family, s),
974
+ get: getSelf,
975
+ set: setSelf,
976
+ type: `selector`
977
+ }), family && { family });
978
+ target.selectors.set(options.key, mySelector);
979
+ const initialValue = getSelf();
980
+ store.logger.info(`\u2728`, mySelector.type, mySelector.key, `=`, initialValue);
981
+ const token = {
982
+ key: options.key,
983
+ type: `selector`
984
+ };
985
+ if (family) {
986
+ token.family = family;
987
+ }
988
+ store.subject.selectorCreation.next(token);
989
+ return token;
990
+ };
991
+
992
+ // internal/src/selector/create-readonly-selector.ts
993
+ var createReadonlySelector = (options, family, store) => {
994
+ const target = newest(store);
995
+ const subject = new Subject();
996
+ const { get } = registerSelector(options.key, store);
997
+ const getSelf = () => {
998
+ const value = options.get({ get });
999
+ cacheValue(options.key, value, subject, store);
1000
+ return value;
1001
+ };
1002
+ const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
1003
+ subject,
1004
+ install: (s) => createSelector(options, family, s),
1005
+ get: getSelf,
1006
+ type: `readonly_selector`
1007
+ }), family && { family });
1008
+ target.readonlySelectors.set(options.key, readonlySelector);
1009
+ const initialValue = getSelf();
1010
+ store.logger.info(
1011
+ `\u2728`,
1012
+ readonlySelector.type,
1013
+ readonlySelector.key,
1014
+ `=`,
1015
+ initialValue
1016
+ );
1017
+ const token = {
1018
+ key: options.key,
1019
+ type: `readonly_selector`
1020
+ };
1021
+ if (family) {
1022
+ token.family = family;
1023
+ }
1024
+ store.subject.selectorCreation.next(token);
1025
+ return token;
1026
+ };
1027
+
1028
+ // internal/src/selector/create-selector.ts
1029
+ function createSelector(options, family, store) {
1030
+ const target = newest(store);
1031
+ const existingWritable = target.selectors.get(options.key);
1032
+ const existingReadonly = target.readonlySelectors.get(options.key);
1033
+ if (existingWritable || existingReadonly) {
1034
+ store.logger.error(
1035
+ `\u274C`,
1036
+ existingReadonly ? `readonly_selector` : `selector`,
1037
+ options.key,
1038
+ `Tried to create selector, but it already exists in the store. (Ignore if you are in development using hot module replacement.)`
1039
+ );
1040
+ }
1041
+ if (`set` in options) {
1042
+ return createReadWriteSelector(options, family, store);
1043
+ }
1044
+ return createReadonlySelector(options, family, store);
1045
+ }
1046
+
1047
+ // internal/src/selector/delete-selector.ts
1048
+ function deleteSelector(selectorToken, store) {
1049
+ const target = newest(store);
1050
+ const { key } = selectorToken;
1051
+ switch (selectorToken.type) {
1052
+ case `selector`:
1053
+ target.selectors.delete(key);
1054
+ break;
1055
+ case `readonly_selector`:
1056
+ target.readonlySelectors.delete(key);
1057
+ break;
1058
+ }
1059
+ target.valueMap.delete(key);
1060
+ target.selectorAtoms.delete(key);
1061
+ const downstreamTokens = target.selectorGraph.getRelationEntries({ upstreamSelectorKey: key }).filter(([_, { source }]) => source === key).map(
1062
+ ([downstreamSelectorKey]) => {
1063
+ var _a;
1064
+ return (_a = target.selectors.get(downstreamSelectorKey)) != null ? _a : target.readonlySelectors.get(downstreamSelectorKey);
1065
+ }
1066
+ );
1067
+ for (const downstreamToken of downstreamTokens) {
1068
+ if (downstreamToken) {
1069
+ deleteSelector(downstreamToken, store);
1070
+ }
1071
+ }
1072
+ target.selectorGraph.delete(key);
1073
+ store.logger.info(`\u{1F525}`, selectorToken.type, `${key}`, `deleted`);
1074
+ }
1075
+
1076
+ // internal/src/subscribe/recall-state.ts
1077
+ var recallState = (state, store) => {
1078
+ const target = newest(store);
1079
+ if (!target.operation.open) {
1080
+ return target.valueMap.get(state.key);
1081
+ }
1082
+ return target.operation.prev.get(state.key);
1083
+ };
1084
+
1085
+ // internal/src/subscribe/subscribe-to-root-atoms.ts
1086
+ var subscribeToRootAtoms = (state, store) => {
1087
+ const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
1088
+ const atom2 = store.atoms.get(atomKey);
1089
+ if (atom2 === void 0) {
1090
+ throw new Error(
1091
+ `Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
1092
+ );
1093
+ }
1094
+ return atom2.subject.subscribe(
1095
+ `${state.type}:${state.key}`,
1096
+ (atomChange) => {
1097
+ store.logger.info(
1098
+ `\u{1F4E2}`,
1099
+ state.type,
1100
+ state.key,
1101
+ `root`,
1102
+ atomKey,
1103
+ `went`,
1104
+ atomChange.oldValue,
1105
+ `->`,
1106
+ atomChange.newValue
1107
+ );
1108
+ const oldValue = recallState(state, store);
1109
+ const newValue = readOrComputeValue(state, store);
1110
+ store.logger.info(
1111
+ `\u2728`,
1112
+ state.type,
1113
+ state.key,
1114
+ `went`,
1115
+ oldValue,
1116
+ `->`,
1117
+ newValue
1118
+ );
1119
+ state.subject.next({ newValue, oldValue });
1120
+ }
1121
+ );
1122
+ });
1123
+ return dependencySubscriptions;
1124
+ };
1125
+
1126
+ // internal/src/subscribe/subscribe-to-state.ts
1127
+ function subscribeToState(token, handleUpdate, key, store) {
1128
+ const state = withdraw(token, store);
1129
+ if (state === void 0) {
1130
+ throw new Error(
1131
+ `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1132
+ );
1133
+ }
1134
+ const unsubFunction = state.subject.subscribe(key, handleUpdate);
1135
+ store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
1136
+ const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
1137
+ const unsubscribe = dependencyUnsubFunctions === null ? () => {
1138
+ store.logger.info(
1139
+ `\u{1F648}`,
1140
+ state.type,
1141
+ state.key,
1142
+ `Removing subscription "${key}"`
1143
+ );
1144
+ unsubFunction();
1145
+ } : () => {
1146
+ store.logger.info(
1147
+ `\u{1F648}`,
1148
+ state.type,
1149
+ state.key,
1150
+ `Removing subscription "${key}"`
1151
+ );
1152
+ unsubFunction();
1153
+ for (const unsubFromDependency of dependencyUnsubFunctions) {
1154
+ unsubFromDependency();
1155
+ }
1156
+ };
1157
+ return unsubscribe;
1158
+ }
1159
+
1160
+ // internal/src/subscribe/subscribe-to-timeline.ts
1161
+ var subscribeToTimeline = (token, handleUpdate, key, store) => {
1162
+ const tl = withdraw(token, store);
1163
+ if (tl === void 0) {
1164
+ throw new Error(
1165
+ `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
1166
+ );
1167
+ }
1168
+ store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
1169
+ const unsubscribe = tl.subject.subscribe(key, handleUpdate);
1170
+ return () => {
1171
+ store.logger.info(
1172
+ `\u{1F648}`,
1173
+ `timeline`,
1174
+ token.key,
1175
+ `Removing subscription "${key}" from timeline`
1176
+ );
1177
+ unsubscribe();
1178
+ };
1179
+ };
1180
+
1181
+ // internal/src/subscribe/subscribe-to-transaction.ts
1182
+ var subscribeToTransaction = (token, handleUpdate, key, store) => {
1183
+ const tx = withdraw(token, store);
1184
+ if (tx === void 0) {
1185
+ throw new Error(
1186
+ `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
1187
+ );
1188
+ }
1189
+ store.logger.info(
1190
+ `\u{1F440}`,
1191
+ `transaction`,
1192
+ token.key,
1193
+ `Adding subscription "${key}"`
1194
+ );
1195
+ const unsubscribe = tx.subject.subscribe(key, handleUpdate);
1196
+ return () => {
1197
+ store.logger.info(
1198
+ `\u{1F648}`,
1199
+ `transaction`,
1200
+ token.key,
1201
+ `Removing subscription "${key}"`
1202
+ );
1203
+ unsubscribe();
1204
+ };
1205
+ };
1206
+
1207
+ // internal/src/mutable/tracker.ts
1208
+ var Tracker = class {
1209
+ constructor(mutableState, store) {
1210
+ this.unsubscribeFromInnerValue = null;
1211
+ this.mutableState = mutableState;
1212
+ const target = newest(store);
1213
+ this.latestUpdateState = this.initializeState(mutableState, target);
1214
+ this.observeCore(mutableState, this.latestUpdateState, target);
1215
+ this.updateCore(mutableState, this.latestUpdateState, target);
1216
+ target.trackers.set(mutableState.key, this);
1217
+ }
1218
+ initializeState(mutableState, store) {
1219
+ const latestUpdateStateKey = `*${mutableState.key}`;
1220
+ store.atoms.delete(latestUpdateStateKey);
1221
+ store.valueMap.delete(latestUpdateStateKey);
1222
+ const familyMetaData = mutableState.family ? {
1223
+ key: `*${mutableState.family.key}`,
1224
+ subKey: mutableState.family.subKey
1225
+ } : void 0;
1226
+ const latestUpdateState = createAtom(
1227
+ {
1228
+ key: latestUpdateStateKey,
1229
+ default: null
1230
+ },
1231
+ familyMetaData,
1232
+ store
1233
+ );
1234
+ if (store.parent) {
1235
+ const parentValue = store.parent.valueMap.get(latestUpdateStateKey);
1236
+ store.valueMap.set(latestUpdateStateKey, parentValue);
1237
+ }
1238
+ return latestUpdateState;
1239
+ }
1240
+ observeCore(mutableState, latestUpdateState, store) {
1241
+ const originalInnerValue = getState(mutableState, store);
1242
+ const target = newest(store);
1243
+ this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
1244
+ `tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
1245
+ (update) => {
1246
+ const unsubscribe = store.subject.operationStatus.subscribe(
1247
+ mutableState.key,
1248
+ () => {
1249
+ unsubscribe();
1250
+ setState(latestUpdateState, update, store);
1251
+ }
1252
+ );
1253
+ }
1254
+ );
1255
+ subscribeToState(
1256
+ mutableState,
1257
+ (update) => {
1258
+ var _a;
1259
+ if (update.newValue !== update.oldValue) {
1260
+ (_a = this.unsubscribeFromInnerValue) == null ? void 0 : _a.call(this);
1261
+ const target2 = newest(store);
1262
+ this.unsubscribeFromInnerValue = update.newValue.subscribe(
1263
+ `tracker:${store.config.name}:${target2.transactionMeta === null ? `main` : target2.transactionMeta.update.key}`,
1264
+ (update2) => {
1265
+ const unsubscribe = store.subject.operationStatus.subscribe(
1266
+ mutableState.key,
1267
+ () => {
1268
+ unsubscribe();
1269
+ setState(latestUpdateState, update2, store);
1270
+ }
1271
+ );
1272
+ }
1273
+ );
1274
+ }
1275
+ },
1276
+ `${store.config.name}: tracker observing inner value`,
1277
+ store
1278
+ );
1279
+ }
1280
+ updateCore(mutableState, latestUpdateState, store) {
1281
+ subscribeToState(
1282
+ latestUpdateState,
1283
+ ({ newValue, oldValue }) => {
1284
+ const timelineId = store.timelineAtoms.getRelatedKey(
1285
+ latestUpdateState.key
1286
+ );
1287
+ if (timelineId) {
1288
+ const timelineData = store.timelines.get(timelineId);
1289
+ if (timelineData == null ? void 0 : timelineData.timeTraveling) {
1290
+ const unsubscribe2 = subscribeToTimeline(
1291
+ { key: timelineId, type: `timeline` },
1292
+ (update) => {
1293
+ unsubscribe2();
1294
+ setState(
1295
+ mutableState,
1296
+ (transceiver) => {
1297
+ if (update === `redo` && newValue) {
1298
+ transceiver.do(newValue);
1299
+ } else if (update === `undo` && oldValue) {
1300
+ transceiver.undo(oldValue);
1301
+ }
1302
+ return transceiver;
1303
+ },
1304
+ store
1305
+ );
1306
+ },
1307
+ `${mutableState.key}: tracker observing timeline`,
1308
+ store
1309
+ );
1310
+ return;
1311
+ }
1312
+ }
1313
+ const unsubscribe = store.subject.operationStatus.subscribe(
1314
+ latestUpdateState.key,
1315
+ () => {
1316
+ unsubscribe();
1317
+ const mutable = getState(mutableState, store);
1318
+ const updateNumber = newValue === null ? -1 : mutable.getUpdateNumber(newValue);
1319
+ const eventOffset = updateNumber - mutable.cacheUpdateNumber;
1320
+ if (newValue && eventOffset === 1) {
1321
+ setState(
1322
+ mutableState,
1323
+ (transceiver) => (transceiver.do(newValue), transceiver),
1324
+ store
1325
+ );
1326
+ }
1327
+ }
1328
+ );
1329
+ },
1330
+ `${store.config.name}: tracker observing latest update`,
1331
+ store
1332
+ );
1333
+ }
1334
+ };
1335
+
1336
+ // internal/src/mutable/create-mutable-atom.ts
1337
+ function createMutableAtom(options, store) {
1338
+ store.logger.info(
1339
+ `\u{1F527}`,
1340
+ `atom`,
1341
+ options.key,
1342
+ `creating in store "${store.config.name}"`
1343
+ );
1344
+ const coreState = createAtom(options, void 0, store);
1345
+ new Tracker(coreState, store);
1346
+ const jsonState = selectJson(coreState, options, store);
1347
+ const target = newest(store);
1348
+ subscribeToState(
1349
+ jsonState,
1350
+ () => {
1351
+ const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
1352
+ if (!trackerHasBeenInitialized) {
1353
+ new Tracker(coreState, store);
1354
+ }
1355
+ },
1356
+ `tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
1357
+ store
1358
+ );
1359
+ return coreState;
1360
+ }
1361
+
1362
+ // internal/src/families/create-atom-family.ts
1363
+ function createAtomFamily(options, store) {
1364
+ const subject = new Subject();
1365
+ const atomFamily2 = Object.assign(
1366
+ (key) => {
1367
+ const subKey = stringifyJson(key);
1368
+ const family = { key: options.key, subKey };
1369
+ const fullKey = `${options.key}(${subKey})`;
1370
+ const existing = withdraw({ key: fullKey, type: `atom` }, store);
1371
+ let token;
1372
+ if (existing) {
1373
+ token = deposit(existing);
1374
+ } else {
1375
+ const individualOptions = {
1376
+ key: fullKey,
1377
+ default: options.default instanceof Function ? options.default(key) : options.default
1378
+ };
1379
+ if (options.effects) {
1380
+ individualOptions.effects = options.effects(key);
1381
+ }
1382
+ token = createAtom(individualOptions, family, store);
1383
+ subject.next(token);
1384
+ }
1385
+ return token;
1386
+ },
1387
+ {
1388
+ key: options.key,
1389
+ type: `atom_family`,
1390
+ subject
1391
+ }
1392
+ );
1393
+ if (`mutable` in options && typeof options.mutable === `boolean`) {
1394
+ Object.assign(atomFamily2, { mutable: options.mutable });
1395
+ }
1396
+ const target = newest(store);
1397
+ target.families.set(options.key, atomFamily2);
1398
+ return atomFamily2;
1399
+ }
1400
+
1401
+ // internal/src/families/create-readonly-selector-family.ts
1402
+ function createReadonlySelectorFamily(options, store) {
1403
+ const subject = new Subject();
1404
+ return Object.assign(
1405
+ (key) => {
1406
+ const target = newest(store);
1407
+ const subKey = stringifyJson(key);
1408
+ const family = { key: options.key, subKey };
1409
+ const fullKey = `${options.key}(${subKey})`;
1410
+ const existing = target.readonlySelectors.get(fullKey);
1411
+ if (existing) {
1412
+ return deposit(existing);
1413
+ }
1414
+ return createSelector(
1415
+ {
1416
+ key: fullKey,
1417
+ get: options.get(key)
1418
+ },
1419
+ family,
1420
+ store
1421
+ );
1422
+ },
1423
+ {
1424
+ key: options.key,
1425
+ type: `readonly_selector_family`,
1426
+ subject
1427
+ }
1428
+ );
1429
+ }
1430
+
1431
+ // internal/src/families/create-selector-family.ts
1432
+ function createSelectorFamily(options, store) {
1433
+ const isReadonly = !(`set` in options);
1434
+ if (isReadonly) {
1435
+ return createReadonlySelectorFamily(options, store);
1436
+ }
1437
+ const target = newest(store);
1438
+ const subject = new Subject();
1439
+ const selectorFamily2 = Object.assign(
1440
+ (key) => {
1441
+ const subKey = stringifyJson(key);
1442
+ const family = { key: options.key, subKey };
1443
+ const fullKey = `${options.key}(${subKey})`;
1444
+ const existing = target.selectors.get(fullKey);
1445
+ if (existing) {
1446
+ return deposit(existing);
1447
+ }
1448
+ const token = createSelector(
1449
+ {
1450
+ key: fullKey,
1451
+ get: options.get(key),
1452
+ set: options.set(key)
1453
+ },
1454
+ family,
1455
+ store
1456
+ );
1457
+ subject.next(token);
1458
+ return token;
1459
+ },
1460
+ {
1461
+ key: options.key,
1462
+ type: `selector_family`
1463
+ }
1464
+ );
1465
+ target.families.set(options.key, selectorFamily2);
1466
+ return selectorFamily2;
1467
+ }
1468
+
1469
+ // internal/src/mutable/tracker-family.ts
1470
+ var FamilyTracker = class {
1471
+ constructor(findMutableState, store) {
1472
+ this.findLatestUpdateState = createAtomFamily(
1473
+ {
1474
+ key: `*${findMutableState.key}`,
1475
+ default: null
1476
+ },
1477
+ store
1478
+ );
1479
+ this.findMutableState = findMutableState;
1480
+ this.findMutableState.subject.subscribe(
1481
+ `store=${store.config.name}::tracker-atom-family`,
1482
+ (atomToken) => {
1483
+ if (atomToken.family) {
1484
+ const key = parseJson(atomToken.family.subKey);
1485
+ this.findLatestUpdateState(key);
1486
+ new Tracker(atomToken, store);
1487
+ }
1488
+ }
1489
+ );
1490
+ this.findLatestUpdateState.subject.subscribe(
1491
+ `store=${store.config.name}::tracker-atom-family`,
1492
+ (atomToken) => {
1493
+ if (atomToken.family) {
1494
+ const key = parseJson(atomToken.family.subKey);
1495
+ const mutableAtomToken = this.findMutableState(key);
1496
+ new Tracker(mutableAtomToken, store);
1497
+ }
1498
+ }
1499
+ );
1500
+ }
1501
+ };
1502
+
1503
+ // internal/src/mutable/create-mutable-atom-family.ts
1504
+ function createMutableAtomFamily(options, store) {
1505
+ const coreFamily = Object.assign(
1506
+ createAtomFamily(options, store),
1507
+ options
1508
+ );
1509
+ selectJsonFamily(coreFamily, options);
1510
+ new FamilyTracker(coreFamily, store);
1511
+ return coreFamily;
1512
+ }
1513
+
1514
+ // internal/src/mutable/get-update-token.ts
1515
+ var getUpdateToken = (mutableAtomToken) => {
1516
+ const key = `*${mutableAtomToken.key}`;
1517
+ const updateToken = { type: `atom`, key };
1518
+ if (mutableAtomToken.family) {
1519
+ updateToken.family = {
1520
+ key: `*${mutableAtomToken.family.key}`,
1521
+ subKey: mutableAtomToken.family.subKey
1522
+ };
1523
+ }
1524
+ return updateToken;
1525
+ };
1526
+
1527
+ // internal/src/mutable/get-update-family.ts
1528
+ var getUpdateFamily = (mutableAtomFamily, store) => {
1529
+ const target = newest(store);
1530
+ const key = `*${mutableAtomFamily.key}`;
1531
+ const updateFamily = target.families.get(key);
1532
+ return updateFamily;
1533
+ };
1534
+
1535
+ // internal/src/mutable/transceiver.ts
1536
+ function isTransceiver(value) {
1537
+ return typeof value === `object` && value !== null && `do` in value && `undo` in value && `subscribe` in value;
1538
+ }
1539
+
1540
+ // internal/src/atom/is-default.ts
1541
+ var isAtomDefault = (key, store) => {
1542
+ const core = newest(store);
1543
+ return core.atomsThatAreDefault.has(key);
1544
+ };
1545
+ var markAtomAsDefault = (key, store) => {
1546
+ const core = newest(store);
1547
+ core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key);
1548
+ };
1549
+ var markAtomAsNotDefault = (key, store) => {
1550
+ const core = newest(store);
1551
+ core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault);
1552
+ core.atomsThatAreDefault.delete(key);
1553
+ };
1554
+
1555
+ // internal/src/atom/create-atom.ts
1556
+ function createAtom(options, family, store) {
1557
+ store.logger.info(
1558
+ `\u{1F528}`,
1559
+ `atom`,
1560
+ options.key,
1561
+ `creating in store "${store.config.name}"`
1562
+ );
1563
+ const target = newest(store);
1564
+ const existing = target.atoms.get(options.key);
1565
+ if (existing) {
1566
+ store.logger.error(
1567
+ `\u274C`,
1568
+ `atom`,
1569
+ options.key,
1570
+ `Tried to create atom, but it already exists in the store.`,
1571
+ `(Ignore if you are in development using hot module replacement.)`
1572
+ );
1573
+ return deposit(existing);
1574
+ }
1575
+ const subject = new Subject();
1576
+ const newAtom = __spreadValues(__spreadProps(__spreadValues({}, options), {
1577
+ type: `atom`,
1578
+ install: (store2) => {
1579
+ store2.logger.info(
1580
+ `\u{1F6E0}\uFE0F`,
1581
+ `atom`,
1582
+ options.key,
1583
+ `installing in store "${store2.config.name}"`
1584
+ );
1585
+ return `mutable` in options ? createMutableAtom(options, store2) : createAtom(options, void 0, store2);
1586
+ },
1587
+ subject
1588
+ }), family && { family });
1589
+ let initialValue = options.default;
1590
+ if (options.default instanceof Function) {
1591
+ initialValue = options.default();
1592
+ }
1593
+ target.atoms.set(newAtom.key, newAtom);
1594
+ markAtomAsDefault(options.key, store);
1595
+ cacheValue(options.key, initialValue, subject, store);
1596
+ const token = deposit(newAtom);
1597
+ if (options.effects) {
1598
+ let effectIndex = 0;
1599
+ const cleanupFunctions = [];
1600
+ for (const effect of options.effects) {
1601
+ const cleanup = effect({
1602
+ setSelf: (next) => setState(token, next, store),
1603
+ onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1604
+ });
1605
+ if (cleanup) {
1606
+ cleanupFunctions.push(cleanup);
1607
+ }
1608
+ ++effectIndex;
1609
+ }
1610
+ newAtom.cleanup = () => {
1611
+ for (const cleanup of cleanupFunctions) {
1612
+ cleanup();
1613
+ }
1614
+ };
1615
+ }
1616
+ store.subject.atomCreation.next(token);
1617
+ return token;
1618
+ }
1619
+
1620
+ // internal/src/atom/delete-atom.ts
1621
+ function deleteAtom2(atomToken, store) {
1622
+ var _a, _b;
1623
+ const target = newest(store);
1624
+ const { key } = atomToken;
1625
+ const atom2 = target.atoms.get(key);
1626
+ if (!atom2) {
1627
+ store.logger.error(
1628
+ `\u274C`,
1629
+ `atom`,
1630
+ `${key}`,
1631
+ `Tried to delete atom, but it does not exist in the store.`
1632
+ );
1633
+ }
1634
+ (_a = atom2 == null ? void 0 : atom2.cleanup) == null ? void 0 : _a.call(atom2);
1635
+ target.atoms.delete(key);
1636
+ target.valueMap.delete(key);
1637
+ const selectorKeys = target.selectorAtoms.getRelatedKeys(key);
1638
+ if (selectorKeys) {
1639
+ for (const selectorKey of selectorKeys) {
1640
+ const token = (_b = target.selectors.get(selectorKey)) != null ? _b : target.readonlySelectors.get(selectorKey);
1641
+ if (token) {
1642
+ deleteSelector(token, store);
1643
+ }
1644
+ }
1645
+ }
1646
+ target.selectorAtoms.delete(key);
1647
+ target.atomsThatAreDefault.delete(key);
1648
+ target.timelineAtoms.delete(key);
1649
+ store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
1650
+ }
1651
+
1652
+ // internal/src/lazy-map.ts
1653
+ var LazyMap = class extends Map {
1654
+ constructor(source) {
1655
+ super();
1656
+ this.source = source;
1657
+ this.deleted = /* @__PURE__ */ new Set();
1658
+ }
1659
+ get(key) {
1660
+ const has = super.has(key);
1661
+ if (has) {
1662
+ return super.get(key);
1663
+ }
1664
+ if (!this.deleted.has(key) && this.source.has(key)) {
1665
+ const value = this.source.get(key);
1666
+ return value;
1667
+ }
1668
+ return void 0;
1669
+ }
1670
+ set(key, value) {
1671
+ this.deleted.delete(key);
1672
+ return super.set(key, value);
1673
+ }
1674
+ hasOwn(key) {
1675
+ return super.has(key);
1676
+ }
1677
+ has(key) {
1678
+ return !this.deleted.has(key) && (super.has(key) || this.source.has(key));
1679
+ }
1680
+ delete(key) {
1681
+ this.deleted.add(key);
1682
+ return super.delete(key);
1683
+ }
1684
+ };
1685
+
1686
+ // internal/src/not-found-error.ts
1687
+ var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
1688
+ function prettyPrintTokenType(token) {
1689
+ if (token.type === `readonly_selector`) {
1690
+ return `Readonly Selector`;
1691
+ }
1692
+ return capitalize(token.type);
1693
+ }
1694
+ var NotFoundError = class extends Error {
1695
+ constructor(token, store) {
1696
+ super(
1697
+ `${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
1698
+ );
1699
+ }
1700
+ };
1701
+
1702
+ // internal/src/timeline/add-atom-to-timeline.ts
1703
+ var addAtomToTimeline = (atomToken, tl, store) => {
1704
+ let maybeAtom = withdraw(atomToken, store);
1705
+ if (maybeAtom == null ? void 0 : maybeAtom.mutable) {
1706
+ console.log(`adding transceiver to timeline`, atomToken.key);
1707
+ const updateToken = getUpdateToken(atomToken);
1708
+ maybeAtom = withdraw(updateToken, store);
1709
+ }
1710
+ const atom2 = maybeAtom;
1711
+ if (atom2 === void 0) {
1712
+ throw new Error(
1713
+ `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
1714
+ );
1715
+ }
1716
+ atom2.subject.subscribe(`timeline`, (update) => {
1717
+ var _a, _b, _c, _d, _e, _f;
1718
+ const target = newest(store);
1719
+ const currentSelectorKey = store.operation.open && store.operation.token.type === `selector` ? store.operation.token.key : null;
1720
+ const currentSelectorTime = store.operation.open && store.operation.token.type === `selector` ? store.operation.time : null;
1721
+ const currentTransactionKey = (_a = target.subject.transactionApplying.state) == null ? void 0 : _a.update.key;
1722
+ const currentTransactionTime = (_b = target.subject.transactionApplying.state) == null ? void 0 : _b.time;
1723
+ store.logger.info(
1724
+ `\u23F3`,
1725
+ `timeline`,
1726
+ tl.key,
1727
+ `atom`,
1728
+ atomToken.key,
1729
+ `went`,
1730
+ update.oldValue,
1731
+ `->`,
1732
+ update.newValue,
1733
+ currentTransactionKey ? `in transaction "${currentTransactionKey}"` : currentSelectorKey ? `in selector "${currentSelectorKey}"` : ``
1734
+ );
1735
+ if (tl.timeTraveling === null) {
1736
+ if (tl.selectorTime && tl.selectorTime !== currentSelectorTime) {
1737
+ const mostRecentUpdate = tl.history.at(-1);
1738
+ if (mostRecentUpdate === void 0) {
1739
+ throw new Error(
1740
+ `Timeline "${tl.key}" has a selectorTime, but no history. This is most likely a bug in AtomIO.`
1741
+ );
1742
+ }
1743
+ }
1744
+ if (currentTransactionKey) {
1745
+ const currentTransaction = withdraw(
1746
+ { key: currentTransactionKey, type: `transaction` },
1747
+ store
1748
+ );
1749
+ if (currentTransaction === void 0) {
1750
+ throw new Error(
1751
+ `Transaction "${currentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${currentTransactionKey}".`
1752
+ );
1753
+ }
1754
+ if (tl.transactionKey !== currentTransactionKey) {
1755
+ if (tl.transactionKey) {
1756
+ store.logger.error(
1757
+ `\u{1F41E}`,
1758
+ `timeline`,
1759
+ tl.key,
1760
+ `unable to resolve transaction "${tl.transactionKey}. This is probably a bug in AtomIO.`
1761
+ );
1762
+ }
1763
+ tl.transactionKey = currentTransactionKey;
1764
+ const unsubscribe = currentTransaction.subject.subscribe(
1765
+ `timeline:${tl.key}`,
1766
+ (update2) => {
1767
+ var _a2, _b2;
1768
+ unsubscribe();
1769
+ if (tl.timeTraveling === null && currentTransactionTime) {
1770
+ if (tl.at !== tl.history.length) {
1771
+ tl.history.splice(tl.at);
1772
+ }
1773
+ const filterUpdates = (updates2) => updates2.filter((updateFromTx) => {
1774
+ const target2 = newest(store);
1775
+ if (`updates` in updateFromTx) {
1776
+ return true;
1777
+ }
1778
+ const atomOrFamilyKeys = target2.timelineAtoms.getRelatedKeys(tl.key);
1779
+ return atomOrFamilyKeys ? [...atomOrFamilyKeys].some(
1780
+ (key) => {
1781
+ var _a3;
1782
+ return key === updateFromTx.key || key === ((_a3 = updateFromTx.family) == null ? void 0 : _a3.key);
1783
+ }
1784
+ ) : false;
1785
+ }).map((updateFromTx) => {
1786
+ if (`updates` in updateFromTx) {
1787
+ return __spreadProps(__spreadValues({}, updateFromTx), {
1788
+ updates: filterUpdates(updateFromTx.updates)
1789
+ });
1790
+ }
1791
+ return updateFromTx;
1792
+ });
1793
+ const updates = filterUpdates(update2.updates);
1794
+ const timelineTransactionUpdate = __spreadProps(__spreadValues({
1795
+ type: `transaction_update`,
1796
+ timestamp: currentTransactionTime
1797
+ }, update2), {
1798
+ updates
1799
+ });
1800
+ const willCapture = (_b2 = (_a2 = tl.shouldCapture) == null ? void 0 : _a2.call(tl, timelineTransactionUpdate, tl)) != null ? _b2 : true;
1801
+ if (willCapture) {
1802
+ tl.history.push(timelineTransactionUpdate);
1803
+ tl.at = tl.history.length;
1804
+ tl.subject.next(timelineTransactionUpdate);
1805
+ }
1806
+ }
1807
+ tl.transactionKey = null;
1808
+ store.logger.info(
1809
+ `\u231B`,
1810
+ `timeline`,
1811
+ tl.key,
1812
+ `got a transaction_update "${update2.key}"`
1813
+ );
1814
+ }
1815
+ );
1816
+ }
1817
+ } else if (currentSelectorKey && currentSelectorTime) {
1818
+ let latestUpdate = tl.history.at(-1);
1819
+ if (currentSelectorTime !== tl.selectorTime) {
1820
+ latestUpdate = {
1821
+ type: `selector_update`,
1822
+ timestamp: currentSelectorTime,
1823
+ key: currentSelectorKey,
1824
+ atomUpdates: []
1825
+ };
1826
+ latestUpdate.atomUpdates.push(__spreadValues({
1827
+ key: atom2.key,
1828
+ type: `atom_update`
1829
+ }, update));
1830
+ if (tl.at !== tl.history.length) {
1831
+ tl.history.splice(tl.at);
1832
+ }
1833
+ tl.history.push(latestUpdate);
1834
+ store.logger.info(
1835
+ `\u231B`,
1836
+ `timeline`,
1837
+ tl.key,
1838
+ `got a selector_update "${currentSelectorKey}" with`,
1839
+ latestUpdate.atomUpdates.map((atomUpdate) => atomUpdate.key)
1840
+ );
1841
+ tl.at = tl.history.length;
1842
+ tl.selectorTime = currentSelectorTime;
1843
+ } else {
1844
+ if ((latestUpdate == null ? void 0 : latestUpdate.type) === `selector_update`) {
1845
+ latestUpdate.atomUpdates.push(__spreadValues({
1846
+ key: atom2.key,
1847
+ type: `atom_update`
1848
+ }, update));
1849
+ store.logger.info(
1850
+ `\u231B`,
1851
+ `timeline`,
1852
+ tl.key,
1853
+ `set selector_update "${currentSelectorKey}" to`,
1854
+ latestUpdate == null ? void 0 : latestUpdate.atomUpdates.map((atomUpdate) => atomUpdate.key)
1855
+ );
1856
+ }
1857
+ }
1858
+ if (latestUpdate) {
1859
+ const willCaptureSelectorUpdate = (_d = (_c = tl.shouldCapture) == null ? void 0 : _c.call(tl, latestUpdate, tl)) != null ? _d : true;
1860
+ if (willCaptureSelectorUpdate) {
1861
+ tl.subject.next(latestUpdate);
1862
+ } else {
1863
+ tl.history.pop();
1864
+ tl.at = tl.history.length;
1865
+ }
1866
+ }
1867
+ } else {
1868
+ const timestamp = Date.now();
1869
+ tl.selectorTime = null;
1870
+ if (tl.at !== tl.history.length) {
1871
+ tl.history.splice(tl.at);
1872
+ }
1873
+ const atomUpdate = {
1874
+ type: `atom_update`,
1875
+ timestamp,
1876
+ key: atom2.key,
1877
+ oldValue: update.oldValue,
1878
+ newValue: update.newValue
1879
+ };
1880
+ if (atom2.family) {
1881
+ atomUpdate.family = atom2.family;
1882
+ }
1883
+ const willCapture = (_f = (_e = tl.shouldCapture) == null ? void 0 : _e.call(tl, atomUpdate, tl)) != null ? _f : true;
1884
+ store.logger.info(
1885
+ `\u231B`,
1886
+ `timeline`,
1887
+ tl.key,
1888
+ `got an atom_update to "${atom2.key}"`
1889
+ );
1890
+ if (willCapture) {
1891
+ tl.history.push(atomUpdate);
1892
+ tl.at = tl.history.length;
1893
+ tl.subject.next(atomUpdate);
1894
+ }
1895
+ }
1896
+ }
1897
+ });
1898
+ };
1899
+
1900
+ // internal/src/mutable/is-mutable.ts
1901
+ function isMutable(atomOrTokenOrFamily, store) {
1902
+ if (`mutable` in atomOrTokenOrFamily) {
1903
+ return atomOrTokenOrFamily.mutable;
1904
+ }
1905
+ if (atomOrTokenOrFamily.type === `atom_family`) {
1906
+ return false;
1907
+ }
1908
+ if (`default` in atomOrTokenOrFamily) {
1909
+ return false;
1910
+ }
1911
+ if (!store) {
1912
+ throw new Error(`Cannot check mutability without a store`);
1913
+ }
1914
+ const atom2 = withdraw(atomOrTokenOrFamily, store);
1915
+ if (!atom2) {
1916
+ throw new Error(`Cannot check mutability without an atom`);
1917
+ }
1918
+ if (`mutable` in atom2) {
1919
+ return atom2.mutable;
1920
+ }
1921
+ return false;
1922
+ }
1923
+
1924
+ // internal/src/timeline/create-timeline.ts
1925
+ function createTimeline(options, store, data) {
1926
+ var _a, _b;
1927
+ const tl = __spreadProps(__spreadValues({
1928
+ type: `timeline`,
1929
+ key: options.key,
1930
+ at: 0,
1931
+ timeTraveling: null,
1932
+ selectorTime: null,
1933
+ transactionKey: null
1934
+ }, data), {
1935
+ history: (_a = data == null ? void 0 : data.history.map((update) => __spreadValues({}, update))) != null ? _a : [],
1936
+ install: (store2) => createTimeline(options, store2, tl),
1937
+ subject: new Subject()
1938
+ });
1939
+ if (options.shouldCapture) {
1940
+ tl.shouldCapture = options.shouldCapture;
1941
+ }
1942
+ const timelineKey = options.key;
1943
+ const target = newest(store);
1944
+ for (const tokenOrFamily of options.atoms) {
1945
+ let atomKey = tokenOrFamily.key;
1946
+ const existingTimelineKey = target.timelineAtoms.getRelatedKey(atomKey);
1947
+ if (existingTimelineKey) {
1948
+ store.logger.error(
1949
+ `\u274C`,
1950
+ `timeline`,
1951
+ options.key,
1952
+ `Failed to add atom "${atomKey}" because it already belongs to timeline "${existingTimelineKey}"`
1953
+ );
1954
+ continue;
1955
+ }
1956
+ if (tokenOrFamily.type === `atom_family`) {
1957
+ let family = tokenOrFamily;
1958
+ if (isMutable(family)) {
1959
+ family = getUpdateFamily(family, store);
1960
+ atomKey = family.key;
1961
+ }
1962
+ family.subject.subscribe(`timeline:${options.key}`, (token2) => {
1963
+ addAtomToTimeline(token2, tl, store);
1964
+ });
1965
+ for (const atom2 of target.atoms.values()) {
1966
+ if (((_b = atom2.family) == null ? void 0 : _b.key) === family.key) {
1967
+ addAtomToTimeline(atom2, tl, store);
1968
+ }
1969
+ }
1970
+ } else {
1971
+ let atom2 = withdraw(tokenOrFamily, store);
1972
+ if (atom2 === void 0) {
1973
+ store.logger.error(
1974
+ `\u274C`,
1975
+ `timeline`,
1976
+ options.key,
1977
+ `Failed to add atom "${atomKey}" because it does not exist in the store`
1978
+ );
1979
+ continue;
1980
+ }
1981
+ if (isMutable(atom2)) {
1982
+ const updateAtom = withdraw(getUpdateToken(atom2), store);
1983
+ if (updateAtom === void 0) {
1984
+ store.logger.error(
1985
+ `\u274C`,
1986
+ `timeline`,
1987
+ options.key,
1988
+ `Failed to add update atom "${atomKey}" because it does not exist in the store`
1989
+ );
1990
+ continue;
1991
+ }
1992
+ atom2 = updateAtom;
1993
+ atomKey = atom2.key;
1994
+ }
1995
+ if (`family` in atom2) {
1996
+ const familyTimelineKey = target.timelineAtoms.getRelatedKey(
1997
+ atom2.family.key
1998
+ );
1999
+ if (familyTimelineKey) {
2000
+ store.logger.error(
2001
+ `\u274C`,
2002
+ `timeline`,
2003
+ options.key,
2004
+ `Failed to add atom "${atom2.key}" because its family "${atom2.family.key}" already belongs to timeline "${familyTimelineKey}"`
2005
+ );
2006
+ continue;
2007
+ }
2008
+ }
2009
+ addAtomToTimeline(atom2, tl, store);
2010
+ }
2011
+ target.timelineAtoms.set({ atomKey, timelineKey });
2012
+ }
2013
+ store.timelines.set(options.key, tl);
2014
+ const token = {
2015
+ key: timelineKey,
2016
+ type: `timeline`
2017
+ };
2018
+ store.subject.timelineCreation.next(token);
2019
+ return token;
2020
+ }
2021
+
2022
+ // internal/src/timeline/time-travel.ts
2023
+ var timeTravel = (direction, token, store) => {
2024
+ const action = direction === `forward` ? `redo` : `undo`;
2025
+ store.logger.info(
2026
+ direction === `forward` ? `\u23E9` : `\u23EA`,
2027
+ `timeline`,
2028
+ token.key,
2029
+ action
2030
+ );
2031
+ const timelineData = store.timelines.get(token.key);
2032
+ if (!timelineData) {
2033
+ store.logger.error(
2034
+ `\u{1F41E}`,
2035
+ `timeline`,
2036
+ token.key,
2037
+ `Failed to ${action}. This timeline has not been initialized.`
2038
+ );
2039
+ return;
2040
+ }
2041
+ if (direction === `forward` && timelineData.at === timelineData.history.length || direction === `backward` && timelineData.at === 0) {
2042
+ store.logger.warn(
2043
+ `\u{1F481}`,
2044
+ `timeline`,
2045
+ token.key,
2046
+ `Failed to ${action} at the ${direction === `forward` ? `end` : `beginning`} of timeline "${token.key}". There is nothing to ${action}.`
2047
+ );
2048
+ return;
2049
+ }
2050
+ timelineData.timeTraveling = direction === `forward` ? `into_future` : `into_past`;
2051
+ if (direction === `backward`) {
2052
+ --timelineData.at;
2053
+ }
2054
+ const update = timelineData.history[timelineData.at];
2055
+ const updateValues = (atomUpdate) => {
2056
+ const { key, newValue, oldValue } = atomUpdate;
2057
+ const value = direction === `forward` ? newValue : oldValue;
2058
+ setState({ key, type: `atom` }, value, store);
2059
+ };
2060
+ const updateValuesFromTransactionUpdate = (transactionUpdate) => {
2061
+ const updates = direction === `forward` ? transactionUpdate.updates : [...transactionUpdate.updates].reverse();
2062
+ for (const updateFromTransaction of updates) {
2063
+ if (`newValue` in updateFromTransaction) {
2064
+ updateValues(updateFromTransaction);
2065
+ } else {
2066
+ updateValuesFromTransactionUpdate(updateFromTransaction);
2067
+ }
2068
+ }
2069
+ };
2070
+ switch (update.type) {
2071
+ case `atom_update`: {
2072
+ updateValues(update);
2073
+ break;
2074
+ }
2075
+ case `selector_update`: {
2076
+ const updates = direction === `forward` ? update.atomUpdates : [...update.atomUpdates].reverse();
2077
+ for (const atomUpdate of updates) {
2078
+ updateValues(atomUpdate);
2079
+ }
2080
+ break;
2081
+ }
2082
+ case `transaction_update`: {
2083
+ updateValuesFromTransactionUpdate(update);
2084
+ break;
2085
+ }
2086
+ }
2087
+ if (direction === `forward`) {
2088
+ ++timelineData.at;
2089
+ }
2090
+ timelineData.subject.next(action);
2091
+ timelineData.timeTraveling = null;
2092
+ store.logger.info(
2093
+ `\u23F9\uFE0F`,
2094
+ `timeline`,
2095
+ token.key,
2096
+ `"${token.key}" is now at ${timelineData.at} / ${timelineData.history.length}`
2097
+ );
2098
+ };
2099
+
2100
+ // internal/src/transaction/abort-transaction.ts
2101
+ var abortTransaction = (store) => {
2102
+ const target = newest(store);
2103
+ if (target.transactionMeta === null || target.parent === null) {
2104
+ store.logger.warn(
2105
+ `\u{1F41E}`,
2106
+ `transaction`,
2107
+ `???`,
2108
+ `abortTransaction called outside of a transaction. This is probably a bug in AtomIO.`
2109
+ );
2110
+ return;
2111
+ }
2112
+ store.logger.info(
2113
+ `\u{1FA82}`,
2114
+ `transaction`,
2115
+ target.transactionMeta.update.key,
2116
+ `Aborting transaction`
2117
+ );
2118
+ target.parent.child = null;
2119
+ };
2120
+
2121
+ // internal/src/transaction/apply-transaction.ts
2122
+ function ingestAtomUpdate(update, parent, child) {
2123
+ var _a, _b, _c;
2124
+ const { key, newValue } = update;
2125
+ const token = { key, type: `atom` };
2126
+ if (!parent.valueMap.has(token.key)) {
2127
+ if (token.family) {
2128
+ const family = parent.families.get(token.family.key);
2129
+ if (family) {
2130
+ family(token.family.subKey);
2131
+ }
2132
+ } else {
2133
+ const newAtom = child.atoms.get(token.key);
2134
+ if (!newAtom) {
2135
+ throw new Error(
2136
+ `Absurd Error: Atom "${token.key}" not found while copying updates from transaction "${(_a = child.transactionMeta) == null ? void 0 : _a.update.key}" to store "${parent.config.name}"`
2137
+ );
2138
+ }
2139
+ parent.atoms.set(newAtom.key, newAtom);
2140
+ parent.valueMap.set(newAtom.key, newAtom.default);
2141
+ parent.logger.info(
2142
+ `\u{1F528}`,
2143
+ `transaction`,
2144
+ (_c = (_b = child.transactionMeta) == null ? void 0 : _b.update.key) != null ? _c : `???`,
2145
+ `Adding atom "${newAtom.key}"`
2146
+ );
2147
+ }
2148
+ }
2149
+ setState(token, newValue, parent);
2150
+ }
2151
+ function ingestTransactionUpdate(transactionUpdate, parent, child) {
2152
+ for (const update of transactionUpdate.updates) {
2153
+ if (`newValue` in update) {
2154
+ ingestAtomUpdate(update, parent, child);
2155
+ } else {
2156
+ ingestTransactionUpdate(update, parent, child);
2157
+ }
2158
+ }
2159
+ }
2160
+ var applyTransaction = (output, store) => {
2161
+ var _a;
2162
+ const child = newest(store);
2163
+ const { parent } = child;
2164
+ if (parent === null || child.transactionMeta === null || ((_a = child.transactionMeta) == null ? void 0 : _a.phase) !== `building`) {
2165
+ store.logger.warn(
2166
+ `\u{1F41E}`,
2167
+ `transaction`,
2168
+ `???`,
2169
+ `applyTransaction called outside of a transaction. This is probably a bug in AtomIO.`
2170
+ );
2171
+ return;
2172
+ }
2173
+ child.transactionMeta.phase = `applying`;
2174
+ child.transactionMeta.update.output = output;
2175
+ parent.child = null;
2176
+ parent.subject.transactionApplying.next(child.transactionMeta);
2177
+ const { updates } = child.transactionMeta.update;
2178
+ store.logger.info(
2179
+ `\u{1F6C4}`,
2180
+ `transaction`,
2181
+ child.transactionMeta.update.key,
2182
+ `Applying transaction with ${updates.length} updates:`,
2183
+ updates
2184
+ );
2185
+ if (parent.transactionMeta === null) {
2186
+ ingestTransactionUpdate(child.transactionMeta.update, parent, child);
2187
+ const myTransaction = withdraw(
2188
+ { key: child.transactionMeta.update.key, type: `transaction` },
2189
+ store
2190
+ );
2191
+ myTransaction == null ? void 0 : myTransaction.subject.next(child.transactionMeta.update);
2192
+ store.logger.info(
2193
+ `\u{1F6EC}`,
2194
+ `transaction`,
2195
+ child.transactionMeta.update.key,
2196
+ `Finished applying transaction.`
2197
+ );
2198
+ } else {
2199
+ ingestTransactionUpdate(child.transactionMeta.update, parent, child);
2200
+ parent.transactionMeta.update.updates.push(child.transactionMeta.update);
2201
+ }
2202
+ parent.subject.transactionApplying.next(null);
2203
+ };
2204
+
2205
+ // internal/src/transaction/build-transaction.ts
2206
+ var buildTransaction = (key, params, store) => {
2207
+ const parent = newest(store);
2208
+ parent.child = {
2209
+ parent,
2210
+ child: null,
2211
+ subject: parent.subject,
2212
+ loggers: parent.loggers,
2213
+ logger: parent.logger,
2214
+ config: parent.config,
2215
+ transactionMeta: {
2216
+ phase: `building`,
2217
+ time: Date.now(),
2218
+ update: {
2219
+ key,
2220
+ updates: [],
2221
+ params,
2222
+ output: void 0
2223
+ }
2224
+ },
2225
+ atoms: new LazyMap(parent.atoms),
2226
+ atomsThatAreDefault: new Set(parent.atomsThatAreDefault),
2227
+ families: new LazyMap(parent.families),
2228
+ operation: { open: false },
2229
+ readonlySelectors: new LazyMap(parent.readonlySelectors),
2230
+ timelines: new LazyMap(parent.timelines),
2231
+ timelineAtoms: new Junction(parent.timelineAtoms.toJSON()),
2232
+ trackers: /* @__PURE__ */ new Map(),
2233
+ transactions: new LazyMap(parent.transactions),
2234
+ selectorAtoms: new Junction(parent.selectorAtoms.toJSON()),
2235
+ selectorGraph: new Junction(parent.selectorGraph.toJSON(), {
2236
+ makeContentKey: (...keys) => keys.sort().join(`:`)
2237
+ }),
2238
+ selectors: new LazyMap(parent.selectors),
2239
+ valueMap: new LazyMap(parent.valueMap)
2240
+ };
2241
+ store.logger.info(
2242
+ `\u{1F6EB}`,
2243
+ `transaction`,
2244
+ key,
2245
+ `Building transaction with params:`,
2246
+ params
2247
+ );
2248
+ };
2249
+
2250
+ // internal/src/transaction/create-transaction.ts
2251
+ function createTransaction(options, store) {
2252
+ const newTransaction = {
2253
+ key: options.key,
2254
+ type: `transaction`,
2255
+ run: (...params) => {
2256
+ buildTransaction(options.key, params, store);
2257
+ try {
2258
+ const output = options.do(
2259
+ {
2260
+ get: (token2) => getState(token2, store),
2261
+ set: (token2, value) => setState(token2, value, store),
2262
+ run: (token2) => runTransaction(token2, store)
2263
+ },
2264
+ ...params
2265
+ );
2266
+ applyTransaction(output, store);
2267
+ return output;
2268
+ } catch (thrown) {
2269
+ abortTransaction(store);
2270
+ store.logger.warn(`\u{1F4A5}`, `transaction`, options.key, `caught:`, thrown);
2271
+ throw thrown;
2272
+ }
2273
+ },
2274
+ install: (store2) => createTransaction(options, store2),
2275
+ subject: new Subject()
2276
+ };
2277
+ const target = newest(store);
2278
+ target.transactions.set(newTransaction.key, newTransaction);
2279
+ const token = deposit(newTransaction);
2280
+ store.subject.transactionCreation.next(token);
2281
+ return token;
2282
+ }
24
2283
 
25
2284
  // src/atom.ts
26
2285
  function atom(options) {
27
2286
  if (`mutable` in options) {
28
- return Internal3.createMutableAtom(options, Internal3.IMPLICIT.STORE);
2287
+ return createMutableAtom(options, IMPLICIT.STORE);
29
2288
  }
30
- return Internal3.createAtom(options, void 0, Internal3.IMPLICIT.STORE);
2289
+ return createAtom(options, void 0, IMPLICIT.STORE);
31
2290
  }
32
2291
  function atomFamily(options) {
33
2292
  if (`mutable` in options) {
34
- return Internal3.createMutableAtomFamily(options, Internal3.IMPLICIT.STORE);
2293
+ return createMutableAtomFamily(options, IMPLICIT.STORE);
35
2294
  }
36
- return Internal3.createAtomFamily(options, Internal3.IMPLICIT.STORE);
2295
+ return createAtomFamily(options, IMPLICIT.STORE);
37
2296
  }
38
- function dispose(token, store = Internal3__namespace.IMPLICIT.STORE) {
2297
+
2298
+ // src/dispose.ts
2299
+ function dispose(token, store = IMPLICIT.STORE) {
39
2300
  switch (token.type) {
40
2301
  case `atom`:
41
- Internal3__namespace.deleteAtom(token, store);
2302
+ deleteAtom2(token, store);
42
2303
  break;
43
2304
  case `selector`:
44
2305
  case `readonly_selector`:
45
- Internal3__namespace.deleteSelector(token, store);
2306
+ deleteSelector(token, store);
46
2307
  break;
47
2308
  }
48
2309
  }
49
- function getState(token, store = Internal3__namespace.IMPLICIT.STORE) {
2310
+
2311
+ // src/get-state.ts
2312
+ function getState(token, store = IMPLICIT.STORE) {
50
2313
  var _a;
51
- const state = (_a = Internal3__namespace.withdraw(token, store)) != null ? _a : Internal3__namespace.withdrawNewFamilyMember(token, store);
2314
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
52
2315
  if (state === void 0) {
53
- throw new Internal3__namespace.NotFoundError(token, store);
2316
+ throw new NotFoundError(token, store);
54
2317
  }
55
- return Internal3__namespace.readOrComputeValue(state, store);
2318
+ return readOrComputeValue(state, store);
56
2319
  }
57
2320
 
58
2321
  // src/logger.ts
@@ -90,78 +2353,90 @@ var AtomIOLogger = class {
90
2353
  };
91
2354
  }
92
2355
  };
2356
+
2357
+ // src/selector.ts
93
2358
  function selector(options) {
94
- return Internal3.createSelector(options, void 0, Internal3.IMPLICIT.STORE);
2359
+ return createSelector(options, void 0, IMPLICIT.STORE);
95
2360
  }
96
2361
  function selectorFamily(options) {
97
- return Internal3.createSelectorFamily(options, Internal3.IMPLICIT.STORE);
2362
+ return createSelectorFamily(options, IMPLICIT.STORE);
98
2363
  }
99
- function setState(token, value, store = Internal3__namespace.IMPLICIT.STORE) {
2364
+
2365
+ // src/set-state.ts
2366
+ function setState(token, value, store = IMPLICIT.STORE) {
100
2367
  var _a;
101
- const rejection = Internal3__namespace.openOperation(token, store);
2368
+ const rejection = openOperation(token, store);
102
2369
  if (rejection) {
103
2370
  return;
104
2371
  }
105
- const state = (_a = Internal3__namespace.withdraw(token, store)) != null ? _a : Internal3__namespace.withdrawNewFamilyMember(token, store);
2372
+ const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
106
2373
  if (state === void 0) {
107
- throw new Internal3__namespace.NotFoundError(token, store);
2374
+ throw new NotFoundError(token, store);
108
2375
  }
109
- Internal3__namespace.setAtomOrSelector(state, value, store);
110
- Internal3__namespace.closeOperation(store);
2376
+ setAtomOrSelector(state, value, store);
2377
+ closeOperation(store);
111
2378
  }
2379
+
2380
+ // src/silo.ts
112
2381
  var Silo = class {
113
2382
  constructor(name, fromStore = null) {
114
- const s = new Internal3.Store(name, fromStore);
2383
+ const s = new Store(name, fromStore);
115
2384
  this.store = s;
116
2385
  this.atom = (options) => {
117
2386
  if (`mutable` in options) {
118
- return Internal3.createMutableAtom(options, s);
2387
+ return createMutableAtom(options, s);
119
2388
  }
120
- return Internal3.createAtom(options, void 0, s);
2389
+ return createAtom(options, void 0, s);
121
2390
  };
122
2391
  this.atomFamily = (options) => {
123
2392
  if (`mutable` in options) {
124
- return Internal3.createMutableAtomFamily(options, s);
2393
+ return createMutableAtomFamily(options, s);
125
2394
  }
126
- return Internal3.createAtomFamily(options, s);
2395
+ return createAtomFamily(options, s);
127
2396
  };
128
- this.selector = (options) => Internal3.createSelector(options, void 0, s);
129
- this.selectorFamily = (options) => Internal3.createSelectorFamily(options, s);
130
- this.transaction = (options) => Internal3.createTransaction(options, s);
131
- this.timeline = (options) => Internal3.createTimeline(options, s);
2397
+ this.selector = (options) => createSelector(options, void 0, s);
2398
+ this.selectorFamily = (options) => createSelectorFamily(options, s);
2399
+ this.transaction = (options) => createTransaction(options, s);
2400
+ this.timeline = (options) => createTimeline(options, s);
132
2401
  this.getState = (token) => getState(token, s);
133
2402
  this.setState = (token, newValue) => setState(token, newValue, s);
134
2403
  this.subscribe = (token, handler, key) => subscribe(token, handler, key, s);
135
- this.undo = (token) => Internal3.timeTravel(`backward`, token, s);
136
- this.redo = (token) => Internal3.timeTravel(`forward`, token, s);
2404
+ this.undo = (token) => timeTravel(`backward`, token, s);
2405
+ this.redo = (token) => timeTravel(`forward`, token, s);
137
2406
  }
138
2407
  };
139
- function subscribe(token, handleUpdate, key = Math.random().toString(36).slice(2), store = Internal3.IMPLICIT.STORE) {
2408
+
2409
+ // src/subscribe.ts
2410
+ function subscribe(token, handleUpdate, key = Math.random().toString(36).slice(2), store = IMPLICIT.STORE) {
140
2411
  switch (token.type) {
141
2412
  case `atom`:
142
2413
  case `readonly_selector`:
143
2414
  case `selector`:
144
- return Internal3.subscribeToState(token, handleUpdate, key, store);
2415
+ return subscribeToState(token, handleUpdate, key, store);
145
2416
  case `transaction`:
146
- return Internal3.subscribeToTransaction(token, handleUpdate, key, store);
2417
+ return subscribeToTransaction(token, handleUpdate, key, store);
147
2418
  case `timeline`:
148
- return Internal3.subscribeToTimeline(token, handleUpdate, key, store);
2419
+ return subscribeToTimeline(token, handleUpdate, key, store);
149
2420
  }
150
2421
  }
2422
+
2423
+ // src/timeline.ts
151
2424
  var timeline = (options) => {
152
- return Internal3.createTimeline(options, Internal3.IMPLICIT.STORE);
2425
+ return createTimeline(options, IMPLICIT.STORE);
153
2426
  };
154
2427
  var redo = (timeline2) => {
155
- Internal3.timeTravel(`forward`, timeline2, Internal3.IMPLICIT.STORE);
2428
+ timeTravel(`forward`, timeline2, IMPLICIT.STORE);
156
2429
  };
157
2430
  var undo = (timeline2) => {
158
- Internal3.timeTravel(`backward`, timeline2, Internal3.IMPLICIT.STORE);
2431
+ timeTravel(`backward`, timeline2, IMPLICIT.STORE);
159
2432
  };
2433
+
2434
+ // src/transaction.ts
160
2435
  function transaction(options) {
161
- return Internal3.createTransaction(options, Internal3.IMPLICIT.STORE);
2436
+ return createTransaction(options, IMPLICIT.STORE);
162
2437
  }
163
- var runTransaction = (token, store = Internal3.IMPLICIT.STORE) => (...parameters) => {
164
- const tx = Internal3.withdraw(token, store);
2438
+ var runTransaction = (token, store = IMPLICIT.STORE) => (...parameters) => {
2439
+ const tx = withdraw(token, store);
165
2440
  if (tx) {
166
2441
  return tx.run(...parameters);
167
2442
  }