atom.io 0.18.1 → 0.18.3

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 (84) hide show
  1. package/data/dist/index.cjs +0 -2
  2. package/data/dist/index.js +2 -4
  3. package/dist/{chunk-JDUNWJFB.js → chunk-BWWVY5O5.js} +1 -3
  4. package/dist/{chunk-NYCVSXQB.js → chunk-CVBEVTM5.js} +0 -2
  5. package/dist/chunk-O47EQUM6.js +29 -0
  6. package/dist/{chunk-PZLG2HP3.js → chunk-U2IICNHQ.js} +0 -2
  7. package/dist/{chunk-XACXFU3B.js → chunk-VAE5OCKN.js} +0 -2
  8. package/dist/{chunk-NYTGCPHB.js → chunk-WX2NCOZR.js} +0 -2
  9. package/dist/{chunk-IZHOMSXA.js → chunk-YDOGCZ53.js} +0 -2
  10. package/dist/index.cjs +0 -2
  11. package/dist/index.js +1 -3
  12. package/internal/dist/index.cjs +46 -102
  13. package/internal/dist/index.d.ts +23 -23
  14. package/internal/dist/index.js +48 -104
  15. package/internal/src/families/create-regular-atom-family.ts +4 -4
  16. package/internal/src/get-state/get-from-store.ts +2 -5
  17. package/internal/src/mutable/create-mutable-atom-family.ts +4 -4
  18. package/internal/src/selector/register-selector.ts +3 -14
  19. package/internal/src/set-state/set-into-store.ts +2 -5
  20. package/internal/src/store/withdraw-new-family-member.ts +38 -28
  21. package/internal/src/store/withdraw.ts +20 -23
  22. package/internal/src/subscribe/subscribe-to-state.ts +2 -3
  23. package/internal/src/subscribe/subscribe-to-timeline.ts +0 -5
  24. package/internal/src/subscribe/subscribe-to-transaction.ts +0 -5
  25. package/internal/src/timeline/add-atom-to-timeline.ts +6 -15
  26. package/internal/src/timeline/create-timeline.ts +0 -27
  27. package/introspection/dist/index.cjs +0 -2
  28. package/introspection/dist/index.js +1 -3
  29. package/json/dist/index.cjs +0 -2
  30. package/json/dist/index.js +3 -5
  31. package/package.json +6 -5
  32. package/react/dist/index.cjs +2 -5
  33. package/react/dist/index.js +3 -6
  34. package/react/src/use-tl.ts +2 -2
  35. package/react-devtools/dist/index.cjs +1 -3
  36. package/react-devtools/dist/index.css +0 -1
  37. package/react-devtools/dist/index.js +4 -6
  38. package/realtime/dist/index.cjs +0 -2
  39. package/realtime/dist/index.js +1 -3
  40. package/realtime-client/dist/index.cjs +1 -3
  41. package/realtime-client/dist/index.js +15 -41
  42. package/realtime-react/dist/index.cjs +0 -2
  43. package/realtime-react/dist/index.js +2 -4
  44. package/realtime-server/dist/index.cjs +3 -5
  45. package/realtime-server/dist/index.js +4 -6
  46. package/realtime-server/src/ipc-sockets/parent-socket.ts +4 -3
  47. package/realtime-testing/dist/index.cjs +43 -7
  48. package/realtime-testing/dist/index.js +6 -9
  49. package/realtime-testing/src/setup-realtime-test.tsx +3 -5
  50. package/transceivers/set-rtx/dist/index.cjs +0 -2
  51. package/transceivers/set-rtx/dist/index.js +1 -3
  52. package/data/dist/index.cjs.map +0 -1
  53. package/data/dist/index.js.map +0 -1
  54. package/dist/chunk-IZHOMSXA.js.map +0 -1
  55. package/dist/chunk-JDUNWJFB.js.map +0 -1
  56. package/dist/chunk-NYCVSXQB.js.map +0 -1
  57. package/dist/chunk-NYTGCPHB.js.map +0 -1
  58. package/dist/chunk-PZLG2HP3.js.map +0 -1
  59. package/dist/chunk-XACXFU3B.js.map +0 -1
  60. package/dist/index.cjs.map +0 -1
  61. package/dist/index.js.map +0 -1
  62. package/internal/dist/index.cjs.map +0 -1
  63. package/internal/dist/index.js.map +0 -1
  64. package/introspection/dist/index.cjs.map +0 -1
  65. package/introspection/dist/index.js.map +0 -1
  66. package/json/dist/index.cjs.map +0 -1
  67. package/json/dist/index.js.map +0 -1
  68. package/react/dist/index.cjs.map +0 -1
  69. package/react/dist/index.js.map +0 -1
  70. package/react-devtools/dist/index.cjs.map +0 -1
  71. package/react-devtools/dist/index.css.map +0 -1
  72. package/react-devtools/dist/index.js.map +0 -1
  73. package/realtime/dist/index.cjs.map +0 -1
  74. package/realtime/dist/index.js.map +0 -1
  75. package/realtime-client/dist/index.cjs.map +0 -1
  76. package/realtime-client/dist/index.js.map +0 -1
  77. package/realtime-react/dist/index.cjs.map +0 -1
  78. package/realtime-react/dist/index.js.map +0 -1
  79. package/realtime-server/dist/index.cjs.map +0 -1
  80. package/realtime-server/dist/index.js.map +0 -1
  81. package/realtime-testing/dist/index.cjs.map +0 -1
  82. package/realtime-testing/dist/index.js.map +0 -1
  83. package/transceivers/set-rtx/dist/index.cjs.map +0 -1
  84. package/transceivers/set-rtx/dist/index.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import { Junction } from '../../dist/chunk-NYTGCPHB.js';
2
- import { __spreadValues, __spreadProps } from '../../dist/chunk-PZLG2HP3.js';
1
+ import { Junction } from '../../dist/chunk-WX2NCOZR.js';
2
+ import { __spreadValues, __spreadProps } from '../../dist/chunk-U2IICNHQ.js';
3
3
  import { stringifyJson, selectJson, parseJson, selectJsonFamily } from 'atom.io/json';
4
4
  import { AtomIOLogger } from 'atom.io';
5
5
 
@@ -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
@@ -359,10 +369,11 @@ function createRegularAtomFamily(options, store) {
359
369
  const subKey = stringifyJson(key);
360
370
  const family = { key: options.key, subKey };
361
371
  const fullKey = `${options.key}(${subKey})`;
362
- const existing = withdraw({ key: fullKey, type: `atom` }, store);
372
+ const target2 = newest(store);
373
+ const atomAlreadyCreated = target2.atoms.has(fullKey);
363
374
  let token;
364
- if (existing) {
365
- token = deposit(existing);
375
+ if (atomAlreadyCreated) {
376
+ token = { type: `atom`, key: fullKey, family };
366
377
  } else {
367
378
  const individualOptions = {
368
379
  key: fullKey,
@@ -720,15 +731,11 @@ var setAtomOrSelector = (state, value, store) => {
720
731
 
721
732
  // internal/src/set-state/set-into-store.ts
722
733
  function setIntoStore(token, value, store) {
723
- var _a;
724
734
  const rejection = openOperation(token, store);
725
735
  if (rejection) {
726
736
  return;
727
737
  }
728
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
729
- if (state === void 0) {
730
- throw new NotFoundError(token, store);
731
- }
738
+ const state = withdrawOrCreate(token, store);
732
739
  setAtomOrSelector(state, value, store);
733
740
  closeOperation(store);
734
741
  }
@@ -813,14 +820,8 @@ var updateSelectorAtoms = (selectorKey, dependency, store) => {
813
820
  // internal/src/selector/register-selector.ts
814
821
  var registerSelector = (selectorKey, store) => ({
815
822
  get: (dependency) => {
816
- var _a;
817
823
  const target = newest(store);
818
- const dependencyState = (_a = withdraw(dependency, store)) != null ? _a : withdrawNewFamilyMember(dependency, store);
819
- if (dependencyState === void 0) {
820
- throw new Error(
821
- `State "${dependency.key}" not found in store "${store.config.name}".`
822
- );
823
- }
824
+ const dependencyState = withdrawOrCreate(dependency, store);
824
825
  const dependencyValue = readOrComputeValue(dependencyState, store);
825
826
  store.logger.info(
826
827
  `\u{1F50C}`,
@@ -843,12 +844,7 @@ var registerSelector = (selectorKey, store) => ({
843
844
  return dependencyValue;
844
845
  },
845
846
  set: (WritableToken, newValue) => {
846
- const state = withdraw(WritableToken, store);
847
- if (state === void 0) {
848
- throw new Error(
849
- `State "${WritableToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
850
- );
851
- }
847
+ const state = withdrawOrCreate(WritableToken, store);
852
848
  setAtomOrSelector(state, newValue, store);
853
849
  },
854
850
  find: (token, key) => findInStore(token, key, store)
@@ -1036,8 +1032,7 @@ var subscribeToRootAtoms = (selector, store) => {
1036
1032
 
1037
1033
  // internal/src/subscribe/subscribe-to-state.ts
1038
1034
  function subscribeToState(token, handleUpdate, key, store) {
1039
- var _a;
1040
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1035
+ const state = withdrawOrCreate(token, store);
1041
1036
  if (state === void 0) {
1042
1037
  throw new Error(
1043
1038
  `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
@@ -1072,11 +1067,6 @@ function subscribeToState(token, handleUpdate, key, store) {
1072
1067
  // internal/src/subscribe/subscribe-to-timeline.ts
1073
1068
  var subscribeToTimeline = (token, handleUpdate, key, store) => {
1074
1069
  const tl = withdraw(token, store);
1075
- if (tl === void 0) {
1076
- throw new Error(
1077
- `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
1078
- );
1079
- }
1080
1070
  store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
1081
1071
  const unsubscribe = tl.subject.subscribe(key, handleUpdate);
1082
1072
  return () => {
@@ -1093,11 +1083,6 @@ var subscribeToTimeline = (token, handleUpdate, key, store) => {
1093
1083
  // internal/src/subscribe/subscribe-to-transaction.ts
1094
1084
  var subscribeToTransaction = (token, handleUpdate, key, store) => {
1095
1085
  const tx = withdraw(token, store);
1096
- if (tx === void 0) {
1097
- throw new Error(
1098
- `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
1099
- );
1100
- }
1101
1086
  store.logger.info(
1102
1087
  `\u{1F440}`,
1103
1088
  `transaction`,
@@ -1372,10 +1357,11 @@ function createMutableAtomFamily(options, store) {
1372
1357
  const subKey = stringifyJson(key);
1373
1358
  const family = { key: options.key, subKey };
1374
1359
  const fullKey = `${options.key}(${subKey})`;
1375
- const existing = withdraw({ key: fullKey, type: `mutable_atom` }, store);
1360
+ const target2 = newest(store);
1361
+ const atomAlreadyCreated = target2.atoms.has(fullKey);
1376
1362
  let token;
1377
- if (existing) {
1378
- token = deposit(existing);
1363
+ if (atomAlreadyCreated) {
1364
+ token = { type: `mutable_atom`, key: fullKey, family };
1379
1365
  } else {
1380
1366
  const individualOptions = {
1381
1367
  key: fullKey,
@@ -1598,11 +1584,7 @@ function getEnvironmentData(store) {
1598
1584
 
1599
1585
  // internal/src/get-state/get-from-store.ts
1600
1586
  function getFromStore(token, store) {
1601
- var _a;
1602
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1603
- if (state === void 0) {
1604
- throw new NotFoundError(token, store);
1605
- }
1587
+ const state = withdrawOrCreate(token, store);
1606
1588
  return readOrComputeValue(state, store);
1607
1589
  }
1608
1590
 
@@ -1674,16 +1656,11 @@ var LazyMap = class extends Map {
1674
1656
  // internal/src/timeline/add-atom-to-timeline.ts
1675
1657
  var addAtomToTimeline = (atomToken, tl, store) => {
1676
1658
  let maybeAtom = withdraw(atomToken, store);
1677
- if ((maybeAtom == null ? void 0 : maybeAtom.type) === `mutable_atom`) {
1659
+ if (maybeAtom.type === `mutable_atom`) {
1678
1660
  const updateToken = getUpdateToken(maybeAtom);
1679
1661
  maybeAtom = withdraw(updateToken, store);
1680
1662
  }
1681
1663
  const atom = maybeAtom;
1682
- if (atom === void 0) {
1683
- throw new Error(
1684
- `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
1685
- );
1686
- }
1687
1664
  store.timelineAtoms.set({ atomKey: atom.key, timelineKey: tl.key });
1688
1665
  atom.subject.subscribe(`timeline`, (update) => {
1689
1666
  var _a, _b, _c, _d, _e, _f;
@@ -1715,15 +1692,11 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1715
1692
  }
1716
1693
  }
1717
1694
  if (currentTransactionKey) {
1718
- const currentTransaction = withdraw(
1719
- { key: currentTransactionKey, type: `transaction` },
1720
- store
1721
- );
1722
- if (currentTransaction === void 0) {
1723
- throw new Error(
1724
- `Transaction "${currentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${currentTransactionKey}".`
1725
- );
1726
- }
1695
+ const txToken = {
1696
+ key: currentTransactionKey,
1697
+ type: `transaction`
1698
+ };
1699
+ const currentTransaction = withdraw(txToken, store);
1727
1700
  if (tl.transactionKey !== currentTransactionKey) {
1728
1701
  if (tl.transactionKey) {
1729
1702
  store.logger.error(
@@ -1895,15 +1868,6 @@ function createTimeline(options, store, data) {
1895
1868
  if (tokenOrFamily.type === `atom_family` || tokenOrFamily.type === `mutable_atom_family`) {
1896
1869
  const familyToken = tokenOrFamily;
1897
1870
  const family = withdraw(familyToken, store);
1898
- if (family === void 0) {
1899
- store.logger.error(
1900
- `\u274C`,
1901
- `timeline`,
1902
- options.key,
1903
- `Failed to add family "${familyToken.key}" because it does not exist in the store`
1904
- );
1905
- continue;
1906
- }
1907
1871
  const familyKey = family.key;
1908
1872
  target.timelineAtoms.set({ atomKey: familyKey, timelineKey });
1909
1873
  family.subject.subscribe(`timeline:${options.key}`, (token2) => {
@@ -1916,26 +1880,8 @@ function createTimeline(options, store, data) {
1916
1880
  }
1917
1881
  } else {
1918
1882
  let atom = withdraw(tokenOrFamily, store);
1919
- if (atom === void 0) {
1920
- store.logger.error(
1921
- `\u274C`,
1922
- `timeline`,
1923
- options.key,
1924
- `Failed to add atom "${atomKey}" because it does not exist in the store`
1925
- );
1926
- continue;
1927
- }
1928
1883
  if (isMutable(atom)) {
1929
1884
  const updateAtom = withdraw(getUpdateToken(atom), store);
1930
- if (updateAtom === void 0) {
1931
- store.logger.error(
1932
- `\u274C`,
1933
- `timeline`,
1934
- options.key,
1935
- `Failed to add update atom "${atomKey}" because it does not exist in the store`
1936
- );
1937
- continue;
1938
- }
1939
1885
  atom = updateAtom;
1940
1886
  atomKey = atom.key;
1941
1887
  }
@@ -2277,6 +2223,4 @@ function getEpochNumberOfAction(transactionKey, store) {
2277
2223
  // internal/src/transaction/index.ts
2278
2224
  var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
2279
2225
 
2280
- 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 };
2281
- //# sourceMappingURL=out.js.map
2282
- //# sourceMappingURL=index.js.map
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 };
@@ -10,7 +10,6 @@ import { stringifyJson } from "atom.io/json"
10
10
 
11
11
  import { createRegularAtom } from "../atom"
12
12
  import { newest } from "../lineage"
13
- import { deposit, withdraw } from "../store"
14
13
  import type { Store } from "../store"
15
14
  import { Subject } from "../subject"
16
15
 
@@ -24,10 +23,11 @@ export function createRegularAtomFamily<T, K extends Json.Serializable>(
24
23
  const subKey = stringifyJson(key)
25
24
  const family: FamilyMetadata = { key: options.key, subKey }
26
25
  const fullKey = `${options.key}(${subKey})`
27
- const existing = withdraw({ key: fullKey, type: `atom` }, store)
26
+ const target = newest(store)
27
+ const atomAlreadyCreated = target.atoms.has(fullKey)
28
28
  let token: RegularAtomToken<any>
29
- if (existing) {
30
- token = deposit(existing)
29
+ if (atomAlreadyCreated) {
30
+ token = { type: `atom`, key: fullKey, family }
31
31
  } else {
32
32
  const individualOptions: RegularAtomOptions<any> = {
33
33
  key: fullKey,
@@ -2,13 +2,10 @@ import type { ReadableToken } from "atom.io"
2
2
 
3
3
  import { NotFoundError } from "../not-found-error"
4
4
  import type { Store } from "../store"
5
- import { withdraw, withdrawNewFamilyMember } from "../store"
5
+ import { withdraw, withdrawOrCreate } from "../store"
6
6
  import { readOrComputeValue } from "./read-or-compute-value"
7
7
 
8
8
  export function getFromStore<T>(token: ReadableToken<T>, store: Store): T {
9
- const state = withdraw(token, store) ?? withdrawNewFamilyMember(token, store)
10
- if (state === undefined) {
11
- throw new NotFoundError(token, store)
12
- }
9
+ const state = withdrawOrCreate(token, store)
13
10
  return readOrComputeValue(state, store)
14
11
  }
@@ -11,7 +11,6 @@ import { stringifyJson } from "atom.io/json"
11
11
 
12
12
  import { newest } from "../lineage"
13
13
  import { createMutableAtom } from "../mutable"
14
- import { deposit, withdraw } from "../store"
15
14
  import type { Store } from "../store"
16
15
  import { Subject } from "../subject"
17
16
  import { FamilyTracker } from "./tracker-family"
@@ -31,10 +30,11 @@ export function createMutableAtomFamily<
31
30
  const subKey = stringifyJson(key)
32
31
  const family: FamilyMetadata = { key: options.key, subKey }
33
32
  const fullKey = `${options.key}(${subKey})`
34
- const existing = withdraw({ key: fullKey, type: `mutable_atom` }, store)
33
+ const target = newest(store)
34
+ const atomAlreadyCreated = target.atoms.has(fullKey)
35
35
  let token: MutableAtomToken<T, J>
36
- if (existing) {
37
- token = deposit(existing)
36
+ if (atomAlreadyCreated) {
37
+ token = { type: `mutable_atom`, key: fullKey, family }
38
38
  } else {
39
39
  const individualOptions: MutableAtomOptions<T, J> = {
40
40
  key: fullKey,
@@ -5,7 +5,7 @@ import { readOrComputeValue } from "../get-state/read-or-compute-value"
5
5
  import { newest } from "../lineage"
6
6
  import { setAtomOrSelector } from "../set-state"
7
7
  import type { Store } from "../store"
8
- import { withdraw, withdrawNewFamilyMember } from "../store"
8
+ import { withdrawOrCreate } from "../store"
9
9
  import { updateSelectorAtoms } from "./update-selector-atoms"
10
10
 
11
11
  export const registerSelector = (
@@ -15,13 +15,7 @@ export const registerSelector = (
15
15
  get: (dependency) => {
16
16
  const target = newest(store)
17
17
 
18
- const dependencyState =
19
- withdraw(dependency, store) ?? withdrawNewFamilyMember(dependency, store)
20
- if (dependencyState === undefined) {
21
- throw new Error(
22
- `State "${dependency.key}" not found in store "${store.config.name}".`,
23
- )
24
- }
18
+ const dependencyState = withdrawOrCreate(dependency, store)
25
19
  const dependencyValue = readOrComputeValue(dependencyState, store)
26
20
 
27
21
  store.logger.info(
@@ -46,12 +40,7 @@ export const registerSelector = (
46
40
  return dependencyValue
47
41
  },
48
42
  set: (WritableToken, newValue) => {
49
- const state = withdraw(WritableToken, store)
50
- if (state === undefined) {
51
- throw new Error(
52
- `State "${WritableToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`,
53
- )
54
- }
43
+ const state = withdrawOrCreate(WritableToken, store)
55
44
  setAtomOrSelector(state, newValue, store)
56
45
  },
57
46
  find: ((token, key) => findInStore(token, key, store)) as typeof findState,
@@ -3,7 +3,7 @@ import type { WritableToken } from "atom.io"
3
3
  import { NotFoundError } from "../not-found-error"
4
4
  import { closeOperation, openOperation } from "../operation"
5
5
  import type { Store } from "../store"
6
- import { withdraw, withdrawNewFamilyMember } from "../store"
6
+ import { withdrawOrCreate } from "../store"
7
7
  import { setAtomOrSelector } from "./set-atom-or-selector"
8
8
 
9
9
  export function setIntoStore<T, New extends T>(
@@ -15,10 +15,7 @@ export function setIntoStore<T, New extends T>(
15
15
  if (rejection) {
16
16
  return
17
17
  }
18
- const state = withdraw(token, store) ?? withdrawNewFamilyMember(token, store)
19
- if (state === undefined) {
20
- throw new NotFoundError(token, store)
21
- }
18
+ const state = withdrawOrCreate(token, store)
22
19
  setAtomOrSelector(state, value, store)
23
20
  closeOperation(store)
24
21
  }
@@ -5,6 +5,7 @@ import type {
5
5
  WritableSelectorToken,
6
6
  WritableToken,
7
7
  } from "atom.io"
8
+
8
9
  import type {
9
10
  Atom,
10
11
  ReadableState,
@@ -13,47 +14,56 @@ import type {
13
14
  WritableSelector,
14
15
  WritableState,
15
16
  } from ".."
16
- import { newest, withdraw } from ".."
17
+ import { NotFoundError, newest, withdraw } from ".."
17
18
 
18
- export function withdrawNewFamilyMember<T>(
19
+ export function withdrawOrCreate<T>(
19
20
  token: RegularAtomToken<T>,
20
21
  store: Store,
21
- ): Atom<T> | undefined
22
- export function withdrawNewFamilyMember<T>(
22
+ ): Atom<T>
23
+ export function withdrawOrCreate<T>(
23
24
  token: WritableSelectorToken<T>,
24
25
  store: Store,
25
- ): WritableSelector<T> | undefined
26
- export function withdrawNewFamilyMember<T>(
26
+ ): WritableSelector<T>
27
+ export function withdrawOrCreate<T>(
27
28
  token: ReadonlySelectorToken<T>,
28
29
  store: Store,
29
- ): ReadonlySelector<T> | undefined
30
- export function withdrawNewFamilyMember<T>(
30
+ ): ReadonlySelector<T>
31
+ export function withdrawOrCreate<T>(
31
32
  token: WritableToken<T>,
32
33
  store: Store,
33
- ): WritableState<T> | undefined
34
- export function withdrawNewFamilyMember<T>(
34
+ ): WritableState<T>
35
+ export function withdrawOrCreate<T>(
35
36
  token: ReadableToken<T>,
36
37
  store: Store,
37
- ): ReadableState<T> | undefined
38
- export function withdrawNewFamilyMember<T>(
38
+ ): ReadableState<T>
39
+ export function withdrawOrCreate<T>(
39
40
  token: ReadableToken<T>,
40
41
  store: Store,
41
- ): ReadableState<T> | undefined {
42
- if (token.family) {
43
- store.logger.info(
44
- `👪`,
45
- token.type,
46
- token.key,
47
- `creating new family member in store "${store.config.name}"`,
48
- )
49
- const target = newest(store)
50
- const family = target.families.get(token.family.key)
51
- if (family) {
52
- const jsonSubKey = JSON.parse(token.family.subKey)
53
- family(jsonSubKey)
54
- const state = withdraw(token, store)
55
- return state
42
+ ): ReadableState<T> {
43
+ try {
44
+ const state = withdraw(token, store)
45
+ return state
46
+ } catch (notFoundError) {
47
+ if (token.family) {
48
+ store.logger.info(
49
+ `👪`,
50
+ token.type,
51
+ token.key,
52
+ `creating new family member in store "${store.config.name}"`,
53
+ )
54
+ const target = newest(store)
55
+ const family = target.families.get(token.family.key)
56
+ if (family) {
57
+ const jsonSubKey = JSON.parse(token.family.subKey)
58
+ family(jsonSubKey)
59
+ const state = withdraw(token, store)
60
+ return state
61
+ }
62
+ throw new NotFoundError(
63
+ { key: token.family.key, type: `${token.type}_family` },
64
+ store,
65
+ )
56
66
  }
67
+ throw notFoundError
57
68
  }
58
- return undefined
59
69
  }
@@ -37,6 +37,8 @@ import type {
37
37
  WritableSelector,
38
38
  WritableState,
39
39
  } from ".."
40
+ import { NotFoundError } from ".."
41
+
40
42
  import type { Timeline } from "../timeline"
41
43
  import type { Transaction } from "../transaction"
42
44
  import type { Store } from "./store"
@@ -63,40 +65,34 @@ export type Withdrawable =
63
65
  export function withdraw<T>(
64
66
  token: RegularAtomToken<T>,
65
67
  store: Store,
66
- ): RegularAtom<T> | undefined
68
+ ): RegularAtom<T>
67
69
  export function withdraw<T extends Transceiver<any>>(
68
70
  token: MutableAtomToken<T, any>,
69
71
  store: Store,
70
- ): MutableAtom<T, any> | undefined
71
- export function withdraw<T>(
72
- token: AtomToken<T>,
73
- store: Store,
74
- ): Atom<T> | undefined
72
+ ): MutableAtom<T, any>
73
+ export function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T>
75
74
  export function withdraw<T>(
76
75
  token: WritableSelectorToken<T>,
77
76
  store: Store,
78
- ): WritableSelector<T> | undefined
77
+ ): WritableSelector<T>
79
78
  export function withdraw<T>(
80
79
  token: ReadonlySelectorToken<T>,
81
80
  store: Store,
82
- ): ReadonlySelector<T> | undefined
83
- export function withdraw<T>(
84
- token: SelectorToken<T>,
85
- store: Store,
86
- ): Selector<T> | undefined
81
+ ): ReadonlySelector<T>
82
+ export function withdraw<T>(token: SelectorToken<T>, store: Store): Selector<T>
87
83
  export function withdraw<T>(
88
84
  token: WritableToken<T>,
89
85
  store: Store,
90
- ): WritableState<T> | undefined
86
+ ): WritableState<T>
91
87
  export function withdraw<T>(
92
88
  token: ReadableToken<T>,
93
89
  store: Store,
94
- ): ReadableState<T> | undefined
90
+ ): ReadableState<T>
95
91
 
96
92
  export function withdraw<T, K extends Json.Serializable>(
97
93
  token: RegularAtomFamilyToken<T, K>,
98
94
  store: Store,
99
- ): RegularAtomFamily<T, K> | undefined
95
+ ): RegularAtomFamily<T, K>
100
96
  export function withdraw<
101
97
  T extends Transceiver<any>,
102
98
  J extends Json.Serializable,
@@ -104,32 +100,32 @@ export function withdraw<
104
100
  >(
105
101
  token: MutableAtomFamilyToken<T, J, K>,
106
102
  store: Store,
107
- ): MutableAtomFamily<T, J, K> | undefined
103
+ ): MutableAtomFamily<T, J, K>
108
104
  export function withdraw<T, K extends Json.Serializable>(
109
105
  token: AtomFamilyToken<T>,
110
106
  store: Store,
111
- ): AtomFamily<T, any> | undefined
107
+ ): AtomFamily<T, any>
112
108
  export function withdraw<T, K extends Json.Serializable>(
113
109
  token: ReadonlySelectorFamilyToken<T, K>,
114
110
  store: Store,
115
- ): ReadonlySelectorFamily<T, any> | undefined
111
+ ): ReadonlySelectorFamily<T, any>
116
112
  export function withdraw<T, K extends Json.Serializable>(
117
113
  token: WritableSelectorFamilyToken<T, K>,
118
114
  store: Store,
119
- ): WritableSelectorFamily<T, any> | undefined
115
+ ): WritableSelectorFamily<T, any>
120
116
  export function withdraw<T, K extends Json.Serializable>(
121
117
  token: SelectorFamilyToken<T, K>,
122
118
  store: Store,
123
- ): SelectorFamily<T, any> | undefined
119
+ ): SelectorFamily<T, any>
124
120
 
125
121
  export function withdraw<T>(
126
122
  token: TransactionToken<T>,
127
123
  store: Store,
128
- ): Transaction<T extends ƒn ? T : never> | undefined
124
+ ): Transaction<T extends ƒn ? T : never>
129
125
  export function withdraw<T>(
130
126
  token: TimelineToken<T>,
131
127
  store: Store,
132
- ): Timeline<T extends TimelineManageable ? T : never> | undefined
128
+ ): Timeline<T extends TimelineManageable ? T : never>
133
129
  export function withdraw<T>(
134
130
  token:
135
131
  | RegularAtomFamilyToken<T, any>
@@ -142,7 +138,7 @@ export function withdraw<T>(
142
138
  ? MutableAtomFamilyToken<T, any, any> | MutableAtomToken<T, any>
143
139
  : never),
144
140
  store: Store,
145
- ): Withdrawable | undefined {
141
+ ): Withdrawable {
146
142
  let withdrawn: Withdrawable | undefined
147
143
  let target: Store | null = store
148
144
  while (target !== null) {
@@ -175,4 +171,5 @@ export function withdraw<T>(
175
171
  }
176
172
  target = target.child
177
173
  }
174
+ throw new NotFoundError(token, store)
178
175
  }
@@ -1,6 +1,6 @@
1
1
  import type { ReadableToken, UpdateHandler } from "atom.io"
2
2
  import type { Store } from "../store"
3
- import { withdraw, withdrawNewFamilyMember } from "../store"
3
+ import { withdrawOrCreate } from "../store"
4
4
  import { subscribeToRootAtoms } from "./subscribe-to-root-atoms"
5
5
 
6
6
  export function subscribeToState<T>(
@@ -9,8 +9,7 @@ export function subscribeToState<T>(
9
9
  key: string,
10
10
  store: Store,
11
11
  ): () => void {
12
- const state =
13
- withdraw<T>(token, store) ?? withdrawNewFamilyMember(token, store)
12
+ const state = withdrawOrCreate(token, store)
14
13
  if (state === undefined) {
15
14
  throw new Error(
16
15
  `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`,
@@ -9,11 +9,6 @@ export const subscribeToTimeline = <ManagedAtom extends TimelineManageable>(
9
9
  store: Store,
10
10
  ): (() => void) => {
11
11
  const tl = withdraw(token, store)
12
- if (tl === undefined) {
13
- throw new Error(
14
- `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`,
15
- )
16
- }
17
12
  store.logger.info(`👀`, `timeline`, token.key, `Adding subscription "${key}"`)
18
13
  const unsubscribe = tl.subject.subscribe(key, handleUpdate)
19
14
  return () => {
@@ -9,11 +9,6 @@ export const subscribeToTransaction = <ƒ extends ƒn>(
9
9
  store: Store,
10
10
  ): (() => void) => {
11
11
  const tx = withdraw(token, store)
12
- if (tx === undefined) {
13
- throw new Error(
14
- `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`,
15
- )
16
- }
17
12
  store.logger.info(
18
13
  `👀`,
19
14
  `transaction`,