atom.io 0.14.3 → 0.14.5

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