atom.io 0.4.1 → 0.5.0

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 (58) hide show
  1. package/README.md +38 -10
  2. package/dist/index.d.mts +598 -0
  3. package/dist/index.d.ts +51 -14
  4. package/dist/index.js +143 -28
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +142 -28
  7. package/dist/index.mjs.map +1 -1
  8. package/json/dist/index.d.mts +18 -0
  9. package/json/dist/index.d.ts +18 -0
  10. package/json/dist/index.js +51 -0
  11. package/json/dist/index.js.map +1 -0
  12. package/json/dist/index.mjs +15 -0
  13. package/json/dist/index.mjs.map +1 -0
  14. package/json/package.json +15 -0
  15. package/package.json +34 -7
  16. package/react/dist/index.d.mts +17 -0
  17. package/react-devtools/dist/index.d.mts +15 -0
  18. package/react-devtools/dist/index.js +1 -1
  19. package/react-devtools/dist/index.js.map +1 -1
  20. package/react-devtools/dist/index.mjs +1 -1
  21. package/react-devtools/dist/index.mjs.map +1 -1
  22. package/realtime/dist/index.d.mts +25 -0
  23. package/realtime/dist/index.d.ts +25 -0
  24. package/realtime/dist/index.js +168 -0
  25. package/realtime/dist/index.js.map +1 -0
  26. package/realtime/dist/index.mjs +130 -0
  27. package/realtime/dist/index.mjs.map +1 -0
  28. package/realtime/package.json +15 -0
  29. package/src/index.ts +22 -0
  30. package/src/internal/atom-internal.ts +1 -1
  31. package/src/internal/families-internal.ts +3 -3
  32. package/src/internal/get.ts +22 -12
  33. package/src/internal/selector-internal.ts +10 -0
  34. package/src/internal/set.ts +1 -1
  35. package/src/internal/store.ts +1 -1
  36. package/src/internal/subscribe-internal.ts +5 -0
  37. package/src/internal/timeline-internal.ts +13 -1
  38. package/src/internal/transaction-internal.ts +24 -9
  39. package/src/json/index.ts +1 -0
  40. package/src/json/select-json.ts +18 -0
  41. package/src/react-explorer/explorer-states.ts +5 -5
  42. package/src/react-explorer/index.ts +1 -1
  43. package/src/react-explorer/space-states.ts +6 -7
  44. package/src/realtime/hook-composition/expose-family.ts +101 -0
  45. package/src/realtime/hook-composition/expose-single.ts +38 -0
  46. package/src/realtime/hook-composition/index.ts +11 -0
  47. package/src/realtime/hook-composition/receive-transaction.ts +19 -0
  48. package/src/realtime/index.ts +1 -0
  49. package/src/realtime-client/hook-composition/compose-realtime-hooks.ts +62 -0
  50. package/src/realtime-client/hook-composition/realtime-client-family-member.ts +28 -0
  51. package/src/realtime-client/hook-composition/realtime-client-family.ts +26 -0
  52. package/src/realtime-client/hook-composition/realtime-client-single.ts +24 -0
  53. package/src/realtime-client/hook-composition/realtime-client-transaction.ts +35 -0
  54. package/src/realtime-client/index.ts +1 -0
  55. package/src/selector.ts +9 -6
  56. package/src/silo.ts +45 -0
  57. package/src/subscribe.ts +13 -1
  58. package/src/transaction.ts +11 -4
package/dist/index.mjs CHANGED
@@ -34,6 +34,9 @@ var __export = (target2, all) => {
34
34
  __defProp(target2, name, { get: all[name], enumerable: true });
35
35
  };
36
36
 
37
+ // ../anvl/src/string/capitalize.ts
38
+ var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
39
+
37
40
  // src/internal/index.ts
38
41
  var internal_exports = {};
39
42
  __export(internal_exports, {
@@ -107,9 +110,9 @@ function lookup(key, store) {
107
110
  return { key, type };
108
111
  }
109
112
  function withdraw(token, store) {
110
- var _a, _b, _c;
113
+ var _a, _b, _c, _d;
111
114
  const core = target(store);
112
- return (_c = (_b = (_a = HAMT.get(token.key, core.atoms)) != null ? _a : HAMT.get(token.key, core.selectors)) != null ? _b : HAMT.get(token.key, core.readonlySelectors)) != null ? _c : HAMT.get(token.key, core.transactions);
115
+ return (_d = (_c = (_b = (_a = HAMT.get(token.key, core.atoms)) != null ? _a : HAMT.get(token.key, core.selectors)) != null ? _b : HAMT.get(token.key, core.readonlySelectors)) != null ? _c : HAMT.get(token.key, core.transactions)) != null ? _d : null;
113
116
  }
114
117
  function deposit(state) {
115
118
  return __spreadValues({
@@ -201,7 +204,7 @@ var recordToEntries = (obj) => Object.entries(obj);
201
204
  var entriesToRecord = (entries) => Object.fromEntries(entries);
202
205
 
203
206
  // ../anvl/src/object/mapObject.ts
204
- import { pipe } from "fp-ts/lib/function";
207
+ import { pipe } from "fp-ts/function";
205
208
  var mapObject = (obj, fn) => pipe(
206
209
  obj,
207
210
  recordToEntries,
@@ -305,13 +308,19 @@ var EMPTY_RELATION_DATA = {
305
308
  a: `from`,
306
309
  b: `to`
307
310
  };
308
- var isRelationData = (isContent, a = `from`, b = `to`) => (input) => hasExactProperties({
309
- contents: isContent ? isRecord(isString, isContent) : hasExactProperties({}),
310
- relations: isRecord(isString, isArray(isString)),
311
- relationType: isRelationType,
312
- a: isLiteral(a),
313
- b: isLiteral(b)
314
- })(input);
311
+ var isRelationData = ({
312
+ from: a = `from`,
313
+ to: b = `to`,
314
+ isContent
315
+ } = {}) => (input) => {
316
+ return hasExactProperties({
317
+ contents: isContent ? isRecord(isString, isContent) : hasExactProperties({}),
318
+ relations: isRecord(isString, isArray(isString)),
319
+ relationType: isRelationType,
320
+ a: isLiteral(a),
321
+ b: isLiteral(b)
322
+ })(input);
323
+ };
315
324
 
316
325
  // ../anvl/src/join/get-related-ids.ts
317
326
  var getRelatedIds = (relationMap, id) => {
@@ -328,6 +337,21 @@ var getRelatedId = (relationMap, id) => {
328
337
  return relations[0];
329
338
  };
330
339
 
340
+ // ../anvl/src/join/make-json-interface.ts
341
+ var makeJsonInterface = (join, ...params) => {
342
+ const isContent = params[0];
343
+ const { a, b } = join;
344
+ const options = {
345
+ from: a,
346
+ to: b,
347
+ isContent
348
+ };
349
+ return {
350
+ toJson: (join2) => join2.toJSON(),
351
+ fromJson: (json) => Join.fromJSON(json, options)
352
+ };
353
+ };
354
+
331
355
  // ../anvl/src/join/relation-contents.ts
332
356
  import { pipe as pipe5 } from "fp-ts/function";
333
357
 
@@ -518,11 +542,16 @@ var setRelations = (current, subject, relations) => {
518
542
  };
519
543
 
520
544
  // ../anvl/src/join/index.ts
521
- var Join = class {
545
+ var Join = class _Join {
522
546
  constructor(json) {
523
547
  this.a = `from`;
524
548
  this.b = `to`;
525
- Object.assign(this, __spreadValues(__spreadValues({}, EMPTY_RELATION_DATA), json));
549
+ this.makeJsonInterface = (...params) => {
550
+ return makeJsonInterface(this, ...params);
551
+ };
552
+ Object.assign(this, __spreadProps(__spreadValues(__spreadValues({}, EMPTY_RELATION_DATA), json), {
553
+ makeJsonInterface: this.makeJsonInterface
554
+ }));
526
555
  }
527
556
  toJSON() {
528
557
  return {
@@ -533,20 +562,20 @@ var Join = class {
533
562
  b: this.b
534
563
  };
535
564
  }
536
- static fromJSON(json, isContent = cannotExist, a = `from`, b = `to`) {
537
- const isValid = isRelationData(isContent, a, b)(json);
565
+ static fromJSON(json, options) {
566
+ const isValid = isRelationData(options)(json);
538
567
  if (isValid) {
539
- return new Join(json);
568
+ return new _Join(json);
540
569
  }
541
570
  throw new Error(
542
571
  `Saved JSON for this Join is invalid: ${JSON.stringify(json)}`
543
572
  );
544
573
  }
545
574
  from(newA) {
546
- return new Join(__spreadProps(__spreadValues({}, this), { a: newA }));
575
+ return new _Join(__spreadProps(__spreadValues({}, this), { a: newA }));
547
576
  }
548
577
  to(newB) {
549
- return new Join(__spreadProps(__spreadValues({}, this), { b: newB }));
578
+ return new _Join(__spreadProps(__spreadValues({}, this), { b: newB }));
550
579
  }
551
580
  getRelatedId(id) {
552
581
  return getRelatedId(this, id);
@@ -570,13 +599,13 @@ var Join = class {
570
599
  return getRelations(this, id);
571
600
  }
572
601
  setRelations(subject, relations) {
573
- return new Join(setRelations(this, subject, relations));
602
+ return new _Join(setRelations(this, subject, relations));
574
603
  }
575
604
  set(relation, ...rest) {
576
- return new Join(setRelationWithContent(this, relation, ...rest));
605
+ return new _Join(setRelationWithContent(this, relation, ...rest));
577
606
  }
578
607
  remove(relation) {
579
- return new Join(
608
+ return new _Join(
580
609
  removeRelation(this, relation)
581
610
  );
582
611
  }
@@ -762,18 +791,22 @@ var applyTransaction = (output, store) => {
762
791
  for (const { key, newValue } of atomUpdates) {
763
792
  const token = { key, type: `atom` };
764
793
  if (!HAMT4.has(token.key, store.valueMap)) {
765
- const atom2 = HAMT4.get(token.key, store.transactionStatus.core.atoms);
766
- store.atoms = HAMT4.set(atom2.key, atom2, store.atoms);
767
- store.valueMap = HAMT4.set(atom2.key, atom2.default, store.valueMap);
768
- (_c = store.config.logger) == null ? void 0 : _c.info(`\u{1F527}`, `add atom "${atom2.key}"`);
794
+ const newAtom = HAMT4.get(token.key, store.transactionStatus.core.atoms);
795
+ store.atoms = HAMT4.set(newAtom.key, newAtom, store.atoms);
796
+ store.valueMap = HAMT4.set(newAtom.key, newAtom.default, store.valueMap);
797
+ (_c = store.config.logger) == null ? void 0 : _c.info(`\u{1F527}`, `add atom "${newAtom.key}"`);
769
798
  }
770
- const state = withdraw(token, store);
771
- setState(state, newValue, store);
799
+ setState(token, newValue, store);
772
800
  }
773
801
  const myTransaction = withdraw(
774
802
  { key: store.transactionStatus.key, type: `transaction` },
775
803
  store
776
804
  );
805
+ if (myTransaction === null) {
806
+ throw new Error(
807
+ `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`
808
+ );
809
+ }
777
810
  myTransaction.subject.next({
778
811
  key: store.transactionStatus.key,
779
812
  atomUpdates,
@@ -789,6 +822,11 @@ var undoTransactionUpdate = (update, store) => {
789
822
  for (const { key, oldValue } of update.atomUpdates) {
790
823
  const token = { key, type: `atom` };
791
824
  const state = withdraw(token, store);
825
+ if (state === null) {
826
+ throw new Error(
827
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
828
+ );
829
+ }
792
830
  setState(state, oldValue, store);
793
831
  }
794
832
  };
@@ -798,6 +836,11 @@ var redoTransactionUpdate = (update, store) => {
798
836
  for (const { key, newValue } of update.atomUpdates) {
799
837
  const token = { key, type: `atom` };
800
838
  const state = withdraw(token, store);
839
+ if (state === null) {
840
+ throw new Error(
841
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
842
+ );
843
+ }
801
844
  setState(state, newValue, store);
802
845
  }
803
846
  };
@@ -1154,6 +1197,11 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1154
1197
  const core = target(store);
1155
1198
  const alreadyRegistered = core.selectorGraph.getRelations(selectorKey).some(({ source }) => source === dependency.key);
1156
1199
  const dependencyState = withdraw(dependency, store);
1200
+ if (dependencyState === null) {
1201
+ throw new Error(
1202
+ `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1203
+ );
1204
+ }
1157
1205
  const dependencyValue = getState__INTERNAL(dependencyState, store);
1158
1206
  if (alreadyRegistered) {
1159
1207
  (_a = store.config.logger) == null ? void 0 : _a.info(
@@ -1178,6 +1226,11 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1178
1226
  },
1179
1227
  set: (stateToken, newValue) => {
1180
1228
  const state = withdraw(stateToken, store);
1229
+ if (state === null) {
1230
+ throw new Error(
1231
+ `State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1232
+ );
1233
+ }
1181
1234
  setState__INTERNAL(state, newValue, store);
1182
1235
  }
1183
1236
  });
@@ -1285,7 +1338,7 @@ var setAtomState = (atom2, next, store = IMPLICIT.STORE) => {
1285
1338
  const newValue = become(next)(oldValue);
1286
1339
  (_a = store.config.logger) == null ? void 0 : _a.info(`<< setting atom "${atom2.key}" to`, newValue);
1287
1340
  cacheValue(atom2.key, newValue, store);
1288
- if (isAtomDefault(atom2.key)) {
1341
+ if (isAtomDefault(atom2.key, store)) {
1289
1342
  markAtomAsNotDefault(atom2.key, store);
1290
1343
  }
1291
1344
  markDone(atom2.key, store);
@@ -1350,6 +1403,11 @@ var emitUpdate = (state, update, store) => {
1350
1403
  var subscribeToRootAtoms = (state, store) => {
1351
1404
  const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomToken) => {
1352
1405
  const atom2 = withdraw(atomToken, store);
1406
+ if (atom2 === null) {
1407
+ throw new Error(
1408
+ `Atom "${atomToken.key}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
1409
+ );
1410
+ }
1353
1411
  return atom2.subject.subscribe((atomChange) => {
1354
1412
  var _a, _b;
1355
1413
  (_a = store.config.logger) == null ? void 0 : _a.info(
@@ -1461,6 +1519,11 @@ function timeline__INTERNAL(options, store = IMPLICIT.STORE) {
1461
1519
  };
1462
1520
  const subscribeToAtom = (token2) => {
1463
1521
  const state = withdraw(token2, store);
1522
+ if (state === null) {
1523
+ throw new Error(
1524
+ `Cannot subscribe to atom "${token2.key}" because it has not been initialized in store "${store.config.name}"`
1525
+ );
1526
+ }
1464
1527
  state.subject.subscribe((update) => {
1465
1528
  var _a2, _b2, _c, _d, _e;
1466
1529
  const storeCurrentSelectorKey = store.operation.open && store.operation.token.type === `selector` ? store.operation.token.key : null;
@@ -1478,6 +1541,11 @@ function timeline__INTERNAL(options, store = IMPLICIT.STORE) {
1478
1541
  { key: storeCurrentTransactionKey, type: `transaction` },
1479
1542
  store
1480
1543
  );
1544
+ if (currentTransaction === null) {
1545
+ throw new Error(
1546
+ `Transaction "${storeCurrentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${storeCurrentTransactionKey}".`
1547
+ );
1548
+ }
1481
1549
  if (timelineData.transactionKey !== storeCurrentTransactionKey) {
1482
1550
  if (timelineData.transactionKey) {
1483
1551
  (_b2 = store.config.logger) == null ? void 0 : _b2.error(
@@ -1653,10 +1721,32 @@ function selectorFamily(options) {
1653
1721
  return selectorFamily__INTERNAL(options);
1654
1722
  }
1655
1723
 
1724
+ // src/silo.ts
1725
+ var silo = (name) => {
1726
+ const store = createStore(name);
1727
+ return {
1728
+ store,
1729
+ atom: (options) => atom__INTERNAL(options, void 0, store),
1730
+ atomFamily: (options) => atomFamily__INTERNAL(options, store),
1731
+ selector: (options) => selector__INTERNAL(options, void 0, store),
1732
+ selectorFamily: (options) => selectorFamily__INTERNAL(options, store),
1733
+ transaction: (options) => transaction__INTERNAL(options, store),
1734
+ timeline: (options) => timeline__INTERNAL(options, store),
1735
+ getState: (token) => getState(token, store),
1736
+ setState: (token, newValue) => setState(token, newValue, store),
1737
+ subscribe: (token, handler) => subscribe(token, handler, store)
1738
+ };
1739
+ };
1740
+
1656
1741
  // src/subscribe.ts
1657
1742
  var subscribe = (token, handleUpdate, store = IMPLICIT.STORE) => {
1658
1743
  var _a;
1659
1744
  const state = withdraw(token, store);
1745
+ if (state === null) {
1746
+ throw new Error(
1747
+ `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1748
+ );
1749
+ }
1660
1750
  const subscription = state.subject.subscribe(handleUpdate);
1661
1751
  (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F440} subscribe to "${state.key}"`);
1662
1752
  const dependencySubscriptions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
@@ -1679,6 +1769,11 @@ var subscribe = (token, handleUpdate, store = IMPLICIT.STORE) => {
1679
1769
  var subscribeToTransaction = (token, handleUpdate, store = IMPLICIT.STORE) => {
1680
1770
  var _a;
1681
1771
  const tx = withdraw(token, store);
1772
+ if (tx === null) {
1773
+ throw new Error(
1774
+ `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
1775
+ );
1776
+ }
1682
1777
  (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F440} subscribe to transaction "${token.key}"`);
1683
1778
  const subscription = tx.subject.subscribe(handleUpdate);
1684
1779
  const unsubscribe = () => {
@@ -1704,11 +1799,24 @@ var undo = (token) => {
1704
1799
  function transaction(options) {
1705
1800
  return transaction__INTERNAL(options);
1706
1801
  }
1707
- var runTransaction = (token, store = IMPLICIT.STORE) => (...parameters) => withdraw(token, store).run(...parameters);
1802
+ var runTransaction = (token, store = IMPLICIT.STORE) => (...parameters) => {
1803
+ const tx = withdraw(token, store);
1804
+ if (tx) {
1805
+ return tx.run(...parameters);
1806
+ }
1807
+ throw new Error(
1808
+ `Cannot run transaction "${token.key}": transaction not found in store "${store.config.name}".`
1809
+ );
1810
+ };
1708
1811
 
1709
1812
  // src/index.ts
1710
1813
  var getState = (token, store = IMPLICIT.STORE) => {
1711
1814
  const state = withdraw(token, store);
1815
+ if (state === null) {
1816
+ throw new Error(
1817
+ `${capitalize(token.type)} "${token.key}" not found in store "${store.config.name}".`
1818
+ );
1819
+ }
1712
1820
  return getState__INTERNAL(state, store);
1713
1821
  };
1714
1822
  var setState = (token, value, store = IMPLICIT.STORE) => {
@@ -1721,6 +1829,11 @@ var setState = (token, value, store = IMPLICIT.STORE) => {
1721
1829
  return;
1722
1830
  }
1723
1831
  const state = withdraw(token, store);
1832
+ if (state === null) {
1833
+ throw new Error(
1834
+ `${capitalize(token.type)} "${token.key}" not found in store "${store.config.name}".`
1835
+ );
1836
+ }
1724
1837
  setState__INTERNAL(state, value, store);
1725
1838
  closeOperation(store);
1726
1839
  };
@@ -1738,6 +1851,7 @@ export {
1738
1851
  selectorFamily,
1739
1852
  setLogLevel,
1740
1853
  setState,
1854
+ silo,
1741
1855
  subscribe,
1742
1856
  subscribeToTransaction,
1743
1857
  timeline,