atom.io 0.18.0 → 0.18.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/chunk-3J2EGSBE.js +31 -0
  2. package/dist/chunk-3J2EGSBE.js.map +1 -0
  3. package/dist/chunk-A4ZCNKWQ.js +18 -0
  4. package/dist/chunk-A4ZCNKWQ.js.map +1 -0
  5. package/dist/{chunk-OEVFAUPE.js → chunk-IZHOMSXA.js} +53 -11
  6. package/dist/chunk-IZHOMSXA.js.map +1 -0
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +5 -1
  9. package/dist/index.js.map +1 -1
  10. package/internal/dist/index.cjs +110 -134
  11. package/internal/dist/index.cjs.map +1 -1
  12. package/internal/dist/index.d.ts +24 -24
  13. package/internal/dist/index.js +110 -134
  14. package/internal/dist/index.js.map +1 -1
  15. package/internal/src/atom/delete-atom.ts +7 -6
  16. package/internal/src/caching.ts +6 -6
  17. package/internal/src/families/create-regular-atom-family.ts +4 -4
  18. package/internal/src/get-state/get-from-store.ts +2 -5
  19. package/internal/src/ingest-updates/ingest-atom-update.ts +6 -2
  20. package/internal/src/mutable/create-mutable-atom-family.ts +4 -4
  21. package/internal/src/selector/register-selector.ts +3 -14
  22. package/internal/src/set-state/copy-mutable-if-needed.ts +5 -0
  23. package/internal/src/set-state/emit-update.ts +25 -11
  24. package/internal/src/set-state/set-atom.ts +4 -3
  25. package/internal/src/set-state/set-into-store.ts +2 -5
  26. package/internal/src/store/withdraw-new-family-member.ts +38 -28
  27. package/internal/src/store/withdraw.ts +20 -23
  28. package/internal/src/subscribe/subscribe-to-state.ts +2 -3
  29. package/internal/src/subscribe/subscribe-to-timeline.ts +0 -5
  30. package/internal/src/subscribe/subscribe-to-transaction.ts +0 -5
  31. package/internal/src/timeline/add-atom-to-timeline.ts +6 -15
  32. package/internal/src/timeline/create-timeline.ts +0 -27
  33. package/internal/src/transaction/apply-transaction.ts +0 -1
  34. package/internal/src/transaction/set-epoch-number.ts +0 -1
  35. package/json/src/index.ts +3 -3
  36. package/package.json +242 -241
  37. package/react/dist/index.cjs +2 -3
  38. package/react/dist/index.cjs.map +1 -1
  39. package/react/dist/index.js +2 -3
  40. package/react/dist/index.js.map +1 -1
  41. package/react/src/use-tl.ts +2 -2
  42. package/react-devtools/dist/index.cjs +1 -1
  43. package/react-devtools/dist/index.cjs.map +1 -1
  44. package/react-devtools/dist/index.js +1 -15
  45. package/react-devtools/dist/index.js.map +1 -1
  46. package/react-devtools/src/StateEditor.tsx +6 -6
  47. package/react-devtools/src/StateIndex.tsx +2 -2
  48. package/react-devtools/src/Updates.tsx +1 -1
  49. package/react-devtools/src/index.ts +3 -3
  50. package/realtime/dist/index.cjs +50 -2
  51. package/realtime/dist/index.cjs.map +1 -1
  52. package/realtime/dist/index.d.ts +110 -3
  53. package/realtime/dist/index.js +47 -4
  54. package/realtime/dist/index.js.map +1 -1
  55. package/realtime/src/index.ts +1 -0
  56. package/realtime/src/realtime-continuity.ts +14 -4
  57. package/realtime/src/shared-room-store.ts +48 -0
  58. package/realtime-client/dist/index.cjs +113 -200
  59. package/realtime-client/dist/index.cjs.map +1 -1
  60. package/realtime-client/dist/index.d.ts +2 -5
  61. package/realtime-client/dist/index.js +22 -190
  62. package/realtime-client/dist/index.js.map +1 -1
  63. package/realtime-client/src/index.ts +0 -2
  64. package/realtime-client/src/pull-mutable-atom-family-member.ts +5 -5
  65. package/realtime-client/src/realtime-client-stores/client-main-store.ts +10 -0
  66. package/realtime-client/src/sync-continuity.ts +56 -9
  67. package/realtime-react/dist/index.cjs +51 -26
  68. package/realtime-react/dist/index.cjs.map +1 -1
  69. package/realtime-react/dist/index.d.ts +2 -6
  70. package/realtime-react/dist/index.js +2 -17
  71. package/realtime-react/dist/index.js.map +1 -1
  72. package/realtime-react/src/index.ts +0 -2
  73. package/realtime-server/dist/index.cjs +399 -327
  74. package/realtime-server/dist/index.cjs.map +1 -1
  75. package/realtime-server/dist/index.d.ts +55 -60
  76. package/realtime-server/dist/index.js +394 -319
  77. package/realtime-server/dist/index.js.map +1 -1
  78. package/realtime-server/src/index.ts +2 -4
  79. package/realtime-server/src/ipc-sockets/child-socket.ts +135 -0
  80. package/realtime-server/src/ipc-sockets/custom-socket.ts +90 -0
  81. package/realtime-server/src/ipc-sockets/index.ts +3 -0
  82. package/realtime-server/src/ipc-sockets/parent-socket.ts +186 -0
  83. package/realtime-server/src/realtime-continuity-synchronizer.ts +225 -96
  84. package/realtime-server/src/realtime-server-stores/index.ts +2 -1
  85. package/realtime-server/src/realtime-server-stores/realtime-continuity-store.ts +50 -31
  86. package/realtime-server/src/realtime-server-stores/server-room-external-actions.ts +64 -0
  87. package/realtime-server/src/realtime-server-stores/server-room-external-store.ts +42 -0
  88. package/realtime-server/src/realtime-server-stores/server-sync-store.ts +49 -26
  89. package/realtime-testing/dist/index.cjs +51 -11
  90. package/realtime-testing/dist/index.cjs.map +1 -1
  91. package/realtime-testing/dist/index.d.ts +1 -0
  92. package/realtime-testing/dist/index.js +11 -11
  93. package/realtime-testing/dist/index.js.map +1 -1
  94. package/realtime-testing/src/setup-realtime-test.tsx +11 -11
  95. package/src/logger.ts +5 -1
  96. package/dist/chunk-OEVFAUPE.js.map +0 -1
  97. package/realtime-client/src/sync-server-action.ts +0 -168
  98. package/realtime-client/src/sync-state.ts +0 -19
  99. package/realtime-react/src/use-sync-server-action.ts +0 -17
  100. package/realtime-react/src/use-sync.ts +0 -17
  101. package/realtime-server/src/ipc-socket.ts +0 -230
  102. package/realtime-server/src/realtime-action-synchronizer.ts +0 -164
  103. package/realtime-server/src/realtime-server-stores/server-room-store.ts +0 -97
@@ -243,28 +243,28 @@ declare const IMPLICIT: {
243
243
  declare const clearStore: (store: Store) => void;
244
244
 
245
245
  type Withdrawable = Atom<any> | AtomFamily<any, any> | MutableAtom<any, any> | MutableAtomFamily<any, any, any> | ReadableState<any> | ReadableState<any> | ReadonlySelector<any> | ReadonlySelectorFamily<any, any> | RegularAtom<any> | RegularAtomFamily<any, any> | Selector<any> | SelectorFamily<any, any> | Timeline<any> | Transaction<any> | WritableSelector<any> | WritableSelectorFamily<any, any> | WritableState<any>;
246
- declare function withdraw<T>(token: RegularAtomToken<T>, store: Store): RegularAtom<T> | undefined;
247
- declare function withdraw<T extends Transceiver<any>>(token: MutableAtomToken<T, any>, store: Store): MutableAtom<T, any> | undefined;
248
- declare function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T> | undefined;
249
- declare function withdraw<T>(token: WritableSelectorToken<T>, store: Store): WritableSelector<T> | undefined;
250
- declare function withdraw<T>(token: ReadonlySelectorToken<T>, store: Store): ReadonlySelector<T> | undefined;
251
- declare function withdraw<T>(token: SelectorToken<T>, store: Store): Selector<T> | undefined;
252
- declare function withdraw<T>(token: WritableToken<T>, store: Store): WritableState<T> | undefined;
253
- declare function withdraw<T>(token: ReadableToken<T>, store: Store): ReadableState<T> | undefined;
254
- declare function withdraw<T, K extends Json.Serializable>(token: RegularAtomFamilyToken<T, K>, store: Store): RegularAtomFamily<T, K> | undefined;
255
- declare function withdraw<T extends Transceiver<any>, J extends Json.Serializable, K extends Json.Serializable>(token: MutableAtomFamilyToken<T, J, K>, store: Store): MutableAtomFamily<T, J, K> | undefined;
256
- declare function withdraw<T, K extends Json.Serializable>(token: AtomFamilyToken<T>, store: Store): AtomFamily<T, any> | undefined;
257
- declare function withdraw<T, K extends Json.Serializable>(token: ReadonlySelectorFamilyToken<T, K>, store: Store): ReadonlySelectorFamily<T, any> | undefined;
258
- declare function withdraw<T, K extends Json.Serializable>(token: WritableSelectorFamilyToken<T, K>, store: Store): WritableSelectorFamily<T, any> | undefined;
259
- declare function withdraw<T, K extends Json.Serializable>(token: SelectorFamilyToken<T, K>, store: Store): SelectorFamily<T, any> | undefined;
260
- declare function withdraw<T>(token: TransactionToken<T>, store: Store): Transaction<T extends _n ? T : never> | undefined;
261
- declare function withdraw<T>(token: TimelineToken<T>, store: Store): Timeline<T extends TimelineManageable ? T : never> | undefined;
262
-
263
- declare function withdrawNewFamilyMember<T>(token: RegularAtomToken<T>, store: Store): Atom<T> | undefined;
264
- declare function withdrawNewFamilyMember<T>(token: WritableSelectorToken<T>, store: Store): WritableSelector<T> | undefined;
265
- declare function withdrawNewFamilyMember<T>(token: ReadonlySelectorToken<T>, store: Store): ReadonlySelector<T> | undefined;
266
- declare function withdrawNewFamilyMember<T>(token: WritableToken<T>, store: Store): WritableState<T> | undefined;
267
- declare function withdrawNewFamilyMember<T>(token: ReadableToken<T>, store: Store): ReadableState<T> | undefined;
246
+ declare function withdraw<T>(token: RegularAtomToken<T>, store: Store): RegularAtom<T>;
247
+ declare function withdraw<T extends Transceiver<any>>(token: MutableAtomToken<T, any>, store: Store): MutableAtom<T, any>;
248
+ declare function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T>;
249
+ declare function withdraw<T>(token: WritableSelectorToken<T>, store: Store): WritableSelector<T>;
250
+ declare function withdraw<T>(token: ReadonlySelectorToken<T>, store: Store): ReadonlySelector<T>;
251
+ declare function withdraw<T>(token: SelectorToken<T>, store: Store): Selector<T>;
252
+ declare function withdraw<T>(token: WritableToken<T>, store: Store): WritableState<T>;
253
+ declare function withdraw<T>(token: ReadableToken<T>, store: Store): ReadableState<T>;
254
+ declare function withdraw<T, K extends Json.Serializable>(token: RegularAtomFamilyToken<T, K>, store: Store): RegularAtomFamily<T, K>;
255
+ declare function withdraw<T extends Transceiver<any>, J extends Json.Serializable, K extends Json.Serializable>(token: MutableAtomFamilyToken<T, J, K>, store: Store): MutableAtomFamily<T, J, K>;
256
+ declare function withdraw<T, K extends Json.Serializable>(token: AtomFamilyToken<T>, store: Store): AtomFamily<T, any>;
257
+ declare function withdraw<T, K extends Json.Serializable>(token: ReadonlySelectorFamilyToken<T, K>, store: Store): ReadonlySelectorFamily<T, any>;
258
+ declare function withdraw<T, K extends Json.Serializable>(token: WritableSelectorFamilyToken<T, K>, store: Store): WritableSelectorFamily<T, any>;
259
+ declare function withdraw<T, K extends Json.Serializable>(token: SelectorFamilyToken<T, K>, store: Store): SelectorFamily<T, any>;
260
+ declare function withdraw<T>(token: TransactionToken<T>, store: Store): Transaction<T extends _n ? T : never>;
261
+ declare function withdraw<T>(token: TimelineToken<T>, store: Store): Timeline<T extends TimelineManageable ? T : never>;
262
+
263
+ declare function withdrawOrCreate<T>(token: RegularAtomToken<T>, store: Store): Atom<T>;
264
+ declare function withdrawOrCreate<T>(token: WritableSelectorToken<T>, store: Store): WritableSelector<T>;
265
+ declare function withdrawOrCreate<T>(token: ReadonlySelectorToken<T>, store: Store): ReadonlySelector<T>;
266
+ declare function withdrawOrCreate<T>(token: WritableToken<T>, store: Store): WritableState<T>;
267
+ declare function withdrawOrCreate<T>(token: ReadableToken<T>, store: Store): ReadableState<T>;
268
268
 
269
269
  interface Transceiver<Signal extends Json.Serializable> {
270
270
  do: (update: Signal) => number | `OUT_OF_RANGE` | null;
@@ -320,7 +320,7 @@ declare function createStandaloneAtom<T extends Transceiver<any>, J extends Json
320
320
 
321
321
  declare function createRegularAtom<T>(options: MutableAtomOptions<any, any> | RegularAtomOptions<T>, family: FamilyMetadata | undefined, store: Store): RegularAtomToken<T>;
322
322
 
323
- declare function deleteAtom(atomToken: RegularAtomToken<unknown>, store: Store): void;
323
+ declare function deleteAtom(atomToken: AtomToken<unknown>, store: Store): void;
324
324
 
325
325
  declare const isAtomDefault: (key: string, store: Store) => boolean;
326
326
  declare const markAtomAsDefault: (key: string, store: Store) => void;
@@ -484,4 +484,4 @@ type Selector<T> = ReadonlySelector<T> | WritableSelector<T>;
484
484
  type WritableState<T> = Atom<T> | WritableSelector<T>;
485
485
  type ReadableState<T> = Atom<T> | Selector<T>;
486
486
 
487
- export { type Atom, type AtomIOState, type AtomKey, type ChildStore, type EnvironmentData, FamilyTracker, Future, IMPLICIT, LazyMap, type Lineage, type Modify, type MutableAtom, NotFoundError, type OperationProgress, type ReadableState, type ReadonlySelector, type ReadonlySelectorKey, type RegularAtom, type RootStore, type Selector, type SelectorKey, type Signal, type StateKey, StatefulSubject, Store, Subject, TRANSACTION_PHASES, type Timeline, type TimelineAtomUpdate, type TimelineSelectorUpdate, type TimelineTransactionUpdate, Tracker, type Transaction, type TransactionEpoch, type TransactionPhase, type TransactionProgress, type Transceiver, type TransceiverMode, type Withdrawable, type WritableSelector, type WritableState, abortTransaction, actUponStore, addAtomToTimeline, applyTransaction, arbitrary, assignTransactionToContinuity, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, findInStore, getContinuityKey, getEnvironmentData, getEpochNumberOfAction, getEpochNumberOfContinuity, getFromStore, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isChildStore, isDone, isMutable, isReadonlySelectorKey, isRootStore, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, setEpochNumberOfAction, setEpochNumberOfContinuity, setIntoStore, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
487
+ export { type Atom, type AtomIOState, type AtomKey, type ChildStore, type EnvironmentData, FamilyTracker, Future, IMPLICIT, LazyMap, type Lineage, type Modify, type MutableAtom, NotFoundError, type OperationProgress, type ReadableState, type ReadonlySelector, type ReadonlySelectorKey, type RegularAtom, type RootStore, type Selector, type SelectorKey, type Signal, type StateKey, StatefulSubject, Store, Subject, TRANSACTION_PHASES, type Timeline, type TimelineAtomUpdate, type TimelineSelectorUpdate, type TimelineTransactionUpdate, Tracker, type Transaction, type TransactionEpoch, type TransactionPhase, type TransactionProgress, type Transceiver, type TransceiverMode, type Withdrawable, type WritableSelector, type WritableState, abortTransaction, actUponStore, addAtomToTimeline, applyTransaction, arbitrary, assignTransactionToContinuity, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, findInStore, getContinuityKey, getEnvironmentData, getEpochNumberOfAction, getEpochNumberOfContinuity, getFromStore, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isChildStore, isDone, isMutable, isReadonlySelectorKey, isRootStore, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, setEpochNumberOfAction, setEpochNumberOfContinuity, setIntoStore, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawOrCreate };
@@ -226,27 +226,37 @@ function withdraw(token, store) {
226
226
  }
227
227
  target = target.child;
228
228
  }
229
+ throw new NotFoundError(token, store);
229
230
  }
230
231
 
231
232
  // internal/src/store/withdraw-new-family-member.ts
232
- function withdrawNewFamilyMember(token, store) {
233
- if (token.family) {
234
- store.logger.info(
235
- `\u{1F46A}`,
236
- token.type,
237
- token.key,
238
- `creating new family member in store "${store.config.name}"`
239
- );
240
- const target = newest(store);
241
- const family = target.families.get(token.family.key);
242
- if (family) {
243
- const jsonSubKey = JSON.parse(token.family.subKey);
244
- family(jsonSubKey);
245
- const state = withdraw(token, store);
246
- return state;
233
+ function withdrawOrCreate(token, store) {
234
+ try {
235
+ const state = withdraw(token, store);
236
+ return state;
237
+ } catch (notFoundError) {
238
+ if (token.family) {
239
+ store.logger.info(
240
+ `\u{1F46A}`,
241
+ token.type,
242
+ token.key,
243
+ `creating new family member in store "${store.config.name}"`
244
+ );
245
+ const target = newest(store);
246
+ const family = target.families.get(token.family.key);
247
+ if (family) {
248
+ const jsonSubKey = JSON.parse(token.family.subKey);
249
+ family(jsonSubKey);
250
+ const state = withdraw(token, store);
251
+ return state;
252
+ }
253
+ throw new NotFoundError(
254
+ { key: token.family.key, type: `${token.type}_family` },
255
+ store
256
+ );
247
257
  }
258
+ throw notFoundError;
248
259
  }
249
- return void 0;
250
260
  }
251
261
 
252
262
  // internal/src/future.ts
@@ -268,6 +278,24 @@ var Future = class extends Promise {
268
278
  }
269
279
  };
270
280
 
281
+ // internal/src/set-state/copy-mutable-if-needed.ts
282
+ function copyMutableIfNeeded(atom, origin, target) {
283
+ const originValue = origin.valueMap.get(atom.key);
284
+ const targetValue = target.valueMap.get(atom.key);
285
+ if (originValue === targetValue) {
286
+ if (originValue === void 0) {
287
+ return typeof atom.default === `function` ? atom.default() : atom.default;
288
+ }
289
+ origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
290
+ const jsonValue = atom.toJson(originValue);
291
+ const copiedValue = atom.fromJson(jsonValue);
292
+ target.valueMap.set(atom.key, copiedValue);
293
+ new Tracker(atom, origin);
294
+ return copiedValue;
295
+ }
296
+ return targetValue;
297
+ }
298
+
271
299
  // internal/src/caching.ts
272
300
  function cacheValue(key, value, subject, target) {
273
301
  const currentValue = target.valueMap.get(key);
@@ -294,7 +322,12 @@ function cacheValue(key, value, subject, target) {
294
322
  return value;
295
323
  }
296
324
  var readCachedValue = (token, target) => {
297
- const value = target.valueMap.get(token.key);
325
+ let value = target.valueMap.get(token.key);
326
+ if (token.type === `mutable_atom` && isChildStore(target)) {
327
+ const { parent } = target;
328
+ const copiedValue = copyMutableIfNeeded(token, parent, target);
329
+ value = copiedValue;
330
+ }
298
331
  return value;
299
332
  };
300
333
  var evictCachedValue = (key, target) => {
@@ -336,10 +369,11 @@ function createRegularAtomFamily(options, store) {
336
369
  const subKey = stringifyJson(key);
337
370
  const family = { key: options.key, subKey };
338
371
  const fullKey = `${options.key}(${subKey})`;
339
- const existing = withdraw({ key: fullKey, type: `atom` }, store);
372
+ const target2 = newest(store);
373
+ const atomAlreadyCreated = target2.atoms.has(fullKey);
340
374
  let token;
341
- if (existing) {
342
- token = deposit(existing);
375
+ if (atomAlreadyCreated) {
376
+ token = { type: `atom`, key: fullKey, family };
343
377
  } else {
344
378
  const individualOptions = {
345
379
  key: fullKey,
@@ -541,34 +575,33 @@ var markDone = (key, store) => {
541
575
  store.operation.done.add(key);
542
576
  };
543
577
 
544
- // internal/src/set-state/copy-mutable-if-needed.ts
545
- function copyMutableIfNeeded(atom, origin, target) {
546
- const originValue = origin.valueMap.get(atom.key);
547
- const targetValue = target.valueMap.get(atom.key);
548
- if (originValue === targetValue) {
549
- origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
550
- const jsonValue = atom.toJson(originValue);
551
- const copiedValue = atom.fromJson(jsonValue);
552
- target.valueMap.set(atom.key, copiedValue);
553
- new Tracker(atom, origin);
554
- return copiedValue;
555
- }
556
- return targetValue;
557
- }
558
-
559
578
  // internal/src/set-state/emit-update.ts
560
579
  var emitUpdate = (state, update, store) => {
561
- store.logger.info(
562
- `\u{1F4E2}`,
563
- state.type,
564
- state.key,
565
- `went (`,
566
- update.oldValue,
567
- `->`,
568
- update.newValue,
569
- `) subscribers:`,
570
- state.subject.subscribers
571
- );
580
+ switch (state.type) {
581
+ case `mutable_atom`:
582
+ store.logger.info(
583
+ `\u{1F4E2}`,
584
+ state.type,
585
+ state.key,
586
+ `is now (`,
587
+ update.newValue,
588
+ `) subscribers:`,
589
+ state.subject.subscribers
590
+ );
591
+ break;
592
+ default:
593
+ store.logger.info(
594
+ `\u{1F4E2}`,
595
+ state.type,
596
+ state.key,
597
+ `went (`,
598
+ update.oldValue,
599
+ `->`,
600
+ update.newValue,
601
+ `) subscribers:`,
602
+ state.subject.subscribers
603
+ );
604
+ }
572
605
  state.subject.next(update);
573
606
  };
574
607
 
@@ -669,14 +702,16 @@ var setAtom = (atom, next, target) => {
669
702
  stowUpdate(atom, update, target);
670
703
  } else if (atom.key.startsWith(`*`)) {
671
704
  const mutableKey = atom.key.slice(1);
672
- target.atoms.get(mutableKey);
705
+ const mutableAtom = target.atoms.get(mutableKey);
673
706
  let transceiver = target.valueMap.get(mutableKey);
674
- if (atom.type === `mutable_atom` && isChildStore(target)) {
707
+ if (mutableAtom.type === `mutable_atom` && isChildStore(target)) {
675
708
  const { parent } = target;
676
- const copiedValue = copyMutableIfNeeded(atom, parent, target);
709
+ const copiedValue = copyMutableIfNeeded(mutableAtom, parent, target);
677
710
  transceiver = copiedValue;
678
711
  }
679
- transceiver.do(update.newValue);
712
+ const accepted = transceiver.do(update.newValue) === null;
713
+ if (accepted)
714
+ evictDownStream(mutableAtom, target);
680
715
  }
681
716
  }
682
717
  };
@@ -696,15 +731,11 @@ var setAtomOrSelector = (state, value, store) => {
696
731
 
697
732
  // internal/src/set-state/set-into-store.ts
698
733
  function setIntoStore(token, value, store) {
699
- var _a;
700
734
  const rejection = openOperation(token, store);
701
735
  if (rejection) {
702
736
  return;
703
737
  }
704
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
705
- if (state === void 0) {
706
- throw new NotFoundError(token, store);
707
- }
738
+ const state = withdrawOrCreate(token, store);
708
739
  setAtomOrSelector(state, value, store);
709
740
  closeOperation(store);
710
741
  }
@@ -789,14 +820,8 @@ var updateSelectorAtoms = (selectorKey, dependency, store) => {
789
820
  // internal/src/selector/register-selector.ts
790
821
  var registerSelector = (selectorKey, store) => ({
791
822
  get: (dependency) => {
792
- var _a;
793
823
  const target = newest(store);
794
- const dependencyState = (_a = withdraw(dependency, store)) != null ? _a : withdrawNewFamilyMember(dependency, store);
795
- if (dependencyState === void 0) {
796
- throw new Error(
797
- `State "${dependency.key}" not found in store "${store.config.name}".`
798
- );
799
- }
824
+ const dependencyState = withdrawOrCreate(dependency, store);
800
825
  const dependencyValue = readOrComputeValue(dependencyState, store);
801
826
  store.logger.info(
802
827
  `\u{1F50C}`,
@@ -819,12 +844,7 @@ var registerSelector = (selectorKey, store) => ({
819
844
  return dependencyValue;
820
845
  },
821
846
  set: (WritableToken, newValue) => {
822
- const state = withdraw(WritableToken, store);
823
- if (state === void 0) {
824
- throw new Error(
825
- `State "${WritableToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
826
- );
827
- }
847
+ const state = withdrawOrCreate(WritableToken, store);
828
848
  setAtomOrSelector(state, newValue, store);
829
849
  },
830
850
  find: (token, key) => findInStore(token, key, store)
@@ -1012,8 +1032,7 @@ var subscribeToRootAtoms = (selector, store) => {
1012
1032
 
1013
1033
  // internal/src/subscribe/subscribe-to-state.ts
1014
1034
  function subscribeToState(token, handleUpdate, key, store) {
1015
- var _a;
1016
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1035
+ const state = withdrawOrCreate(token, store);
1017
1036
  if (state === void 0) {
1018
1037
  throw new Error(
1019
1038
  `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
@@ -1048,11 +1067,6 @@ function subscribeToState(token, handleUpdate, key, store) {
1048
1067
  // internal/src/subscribe/subscribe-to-timeline.ts
1049
1068
  var subscribeToTimeline = (token, handleUpdate, key, store) => {
1050
1069
  const tl = withdraw(token, store);
1051
- if (tl === void 0) {
1052
- throw new Error(
1053
- `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
1054
- );
1055
- }
1056
1070
  store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
1057
1071
  const unsubscribe = tl.subject.subscribe(key, handleUpdate);
1058
1072
  return () => {
@@ -1069,11 +1083,6 @@ var subscribeToTimeline = (token, handleUpdate, key, store) => {
1069
1083
  // internal/src/subscribe/subscribe-to-transaction.ts
1070
1084
  var subscribeToTransaction = (token, handleUpdate, key, store) => {
1071
1085
  const tx = withdraw(token, store);
1072
- if (tx === void 0) {
1073
- throw new Error(
1074
- `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
1075
- );
1076
- }
1077
1086
  store.logger.info(
1078
1087
  `\u{1F440}`,
1079
1088
  `transaction`,
@@ -1348,10 +1357,11 @@ function createMutableAtomFamily(options, store) {
1348
1357
  const subKey = stringifyJson(key);
1349
1358
  const family = { key: options.key, subKey };
1350
1359
  const fullKey = `${options.key}(${subKey})`;
1351
- const existing = withdraw({ key: fullKey, type: `mutable_atom` }, store);
1360
+ const target2 = newest(store);
1361
+ const atomAlreadyCreated = target2.atoms.has(fullKey);
1352
1362
  let token;
1353
- if (existing) {
1354
- token = deposit(existing);
1363
+ if (atomAlreadyCreated) {
1364
+ token = { type: `mutable_atom`, key: fullKey, family };
1355
1365
  } else {
1356
1366
  const individualOptions = {
1357
1367
  key: fullKey,
@@ -1551,6 +1561,10 @@ function deleteAtom(atomToken, store) {
1551
1561
  target.selectorAtoms.delete(key);
1552
1562
  target.atomsThatAreDefault.delete(key);
1553
1563
  target.timelineAtoms.delete(key);
1564
+ if (atomToken.type === `mutable_atom`) {
1565
+ const updateToken = getUpdateToken(atomToken);
1566
+ deleteAtom(updateToken, store);
1567
+ }
1554
1568
  store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
1555
1569
  }
1556
1570
 
@@ -1570,11 +1584,7 @@ function getEnvironmentData(store) {
1570
1584
 
1571
1585
  // internal/src/get-state/get-from-store.ts
1572
1586
  function getFromStore(token, store) {
1573
- var _a;
1574
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1575
- if (state === void 0) {
1576
- throw new NotFoundError(token, store);
1577
- }
1587
+ const state = withdrawOrCreate(token, store);
1578
1588
  return readOrComputeValue(state, store);
1579
1589
  }
1580
1590
 
@@ -1582,7 +1592,11 @@ function getFromStore(token, store) {
1582
1592
  function ingestAtomUpdate(applying, atomUpdate, store) {
1583
1593
  const { key, newValue, oldValue } = atomUpdate;
1584
1594
  const value = applying === `newValue` ? newValue : oldValue;
1585
- setIntoStore({ key, type: `atom` }, value, store);
1595
+ const token = { key, type: `atom` };
1596
+ if (atomUpdate.family) {
1597
+ Object.assign(token, { family: atomUpdate.family });
1598
+ }
1599
+ setIntoStore(token, value, store);
1586
1600
  }
1587
1601
 
1588
1602
  // internal/src/ingest-updates/ingest-selector-update.ts
@@ -1642,16 +1656,11 @@ var LazyMap = class extends Map {
1642
1656
  // internal/src/timeline/add-atom-to-timeline.ts
1643
1657
  var addAtomToTimeline = (atomToken, tl, store) => {
1644
1658
  let maybeAtom = withdraw(atomToken, store);
1645
- if ((maybeAtom == null ? void 0 : maybeAtom.type) === `mutable_atom`) {
1659
+ if (maybeAtom.type === `mutable_atom`) {
1646
1660
  const updateToken = getUpdateToken(maybeAtom);
1647
1661
  maybeAtom = withdraw(updateToken, store);
1648
1662
  }
1649
1663
  const atom = maybeAtom;
1650
- if (atom === void 0) {
1651
- throw new Error(
1652
- `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
1653
- );
1654
- }
1655
1664
  store.timelineAtoms.set({ atomKey: atom.key, timelineKey: tl.key });
1656
1665
  atom.subject.subscribe(`timeline`, (update) => {
1657
1666
  var _a, _b, _c, _d, _e, _f;
@@ -1683,15 +1692,11 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1683
1692
  }
1684
1693
  }
1685
1694
  if (currentTransactionKey) {
1686
- const currentTransaction = withdraw(
1687
- { key: currentTransactionKey, type: `transaction` },
1688
- store
1689
- );
1690
- if (currentTransaction === void 0) {
1691
- throw new Error(
1692
- `Transaction "${currentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${currentTransactionKey}".`
1693
- );
1694
- }
1695
+ const txToken = {
1696
+ key: currentTransactionKey,
1697
+ type: `transaction`
1698
+ };
1699
+ const currentTransaction = withdraw(txToken, store);
1695
1700
  if (tl.transactionKey !== currentTransactionKey) {
1696
1701
  if (tl.transactionKey) {
1697
1702
  store.logger.error(
@@ -1863,15 +1868,6 @@ function createTimeline(options, store, data) {
1863
1868
  if (tokenOrFamily.type === `atom_family` || tokenOrFamily.type === `mutable_atom_family`) {
1864
1869
  const familyToken = tokenOrFamily;
1865
1870
  const family = withdraw(familyToken, store);
1866
- if (family === void 0) {
1867
- store.logger.error(
1868
- `\u274C`,
1869
- `timeline`,
1870
- options.key,
1871
- `Failed to add family "${familyToken.key}" because it does not exist in the store`
1872
- );
1873
- continue;
1874
- }
1875
1871
  const familyKey = family.key;
1876
1872
  target.timelineAtoms.set({ atomKey: familyKey, timelineKey });
1877
1873
  family.subject.subscribe(`timeline:${options.key}`, (token2) => {
@@ -1884,26 +1880,8 @@ function createTimeline(options, store, data) {
1884
1880
  }
1885
1881
  } else {
1886
1882
  let atom = withdraw(tokenOrFamily, store);
1887
- if (atom === void 0) {
1888
- store.logger.error(
1889
- `\u274C`,
1890
- `timeline`,
1891
- options.key,
1892
- `Failed to add atom "${atomKey}" because it does not exist in the store`
1893
- );
1894
- continue;
1895
- }
1896
1883
  if (isMutable(atom)) {
1897
1884
  const updateAtom = withdraw(getUpdateToken(atom), store);
1898
- if (updateAtom === void 0) {
1899
- store.logger.error(
1900
- `\u274C`,
1901
- `timeline`,
1902
- options.key,
1903
- `Failed to add update atom "${atomKey}" because it does not exist in the store`
1904
- );
1905
- continue;
1906
- }
1907
1885
  atom = updateAtom;
1908
1886
  atomKey = atom.key;
1909
1887
  }
@@ -2050,7 +2028,6 @@ function setEpochNumberOfAction(transactionKey, newEpoch, store) {
2050
2028
  const continuityKey = store.transactionMeta.actionContinuities.getRelatedKey(transactionKey);
2051
2029
  if (continuityKey !== void 0) {
2052
2030
  store.transactionMeta.epoch.set(continuityKey, newEpoch);
2053
- console.log(`epoch is now`, newEpoch);
2054
2031
  }
2055
2032
  }
2056
2033
 
@@ -2102,7 +2079,6 @@ var applyTransaction = (output, store) => {
2102
2079
  }
2103
2080
  ingestTransactionUpdate(`newValue`, child.transactionMeta.update, parent);
2104
2081
  if (isRootStore(parent)) {
2105
- console.log(child.transactionMeta.update);
2106
2082
  setEpochNumberOfAction(
2107
2083
  child.transactionMeta.update.key,
2108
2084
  child.transactionMeta.update.epoch,
@@ -2247,6 +2223,6 @@ function getEpochNumberOfAction(transactionKey, store) {
2247
2223
  // internal/src/transaction/index.ts
2248
2224
  var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
2249
2225
 
2250
- export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, StatefulSubject, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, actUponStore, addAtomToTimeline, applyTransaction, arbitrary, assignTransactionToContinuity, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, findInStore, getContinuityKey, getEnvironmentData, getEpochNumberOfAction, getEpochNumberOfContinuity, getFromStore, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isChildStore, isDone, isMutable, isReadonlySelectorKey, isRootStore, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, setEpochNumberOfAction, setEpochNumberOfContinuity, setIntoStore, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
2226
+ export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, StatefulSubject, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, actUponStore, addAtomToTimeline, applyTransaction, arbitrary, assignTransactionToContinuity, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, findInStore, getContinuityKey, getEnvironmentData, getEpochNumberOfAction, getEpochNumberOfContinuity, getFromStore, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isChildStore, isDone, isMutable, isReadonlySelectorKey, isRootStore, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, setEpochNumberOfAction, setEpochNumberOfContinuity, setIntoStore, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawOrCreate };
2251
2227
  //# sourceMappingURL=out.js.map
2252
2228
  //# sourceMappingURL=index.js.map