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,28 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var AtomIO = require('atom.io');
3
+ var atom_io = require('atom.io');
4
4
  var json = require('atom.io/json');
5
5
 
6
- function _interopNamespace(e) {
7
- if (e && e.__esModule) return e;
8
- var n = Object.create(null);
9
- if (e) {
10
- Object.keys(e).forEach(function (k) {
11
- if (k !== 'default') {
12
- var d = Object.getOwnPropertyDescriptor(e, k);
13
- Object.defineProperty(n, k, d.get ? d : {
14
- enumerable: true,
15
- get: function () { return e[k]; }
16
- });
17
- }
18
- });
19
- }
20
- n.default = e;
21
- return Object.freeze(n);
22
- }
23
-
24
- var AtomIO__namespace = /*#__PURE__*/_interopNamespace(AtomIO);
25
-
26
6
  var __defProp = Object.defineProperty;
27
7
  var __defProps = Object.defineProperties;
28
8
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -43,6 +23,25 @@ var __spreadValues = (a, b) => {
43
23
  };
44
24
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
45
25
 
26
+ // src/future.ts
27
+ var Future = class extends Promise {
28
+ constructor(executor) {
29
+ super((resolve, reject) => {
30
+ const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
31
+ const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
32
+ if (typeof executor === `function`) {
33
+ executor(pass, fail);
34
+ } else {
35
+ executor.then(pass, fail);
36
+ }
37
+ });
38
+ this.isCanceled = false;
39
+ }
40
+ cancel() {
41
+ this.isCanceled = true;
42
+ }
43
+ };
44
+
46
45
  // src/store/deposit.ts
47
46
  function deposit(state) {
48
47
  const token = {
@@ -122,13 +121,13 @@ var applyTransaction = (output, store) => {
122
121
  (_d = store.config.logger) == null ? void 0 : _d.info(`\u{1F527}`, `add atom "${newAtom.key}"`);
123
122
  }
124
123
  }
125
- AtomIO.setState(token, newValue, store);
124
+ atom_io.setState(token, newValue, store);
126
125
  }
127
126
  const myTransaction = withdraw(
128
127
  { key: store.transactionStatus.key, type: `transaction` },
129
128
  store
130
129
  );
131
- if (myTransaction === null) {
130
+ if (myTransaction === void 0) {
132
131
  throw new Error(
133
132
  `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`
134
133
  );
@@ -384,8 +383,8 @@ function transaction__INTERNAL(options, store = IMPLICIT.STORE) {
384
383
  try {
385
384
  const output = options.do(
386
385
  {
387
- get: (token2) => AtomIO.getState(token2, store),
388
- set: (token2, value) => AtomIO.setState(token2, value, store)
386
+ get: (token2) => atom_io.getState(token2, store),
387
+ set: (token2, value) => atom_io.setState(token2, value, store)
389
388
  },
390
389
  ...params
391
390
  );
@@ -416,12 +415,12 @@ var redoTransactionUpdate = (update, store) => {
416
415
  for (const { key, newValue } of update.atomUpdates) {
417
416
  const token = { key, type: `atom` };
418
417
  const state = withdraw(token, store);
419
- if (state === null) {
418
+ if (state === void 0) {
420
419
  throw new Error(
421
420
  `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
422
421
  );
423
422
  }
424
- AtomIO.setState(state, newValue, store);
423
+ atom_io.setState(state, newValue, store);
425
424
  }
426
425
  };
427
426
  var undoTransactionUpdate = (update, store) => {
@@ -430,12 +429,12 @@ var undoTransactionUpdate = (update, store) => {
430
429
  for (const { key, oldValue } of update.atomUpdates) {
431
430
  const token = { key, type: `atom` };
432
431
  const state = withdraw(token, store);
433
- if (state === null) {
432
+ if (state === void 0) {
434
433
  throw new Error(
435
434
  `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
436
435
  );
437
436
  }
438
- AtomIO.setState(state, oldValue, store);
437
+ atom_io.setState(state, oldValue, store);
439
438
  }
440
439
  };
441
440
 
@@ -546,7 +545,7 @@ var clearStore = (store = IMPLICIT.STORE) => {
546
545
  // src/timeline/add-atom-to-timeline.ts
547
546
  var addAtomToTimeline = (atomToken, tl, store = IMPLICIT.STORE) => {
548
547
  const atom = withdraw(atomToken, store);
549
- if (atom === null) {
548
+ if (atom === void 0) {
550
549
  throw new Error(
551
550
  `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
552
551
  );
@@ -578,7 +577,7 @@ var addAtomToTimeline = (atomToken, tl, store = IMPLICIT.STORE) => {
578
577
  { key: currentTransactionKey, type: `transaction` },
579
578
  store
580
579
  );
581
- if (currentTransaction === null) {
580
+ if (currentTransaction === void 0) {
582
581
  throw new Error(
583
582
  `Transaction "${currentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${currentTransactionKey}".`
584
583
  );
@@ -712,14 +711,14 @@ var redo__INTERNAL = (token, store = IMPLICIT.STORE) => {
712
711
  switch (update.type) {
713
712
  case `atom_update`: {
714
713
  const { key, newValue } = update;
715
- AtomIO.setState({ key, type: `atom` }, newValue, store);
714
+ atom_io.setState({ key, type: `atom` }, newValue, store);
716
715
  break;
717
716
  }
718
717
  case `selector_update`:
719
718
  case `transaction_update`: {
720
719
  for (const atomUpdate of update.atomUpdates) {
721
720
  const { key, newValue } = atomUpdate;
722
- AtomIO.setState({ key, type: `atom` }, newValue, store);
721
+ atom_io.setState({ key, type: `atom` }, newValue, store);
723
722
  }
724
723
  break;
725
724
  }
@@ -753,14 +752,14 @@ var undo__INTERNAL = (token, store = IMPLICIT.STORE) => {
753
752
  switch (update.type) {
754
753
  case `atom_update`: {
755
754
  const { key, oldValue } = update;
756
- AtomIO.setState({ key, type: `atom` }, oldValue, store);
755
+ atom_io.setState({ key, type: `atom` }, oldValue, store);
757
756
  break;
758
757
  }
759
758
  case `selector_update`:
760
759
  case `transaction_update`: {
761
760
  for (const atomUpdate of [...update.atomUpdates].reverse()) {
762
761
  const { key, oldValue } = atomUpdate;
763
- AtomIO.setState({ key, type: `atom` }, oldValue, store);
762
+ atom_io.setState({ key, type: `atom` }, oldValue, store);
764
763
  }
765
764
  break;
766
765
  }
@@ -877,7 +876,7 @@ function withdraw(token, store) {
877
876
  return state;
878
877
  }
879
878
  }
880
- return null;
879
+ return void 0;
881
880
  }
882
881
 
883
882
  // src/store/withdraw-new-family-member.ts
@@ -896,12 +895,31 @@ function withdrawNewFamilyMember(token, store) {
896
895
  return state;
897
896
  }
898
897
  }
899
- return null;
898
+ return void 0;
900
899
  }
901
900
 
902
901
  // src/caching.ts
903
- var cacheValue = (key, value, store = IMPLICIT.STORE) => {
904
- target(store).valueMap.set(key, value);
902
+ var cacheValue = (key, value, subject, store = IMPLICIT.STORE) => {
903
+ const currentValue = target(store).valueMap.get(key);
904
+ if (currentValue instanceof Future) {
905
+ currentValue.cancel();
906
+ }
907
+ if (value instanceof Promise) {
908
+ const future = new Future(value);
909
+ target(store).valueMap.set(key, future);
910
+ future.then((value2) => {
911
+ cacheValue(key, value2, subject, store);
912
+ subject.next({ newValue: value2, oldValue: value2 });
913
+ }).catch((error) => {
914
+ var _a;
915
+ (_a = store.config.logger) == null ? void 0 : _a.error(
916
+ `Promised value for "${key}" rejected:`,
917
+ error
918
+ );
919
+ });
920
+ } else {
921
+ target(store).valueMap.set(key, value);
922
+ }
905
923
  };
906
924
  var readCachedValue = (key, store = IMPLICIT.STORE) => target(store).valueMap.get(key);
907
925
  var isValueCached = (key, store = IMPLICIT.STORE) => target(store).valueMap.has(key);
@@ -933,7 +951,7 @@ var Tracker = class {
933
951
  return latestUpdateState;
934
952
  }
935
953
  observeCore(mutableState, latestUpdateState, store = IMPLICIT.STORE) {
936
- const originalInnerValue = AtomIO__namespace.getState(mutableState, store);
954
+ const originalInnerValue = atom_io.getState(mutableState, store);
937
955
  this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
938
956
  `tracker:${store.config.name}:${store.transactionStatus.phase === `idle` ? `main` : store.transactionStatus.key}`,
939
957
  (update) => {
@@ -941,12 +959,12 @@ var Tracker = class {
941
959
  mutableState.key,
942
960
  () => {
943
961
  unsubscribe();
944
- AtomIO__namespace.setState(latestUpdateState, update, store);
962
+ atom_io.setState(latestUpdateState, update, store);
945
963
  }
946
964
  );
947
965
  }
948
966
  );
949
- AtomIO__namespace.subscribe(
967
+ atom_io.subscribe(
950
968
  mutableState,
951
969
  (update) => {
952
970
  var _a;
@@ -959,7 +977,7 @@ var Tracker = class {
959
977
  mutableState.key,
960
978
  () => {
961
979
  unsubscribe();
962
- AtomIO__namespace.setState(latestUpdateState, update2, store);
980
+ atom_io.setState(latestUpdateState, update2, store);
963
981
  }
964
982
  );
965
983
  }
@@ -971,7 +989,7 @@ var Tracker = class {
971
989
  );
972
990
  }
973
991
  updateCore(mutableState, latestUpdateState, store = IMPLICIT.STORE) {
974
- AtomIO__namespace.subscribe(
992
+ atom_io.subscribe(
975
993
  latestUpdateState,
976
994
  ({ newValue, oldValue }) => {
977
995
  const timelineId = store.timelineAtoms.getRelatedKey(
@@ -980,11 +998,11 @@ var Tracker = class {
980
998
  if (timelineId) {
981
999
  const timelineData = store.timelines.get(timelineId);
982
1000
  if (timelineData == null ? void 0 : timelineData.timeTraveling) {
983
- const unsubscribe2 = AtomIO__namespace.subscribeToTimeline(
1001
+ const unsubscribe2 = atom_io.subscribeToTimeline(
984
1002
  { key: timelineId, type: `timeline` },
985
1003
  (update) => {
986
1004
  unsubscribe2();
987
- AtomIO__namespace.setState(
1005
+ atom_io.setState(
988
1006
  mutableState,
989
1007
  (transceiver) => {
990
1008
  if (update === `redo` && newValue) {
@@ -1006,7 +1024,7 @@ var Tracker = class {
1006
1024
  () => {
1007
1025
  unsubscribe();
1008
1026
  if (newValue) {
1009
- AtomIO__namespace.setState(
1027
+ atom_io.setState(
1010
1028
  mutableState,
1011
1029
  (transceiver) => (transceiver.do(newValue), transceiver),
1012
1030
  store
@@ -1030,7 +1048,7 @@ function createMutableAtom(options, store = IMPLICIT.STORE) {
1030
1048
  const coreState = createAtom(options, void 0, store);
1031
1049
  new Tracker(coreState, store);
1032
1050
  const jsonState = json.selectJson(coreState, options, store);
1033
- AtomIO__namespace.subscribe(
1051
+ atom_io.subscribe(
1034
1052
  jsonState,
1035
1053
  () => {
1036
1054
  var _a2;
@@ -1118,7 +1136,7 @@ var openOperation = (token, store) => {
1118
1136
  (_a = store.config.logger) == null ? void 0 : _a.error(
1119
1137
  `\u274C failed to setState to "${token.key}" during a setState for "${core.operation.token.key}"`
1120
1138
  );
1121
- throw Symbol(`violation`);
1139
+ return `rejection`;
1122
1140
  }
1123
1141
  core.operation = {
1124
1142
  open: true,
@@ -1296,14 +1314,14 @@ var stowUpdate = (state, update, store) => {
1296
1314
  logger == null ? void 0 : logger.info(`\u{1F4DD} ${key} stowed (`, update.oldValue, `->`, update.newValue, `)`);
1297
1315
  };
1298
1316
 
1299
- // src/set-state/set-atom-state.ts
1300
- var setAtomState = (atom, next, store = IMPLICIT.STORE) => {
1317
+ // src/set-state/set-atom.ts
1318
+ var setAtom = (atom, next, store = IMPLICIT.STORE) => {
1301
1319
  var _a, _b;
1302
1320
  const oldValue = getState__INTERNAL(atom, store);
1303
1321
  let newValue = copyMutableIfWithinTransaction(atom, store);
1304
1322
  newValue = become(next)(newValue);
1305
1323
  (_a = store.config.logger) == null ? void 0 : _a.info(`<< setting atom "${atom.key}" to`, newValue);
1306
- cacheValue(atom.key, newValue, store);
1324
+ cacheValue(atom.key, newValue, atom.subject, store);
1307
1325
  if (isAtomDefault(atom.key, store)) {
1308
1326
  markAtomAsNotDefault(atom.key, store);
1309
1327
  }
@@ -1320,22 +1338,12 @@ var setAtomState = (atom, next, store = IMPLICIT.STORE) => {
1320
1338
  }
1321
1339
  };
1322
1340
 
1323
- // src/set-state/set-selector-state.ts
1324
- var setSelectorState = (selector, next, store = IMPLICIT.STORE) => {
1325
- var _a, _b;
1326
- const oldValue = getState__INTERNAL(selector, store);
1327
- const newValue = become(next)(oldValue);
1328
- (_a = store.config.logger) == null ? void 0 : _a.info(`<< setting selector "${selector.key}" to`, newValue);
1329
- (_b = store.config.logger) == null ? void 0 : _b.info(` || propagating change made to "${selector.key}"`);
1330
- selector.set(newValue);
1331
- };
1332
-
1333
1341
  // src/set-state/set-state-internal.ts
1334
1342
  var setState__INTERNAL = (state, value, store = IMPLICIT.STORE) => {
1335
- if (`set` in state) {
1336
- setSelectorState(state, value, store);
1343
+ if (state.type === `selector`) {
1344
+ state.set(value);
1337
1345
  } else {
1338
- setAtomState(state, value, store);
1346
+ setAtom(state, value, store);
1339
1347
  }
1340
1348
  };
1341
1349
 
@@ -1401,7 +1409,7 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1401
1409
  const core = target(store);
1402
1410
  const alreadyRegistered = core.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
1403
1411
  const dependencyState = withdraw(dependency, store);
1404
- if (dependencyState === null) {
1412
+ if (dependencyState === void 0) {
1405
1413
  throw new Error(
1406
1414
  `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1407
1415
  );
@@ -1433,7 +1441,7 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1433
1441
  },
1434
1442
  set: (stateToken, newValue) => {
1435
1443
  const state = withdraw(stateToken, store);
1436
- if (state === null) {
1444
+ if (state === void 0) {
1437
1445
  throw new Error(
1438
1446
  `State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1439
1447
  );
@@ -1449,21 +1457,21 @@ var createReadWriteSelector = (options, family, store, core) => {
1449
1457
  const { get, set } = registerSelector(options.key, store);
1450
1458
  const getSelf = () => {
1451
1459
  const value = options.get({ get });
1452
- cacheValue(options.key, value, store);
1460
+ cacheValue(options.key, value, subject, store);
1453
1461
  return value;
1454
1462
  };
1455
1463
  const setSelf = (next) => {
1456
1464
  var _a2;
1457
1465
  const oldValue = getSelf();
1466
+ const newValue = become(next)(oldValue);
1458
1467
  (_a2 = store.config.logger) == null ? void 0 : _a2.info(
1459
1468
  ` <- "${options.key}" went (`,
1460
1469
  oldValue,
1461
1470
  `->`,
1462
- next,
1471
+ newValue,
1463
1472
  `)`
1464
1473
  );
1465
- const newValue = become(next)(oldValue);
1466
- cacheValue(options.key, newValue, store);
1474
+ cacheValue(options.key, newValue, subject, store);
1467
1475
  markDone(options.key, store);
1468
1476
  if (store.transactionStatus.phase === `idle`) {
1469
1477
  subject.next({ newValue, oldValue });
@@ -1498,7 +1506,7 @@ var createReadonlySelector = (options, family, store, core) => {
1498
1506
  const { get } = registerSelector(options.key, store);
1499
1507
  const getSelf = () => {
1500
1508
  const value = options.get({ get });
1501
- cacheValue(options.key, value, store);
1509
+ cacheValue(options.key, value, subject, store);
1502
1510
  return value;
1503
1511
  };
1504
1512
  const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
@@ -1744,12 +1752,12 @@ function createAtom(options, family, store = IMPLICIT.STORE) {
1744
1752
  const initialValue = options.default instanceof Function ? options.default() : options.default;
1745
1753
  core.atoms.set(newAtom.key, newAtom);
1746
1754
  markAtomAsDefault(options.key, store);
1747
- cacheValue(options.key, initialValue, store);
1755
+ cacheValue(options.key, initialValue, subject, store);
1748
1756
  const token = deposit(newAtom);
1749
1757
  for (const effect of (_e = options.effects) != null ? _e : []) {
1750
1758
  effect({
1751
- setSelf: (next) => AtomIO.setState(token, next, store),
1752
- onSet: (handle) => AtomIO.subscribe(token, handle, `effect[${subject.subscribers.size}]`, store)
1759
+ setSelf: (next) => atom_io.setState(token, next, store),
1760
+ onSet: (handle) => atom_io.subscribe(token, handle, `effect[${subject.subscribers.size}]`, store)
1753
1761
  });
1754
1762
  }
1755
1763
  store.subject.atomCreation.next(token);
@@ -1782,7 +1790,7 @@ var recallState = (state, store = IMPLICIT.STORE) => {
1782
1790
  var subscribeToRootAtoms = (state, store) => {
1783
1791
  const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomToken) => {
1784
1792
  const atom = withdraw(atomToken, store);
1785
- if (atom === null) {
1793
+ if (atom === void 0) {
1786
1794
  throw new Error(
1787
1795
  `Atom "${atomToken.key}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
1788
1796
  );
@@ -1815,6 +1823,7 @@ var subscribeToRootAtoms = (state, store) => {
1815
1823
  };
1816
1824
 
1817
1825
  exports.FamilyTracker = FamilyTracker;
1826
+ exports.Future = Future;
1818
1827
  exports.IMPLICIT = IMPLICIT;
1819
1828
  exports.Store = Store;
1820
1829
  exports.Subject = Subject;