atom.io 0.18.1 → 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 (45) hide show
  1. package/dist/chunk-3J2EGSBE.js +31 -0
  2. package/dist/chunk-3J2EGSBE.js.map +1 -0
  3. package/dist/{chunk-JDUNWJFB.js → chunk-A4ZCNKWQ.js} +2 -2
  4. package/dist/chunk-A4ZCNKWQ.js.map +1 -0
  5. package/internal/dist/index.cjs +46 -100
  6. package/internal/dist/index.cjs.map +1 -1
  7. package/internal/dist/index.d.ts +23 -23
  8. package/internal/dist/index.js +46 -100
  9. package/internal/dist/index.js.map +1 -1
  10. package/internal/src/families/create-regular-atom-family.ts +4 -4
  11. package/internal/src/get-state/get-from-store.ts +2 -5
  12. package/internal/src/mutable/create-mutable-atom-family.ts +4 -4
  13. package/internal/src/selector/register-selector.ts +3 -14
  14. package/internal/src/set-state/set-into-store.ts +2 -5
  15. package/internal/src/store/withdraw-new-family-member.ts +38 -28
  16. package/internal/src/store/withdraw.ts +20 -23
  17. package/internal/src/subscribe/subscribe-to-state.ts +2 -3
  18. package/internal/src/subscribe/subscribe-to-timeline.ts +0 -5
  19. package/internal/src/subscribe/subscribe-to-transaction.ts +0 -5
  20. package/internal/src/timeline/add-atom-to-timeline.ts +6 -15
  21. package/internal/src/timeline/create-timeline.ts +0 -27
  22. package/package.json +6 -5
  23. package/react/dist/index.cjs +2 -3
  24. package/react/dist/index.cjs.map +1 -1
  25. package/react/dist/index.js +2 -3
  26. package/react/dist/index.js.map +1 -1
  27. package/react/src/use-tl.ts +2 -2
  28. package/react-devtools/dist/index.cjs +1 -1
  29. package/react-devtools/dist/index.cjs.map +1 -1
  30. package/react-devtools/dist/index.js +1 -1
  31. package/realtime-client/dist/index.cjs +1 -1
  32. package/realtime-client/dist/index.cjs.map +1 -1
  33. package/realtime-client/dist/index.js +13 -37
  34. package/realtime-client/dist/index.js.map +1 -1
  35. package/realtime-server/dist/index.cjs +3 -3
  36. package/realtime-server/dist/index.cjs.map +1 -1
  37. package/realtime-server/dist/index.js +3 -3
  38. package/realtime-server/dist/index.js.map +1 -1
  39. package/realtime-server/src/ipc-sockets/parent-socket.ts +4 -3
  40. package/realtime-testing/dist/index.cjs +43 -5
  41. package/realtime-testing/dist/index.cjs.map +1 -1
  42. package/realtime-testing/dist/index.js +4 -5
  43. package/realtime-testing/dist/index.js.map +1 -1
  44. package/realtime-testing/src/setup-realtime-test.tsx +3 -5
  45. package/dist/chunk-JDUNWJFB.js.map +0 -1
@@ -0,0 +1,31 @@
1
+ import { lazyLocalStorageEffect } from './chunk-A4ZCNKWQ.js';
2
+ import * as AtomIO from 'atom.io';
3
+
4
+ var myIdState__INTERNAL = AtomIO.atom({
5
+ key: `mySocketId__INTERNAL`,
6
+ default: void 0
7
+ });
8
+ var myIdState = AtomIO.selector({
9
+ key: `mySocketId`,
10
+ get: ({ get }) => get(myIdState__INTERNAL)
11
+ });
12
+ var usernameEffects = typeof window === `undefined` ? [] : [lazyLocalStorageEffect(`myUsername`)];
13
+ var myUsernameState = AtomIO.atom({
14
+ key: `myUsername`,
15
+ default: null,
16
+ effects: usernameEffects
17
+ });
18
+ var optimisticUpdateQueue = AtomIO.atom({
19
+ key: `updateQueue`,
20
+ default: []
21
+ });
22
+ var confirmedUpdateQueue = AtomIO.atom(
23
+ {
24
+ key: `serverConfirmedUpdateQueue`,
25
+ default: []
26
+ }
27
+ );
28
+
29
+ export { confirmedUpdateQueue, myIdState, myIdState__INTERNAL, myUsernameState, optimisticUpdateQueue };
30
+ //# sourceMappingURL=out.js.map
31
+ //# sourceMappingURL=chunk-3J2EGSBE.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../realtime-client/src/realtime-client-stores/client-main-store.ts","../realtime-client/src/realtime-client-stores/client-sync-store.ts"],"names":["AtomIO"],"mappings":";;;;;AAAA,YAAY,YAAY;AAIjB,IAAM,sBAA6B,YAAyB;AAAA,EAClE,KAAK;AAAA,EACL,SAAS;AACV,CAAC;AACM,IAAM,YAAmB,gBAA6B;AAAA,EAC5D,KAAK;AAAA,EACL,KAAK,CAAC,EAAE,IAAI,MAAM,IAAI,mBAAmB;AAC1C,CAAC;AAED,IAAM,kBACL,OAAO,WAAW,cAAc,CAAC,IAAI,CAAC,uBAAuB,YAAY,CAAC;AACpE,IAAM,kBAAyB,YAAoB;AAAA,EACzD,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AACV,CAAC;;;ACnBD,YAAYA,aAAY;AAEjB,IAAM,wBAA+B,aAE1C;AAAA,EACD,KAAK;AAAA,EACL,SAAS,CAAC;AACX,CAAC;AAEM,IAAM,uBAA8B;AAAA,EAC1C;AAAA,IACC,KAAK;AAAA,IACL,SAAS,CAAC;AAAA,EACX;AACD","sourcesContent":["import * as AtomIO from \"atom.io\"\n\nimport { lazyLocalStorageEffect } from \"~/packages/atom.io/__unstable__/web-effects/src/storage\"\n\nexport const myIdState__INTERNAL = AtomIO.atom<string | undefined>({\n\tkey: `mySocketId__INTERNAL`,\n\tdefault: undefined,\n})\nexport const myIdState = AtomIO.selector<string | undefined>({\n\tkey: `mySocketId`,\n\tget: ({ get }) => get(myIdState__INTERNAL),\n})\n\nconst usernameEffects =\n\ttypeof window === `undefined` ? [] : [lazyLocalStorageEffect(`myUsername`)]\nexport const myUsernameState = AtomIO.atom<string | null>({\n\tkey: `myUsername`,\n\tdefault: null,\n\teffects: usernameEffects,\n})\n","import * as AtomIO from \"atom.io\"\n\nexport const optimisticUpdateQueue = AtomIO.atom<\n\tAtomIO.TransactionUpdate<any>[]\n>({\n\tkey: `updateQueue`,\n\tdefault: [],\n})\n\nexport const confirmedUpdateQueue = AtomIO.atom<AtomIO.TransactionUpdate<any>[]>(\n\t{\n\t\tkey: `serverConfirmedUpdateQueue`,\n\t\tdefault: [],\n\t},\n)\n"]}
@@ -11,8 +11,8 @@ var persistAtom = (storage) => ({ stringify, parse }) => (key) => ({ setSelf, on
11
11
  storage.setItem(key, stringify(newValue));
12
12
  });
13
13
  };
14
- var lazyLocalStorageEffect = persistAtom(localStorage)(JSON);
14
+ var lazyLocalStorageEffect = persistAtom(window.localStorage)(JSON);
15
15
 
16
16
  export { lazyLocalStorageEffect };
17
17
  //# sourceMappingURL=out.js.map
18
- //# sourceMappingURL=chunk-JDUNWJFB.js.map
18
+ //# sourceMappingURL=chunk-A4ZCNKWQ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../__unstable__/web-effects/src/storage.ts"],"names":[],"mappings":";AAQO,IAAM,cACZ,CAAI,YACJ,CAAC,EAAE,WAAW,MAAM,MACpB,CAAC,QACD,CAAC,EAAE,SAAS,MAAM,MAAM;AACvB,QAAM,aAAa,QAAQ,QAAQ,GAAG;AACtC,MAAI,cAAc;AAAM,YAAQ,MAAM,UAAU,CAAC;AAEjD,QAAM,CAAC,EAAE,SAAS,MAAM;AACvB,QAAI,YAAY,MAAM;AACrB,cAAQ,WAAW,GAAG;AACtB;AAAA,IACD;AACA,YAAQ,QAAQ,KAAK,UAAU,QAAQ,CAAC;AAAA,EACzC,CAAC;AACF;AAEM,IAAM,yBAEQ,YAAY,OAAO,YAAY,EAAE,IAAI","sourcesContent":["import type { AtomEffect } from \"atom.io\"\nimport type { Json } from \"atom.io/json\"\n\nexport type StringInterface<T> = {\n\tstringify: (t: T) => string\n\tparse: (s: string) => T\n}\n\nexport const persistAtom =\n\t<T>(storage: Storage) =>\n\t({ stringify, parse }: StringInterface<T>) =>\n\t(key: string): AtomEffect<T> =>\n\t({ setSelf, onSet }) => {\n\t\tconst savedValue = storage.getItem(key)\n\t\tif (savedValue != null) setSelf(parse(savedValue))\n\n\t\tonSet(({ newValue }) => {\n\t\t\tif (newValue == null) {\n\t\t\t\tstorage.removeItem(key)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tstorage.setItem(key, stringify(newValue))\n\t\t})\n\t}\n\nexport const lazyLocalStorageEffect: <J extends Json.Serializable>(\n\tkey: string,\n) => AtomEffect<J> = persistAtom(window.localStorage)(JSON)\n"]}
@@ -504,27 +504,37 @@ function withdraw(token, store) {
504
504
  }
505
505
  target = target.child;
506
506
  }
507
+ throw new NotFoundError(token, store);
507
508
  }
508
509
 
509
510
  // internal/src/store/withdraw-new-family-member.ts
510
- function withdrawNewFamilyMember(token, store) {
511
- if (token.family) {
512
- store.logger.info(
513
- `\u{1F46A}`,
514
- token.type,
515
- token.key,
516
- `creating new family member in store "${store.config.name}"`
517
- );
518
- const target = newest(store);
519
- const family = target.families.get(token.family.key);
520
- if (family) {
521
- const jsonSubKey = JSON.parse(token.family.subKey);
522
- family(jsonSubKey);
523
- const state = withdraw(token, store);
524
- return state;
511
+ function withdrawOrCreate(token, store) {
512
+ try {
513
+ const state = withdraw(token, store);
514
+ return state;
515
+ } catch (notFoundError) {
516
+ if (token.family) {
517
+ store.logger.info(
518
+ `\u{1F46A}`,
519
+ token.type,
520
+ token.key,
521
+ `creating new family member in store "${store.config.name}"`
522
+ );
523
+ const target = newest(store);
524
+ const family = target.families.get(token.family.key);
525
+ if (family) {
526
+ const jsonSubKey = JSON.parse(token.family.subKey);
527
+ family(jsonSubKey);
528
+ const state = withdraw(token, store);
529
+ return state;
530
+ }
531
+ throw new NotFoundError(
532
+ { key: token.family.key, type: `${token.type}_family` },
533
+ store
534
+ );
525
535
  }
536
+ throw notFoundError;
526
537
  }
527
- return void 0;
528
538
  }
529
539
 
530
540
  // internal/src/future.ts
@@ -637,10 +647,11 @@ function createRegularAtomFamily(options, store) {
637
647
  const subKey = json.stringifyJson(key);
638
648
  const family = { key: options.key, subKey };
639
649
  const fullKey = `${options.key}(${subKey})`;
640
- const existing = withdraw({ key: fullKey, type: `atom` }, store);
650
+ const target2 = newest(store);
651
+ const atomAlreadyCreated = target2.atoms.has(fullKey);
641
652
  let token;
642
- if (existing) {
643
- token = deposit(existing);
653
+ if (atomAlreadyCreated) {
654
+ token = { type: `atom`, key: fullKey, family };
644
655
  } else {
645
656
  const individualOptions = {
646
657
  key: fullKey,
@@ -998,15 +1009,11 @@ var setAtomOrSelector = (state, value, store) => {
998
1009
 
999
1010
  // internal/src/set-state/set-into-store.ts
1000
1011
  function setIntoStore(token, value, store) {
1001
- var _a;
1002
1012
  const rejection = openOperation(token, store);
1003
1013
  if (rejection) {
1004
1014
  return;
1005
1015
  }
1006
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1007
- if (state === void 0) {
1008
- throw new NotFoundError(token, store);
1009
- }
1016
+ const state = withdrawOrCreate(token, store);
1010
1017
  setAtomOrSelector(state, value, store);
1011
1018
  closeOperation(store);
1012
1019
  }
@@ -1091,14 +1098,8 @@ var updateSelectorAtoms = (selectorKey, dependency, store) => {
1091
1098
  // internal/src/selector/register-selector.ts
1092
1099
  var registerSelector = (selectorKey, store) => ({
1093
1100
  get: (dependency) => {
1094
- var _a;
1095
1101
  const target = newest(store);
1096
- const dependencyState = (_a = withdraw(dependency, store)) != null ? _a : withdrawNewFamilyMember(dependency, store);
1097
- if (dependencyState === void 0) {
1098
- throw new Error(
1099
- `State "${dependency.key}" not found in store "${store.config.name}".`
1100
- );
1101
- }
1102
+ const dependencyState = withdrawOrCreate(dependency, store);
1102
1103
  const dependencyValue = readOrComputeValue(dependencyState, store);
1103
1104
  store.logger.info(
1104
1105
  `\u{1F50C}`,
@@ -1121,12 +1122,7 @@ var registerSelector = (selectorKey, store) => ({
1121
1122
  return dependencyValue;
1122
1123
  },
1123
1124
  set: (WritableToken, newValue) => {
1124
- const state = withdraw(WritableToken, store);
1125
- if (state === void 0) {
1126
- throw new Error(
1127
- `State "${WritableToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1128
- );
1129
- }
1125
+ const state = withdrawOrCreate(WritableToken, store);
1130
1126
  setAtomOrSelector(state, newValue, store);
1131
1127
  },
1132
1128
  find: (token, key) => findInStore(token, key, store)
@@ -1314,8 +1310,7 @@ var subscribeToRootAtoms = (selector, store) => {
1314
1310
 
1315
1311
  // internal/src/subscribe/subscribe-to-state.ts
1316
1312
  function subscribeToState(token, handleUpdate, key, store) {
1317
- var _a;
1318
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1313
+ const state = withdrawOrCreate(token, store);
1319
1314
  if (state === void 0) {
1320
1315
  throw new Error(
1321
1316
  `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
@@ -1350,11 +1345,6 @@ function subscribeToState(token, handleUpdate, key, store) {
1350
1345
  // internal/src/subscribe/subscribe-to-timeline.ts
1351
1346
  var subscribeToTimeline = (token, handleUpdate, key, store) => {
1352
1347
  const tl = withdraw(token, store);
1353
- if (tl === void 0) {
1354
- throw new Error(
1355
- `Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
1356
- );
1357
- }
1358
1348
  store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
1359
1349
  const unsubscribe = tl.subject.subscribe(key, handleUpdate);
1360
1350
  return () => {
@@ -1371,11 +1361,6 @@ var subscribeToTimeline = (token, handleUpdate, key, store) => {
1371
1361
  // internal/src/subscribe/subscribe-to-transaction.ts
1372
1362
  var subscribeToTransaction = (token, handleUpdate, key, store) => {
1373
1363
  const tx = withdraw(token, store);
1374
- if (tx === void 0) {
1375
- throw new Error(
1376
- `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
1377
- );
1378
- }
1379
1364
  store.logger.info(
1380
1365
  `\u{1F440}`,
1381
1366
  `transaction`,
@@ -1650,10 +1635,11 @@ function createMutableAtomFamily(options, store) {
1650
1635
  const subKey = json.stringifyJson(key);
1651
1636
  const family = { key: options.key, subKey };
1652
1637
  const fullKey = `${options.key}(${subKey})`;
1653
- const existing = withdraw({ key: fullKey, type: `mutable_atom` }, store);
1638
+ const target2 = newest(store);
1639
+ const atomAlreadyCreated = target2.atoms.has(fullKey);
1654
1640
  let token;
1655
- if (existing) {
1656
- token = deposit(existing);
1641
+ if (atomAlreadyCreated) {
1642
+ token = { type: `mutable_atom`, key: fullKey, family };
1657
1643
  } else {
1658
1644
  const individualOptions = {
1659
1645
  key: fullKey,
@@ -1876,11 +1862,7 @@ function getEnvironmentData(store) {
1876
1862
 
1877
1863
  // internal/src/get-state/get-from-store.ts
1878
1864
  function getFromStore(token, store) {
1879
- var _a;
1880
- const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
1881
- if (state === void 0) {
1882
- throw new NotFoundError(token, store);
1883
- }
1865
+ const state = withdrawOrCreate(token, store);
1884
1866
  return readOrComputeValue(state, store);
1885
1867
  }
1886
1868
 
@@ -1952,16 +1934,11 @@ var LazyMap = class extends Map {
1952
1934
  // internal/src/timeline/add-atom-to-timeline.ts
1953
1935
  var addAtomToTimeline = (atomToken, tl, store) => {
1954
1936
  let maybeAtom = withdraw(atomToken, store);
1955
- if ((maybeAtom == null ? void 0 : maybeAtom.type) === `mutable_atom`) {
1937
+ if (maybeAtom.type === `mutable_atom`) {
1956
1938
  const updateToken = getUpdateToken(maybeAtom);
1957
1939
  maybeAtom = withdraw(updateToken, store);
1958
1940
  }
1959
1941
  const atom = maybeAtom;
1960
- if (atom === void 0) {
1961
- throw new Error(
1962
- `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
1963
- );
1964
- }
1965
1942
  store.timelineAtoms.set({ atomKey: atom.key, timelineKey: tl.key });
1966
1943
  atom.subject.subscribe(`timeline`, (update) => {
1967
1944
  var _a, _b, _c, _d, _e, _f;
@@ -1993,15 +1970,11 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1993
1970
  }
1994
1971
  }
1995
1972
  if (currentTransactionKey) {
1996
- const currentTransaction = withdraw(
1997
- { key: currentTransactionKey, type: `transaction` },
1998
- store
1999
- );
2000
- if (currentTransaction === void 0) {
2001
- throw new Error(
2002
- `Transaction "${currentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${currentTransactionKey}".`
2003
- );
2004
- }
1973
+ const txToken = {
1974
+ key: currentTransactionKey,
1975
+ type: `transaction`
1976
+ };
1977
+ const currentTransaction = withdraw(txToken, store);
2005
1978
  if (tl.transactionKey !== currentTransactionKey) {
2006
1979
  if (tl.transactionKey) {
2007
1980
  store.logger.error(
@@ -2173,15 +2146,6 @@ function createTimeline(options, store, data) {
2173
2146
  if (tokenOrFamily.type === `atom_family` || tokenOrFamily.type === `mutable_atom_family`) {
2174
2147
  const familyToken = tokenOrFamily;
2175
2148
  const family = withdraw(familyToken, store);
2176
- if (family === void 0) {
2177
- store.logger.error(
2178
- `\u274C`,
2179
- `timeline`,
2180
- options.key,
2181
- `Failed to add family "${familyToken.key}" because it does not exist in the store`
2182
- );
2183
- continue;
2184
- }
2185
2149
  const familyKey = family.key;
2186
2150
  target.timelineAtoms.set({ atomKey: familyKey, timelineKey });
2187
2151
  family.subject.subscribe(`timeline:${options.key}`, (token2) => {
@@ -2194,26 +2158,8 @@ function createTimeline(options, store, data) {
2194
2158
  }
2195
2159
  } else {
2196
2160
  let atom = withdraw(tokenOrFamily, store);
2197
- if (atom === void 0) {
2198
- store.logger.error(
2199
- `\u274C`,
2200
- `timeline`,
2201
- options.key,
2202
- `Failed to add atom "${atomKey}" because it does not exist in the store`
2203
- );
2204
- continue;
2205
- }
2206
2161
  if (isMutable(atom)) {
2207
2162
  const updateAtom = withdraw(getUpdateToken(atom), store);
2208
- if (updateAtom === void 0) {
2209
- store.logger.error(
2210
- `\u274C`,
2211
- `timeline`,
2212
- options.key,
2213
- `Failed to add update atom "${atomKey}" because it does not exist in the store`
2214
- );
2215
- continue;
2216
- }
2217
2163
  atom = updateAtom;
2218
2164
  atomKey = atom.key;
2219
2165
  }
@@ -2637,6 +2583,6 @@ exports.traceAllSelectorAtoms = traceAllSelectorAtoms;
2637
2583
  exports.traceSelectorAtoms = traceSelectorAtoms;
2638
2584
  exports.updateSelectorAtoms = updateSelectorAtoms;
2639
2585
  exports.withdraw = withdraw;
2640
- exports.withdrawNewFamilyMember = withdrawNewFamilyMember;
2586
+ exports.withdrawOrCreate = withdrawOrCreate;
2641
2587
  //# sourceMappingURL=out.js.map
2642
2588
  //# sourceMappingURL=index.cjs.map