atom.io 0.7.0 → 0.8.1

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 (46) hide show
  1. package/dist/index.d.mts +8 -5
  2. package/dist/index.d.ts +8 -5
  3. package/dist/index.js +67 -72
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +34 -38
  6. package/dist/index.mjs.map +1 -1
  7. package/internal/dist/index.d.mts +42 -26
  8. package/internal/dist/index.d.ts +42 -26
  9. package/internal/dist/index.js +85 -76
  10. package/internal/dist/index.js.map +1 -1
  11. package/internal/dist/index.mjs +74 -47
  12. package/internal/dist/index.mjs.map +1 -1
  13. package/internal/src/atom/create-atom.ts +1 -1
  14. package/internal/src/caching.ts +25 -1
  15. package/internal/src/future.ts +37 -0
  16. package/internal/src/index.ts +1 -0
  17. package/internal/src/mutable/create-mutable-atom-family.ts +4 -4
  18. package/internal/src/mutable/create-mutable-atom.ts +6 -5
  19. package/internal/src/mutable/is-atom-token-mutable.ts +3 -3
  20. package/internal/src/mutable/tracker-family.ts +4 -4
  21. package/internal/src/mutable/tracker.ts +20 -19
  22. package/internal/src/operation.ts +5 -2
  23. package/internal/src/selector/create-read-write-selector.ts +4 -4
  24. package/internal/src/selector/create-readonly-selector.ts +1 -1
  25. package/internal/src/selector/register-selector.ts +2 -2
  26. package/internal/src/set-state/{set-atom-state.ts → set-atom.ts} +2 -2
  27. package/internal/src/set-state/set-selector-state.ts +1 -12
  28. package/internal/src/set-state/set-state-internal.ts +4 -5
  29. package/internal/src/store/withdraw-new-family-member.ts +7 -7
  30. package/internal/src/store/withdraw.ts +15 -9
  31. package/internal/src/subscribe/subscribe-to-root-atoms.ts +1 -1
  32. package/internal/src/timeline/add-atom-to-timeline.ts +2 -2
  33. package/internal/src/transaction/apply-transaction.ts +1 -1
  34. package/internal/src/transaction/redo-transaction.ts +1 -1
  35. package/internal/src/transaction/undo-transaction.ts +1 -1
  36. package/package.json +9 -9
  37. package/react-devtools/dist/index.d.mts +4 -4
  38. package/react-devtools/dist/index.d.ts +4 -4
  39. package/react-devtools/dist/index.js +19 -5
  40. package/react-devtools/dist/index.js.map +1 -1
  41. package/react-devtools/dist/index.mjs +15 -1
  42. package/react-devtools/dist/index.mjs.map +1 -1
  43. package/react-devtools/src/index.ts +1 -1
  44. package/src/get-set.ts +48 -0
  45. package/src/index.ts +4 -67
  46. package/src/subscribe.ts +3 -3
@@ -1,5 +1,4 @@
1
- import * as AtomIO from 'atom.io';
2
- import { setState, getState, subscribe } from 'atom.io';
1
+ import { setState, getState, subscribe, subscribeToTimeline } from 'atom.io';
3
2
  import { selectJson, stringifyJson, parseJson, selectJsonFamily } from 'atom.io/json';
4
3
 
5
4
  var __defProp = Object.defineProperty;
@@ -22,6 +21,25 @@ var __spreadValues = (a, b) => {
22
21
  };
23
22
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
23
 
24
+ // src/future.ts
25
+ var Future = class extends Promise {
26
+ constructor(executor) {
27
+ super((resolve, reject) => {
28
+ const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
29
+ const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
30
+ if (typeof executor === `function`) {
31
+ executor(pass, fail);
32
+ } else {
33
+ executor.then(pass, fail);
34
+ }
35
+ });
36
+ this.isCanceled = false;
37
+ }
38
+ cancel() {
39
+ this.isCanceled = true;
40
+ }
41
+ };
42
+
25
43
  // src/store/deposit.ts
26
44
  function deposit(state) {
27
45
  const token = {
@@ -107,7 +125,7 @@ var applyTransaction = (output, store) => {
107
125
  { key: store.transactionStatus.key, type: `transaction` },
108
126
  store
109
127
  );
110
- if (myTransaction === null) {
128
+ if (myTransaction === void 0) {
111
129
  throw new Error(
112
130
  `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`
113
131
  );
@@ -395,7 +413,7 @@ var redoTransactionUpdate = (update, store) => {
395
413
  for (const { key, newValue } of update.atomUpdates) {
396
414
  const token = { key, type: `atom` };
397
415
  const state = withdraw(token, store);
398
- if (state === null) {
416
+ if (state === void 0) {
399
417
  throw new Error(
400
418
  `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
401
419
  );
@@ -409,7 +427,7 @@ var undoTransactionUpdate = (update, store) => {
409
427
  for (const { key, oldValue } of update.atomUpdates) {
410
428
  const token = { key, type: `atom` };
411
429
  const state = withdraw(token, store);
412
- if (state === null) {
430
+ if (state === void 0) {
413
431
  throw new Error(
414
432
  `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
415
433
  );
@@ -525,7 +543,7 @@ var clearStore = (store = IMPLICIT.STORE) => {
525
543
  // src/timeline/add-atom-to-timeline.ts
526
544
  var addAtomToTimeline = (atomToken, tl, store = IMPLICIT.STORE) => {
527
545
  const atom = withdraw(atomToken, store);
528
- if (atom === null) {
546
+ if (atom === void 0) {
529
547
  throw new Error(
530
548
  `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
531
549
  );
@@ -557,7 +575,7 @@ var addAtomToTimeline = (atomToken, tl, store = IMPLICIT.STORE) => {
557
575
  { key: currentTransactionKey, type: `transaction` },
558
576
  store
559
577
  );
560
- if (currentTransaction === null) {
578
+ if (currentTransaction === void 0) {
561
579
  throw new Error(
562
580
  `Transaction "${currentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${currentTransactionKey}".`
563
581
  );
@@ -856,7 +874,7 @@ function withdraw(token, store) {
856
874
  return state;
857
875
  }
858
876
  }
859
- return null;
877
+ return void 0;
860
878
  }
861
879
 
862
880
  // src/store/withdraw-new-family-member.ts
@@ -875,12 +893,31 @@ function withdrawNewFamilyMember(token, store) {
875
893
  return state;
876
894
  }
877
895
  }
878
- return null;
896
+ return void 0;
879
897
  }
880
898
 
881
899
  // src/caching.ts
882
- var cacheValue = (key, value, store = IMPLICIT.STORE) => {
883
- target(store).valueMap.set(key, value);
900
+ var cacheValue = (key, value, subject, store = IMPLICIT.STORE) => {
901
+ const currentValue = target(store).valueMap.get(key);
902
+ if (currentValue instanceof Future) {
903
+ currentValue.cancel();
904
+ }
905
+ if (value instanceof Promise) {
906
+ const future = new Future(value);
907
+ target(store).valueMap.set(key, future);
908
+ future.then((value2) => {
909
+ cacheValue(key, value2, subject, store);
910
+ subject.next({ newValue: value2, oldValue: value2 });
911
+ }).catch((error) => {
912
+ var _a;
913
+ (_a = store.config.logger) == null ? void 0 : _a.error(
914
+ `Promised value for "${key}" rejected:`,
915
+ error
916
+ );
917
+ });
918
+ } else {
919
+ target(store).valueMap.set(key, value);
920
+ }
884
921
  };
885
922
  var readCachedValue = (key, store = IMPLICIT.STORE) => target(store).valueMap.get(key);
886
923
  var isValueCached = (key, store = IMPLICIT.STORE) => target(store).valueMap.has(key);
@@ -912,7 +949,7 @@ var Tracker = class {
912
949
  return latestUpdateState;
913
950
  }
914
951
  observeCore(mutableState, latestUpdateState, store = IMPLICIT.STORE) {
915
- const originalInnerValue = AtomIO.getState(mutableState, store);
952
+ const originalInnerValue = getState(mutableState, store);
916
953
  this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
917
954
  `tracker:${store.config.name}:${store.transactionStatus.phase === `idle` ? `main` : store.transactionStatus.key}`,
918
955
  (update) => {
@@ -920,12 +957,12 @@ var Tracker = class {
920
957
  mutableState.key,
921
958
  () => {
922
959
  unsubscribe();
923
- AtomIO.setState(latestUpdateState, update, store);
960
+ setState(latestUpdateState, update, store);
924
961
  }
925
962
  );
926
963
  }
927
964
  );
928
- AtomIO.subscribe(
965
+ subscribe(
929
966
  mutableState,
930
967
  (update) => {
931
968
  var _a;
@@ -938,7 +975,7 @@ var Tracker = class {
938
975
  mutableState.key,
939
976
  () => {
940
977
  unsubscribe();
941
- AtomIO.setState(latestUpdateState, update2, store);
978
+ setState(latestUpdateState, update2, store);
942
979
  }
943
980
  );
944
981
  }
@@ -950,7 +987,7 @@ var Tracker = class {
950
987
  );
951
988
  }
952
989
  updateCore(mutableState, latestUpdateState, store = IMPLICIT.STORE) {
953
- AtomIO.subscribe(
990
+ subscribe(
954
991
  latestUpdateState,
955
992
  ({ newValue, oldValue }) => {
956
993
  const timelineId = store.timelineAtoms.getRelatedKey(
@@ -959,11 +996,11 @@ var Tracker = class {
959
996
  if (timelineId) {
960
997
  const timelineData = store.timelines.get(timelineId);
961
998
  if (timelineData == null ? void 0 : timelineData.timeTraveling) {
962
- const unsubscribe2 = AtomIO.subscribeToTimeline(
999
+ const unsubscribe2 = subscribeToTimeline(
963
1000
  { key: timelineId, type: `timeline` },
964
1001
  (update) => {
965
1002
  unsubscribe2();
966
- AtomIO.setState(
1003
+ setState(
967
1004
  mutableState,
968
1005
  (transceiver) => {
969
1006
  if (update === `redo` && newValue) {
@@ -985,7 +1022,7 @@ var Tracker = class {
985
1022
  () => {
986
1023
  unsubscribe();
987
1024
  if (newValue) {
988
- AtomIO.setState(
1025
+ setState(
989
1026
  mutableState,
990
1027
  (transceiver) => (transceiver.do(newValue), transceiver),
991
1028
  store
@@ -1009,7 +1046,7 @@ function createMutableAtom(options, store = IMPLICIT.STORE) {
1009
1046
  const coreState = createAtom(options, void 0, store);
1010
1047
  new Tracker(coreState, store);
1011
1048
  const jsonState = selectJson(coreState, options, store);
1012
- AtomIO.subscribe(
1049
+ subscribe(
1013
1050
  jsonState,
1014
1051
  () => {
1015
1052
  var _a2;
@@ -1097,7 +1134,7 @@ var openOperation = (token, store) => {
1097
1134
  (_a = store.config.logger) == null ? void 0 : _a.error(
1098
1135
  `\u274C failed to setState to "${token.key}" during a setState for "${core.operation.token.key}"`
1099
1136
  );
1100
- throw Symbol(`violation`);
1137
+ return `rejection`;
1101
1138
  }
1102
1139
  core.operation = {
1103
1140
  open: true,
@@ -1275,14 +1312,14 @@ var stowUpdate = (state, update, store) => {
1275
1312
  logger == null ? void 0 : logger.info(`\u{1F4DD} ${key} stowed (`, update.oldValue, `->`, update.newValue, `)`);
1276
1313
  };
1277
1314
 
1278
- // src/set-state/set-atom-state.ts
1279
- var setAtomState = (atom, next, store = IMPLICIT.STORE) => {
1315
+ // src/set-state/set-atom.ts
1316
+ var setAtom = (atom, next, store = IMPLICIT.STORE) => {
1280
1317
  var _a, _b;
1281
1318
  const oldValue = getState__INTERNAL(atom, store);
1282
1319
  let newValue = copyMutableIfWithinTransaction(atom, store);
1283
1320
  newValue = become(next)(newValue);
1284
1321
  (_a = store.config.logger) == null ? void 0 : _a.info(`<< setting atom "${atom.key}" to`, newValue);
1285
- cacheValue(atom.key, newValue, store);
1322
+ cacheValue(atom.key, newValue, atom.subject, store);
1286
1323
  if (isAtomDefault(atom.key, store)) {
1287
1324
  markAtomAsNotDefault(atom.key, store);
1288
1325
  }
@@ -1299,22 +1336,12 @@ var setAtomState = (atom, next, store = IMPLICIT.STORE) => {
1299
1336
  }
1300
1337
  };
1301
1338
 
1302
- // src/set-state/set-selector-state.ts
1303
- var setSelectorState = (selector, next, store = IMPLICIT.STORE) => {
1304
- var _a, _b;
1305
- const oldValue = getState__INTERNAL(selector, store);
1306
- const newValue = become(next)(oldValue);
1307
- (_a = store.config.logger) == null ? void 0 : _a.info(`<< setting selector "${selector.key}" to`, newValue);
1308
- (_b = store.config.logger) == null ? void 0 : _b.info(` || propagating change made to "${selector.key}"`);
1309
- selector.set(newValue);
1310
- };
1311
-
1312
1339
  // src/set-state/set-state-internal.ts
1313
1340
  var setState__INTERNAL = (state, value, store = IMPLICIT.STORE) => {
1314
- if (`set` in state) {
1315
- setSelectorState(state, value, store);
1341
+ if (state.type === `selector`) {
1342
+ state.set(value);
1316
1343
  } else {
1317
- setAtomState(state, value, store);
1344
+ setAtom(state, value, store);
1318
1345
  }
1319
1346
  };
1320
1347
 
@@ -1380,7 +1407,7 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1380
1407
  const core = target(store);
1381
1408
  const alreadyRegistered = core.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
1382
1409
  const dependencyState = withdraw(dependency, store);
1383
- if (dependencyState === null) {
1410
+ if (dependencyState === void 0) {
1384
1411
  throw new Error(
1385
1412
  `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1386
1413
  );
@@ -1412,7 +1439,7 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1412
1439
  },
1413
1440
  set: (stateToken, newValue) => {
1414
1441
  const state = withdraw(stateToken, store);
1415
- if (state === null) {
1442
+ if (state === void 0) {
1416
1443
  throw new Error(
1417
1444
  `State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1418
1445
  );
@@ -1428,21 +1455,21 @@ var createReadWriteSelector = (options, family, store, core) => {
1428
1455
  const { get, set } = registerSelector(options.key, store);
1429
1456
  const getSelf = () => {
1430
1457
  const value = options.get({ get });
1431
- cacheValue(options.key, value, store);
1458
+ cacheValue(options.key, value, subject, store);
1432
1459
  return value;
1433
1460
  };
1434
1461
  const setSelf = (next) => {
1435
1462
  var _a2;
1436
1463
  const oldValue = getSelf();
1464
+ const newValue = become(next)(oldValue);
1437
1465
  (_a2 = store.config.logger) == null ? void 0 : _a2.info(
1438
1466
  ` <- "${options.key}" went (`,
1439
1467
  oldValue,
1440
1468
  `->`,
1441
- next,
1469
+ newValue,
1442
1470
  `)`
1443
1471
  );
1444
- const newValue = become(next)(oldValue);
1445
- cacheValue(options.key, newValue, store);
1472
+ cacheValue(options.key, newValue, subject, store);
1446
1473
  markDone(options.key, store);
1447
1474
  if (store.transactionStatus.phase === `idle`) {
1448
1475
  subject.next({ newValue, oldValue });
@@ -1477,7 +1504,7 @@ var createReadonlySelector = (options, family, store, core) => {
1477
1504
  const { get } = registerSelector(options.key, store);
1478
1505
  const getSelf = () => {
1479
1506
  const value = options.get({ get });
1480
- cacheValue(options.key, value, store);
1507
+ cacheValue(options.key, value, subject, store);
1481
1508
  return value;
1482
1509
  };
1483
1510
  const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
@@ -1723,7 +1750,7 @@ function createAtom(options, family, store = IMPLICIT.STORE) {
1723
1750
  const initialValue = options.default instanceof Function ? options.default() : options.default;
1724
1751
  core.atoms.set(newAtom.key, newAtom);
1725
1752
  markAtomAsDefault(options.key, store);
1726
- cacheValue(options.key, initialValue, store);
1753
+ cacheValue(options.key, initialValue, subject, store);
1727
1754
  const token = deposit(newAtom);
1728
1755
  for (const effect of (_e = options.effects) != null ? _e : []) {
1729
1756
  effect({
@@ -1761,7 +1788,7 @@ var recallState = (state, store = IMPLICIT.STORE) => {
1761
1788
  var subscribeToRootAtoms = (state, store) => {
1762
1789
  const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomToken) => {
1763
1790
  const atom = withdraw(atomToken, store);
1764
- if (atom === null) {
1791
+ if (atom === void 0) {
1765
1792
  throw new Error(
1766
1793
  `Atom "${atomToken.key}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
1767
1794
  );
@@ -1793,6 +1820,6 @@ var subscribeToRootAtoms = (state, store) => {
1793
1820
  return dependencySubscriptions;
1794
1821
  };
1795
1822
 
1796
- export { FamilyTracker, IMPLICIT, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, addAtomToTimeline, applyTransaction, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtom, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelectorFamily, createSelector, createSelectorFamily, deleteAtom, deposit, getJsonToken, getState__INTERNAL, getUpdateToken, isAtomDefault, isAtomMutable, isAtomTokenMutable, isDone, isSelectorDefault, isTransceiver, isValueCached, lookup, lookupSelectorSources, markAtomAsDefault, markAtomAsNotDefault, markDone, openOperation, readCachedValue, redoTransactionUpdate, redo__INTERNAL, registerSelector, setState__INTERNAL, subscribeToRootAtoms, target, timeline__INTERNAL, traceAllSelectorAtoms, traceSelectorAtoms, transaction__INTERNAL, undoTransactionUpdate, undo__INTERNAL, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
1823
+ export { FamilyTracker, Future, IMPLICIT, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, addAtomToTimeline, applyTransaction, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtom, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelectorFamily, createSelector, createSelectorFamily, deleteAtom, deposit, getJsonToken, getState__INTERNAL, getUpdateToken, isAtomDefault, isAtomMutable, isAtomTokenMutable, isDone, isSelectorDefault, isTransceiver, isValueCached, lookup, lookupSelectorSources, markAtomAsDefault, markAtomAsNotDefault, markDone, openOperation, readCachedValue, redoTransactionUpdate, redo__INTERNAL, registerSelector, setState__INTERNAL, subscribeToRootAtoms, target, timeline__INTERNAL, traceAllSelectorAtoms, traceSelectorAtoms, transaction__INTERNAL, undoTransactionUpdate, undo__INTERNAL, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
1797
1824
  //# sourceMappingURL=out.js.map
1798
1825
  //# sourceMappingURL=index.mjs.map