atom.io 0.14.4 → 0.14.6

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 (91) hide show
  1. package/data/dist/index.cjs +47 -1416
  2. package/data/dist/index.cjs.map +1 -1
  3. package/data/dist/index.d.cts +16 -317
  4. package/data/dist/index.d.ts +16 -317
  5. package/data/dist/index.js +4 -6
  6. package/data/dist/index.js.map +1 -1
  7. package/dist/chunk-NYTGCPHB.js +261 -0
  8. package/dist/chunk-NYTGCPHB.js.map +1 -0
  9. package/dist/chunk-PZLG2HP3.js +36 -0
  10. package/dist/chunk-PZLG2HP3.js.map +1 -0
  11. package/dist/chunk-XACXFU3B.js +46 -0
  12. package/dist/chunk-XACXFU3B.js.map +1 -0
  13. package/dist/index.cjs +55 -2330
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +21 -269
  16. package/dist/index.d.ts +21 -269
  17. package/dist/index.js +152 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/metafile-cjs.json +1 -1
  20. package/dist/metafile-esm.json +1 -1
  21. package/internal/dist/index.cjs +30 -159
  22. package/internal/dist/index.cjs.map +1 -1
  23. package/internal/dist/index.d.cts +28 -229
  24. package/internal/dist/index.d.ts +28 -229
  25. package/internal/dist/index.js +2027 -1
  26. package/internal/dist/index.js.map +1 -1
  27. package/introspection/dist/index.cjs +45 -1565
  28. package/introspection/dist/index.cjs.map +1 -1
  29. package/introspection/dist/index.d.cts +4 -376
  30. package/introspection/dist/index.d.ts +4 -376
  31. package/introspection/dist/index.js +298 -2
  32. package/introspection/dist/index.js.map +1 -1
  33. package/json/dist/index.cjs +5 -1502
  34. package/json/dist/index.cjs.map +1 -1
  35. package/json/dist/index.d.cts +7 -373
  36. package/json/dist/index.d.ts +7 -373
  37. package/json/dist/index.js +37 -1
  38. package/json/dist/index.js.map +1 -1
  39. package/package.json +1 -1
  40. package/react/dist/index.cjs +14 -1549
  41. package/react/dist/index.cjs.map +1 -1
  42. package/react/dist/index.d.cts +4 -377
  43. package/react/dist/index.d.ts +4 -377
  44. package/react/dist/index.js +62 -2
  45. package/react/dist/index.js.map +1 -1
  46. package/react-devtools/dist/index.cjs +135 -2153
  47. package/react-devtools/dist/index.cjs.map +1 -1
  48. package/react-devtools/dist/index.d.cts +24 -149
  49. package/react-devtools/dist/index.d.ts +24 -149
  50. package/react-devtools/dist/index.js +5 -3
  51. package/react-devtools/dist/index.js.map +1 -1
  52. package/realtime-client/dist/index.cjs +34 -1529
  53. package/realtime-client/dist/index.cjs.map +1 -1
  54. package/realtime-client/dist/index.d.cts +12 -384
  55. package/realtime-client/dist/index.d.ts +12 -384
  56. package/realtime-client/dist/index.js +165 -2
  57. package/realtime-client/dist/index.js.map +1 -1
  58. package/realtime-react/dist/index.cjs +35 -1727
  59. package/realtime-react/dist/index.cjs.map +1 -1
  60. package/realtime-react/dist/index.d.cts +9 -49
  61. package/realtime-react/dist/index.d.ts +9 -49
  62. package/realtime-react/dist/index.js +66 -4
  63. package/realtime-react/dist/index.js.map +1 -1
  64. package/realtime-server/dist/index.cjs +57 -1548
  65. package/realtime-server/dist/index.cjs.map +1 -1
  66. package/realtime-server/dist/index.d.cts +11 -394
  67. package/realtime-server/dist/index.d.ts +11 -394
  68. package/realtime-server/dist/index.js +21 -27
  69. package/realtime-server/dist/index.js.map +1 -1
  70. package/realtime-testing/dist/index.cjs +15 -2376
  71. package/realtime-testing/dist/index.cjs.map +1 -1
  72. package/realtime-testing/dist/index.d.cts +3 -489
  73. package/realtime-testing/dist/index.d.ts +3 -489
  74. package/realtime-testing/dist/index.js +10 -9
  75. package/realtime-testing/dist/index.js.map +1 -1
  76. package/transceivers/set-rtx/dist/index.cjs +9 -31
  77. package/transceivers/set-rtx/dist/index.cjs.map +1 -1
  78. package/transceivers/set-rtx/dist/index.d.cts +3 -29
  79. package/transceivers/set-rtx/dist/index.d.ts +3 -29
  80. package/transceivers/set-rtx/dist/index.js +3 -2
  81. package/transceivers/set-rtx/dist/index.js.map +1 -1
  82. package/dist/chunk-GMN5KH6A.js +0 -309
  83. package/dist/chunk-GMN5KH6A.js.map +0 -1
  84. package/dist/chunk-HQWWV67P.js +0 -62
  85. package/dist/chunk-HQWWV67P.js.map +0 -1
  86. package/dist/chunk-MK4OJD24.js +0 -67
  87. package/dist/chunk-MK4OJD24.js.map +0 -1
  88. package/dist/chunk-N7ADBQJG.js +0 -176
  89. package/dist/chunk-N7ADBQJG.js.map +0 -1
  90. package/dist/chunk-PURABO5G.js +0 -2581
  91. package/dist/chunk-PURABO5G.js.map +0 -1
@@ -1,7 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ var internal = require('atom.io/internal');
3
4
  var React2 = require('react');
4
5
  var jsxRuntime = require('react/jsx-runtime');
6
+ var atom_io = require('atom.io');
5
7
 
6
8
  function _interopNamespace(e) {
7
9
  if (e && e.__esModule) return e;
@@ -23,1551 +25,14 @@ function _interopNamespace(e) {
23
25
 
24
26
  var React2__namespace = /*#__PURE__*/_interopNamespace(React2);
25
27
 
26
- var __defProp = Object.defineProperty;
27
- var __defProps = Object.defineProperties;
28
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
29
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
30
- var __hasOwnProp = Object.prototype.hasOwnProperty;
31
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
32
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
33
- var __spreadValues = (a, b) => {
34
- for (var prop in b || (b = {}))
35
- if (__hasOwnProp.call(b, prop))
36
- __defNormalProp(a, prop, b[prop]);
37
- if (__getOwnPropSymbols)
38
- for (var prop of __getOwnPropSymbols(b)) {
39
- if (__propIsEnum.call(b, prop))
40
- __defNormalProp(a, prop, b[prop]);
41
- }
42
- return a;
43
- };
44
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
45
-
46
- // src/get-state.ts
47
- function getState(token, store = IMPLICIT.STORE) {
48
- var _a;
49
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
50
- if (state === void 0) {
51
- throw new NotFoundError(token, store);
52
- }
53
- return readOrComputeValue(state, store);
54
- }
55
-
56
- // src/logger.ts
57
- var simpleLog = (logLevel) => (icon, tokenType, tokenKey, message, ...rest) => {
58
- console[logLevel](`${icon} ${tokenType} "${tokenKey}" ${message}`, ...rest);
59
- };
60
- var simpleLogger = {
61
- error: simpleLog(`error`),
62
- info: simpleLog(`info`),
63
- warn: simpleLog(`warn`)
64
- };
65
- var AtomIOLogger = class {
66
- constructor(logLevel, filter, logger = simpleLogger) {
67
- this.logLevel = logLevel;
68
- this.filter = filter;
69
- this.logger = logger;
70
- this.error = (...args) => {
71
- var _a, _b;
72
- if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== null) {
73
- this.logger.error(...args);
74
- }
75
- };
76
- this.info = (...args) => {
77
- var _a, _b;
78
- if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel === `info`) {
79
- this.logger.info(...args);
80
- }
81
- };
82
- this.warn = (...args) => {
83
- var _a, _b;
84
- if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== `error` && this.logLevel !== null) {
85
- this.logger.warn(...args);
86
- }
87
- };
88
- }
89
- };
90
-
91
- // src/set-state.ts
92
- function setState(token, value, store = IMPLICIT.STORE) {
93
- var _a;
94
- const rejection = openOperation(token, store);
95
- if (rejection) {
96
- return;
97
- }
98
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
99
- if (state === void 0) {
100
- throw new NotFoundError(token, store);
101
- }
102
- setAtomOrSelector(state, value, store);
103
- closeOperation(store);
104
- }
105
-
106
- // src/timeline.ts
107
- var redo = (timeline) => {
108
- timeTravel(`forward`, timeline, IMPLICIT.STORE);
109
- };
110
- var undo = (timeline) => {
111
- timeTravel(`backward`, timeline, IMPLICIT.STORE);
112
- };
113
-
114
- // internal/src/future.ts
115
- var Future = class extends Promise {
116
- constructor(executor) {
117
- super((resolve, reject) => {
118
- const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
119
- const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
120
- if (typeof executor === `function`) {
121
- executor(pass, fail);
122
- } else {
123
- executor.then(pass, fail);
124
- }
125
- });
126
- this.isCanceled = false;
127
- }
128
- cancel() {
129
- this.isCanceled = true;
130
- }
131
- };
132
-
133
- // internal/src/lineage.ts
134
- function newest(scion) {
135
- while (scion.child !== null) {
136
- scion = scion.child;
137
- }
138
- return scion;
139
- }
140
-
141
- // internal/src/caching.ts
142
- function cacheValue(key, value, subject, store) {
143
- const target = newest(store);
144
- const currentValue = target.valueMap.get(key);
145
- if (currentValue instanceof Future) {
146
- currentValue.cancel();
147
- }
148
- if (value instanceof Promise) {
149
- const future = new Future(value);
150
- newest(store).valueMap.set(key, future);
151
- future.then((resolved) => {
152
- if (future.isCanceled) {
153
- return;
154
- }
155
- cacheValue(key, resolved, subject, store);
156
- subject.next({ newValue: resolved, oldValue: future });
157
- }).catch((thrown) => {
158
- if (thrown !== `canceled`) {
159
- store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
160
- }
161
- });
162
- return future;
163
- }
164
- target.valueMap.set(key, value);
165
- return value;
166
- }
167
- var readCachedValue = (key, store) => {
168
- return newest(store).valueMap.get(key);
169
- };
170
- var isValueCached = (key, store) => {
171
- return newest(store).valueMap.has(key);
172
- };
173
- var evictCachedValue = (key, store) => {
174
- const core = newest(store);
175
- const currentValue = core.valueMap.get(key);
176
- if (currentValue instanceof Future) {
177
- currentValue.cancel();
178
- }
179
- if (core.operation.open) {
180
- core.operation.prev.set(key, currentValue);
181
- }
182
- core.valueMap.delete(key);
183
- store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
184
- };
185
-
186
- // json/src/select-json.ts
187
- var selectJson = (atom, transform, store = IMPLICIT.STORE) => {
188
- return createSelector(
189
- {
190
- key: `${atom.key}:JSON`,
191
- get: ({ get }) => transform.toJson(get(atom)),
192
- set: ({ set }, newValue) => set(atom, transform.fromJson(newValue))
193
- },
194
- void 0,
195
- store
196
- );
197
- };
198
-
199
- // internal/src/read-or-compute-value.ts
200
- var readOrComputeValue = (state, store) => {
201
- if (isValueCached(state.key, store)) {
202
- store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
203
- return readCachedValue(state.key, store);
204
- }
205
- if (state.type !== `atom`) {
206
- store.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
207
- return state.get();
208
- }
209
- const fallback = state.default instanceof Function ? state.default() : state.default;
210
- store.logger.info(
211
- `\u{1F481}`,
212
- `atom`,
213
- state.key,
214
- `could not find cached value; using default`,
215
- fallback
216
- );
217
- return state.default instanceof Function ? state.default() : state.default;
218
- };
219
-
220
- // internal/src/operation.ts
221
- var openOperation = (token, store) => {
222
- const target = newest(store);
223
- if (target.operation.open) {
224
- store.logger.error(
225
- `\u274C`,
226
- token.type,
227
- token.key,
228
- `failed to setState during a setState for "${target.operation.token.key}"`
229
- );
230
- return `rejection`;
231
- }
232
- target.operation = {
233
- open: true,
234
- done: /* @__PURE__ */ new Set(),
235
- prev: /* @__PURE__ */ new Map(),
236
- time: Date.now(),
237
- token
238
- };
239
- store.logger.info(
240
- `\u2B55`,
241
- token.type,
242
- token.key,
243
- `operation start in store "${store.config.name}"${target.transactionMeta === null ? `` : ` ${target.transactionMeta.phase} "${target.transactionMeta.update.key}"`}`
244
- );
245
- };
246
- var closeOperation = (store) => {
247
- const target = newest(store);
248
- if (target.operation.open) {
249
- store.logger.info(
250
- `\u{1F534}`,
251
- target.operation.token.type,
252
- target.operation.token.key,
253
- `operation done in store "${store.config.name}"`
254
- );
255
- }
256
- target.operation = { open: false };
257
- store.subject.operationStatus.next(target.operation);
258
- };
259
- var isDone = (key, store) => {
260
- const target = newest(store);
261
- if (!target.operation.open) {
262
- store.logger.warn(
263
- `\u{1F41E}`,
264
- `unknown`,
265
- key,
266
- `isDone called outside of an operation. This is probably a bug.`
267
- );
268
- return true;
269
- }
270
- return target.operation.done.has(key);
271
- };
272
- var markDone = (key, store) => {
273
- const target = newest(store);
274
- if (!target.operation.open) {
275
- store.logger.warn(
276
- `\u{1F41E}`,
277
- `unknown`,
278
- key,
279
- `markDone called outside of an operation. This is probably a bug.`
280
- );
281
- return;
282
- }
283
- target.operation.done.add(key);
284
- };
285
-
286
- // internal/src/set-state/become.ts
287
- var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
288
- originalThing instanceof Function ? originalThing() : originalThing
289
- ) : nextVersionOfThing;
290
-
291
- // internal/src/subject.ts
292
- var Subject = class {
293
- constructor() {
294
- this.subscribers = /* @__PURE__ */ new Map();
295
- }
296
- subscribe(key, subscriber) {
297
- this.subscribers.set(key, subscriber);
298
- const unsubscribe = () => this.unsubscribe(key);
299
- return unsubscribe;
300
- }
301
- unsubscribe(key) {
302
- this.subscribers.delete(key);
303
- }
304
- next(value) {
305
- const subscribers = this.subscribers.values();
306
- for (const subscriber of subscribers) {
307
- subscriber(value);
308
- }
309
- }
310
- };
311
- var StatefulSubject = class extends Subject {
312
- constructor(initialState) {
313
- super();
314
- this.state = initialState;
315
- }
316
- next(value) {
317
- this.state = value;
318
- super.next(value);
319
- }
320
- };
321
-
322
- // internal/src/set-state/copy-mutable-if-needed.ts
323
- function copyMutableIfNeeded(atom, transform, origin, target) {
324
- const originValue = origin.valueMap.get(atom.key);
325
- const targetValue = target.valueMap.get(atom.key);
326
- if (originValue === targetValue) {
327
- origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
328
- const jsonValue = transform.toJson(originValue);
329
- const copiedValue = transform.fromJson(jsonValue);
330
- target.valueMap.set(atom.key, copiedValue);
331
- new Tracker(atom, origin);
332
- return copiedValue;
333
- }
334
- return targetValue;
335
- }
336
-
337
- // internal/src/set-state/copy-mutable-in-transaction.ts
338
- function copyMutableIfWithinTransaction(oldValue, atom, store) {
339
- const target = newest(store);
340
- const parent = target.parent;
341
- if (parent !== null) {
342
- if (`toJson` in atom && `fromJson` in atom) {
343
- const copiedValue = copyMutableIfNeeded(atom, atom, parent, target);
344
- return copiedValue;
345
- }
346
- if (`family` in atom) {
347
- const family = parent.families.get(atom.family.key);
348
- if (family && family.type === `atom_family`) {
349
- const result = copyMutableFamilyMemberWithinTransaction(
350
- atom,
351
- family,
352
- parent,
353
- target
354
- );
355
- if (result) {
356
- return result;
357
- }
358
- }
359
- }
360
- }
361
- return oldValue;
362
- }
363
- function copyMutableFamilyMemberWithinTransaction(atom, family, origin, target) {
364
- if (`toJson` in family && `fromJson` in family) {
365
- const copyCreated = copyMutableIfNeeded(atom, family, origin, target);
366
- return copyCreated;
367
- }
368
- return null;
369
- }
370
-
371
- // internal/src/set-state/emit-update.ts
372
- var emitUpdate = (state, update, store) => {
373
- store.logger.info(
374
- `\u{1F4E2}`,
375
- state.type,
376
- state.key,
377
- `went (`,
378
- update.oldValue,
379
- `->`,
380
- update.newValue,
381
- `) subscribers:`,
382
- state.subject.subscribers
383
- );
384
- state.subject.next(update);
385
- };
386
-
387
- // internal/src/set-state/evict-downstream.ts
388
- var evictDownStream = (atom, store) => {
389
- const target = newest(store);
390
- const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom.key);
391
- store.logger.info(
392
- `\u{1F9F9}`,
393
- atom.type,
394
- atom.key,
395
- downstreamKeys ? `evicting ${downstreamKeys.size} states downstream:` : `no downstream states`,
396
- downstreamKeys != null ? downstreamKeys : `to evict`
397
- );
398
- if (downstreamKeys) {
399
- if (target.operation.open) {
400
- store.logger.info(
401
- `\u{1F9F9}`,
402
- atom.type,
403
- atom.key,
404
- `[ ${[...target.operation.done].join(`, `)} ] already done`
405
- );
406
- }
407
- for (const key of downstreamKeys) {
408
- if (isDone(key, store)) {
409
- continue;
410
- }
411
- evictCachedValue(key, store);
412
- markDone(key, store);
413
- }
414
- }
415
- };
416
-
417
- // internal/src/set-state/stow-update.ts
418
- function shouldUpdateBeStowed(key, update) {
419
- if (isTransceiver(update.newValue)) {
420
- return false;
421
- }
422
- if (key.includes(`\u{1F441}\u200D\u{1F5E8}`)) {
423
- return false;
424
- }
425
- return true;
426
- }
427
- var stowUpdate = (state, update, store) => {
428
- const { key } = state;
429
- const target = newest(store);
430
- if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
431
- store.logger.error(
432
- `\u{1F41E}`,
433
- `atom`,
434
- key,
435
- `stowUpdate called outside of a transaction. This is probably a bug.`
436
- );
437
- return;
438
- }
439
- const shouldStow = shouldUpdateBeStowed(key, update);
440
- if (!shouldStow) {
441
- return;
442
- }
443
- const atomUpdate = __spreadValues({ key }, update);
444
- if (state.family) {
445
- atomUpdate.family = state.family;
446
- }
447
- target.transactionMeta.update.updates.push(atomUpdate);
448
- store.logger.info(
449
- `\u{1F4C1}`,
450
- `atom`,
451
- key,
452
- `stowed (`,
453
- update.oldValue,
454
- `->`,
455
- update.newValue,
456
- `)`
457
- );
458
- };
459
-
460
- // internal/src/set-state/set-atom.ts
461
- var setAtom = (atom, next, store) => {
462
- const target = newest(store);
463
- const oldValue = readOrComputeValue(atom, store);
464
- let newValue = copyMutableIfWithinTransaction(oldValue, atom, store);
465
- newValue = become(next)(newValue);
466
- store.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
467
- newValue = cacheValue(atom.key, newValue, atom.subject, store);
468
- if (isAtomDefault(atom.key, store)) {
469
- markAtomAsNotDefault(atom.key, store);
470
- }
471
- markDone(atom.key, store);
472
- evictDownStream(atom, store);
473
- const update = { oldValue, newValue };
474
- if (target.transactionMeta === null || target.transactionMeta.phase === `applying`) {
475
- emitUpdate(atom, update, store);
476
- } else {
477
- stowUpdate(atom, update, store);
478
- }
479
- };
480
-
481
- // internal/src/set-state/set-atom-or-selector.ts
482
- var setAtomOrSelector = (state, value, store) => {
483
- if (state.type === `selector`) {
484
- state.set(value);
485
- } else {
486
- setAtom(state, value, store);
487
- }
488
- };
489
-
490
- // internal/src/store/deposit.ts
491
- function deposit(state) {
492
- const token = {
493
- key: state.key,
494
- type: state.type
495
- };
496
- if (`family` in state) {
497
- token.family = state.family;
498
- }
499
- return token;
500
- }
501
-
502
- // ../rel8/junction/src/junction.ts
503
- var Junction = class {
504
- constructor(data, config) {
505
- this.relations = /* @__PURE__ */ new Map();
506
- this.contents = /* @__PURE__ */ new Map();
507
- this.makeContentKey = (a, b) => `${a}:${b}`;
508
- var _a, _b, _c, _d;
509
- this.a = data.between[0];
510
- this.b = data.between[1];
511
- this.cardinality = data.cardinality;
512
- if (!(config == null ? void 0 : config.externalStore)) {
513
- this.relations = new Map((_a = data.relations) == null ? void 0 : _a.map(([a, b]) => [a, new Set(b)]));
514
- this.contents = new Map(data.contents);
515
- }
516
- this.isContent = (_b = config == null ? void 0 : config.isContent) != null ? _b : null;
517
- if (config == null ? void 0 : config.makeContentKey) {
518
- this.makeContentKey = config.makeContentKey;
519
- }
520
- if (config == null ? void 0 : config.externalStore) {
521
- const externalStore = config.externalStore;
522
- this.has = (a, b) => externalStore.has(a, b);
523
- this.addRelation = (a, b) => {
524
- externalStore.addRelation(a, b);
525
- };
526
- this.deleteRelation = (a, b) => {
527
- externalStore.deleteRelation(a, b);
528
- };
529
- this.replaceRelationsSafely = (a, bs) => {
530
- externalStore.replaceRelationsSafely(a, bs);
531
- };
532
- this.replaceRelationsUnsafely = (a, bs) => {
533
- externalStore.replaceRelationsUnsafely(a, bs);
534
- };
535
- this.getRelatedKeys = (key) => externalStore.getRelatedKeys(key);
536
- if (externalStore.getContent) {
537
- this.getContentInternal = (contentKey) => {
538
- return externalStore.getContent(contentKey);
539
- };
540
- this.setContent = (contentKey, content) => {
541
- externalStore.setContent(contentKey, content);
542
- };
543
- this.deleteContent = (contentKey) => {
544
- externalStore.deleteContent(contentKey);
545
- };
546
- }
547
- for (const [x, ys] of (_c = data.relations) != null ? _c : []) {
548
- for (const y of ys)
549
- this.addRelation(x, y);
550
- }
551
- for (const [contentKey, content] of (_d = data.contents) != null ? _d : []) {
552
- this.setContent(contentKey, content);
553
- }
554
- }
555
- }
556
- getRelatedKeys(key) {
557
- return this.relations.get(key);
558
- }
559
- addRelation(a, b) {
560
- let aRelations = this.relations.get(a);
561
- let bRelations = this.relations.get(b);
562
- if (aRelations) {
563
- aRelations.add(b);
564
- } else {
565
- aRelations = /* @__PURE__ */ new Set([b]);
566
- this.relations.set(a, aRelations);
567
- }
568
- if (bRelations) {
569
- bRelations.add(a);
570
- } else {
571
- bRelations = /* @__PURE__ */ new Set([a]);
572
- this.relations.set(b, bRelations);
573
- }
574
- }
575
- deleteRelation(a, b) {
576
- const aRelations = this.relations.get(a);
577
- if (aRelations) {
578
- aRelations.delete(b);
579
- if (aRelations.size === 0) {
580
- this.relations.delete(a);
581
- }
582
- const bRelations = this.relations.get(b);
583
- if (bRelations) {
584
- bRelations.delete(a);
585
- if (bRelations.size === 0) {
586
- this.relations.delete(b);
587
- }
588
- }
589
- }
590
- }
591
- replaceRelationsUnsafely(a, bs) {
592
- this.relations.set(a, new Set(bs));
593
- for (const b of bs) {
594
- const bRelations = /* @__PURE__ */ new Set([a]);
595
- this.relations.set(b, bRelations);
596
- }
597
- }
598
- replaceRelationsSafely(a, bs) {
599
- const aRelationsPrev = this.relations.get(a);
600
- if (aRelationsPrev) {
601
- for (const b of aRelationsPrev) {
602
- const bRelations = this.relations.get(b);
603
- if (bRelations) {
604
- if (bRelations.size === 1) {
605
- this.relations.delete(b);
606
- } else {
607
- bRelations.delete(a);
608
- }
609
- this.contents.delete(this.makeContentKey(a, b));
610
- }
611
- }
612
- }
613
- this.relations.set(a, new Set(bs));
614
- for (const b of bs) {
615
- let bRelations = this.relations.get(b);
616
- if (bRelations) {
617
- bRelations.add(a);
618
- } else {
619
- bRelations = /* @__PURE__ */ new Set([a]);
620
- this.relations.set(b, bRelations);
621
- }
622
- }
623
- }
624
- getContentInternal(contentKey) {
625
- return this.contents.get(contentKey);
626
- }
627
- setContent(contentKey, content) {
628
- this.contents.set(contentKey, content);
629
- }
630
- deleteContent(contentKey) {
631
- this.contents.delete(contentKey);
632
- }
633
- toJSON() {
634
- return {
635
- between: [this.a, this.b],
636
- cardinality: this.cardinality,
637
- relations: [...this.relations.entries()].map(([a, b]) => [a, [...b]]),
638
- contents: [...this.contents.entries()]
639
- };
640
- }
641
- set(a, ...rest) {
642
- var _a;
643
- const b = typeof rest[0] === `string` ? rest[0] : a[this.b];
644
- const content = ((_a = rest[1]) != null ? _a : typeof rest[0] === `string`) ? void 0 : rest[0];
645
- a = typeof a === `string` ? a : a[this.a];
646
- switch (this.cardinality) {
647
- case `1:1`: {
648
- const bPrev = this.getRelatedKey(a);
649
- if (bPrev && bPrev !== b)
650
- this.delete(bPrev, a);
651
- }
652
- case `1:n`: {
653
- const aPrev = this.getRelatedKey(b);
654
- if (aPrev && aPrev !== a)
655
- this.delete(aPrev, b);
656
- }
657
- }
658
- if (content) {
659
- const contentKey = this.makeContentKey(a, b);
660
- this.setContent(contentKey, content);
661
- }
662
- this.addRelation(a, b);
663
- return this;
664
- }
665
- delete(x, b) {
666
- b = typeof b === `string` ? b : x[this.b];
667
- const a = typeof x === `string` ? x : x[this.a];
668
- if (a === void 0 && typeof b === `string`) {
669
- const bRelations = this.getRelatedKeys(b);
670
- if (bRelations) {
671
- for (const a2 of bRelations) {
672
- this.delete(a2, b);
673
- }
674
- }
675
- }
676
- if (typeof a === `string` && b === void 0) {
677
- const aRelations = this.getRelatedKeys(a);
678
- if (aRelations) {
679
- for (const b2 of aRelations) {
680
- this.delete(a, b2);
681
- }
682
- }
683
- }
684
- if (typeof a === `string` && typeof b === `string`) {
685
- this.deleteRelation(a, b);
686
- const contentKey = this.makeContentKey(a, b);
687
- this.deleteContent(contentKey);
688
- }
689
- return this;
690
- }
691
- getRelatedKey(key) {
692
- const relations = this.getRelatedKeys(key);
693
- if (relations) {
694
- if (relations.size > 1) {
695
- console.warn(
696
- `${relations.size} related keys were found for key "${key}": (${[
697
- ...relations
698
- ].map((k) => `"${k}"`).join(`, `)}). Only one related key was expected.`
699
- );
700
- }
701
- for (const relation of relations) {
702
- return relation;
703
- }
704
- }
705
- }
706
- replaceRelations(a, relations, config) {
707
- const hasContent = !Array.isArray(relations);
708
- const bs = hasContent ? Object.keys(relations) : relations;
709
- if (config == null ? void 0 : config.reckless) {
710
- this.replaceRelationsUnsafely(a, bs);
711
- } else {
712
- this.replaceRelationsSafely(a, bs);
713
- }
714
- if (hasContent) {
715
- for (const b of bs) {
716
- const contentKey = this.makeContentKey(a, b);
717
- const content = relations[b];
718
- this.setContent(contentKey, content);
719
- }
720
- }
721
- return this;
722
- }
723
- getContent(a, b) {
724
- const contentKey = this.makeContentKey(a, b);
725
- return this.getContentInternal(contentKey);
726
- }
727
- getRelationEntries(input) {
728
- const a = input[this.a];
729
- const b = input[this.b];
730
- if (a !== void 0 && b === void 0) {
731
- const aRelations = this.getRelatedKeys(a);
732
- if (aRelations) {
733
- return [...aRelations].map((b2) => {
734
- var _a;
735
- return [b2, (_a = this.getContent(a, b2)) != null ? _a : null];
736
- });
737
- }
738
- }
739
- if (a === void 0 && b !== void 0) {
740
- const bRelations = this.getRelatedKeys(b);
741
- if (bRelations) {
742
- return [...bRelations].map((a2) => {
743
- var _a;
744
- return [a2, (_a = this.getContent(a2, b)) != null ? _a : null];
745
- });
746
- }
747
- }
748
- return [];
749
- }
750
- has(a, b) {
751
- var _a;
752
- if (b) {
753
- const setA = this.getRelatedKeys(a);
754
- return (_a = setA == null ? void 0 : setA.has(b)) != null ? _a : false;
755
- }
756
- return this.relations.has(a);
757
- }
758
- };
759
-
760
- // internal/src/store/store.ts
761
- var Store = class {
762
- constructor(name, store = null) {
763
- this.parent = null;
764
- this.child = null;
765
- this.valueMap = /* @__PURE__ */ new Map();
766
- this.atoms = /* @__PURE__ */ new Map();
767
- this.selectors = /* @__PURE__ */ new Map();
768
- this.readonlySelectors = /* @__PURE__ */ new Map();
769
- this.trackers = /* @__PURE__ */ new Map();
770
- this.families = /* @__PURE__ */ new Map();
771
- this.timelines = /* @__PURE__ */ new Map();
772
- this.transactions = /* @__PURE__ */ new Map();
773
- this.atomsThatAreDefault = /* @__PURE__ */ new Set();
774
- this.timelineAtoms = new Junction({
775
- between: [`timelineKey`, `atomKey`],
776
- cardinality: `1:n`
777
- });
778
- this.selectorAtoms = new Junction({
779
- between: [`selectorKey`, `atomKey`],
780
- cardinality: `n:n`
781
- });
782
- this.selectorGraph = new Junction(
783
- {
784
- between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
785
- cardinality: `n:n`
786
- },
787
- {
788
- makeContentKey: (...keys) => keys.sort().join(`:`)
789
- }
790
- );
791
- this.subject = {
792
- atomCreation: new Subject(),
793
- selectorCreation: new Subject(),
794
- transactionCreation: new Subject(),
795
- timelineCreation: new Subject(),
796
- transactionApplying: new StatefulSubject(null),
797
- operationStatus: new Subject()
798
- };
799
- this.operation = { open: false };
800
- this.transactionMeta = null;
801
- this.config = {
802
- name: `IMPLICIT_STORE`
803
- };
804
- this.loggers = [
805
- new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
806
- ];
807
- this.logger = {
808
- error: (...messages) => {
809
- for (const logger of this.loggers)
810
- logger.error(...messages);
811
- },
812
- info: (...messages) => {
813
- for (const logger of this.loggers)
814
- logger.info(...messages);
815
- },
816
- warn: (...messages) => {
817
- for (const logger of this.loggers)
818
- logger.warn(...messages);
819
- }
820
- };
821
- if (store !== null) {
822
- this.valueMap = new Map(store == null ? void 0 : store.valueMap);
823
- this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
824
- this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
825
- this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
826
- name
827
- });
828
- for (const [, atom] of store.atoms) {
829
- atom.install(this);
830
- }
831
- for (const [, selector] of store.readonlySelectors) {
832
- selector.install(this);
833
- }
834
- for (const [, selector] of store.selectors) {
835
- selector.install(this);
836
- }
837
- for (const [, tx] of store.transactions) {
838
- tx.install(this);
839
- }
840
- for (const [, timeline] of store.timelines) {
841
- timeline.install(this);
842
- }
843
- }
844
- }
845
- };
846
- var IMPLICIT = {
847
- STORE_INTERNAL: void 0,
848
- get STORE() {
849
- var _a;
850
- return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
851
- }
852
- };
853
-
854
- // internal/src/store/withdraw.ts
855
- function withdraw(token, store) {
856
- var _a, _b, _c, _d;
857
- const target = newest(store);
858
- 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);
859
- if (state) {
860
- return state;
861
- }
862
- return void 0;
863
- }
864
-
865
- // internal/src/store/withdraw-new-family-member.ts
866
- function withdrawNewFamilyMember(token, store) {
867
- if (token.family) {
868
- store.logger.info(
869
- `\u{1F46A}`,
870
- token.type,
871
- token.key,
872
- `creating new family member in store "${store.config.name}"`
873
- );
874
- const target = newest(store);
875
- const family = target.families.get(token.family.key);
876
- if (family) {
877
- const jsonSubKey = JSON.parse(token.family.subKey);
878
- family(jsonSubKey);
879
- const state = withdraw(token, store);
880
- return state;
881
- }
882
- }
883
- return void 0;
884
- }
885
-
886
- // internal/src/keys.ts
887
- var isAtomKey = (key, store) => newest(store).atoms.has(key);
888
- var isSelectorKey = (key, store) => newest(store).selectors.has(key);
889
- var isReadonlySelectorKey = (key, store) => newest(store).readonlySelectors.has(key);
890
- var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
891
-
892
- // internal/src/selector/get-selector-dependency-keys.ts
893
- var getSelectorDependencyKeys = (key, store) => {
894
- const sources = newest(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
895
- return sources;
896
- };
897
-
898
- // internal/src/selector/trace-selector-atoms.ts
899
- var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
900
- const rootKeys = [];
901
- const indirectDependencyKeys = getSelectorDependencyKeys(
902
- directDependencyKey,
903
- store
904
- );
905
- let depth = 0;
906
- while (indirectDependencyKeys.length > 0) {
907
- const indirectDependencyKey = indirectDependencyKeys.shift();
908
- ++depth;
909
- if (depth > 99999) {
910
- throw new Error(
911
- `Maximum selector dependency depth exceeded (> 99999) in selector "${selectorKey}". This is likely due to a circular dependency.`
912
- );
913
- }
914
- if (!isAtomKey(indirectDependencyKey, store)) {
915
- indirectDependencyKeys.push(
916
- ...getSelectorDependencyKeys(indirectDependencyKey, store)
917
- );
918
- } else if (!rootKeys.includes(indirectDependencyKey)) {
919
- rootKeys.push(indirectDependencyKey);
920
- }
921
- }
922
- return rootKeys;
923
- };
924
- var traceAllSelectorAtoms = (selectorKey, store) => {
925
- const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
926
- return directDependencyKeys.flatMap(
927
- (depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
928
- );
929
- };
930
-
931
- // internal/src/selector/update-selector-atoms.ts
932
- var updateSelectorAtoms = (selectorKey, dependency, store) => {
933
- const target = newest(store);
934
- if (dependency.type === `atom`) {
935
- target.selectorAtoms.set({
936
- selectorKey,
937
- atomKey: dependency.key
938
- });
939
- store.logger.info(
940
- `\u{1F50D}`,
941
- `selector`,
942
- selectorKey,
943
- `discovers root atom "${dependency.key}"`
944
- );
945
- } else {
946
- const rootKeys = traceSelectorAtoms(selectorKey, dependency.key, store);
947
- store.logger.info(
948
- `\u{1F50D}`,
949
- `selector`,
950
- selectorKey,
951
- `discovers root atoms: [ ${rootKeys.map((key) => `"${key}"`).join(`, `)} ]`
952
- );
953
- for (const atomKey of rootKeys) {
954
- target.selectorAtoms = target.selectorAtoms.set({
955
- selectorKey,
956
- atomKey
957
- });
958
- }
959
- }
960
- };
961
-
962
- // internal/src/selector/register-selector.ts
963
- var registerSelector = (selectorKey, store) => ({
964
- get: (dependency) => {
965
- const target = newest(store);
966
- const alreadyRegistered = target.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
967
- const dependencyState = withdraw(dependency, store);
968
- if (dependencyState === void 0) {
969
- throw new Error(
970
- `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
971
- );
972
- }
973
- const dependencyValue = readOrComputeValue(dependencyState, store);
974
- store.logger.info(
975
- `\u{1F50C}`,
976
- `selector`,
977
- selectorKey,
978
- `registers dependency ( "${dependency.key}" =`,
979
- dependencyValue,
980
- `)`
981
- );
982
- if (!alreadyRegistered) {
983
- target.selectorGraph.set(
984
- {
985
- upstreamSelectorKey: dependency.key,
986
- downstreamSelectorKey: selectorKey
987
- },
988
- {
989
- source: dependency.key
990
- }
991
- );
992
- }
993
- updateSelectorAtoms(selectorKey, dependency, store);
994
- return dependencyValue;
995
- },
996
- set: (stateToken, newValue) => {
997
- const state = withdraw(stateToken, store);
998
- if (state === void 0) {
999
- throw new Error(
1000
- `State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1001
- );
1002
- }
1003
- setAtomOrSelector(state, newValue, store);
1004
- }
1005
- });
1006
-
1007
- // internal/src/selector/create-read-write-selector.ts
1008
- var createReadWriteSelector = (options, family, store) => {
1009
- const target = newest(store);
1010
- const subject = new Subject();
1011
- const { get, set } = registerSelector(options.key, store);
1012
- const getSelf = () => {
1013
- const value = options.get({ get });
1014
- cacheValue(options.key, value, subject, store);
1015
- return value;
1016
- };
1017
- const setSelf = (next) => {
1018
- const oldValue = getSelf();
1019
- const newValue = become(next)(oldValue);
1020
- store.logger.info(
1021
- `\u{1F4DD}`,
1022
- `selector`,
1023
- options.key,
1024
- `set (`,
1025
- oldValue,
1026
- `->`,
1027
- newValue,
1028
- `)`
1029
- );
1030
- cacheValue(options.key, newValue, subject, store);
1031
- markDone(options.key, store);
1032
- if (target.transactionMeta === null) {
1033
- subject.next({ newValue, oldValue });
1034
- }
1035
- options.set({ get, set }, newValue);
1036
- };
1037
- const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
1038
- subject,
1039
- install: (s) => createSelector(options, family, s),
1040
- get: getSelf,
1041
- set: setSelf,
1042
- type: `selector`
1043
- }), family && { family });
1044
- target.selectors.set(options.key, mySelector);
1045
- const initialValue = getSelf();
1046
- store.logger.info(`\u2728`, mySelector.type, mySelector.key, `=`, initialValue);
1047
- const token = {
1048
- key: options.key,
1049
- type: `selector`
1050
- };
1051
- if (family) {
1052
- token.family = family;
1053
- }
1054
- store.subject.selectorCreation.next(token);
1055
- return token;
1056
- };
1057
-
1058
- // internal/src/selector/create-readonly-selector.ts
1059
- var createReadonlySelector = (options, family, store) => {
1060
- const target = newest(store);
1061
- const subject = new Subject();
1062
- const { get } = registerSelector(options.key, store);
1063
- const getSelf = () => {
1064
- const value = options.get({ get });
1065
- cacheValue(options.key, value, subject, store);
1066
- return value;
1067
- };
1068
- const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
1069
- subject,
1070
- install: (s) => createSelector(options, family, s),
1071
- get: getSelf,
1072
- type: `readonly_selector`
1073
- }), family && { family });
1074
- target.readonlySelectors.set(options.key, readonlySelector);
1075
- const initialValue = getSelf();
1076
- store.logger.info(
1077
- `\u2728`,
1078
- readonlySelector.type,
1079
- readonlySelector.key,
1080
- `=`,
1081
- initialValue
1082
- );
1083
- const token = {
1084
- key: options.key,
1085
- type: `readonly_selector`
1086
- };
1087
- if (family) {
1088
- token.family = family;
1089
- }
1090
- store.subject.selectorCreation.next(token);
1091
- return token;
1092
- };
1093
-
1094
- // internal/src/selector/create-selector.ts
1095
- function createSelector(options, family, store) {
1096
- const target = newest(store);
1097
- const existingWritable = target.selectors.get(options.key);
1098
- const existingReadonly = target.readonlySelectors.get(options.key);
1099
- if (existingWritable || existingReadonly) {
1100
- store.logger.error(
1101
- `\u274C`,
1102
- existingReadonly ? `readonly_selector` : `selector`,
1103
- options.key,
1104
- `Tried to create selector, but it already exists in the store. (Ignore if you are in development using hot module replacement.)`
1105
- );
1106
- }
1107
- if (`set` in options) {
1108
- return createReadWriteSelector(options, family, store);
1109
- }
1110
- return createReadonlySelector(options, family, store);
1111
- }
1112
-
1113
- // internal/src/subscribe/recall-state.ts
1114
- var recallState = (state, store) => {
1115
- const target = newest(store);
1116
- if (!target.operation.open) {
1117
- return target.valueMap.get(state.key);
1118
- }
1119
- return target.operation.prev.get(state.key);
1120
- };
1121
-
1122
- // internal/src/subscribe/subscribe-to-root-atoms.ts
1123
- var subscribeToRootAtoms = (state, store) => {
1124
- const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
1125
- const atom = store.atoms.get(atomKey);
1126
- if (atom === void 0) {
1127
- throw new Error(
1128
- `Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
1129
- );
1130
- }
1131
- return atom.subject.subscribe(
1132
- `${state.type}:${state.key}`,
1133
- (atomChange) => {
1134
- store.logger.info(
1135
- `\u{1F4E2}`,
1136
- state.type,
1137
- state.key,
1138
- `root`,
1139
- atomKey,
1140
- `went`,
1141
- atomChange.oldValue,
1142
- `->`,
1143
- atomChange.newValue
1144
- );
1145
- const oldValue = recallState(state, store);
1146
- const newValue = readOrComputeValue(state, store);
1147
- store.logger.info(
1148
- `\u2728`,
1149
- state.type,
1150
- state.key,
1151
- `went`,
1152
- oldValue,
1153
- `->`,
1154
- newValue
1155
- );
1156
- state.subject.next({ newValue, oldValue });
1157
- }
1158
- );
1159
- });
1160
- return dependencySubscriptions;
1161
- };
1162
-
1163
- // internal/src/subscribe/subscribe-to-state.ts
1164
- function subscribeToState(token, handleUpdate, key, store) {
1165
- const state = withdraw(token, store);
1166
- if (state === void 0) {
1167
- throw new Error(
1168
- `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1169
- );
1170
- }
1171
- const unsubFunction = state.subject.subscribe(key, handleUpdate);
1172
- store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
1173
- const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
1174
- const unsubscribe = dependencyUnsubFunctions === null ? () => {
1175
- store.logger.info(
1176
- `\u{1F648}`,
1177
- state.type,
1178
- state.key,
1179
- `Removing subscription "${key}"`
1180
- );
1181
- unsubFunction();
1182
- } : () => {
1183
- store.logger.info(
1184
- `\u{1F648}`,
1185
- state.type,
1186
- state.key,
1187
- `Removing subscription "${key}"`
1188
- );
1189
- unsubFunction();
1190
- for (const unsubFromDependency of dependencyUnsubFunctions) {
1191
- unsubFromDependency();
1192
- }
1193
- };
1194
- return unsubscribe;
1195
- }
1196
-
1197
- // internal/src/subscribe/subscribe-to-timeline.ts
1198
- var subscribeToTimeline = (token, handleUpdate, key, store) => {
1199
- const tl = withdraw(token, store);
1200
- if (tl === void 0) {
1201
- throw new Error(
1202
- `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
1203
- );
1204
- }
1205
- store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
1206
- const unsubscribe = tl.subject.subscribe(key, handleUpdate);
1207
- return () => {
1208
- store.logger.info(
1209
- `\u{1F648}`,
1210
- `timeline`,
1211
- token.key,
1212
- `Removing subscription "${key}" from timeline`
1213
- );
1214
- unsubscribe();
1215
- };
1216
- };
1217
-
1218
- // internal/src/mutable/tracker.ts
1219
- var Tracker = class {
1220
- constructor(mutableState, store) {
1221
- this.unsubscribeFromInnerValue = null;
1222
- this.mutableState = mutableState;
1223
- const target = newest(store);
1224
- this.latestUpdateState = this.initializeState(mutableState, target);
1225
- this.observeCore(mutableState, this.latestUpdateState, target);
1226
- this.updateCore(mutableState, this.latestUpdateState, target);
1227
- target.trackers.set(mutableState.key, this);
1228
- }
1229
- initializeState(mutableState, store) {
1230
- const latestUpdateStateKey = `*${mutableState.key}`;
1231
- store.atoms.delete(latestUpdateStateKey);
1232
- store.valueMap.delete(latestUpdateStateKey);
1233
- const familyMetaData = mutableState.family ? {
1234
- key: `*${mutableState.family.key}`,
1235
- subKey: mutableState.family.subKey
1236
- } : void 0;
1237
- const latestUpdateState = createAtom(
1238
- {
1239
- key: latestUpdateStateKey,
1240
- default: null
1241
- },
1242
- familyMetaData,
1243
- store
1244
- );
1245
- if (store.parent) {
1246
- const parentValue = store.parent.valueMap.get(latestUpdateStateKey);
1247
- store.valueMap.set(latestUpdateStateKey, parentValue);
1248
- }
1249
- return latestUpdateState;
1250
- }
1251
- observeCore(mutableState, latestUpdateState, store) {
1252
- const originalInnerValue = getState(mutableState, store);
1253
- const target = newest(store);
1254
- this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
1255
- `tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
1256
- (update) => {
1257
- const unsubscribe = store.subject.operationStatus.subscribe(
1258
- mutableState.key,
1259
- () => {
1260
- unsubscribe();
1261
- setState(latestUpdateState, update, store);
1262
- }
1263
- );
1264
- }
1265
- );
1266
- subscribeToState(
1267
- mutableState,
1268
- (update) => {
1269
- var _a;
1270
- if (update.newValue !== update.oldValue) {
1271
- (_a = this.unsubscribeFromInnerValue) == null ? void 0 : _a.call(this);
1272
- const target2 = newest(store);
1273
- this.unsubscribeFromInnerValue = update.newValue.subscribe(
1274
- `tracker:${store.config.name}:${target2.transactionMeta === null ? `main` : target2.transactionMeta.update.key}`,
1275
- (update2) => {
1276
- const unsubscribe = store.subject.operationStatus.subscribe(
1277
- mutableState.key,
1278
- () => {
1279
- unsubscribe();
1280
- setState(latestUpdateState, update2, store);
1281
- }
1282
- );
1283
- }
1284
- );
1285
- }
1286
- },
1287
- `${store.config.name}: tracker observing inner value`,
1288
- store
1289
- );
1290
- }
1291
- updateCore(mutableState, latestUpdateState, store) {
1292
- subscribeToState(
1293
- latestUpdateState,
1294
- ({ newValue, oldValue }) => {
1295
- const timelineId = store.timelineAtoms.getRelatedKey(
1296
- latestUpdateState.key
1297
- );
1298
- if (timelineId) {
1299
- const timelineData = store.timelines.get(timelineId);
1300
- if (timelineData == null ? void 0 : timelineData.timeTraveling) {
1301
- const unsubscribe2 = subscribeToTimeline(
1302
- { key: timelineId, type: `timeline` },
1303
- (update) => {
1304
- unsubscribe2();
1305
- setState(
1306
- mutableState,
1307
- (transceiver) => {
1308
- if (update === `redo` && newValue) {
1309
- transceiver.do(newValue);
1310
- } else if (update === `undo` && oldValue) {
1311
- transceiver.undo(oldValue);
1312
- }
1313
- return transceiver;
1314
- },
1315
- store
1316
- );
1317
- },
1318
- `${mutableState.key}: tracker observing timeline`,
1319
- store
1320
- );
1321
- return;
1322
- }
1323
- }
1324
- const unsubscribe = store.subject.operationStatus.subscribe(
1325
- latestUpdateState.key,
1326
- () => {
1327
- unsubscribe();
1328
- const mutable = getState(mutableState, store);
1329
- const updateNumber = newValue === null ? -1 : mutable.getUpdateNumber(newValue);
1330
- const eventOffset = updateNumber - mutable.cacheUpdateNumber;
1331
- if (newValue && eventOffset === 1) {
1332
- setState(
1333
- mutableState,
1334
- (transceiver) => (transceiver.do(newValue), transceiver),
1335
- store
1336
- );
1337
- }
1338
- }
1339
- );
1340
- },
1341
- `${store.config.name}: tracker observing latest update`,
1342
- store
1343
- );
1344
- }
1345
- };
1346
-
1347
- // internal/src/mutable/create-mutable-atom.ts
1348
- function createMutableAtom(options, store) {
1349
- store.logger.info(
1350
- `\u{1F527}`,
1351
- `atom`,
1352
- options.key,
1353
- `creating in store "${store.config.name}"`
1354
- );
1355
- const coreState = createAtom(options, void 0, store);
1356
- new Tracker(coreState, store);
1357
- const jsonState = selectJson(coreState, options, store);
1358
- const target = newest(store);
1359
- subscribeToState(
1360
- jsonState,
1361
- () => {
1362
- const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
1363
- if (!trackerHasBeenInitialized) {
1364
- new Tracker(coreState, store);
1365
- }
1366
- },
1367
- `tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
1368
- store
1369
- );
1370
- return coreState;
1371
- }
1372
-
1373
- // internal/src/mutable/get-json-token.ts
1374
- var getJsonToken = (mutableAtomToken) => {
1375
- const key = mutableAtomToken.family ? `${mutableAtomToken.family.key}:JSON(${mutableAtomToken.family.subKey})` : `${mutableAtomToken.key}:JSON`;
1376
- const jsonToken = { type: `selector`, key };
1377
- if (mutableAtomToken.family) {
1378
- jsonToken.family = {
1379
- key: `${mutableAtomToken.family.key}:JSON`,
1380
- subKey: mutableAtomToken.family.subKey
1381
- };
1382
- }
1383
- return jsonToken;
1384
- };
1385
-
1386
- // internal/src/mutable/transceiver.ts
1387
- function isTransceiver(value) {
1388
- return typeof value === `object` && value !== null && `do` in value && `undo` in value && `subscribe` in value;
1389
- }
1390
-
1391
- // internal/src/atom/is-default.ts
1392
- var isAtomDefault = (key, store) => {
1393
- const core = newest(store);
1394
- return core.atomsThatAreDefault.has(key);
1395
- };
1396
- var markAtomAsDefault = (key, store) => {
1397
- const core = newest(store);
1398
- core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key);
1399
- };
1400
- var markAtomAsNotDefault = (key, store) => {
1401
- const core = newest(store);
1402
- core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault);
1403
- core.atomsThatAreDefault.delete(key);
1404
- };
1405
-
1406
- // internal/src/atom/create-atom.ts
1407
- function createAtom(options, family, store) {
1408
- store.logger.info(
1409
- `\u{1F528}`,
1410
- `atom`,
1411
- options.key,
1412
- `creating in store "${store.config.name}"`
1413
- );
1414
- const target = newest(store);
1415
- const existing = target.atoms.get(options.key);
1416
- if (existing) {
1417
- store.logger.error(
1418
- `\u274C`,
1419
- `atom`,
1420
- options.key,
1421
- `Tried to create atom, but it already exists in the store.`,
1422
- `(Ignore if you are in development using hot module replacement.)`
1423
- );
1424
- return deposit(existing);
1425
- }
1426
- const subject = new Subject();
1427
- const newAtom = __spreadValues(__spreadProps(__spreadValues({}, options), {
1428
- type: `atom`,
1429
- install: (store2) => {
1430
- store2.logger.info(
1431
- `\u{1F6E0}\uFE0F`,
1432
- `atom`,
1433
- options.key,
1434
- `installing in store "${store2.config.name}"`
1435
- );
1436
- return `mutable` in options ? createMutableAtom(options, store2) : createAtom(options, void 0, store2);
1437
- },
1438
- subject
1439
- }), family && { family });
1440
- let initialValue = options.default;
1441
- if (options.default instanceof Function) {
1442
- initialValue = options.default();
1443
- }
1444
- target.atoms.set(newAtom.key, newAtom);
1445
- markAtomAsDefault(options.key, store);
1446
- cacheValue(options.key, initialValue, subject, store);
1447
- const token = deposit(newAtom);
1448
- if (options.effects) {
1449
- let effectIndex = 0;
1450
- const cleanupFunctions = [];
1451
- for (const effect of options.effects) {
1452
- const cleanup = effect({
1453
- setSelf: (next) => setState(token, next, store),
1454
- onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1455
- });
1456
- if (cleanup) {
1457
- cleanupFunctions.push(cleanup);
1458
- }
1459
- ++effectIndex;
1460
- }
1461
- newAtom.cleanup = () => {
1462
- for (const cleanup of cleanupFunctions) {
1463
- cleanup();
1464
- }
1465
- };
1466
- }
1467
- store.subject.atomCreation.next(token);
1468
- return token;
1469
- }
1470
-
1471
- // internal/src/not-found-error.ts
1472
- var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
1473
- function prettyPrintTokenType(token) {
1474
- if (token.type === `readonly_selector`) {
1475
- return `Readonly Selector`;
1476
- }
1477
- return capitalize(token.type);
1478
- }
1479
- var NotFoundError = class extends Error {
1480
- constructor(token, store) {
1481
- super(
1482
- `${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
1483
- );
1484
- }
1485
- };
1486
-
1487
- // internal/src/timeline/time-travel.ts
1488
- var timeTravel = (direction, token, store) => {
1489
- const action = direction === `forward` ? `redo` : `undo`;
1490
- store.logger.info(
1491
- direction === `forward` ? `\u23E9` : `\u23EA`,
1492
- `timeline`,
1493
- token.key,
1494
- action
1495
- );
1496
- const timelineData = store.timelines.get(token.key);
1497
- if (!timelineData) {
1498
- store.logger.error(
1499
- `\u{1F41E}`,
1500
- `timeline`,
1501
- token.key,
1502
- `Failed to ${action}. This timeline has not been initialized.`
1503
- );
1504
- return;
1505
- }
1506
- if (direction === `forward` && timelineData.at === timelineData.history.length || direction === `backward` && timelineData.at === 0) {
1507
- store.logger.warn(
1508
- `\u{1F481}`,
1509
- `timeline`,
1510
- token.key,
1511
- `Failed to ${action} at the ${direction === `forward` ? `end` : `beginning`} of timeline "${token.key}". There is nothing to ${action}.`
1512
- );
1513
- return;
1514
- }
1515
- timelineData.timeTraveling = direction === `forward` ? `into_future` : `into_past`;
1516
- if (direction === `backward`) {
1517
- --timelineData.at;
1518
- }
1519
- const update = timelineData.history[timelineData.at];
1520
- const updateValues = (atomUpdate) => {
1521
- const { key, newValue, oldValue } = atomUpdate;
1522
- const value = direction === `forward` ? newValue : oldValue;
1523
- setState({ key, type: `atom` }, value, store);
1524
- };
1525
- const updateValuesFromTransactionUpdate = (transactionUpdate) => {
1526
- const updates = direction === `forward` ? transactionUpdate.updates : [...transactionUpdate.updates].reverse();
1527
- for (const updateFromTransaction of updates) {
1528
- if (`newValue` in updateFromTransaction) {
1529
- updateValues(updateFromTransaction);
1530
- } else {
1531
- updateValuesFromTransactionUpdate(updateFromTransaction);
1532
- }
1533
- }
1534
- };
1535
- switch (update.type) {
1536
- case `atom_update`: {
1537
- updateValues(update);
1538
- break;
1539
- }
1540
- case `selector_update`: {
1541
- const updates = direction === `forward` ? update.atomUpdates : [...update.atomUpdates].reverse();
1542
- for (const atomUpdate of updates) {
1543
- updateValues(atomUpdate);
1544
- }
1545
- break;
1546
- }
1547
- case `transaction_update`: {
1548
- updateValuesFromTransactionUpdate(update);
1549
- break;
1550
- }
1551
- }
1552
- if (direction === `forward`) {
1553
- ++timelineData.at;
1554
- }
1555
- timelineData.subject.next(action);
1556
- timelineData.timeTraveling = null;
1557
- store.logger.info(
1558
- `\u23F9\uFE0F`,
1559
- `timeline`,
1560
- token.key,
1561
- `"${token.key}" is now at ${timelineData.at} / ${timelineData.history.length}`
1562
- );
1563
- };
1564
- var StoreContext = React2__namespace.createContext(IMPLICIT.STORE);
1565
- var StoreProvider = ({ children, store = IMPLICIT.STORE }) => /* @__PURE__ */ jsxRuntime.jsx(StoreContext.Provider, { value: store, children });
28
+ // react/src/store-context.tsx
29
+ var StoreContext = React2__namespace.createContext(internal.IMPLICIT.STORE);
30
+ var StoreProvider = ({ children, store = internal.IMPLICIT.STORE }) => /* @__PURE__ */ jsxRuntime.jsx(StoreContext.Provider, { value: store, children });
1566
31
  function useI(token) {
1567
32
  const store = React2__namespace.useContext(StoreContext);
1568
33
  const setter = React2__namespace.useRef(null);
1569
34
  if (setter.current === null) {
1570
- setter.current = (next) => setState(token, next, store);
35
+ setter.current = (next) => atom_io.setState(token, next, store);
1571
36
  }
1572
37
  return setter.current;
1573
38
  }
@@ -1575,22 +40,22 @@ function useO(token) {
1575
40
  const store = React2__namespace.useContext(StoreContext);
1576
41
  const id = React2__namespace.useId();
1577
42
  return React2__namespace.useSyncExternalStore(
1578
- (dispatch) => subscribeToState(token, dispatch, `use-o:${id}`, store),
1579
- () => getState(token, store),
1580
- () => getState(token, store)
43
+ (dispatch) => internal.subscribeToState(token, dispatch, `use-o:${id}`, store),
44
+ () => atom_io.getState(token, store),
45
+ () => atom_io.getState(token, store)
1581
46
  );
1582
47
  }
1583
48
  function useJSON(token) {
1584
- const jsonToken = getJsonToken(token);
49
+ const jsonToken = internal.getJsonToken(token);
1585
50
  return useO(jsonToken);
1586
51
  }
1587
52
  function useTL(token) {
1588
53
  const store = React2__namespace.useContext(StoreContext);
1589
54
  const id = React2__namespace.useId();
1590
- const timeline = withdraw(token, store);
55
+ const timeline = internal.withdraw(token, store);
1591
56
  const base = React2__namespace.useRef({
1592
- undo: () => undo(token),
1593
- redo: () => redo(token)
57
+ undo: () => atom_io.undo(token),
58
+ redo: () => atom_io.redo(token)
1594
59
  });
1595
60
  const rebuildMeta = React2__namespace.useCallback(() => {
1596
61
  var _a, _b;
@@ -1610,7 +75,7 @@ function useTL(token) {
1610
75
  return meta.current;
1611
76
  }, []);
1612
77
  return React2__namespace.useSyncExternalStore(
1613
- (dispatch) => subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),
78
+ (dispatch) => internal.subscribeToTimeline(token, dispatch, `use-tl:${id}`, store),
1614
79
  retrieve,
1615
80
  retrieve
1616
81
  );