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.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import * as Rx from 'rxjs';
2
2
  import { Hamt } from 'hamt_plus';
3
- import { Refinement } from 'fp-ts/Refinement';
3
+ import { Refinement } from 'fp-ts/lib/Refinement';
4
+
5
+ type ƒn = (...parameters: any[]) => any;
6
+
7
+ type JsonInterface<T, J extends Json = Json> = {
8
+ toJson: (t: T) => J;
9
+ fromJson: (json: J) => T;
10
+ };
4
11
 
5
12
  type Primitive = boolean | number | string | null;
6
13
  type Serializable = Primitive | Readonly<{
@@ -23,6 +30,11 @@ type RelationData<CONTENT extends JsonObj | null = null, A extends string = `fro
23
30
  a: A;
24
31
  b: B;
25
32
  };
33
+ type IsRelationDataOptions<CONTENT extends JsonObj | null = null, A extends string = `from`, B extends string = `to`> = {
34
+ from?: A;
35
+ to?: B;
36
+ isContent?: (json: Json) => json is CONTENT;
37
+ };
26
38
 
27
39
  type NullSafeUnion<Base, Extension> = Extension extends null ? Base : Base & Extension;
28
40
  type NullSafeRest<MaybeArg> = MaybeArg extends null ? [] | [undefined] : [MaybeArg];
@@ -35,9 +47,11 @@ declare class Join<CONTENT extends JsonObj | null = null, A extends string = `fr
35
47
  readonly contents: Record<string, CONTENT>;
36
48
  constructor(json?: Partial<RelationData<CONTENT, A, B>>);
37
49
  toJSON(): RelationData<CONTENT, A, B>;
38
- static fromJSON<CONTENT extends JsonObj | null, A extends string, B extends string>(json: Json, isContent?: Refinement<unknown, CONTENT>, a?: A, b?: B): Join<CONTENT, A, B>;
50
+ static fromJSON<CONTENT extends JsonObj | null, A extends string, B extends string>(json: Json, options?: IsRelationDataOptions<CONTENT, A, B>): Join<CONTENT, A, B>;
39
51
  from<AA extends string>(newA: AA): Join<CONTENT, AA, B>;
40
52
  to<BB extends string>(newB: BB): Join<CONTENT, A, BB>;
53
+ makeJsonInterface: (...params: CONTENT extends null ? [
54
+ ] : [Refinement<unknown, CONTENT>]) => JsonInterface<Join<CONTENT, A, B>, RelationData<CONTENT, A, B>>;
41
55
  getRelatedId(id: string): string | undefined;
42
56
  getRelatedIds(id: string): string[];
43
57
  getContent(idA: string, idB: string): CONTENT | undefined;
@@ -131,18 +145,18 @@ type Atom<T> = {
131
145
  declare function atom__INTERNAL<T>(options: AtomOptions<T>, family?: FamilyMetadata, store?: Store): AtomToken<T>;
132
146
 
133
147
  declare function atomFamily__INTERNAL<T, K extends Serializable>(options: AtomFamilyOptions<T, K>, store?: Store): AtomFamily<T, K>;
134
- declare function readonlySelectorFamily__INTERNAL<T, K extends Serializable>(options: SelectorFamilyOptions<T, K>, store?: Store): ReadonlySelectorFamily<T, K>;
148
+ declare function readonlySelectorFamily__INTERNAL<T, K extends Serializable>(options: ReadonlySelectorFamilyOptions<T, K>, store?: Store): ReadonlySelectorFamily<T, K>;
135
149
  declare function selectorFamily__INTERNAL<T, K extends Serializable>(options: SelectorFamilyOptions<T, K>, store?: Store): SelectorFamily<T, K>;
136
150
  declare function selectorFamily__INTERNAL<T, K extends Serializable>(options: ReadonlySelectorFamilyOptions<T, K>, store?: Store): ReadonlySelectorFamily<T, K>;
137
151
 
138
152
  declare const computeSelectorState: <T>(selector: ReadonlySelector<T> | Selector<T>) => T;
139
153
  declare function lookup(key: string, store: Store): AtomToken<unknown> | ReadonlySelectorToken<unknown> | SelectorToken<unknown>;
140
- declare function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T>;
141
- declare function withdraw<T>(token: SelectorToken<T>, store: Store): Selector<T>;
142
- declare function withdraw<T>(token: StateToken<T>, store: Store): Atom<T> | Selector<T>;
143
- declare function withdraw<T>(token: ReadonlySelectorToken<T>, store: Store): ReadonlySelector<T>;
144
- declare function withdraw<T>(token: TransactionToken<T>, store: Store): Transaction<T extends ƒn ? T : never>;
145
- declare function withdraw<T>(token: ReadonlySelectorToken<T> | StateToken<T>, store: Store): Atom<T> | ReadonlySelector<T> | Selector<T>;
154
+ declare function withdraw<T>(token: AtomToken<T>, store: Store): Atom<T> | null;
155
+ declare function withdraw<T>(token: SelectorToken<T>, store: Store): Selector<T> | null;
156
+ declare function withdraw<T>(token: StateToken<T>, store: Store): Atom<T> | Selector<T> | null;
157
+ declare function withdraw<T>(token: ReadonlySelectorToken<T>, store: Store): ReadonlySelector<T> | null;
158
+ declare function withdraw<T>(token: TransactionToken<T>, store: Store): Transaction<T extends ƒn ? T : never> | null;
159
+ declare function withdraw<T>(token: ReadonlySelectorToken<T> | StateToken<T>, store: Store): Atom<T> | ReadonlySelector<T> | Selector<T> | null;
146
160
  declare function deposit<T>(state: Atom<T>): AtomToken<T>;
147
161
  declare function deposit<T>(state: Selector<T>): SelectorToken<T>;
148
162
  declare function deposit<T>(state: Atom<T> | Selector<T>): StateToken<T>;
@@ -210,7 +224,6 @@ declare const storeSelector: (selector: Selector<any>, store?: Store) => void;
210
224
  declare const storeReadonlySelector: (selector: ReadonlySelector<any>, store?: Store) => void;
211
225
  declare const hasKeyBeenUsed: (key: string, store?: Store) => boolean;
212
226
 
213
- type ƒn = (...parameters: any[]) => any;
214
227
  type Transactors = {
215
228
  get: <S>(state: ReadonlySelectorToken<S> | StateToken<S>) => S;
216
229
  set: <S>(state: StateToken<S>, newValue: S | ((oldValue: S) => S)) => void;
@@ -487,15 +500,21 @@ type SelectorOptions<T> = {
487
500
  get: Read<() => T>;
488
501
  set: Write<(newValue: T) => void>;
489
502
  };
490
- type ReadonlySelectorOptions<T> = Omit<SelectorOptions<T>, `set`>;
491
- declare function selector<T>(options: ReadonlySelectorOptions<T>): ReadonlySelectorToken<T>;
503
+ type ReadonlySelectorOptions<T> = {
504
+ key: string;
505
+ get: Read<() => T>;
506
+ };
492
507
  declare function selector<T>(options: SelectorOptions<T>): SelectorToken<T>;
508
+ declare function selector<T>(options: ReadonlySelectorOptions<T>): ReadonlySelectorToken<T>;
493
509
  type SelectorFamilyOptions<T, K extends Serializable> = {
494
510
  key: string;
495
511
  get: (key: K) => Read<() => T>;
496
512
  set: (key: K) => Write<(newValue: T) => void>;
497
513
  };
498
- type ReadonlySelectorFamilyOptions<T, K extends Serializable> = Omit<SelectorFamilyOptions<T, K>, `set`>;
514
+ type ReadonlySelectorFamilyOptions<T, K extends Serializable> = {
515
+ key: string;
516
+ get: (key: K) => Read<() => T>;
517
+ };
499
518
  type SelectorFamily<T, K extends Serializable = Serializable> = ((key: K) => SelectorToken<T>) & {
500
519
  key: string;
501
520
  type: `selector_family`;
@@ -509,6 +528,20 @@ type ReadonlySelectorFamily<T, K extends Serializable = Serializable> = ((key: K
509
528
  declare function selectorFamily<T, K extends Serializable>(options: SelectorFamilyOptions<T, K>): SelectorFamily<T, K>;
510
529
  declare function selectorFamily<T, K extends Serializable>(options: ReadonlySelectorFamilyOptions<T, K>): ReadonlySelectorFamily<T, K>;
511
530
 
531
+ type Silo = ReturnType<typeof silo>;
532
+ declare const silo: (name: string) => {
533
+ store: Store;
534
+ atom: typeof atom;
535
+ atomFamily: typeof atomFamily;
536
+ selector: typeof selector;
537
+ selectorFamily: typeof selectorFamily;
538
+ transaction: typeof transaction;
539
+ timeline: typeof timeline;
540
+ getState: typeof getState;
541
+ setState: typeof setState;
542
+ subscribe: typeof subscribe;
543
+ };
544
+
512
545
  type StateUpdate<T> = {
513
546
  newValue: T;
514
547
  oldValue: T;
@@ -534,17 +567,20 @@ type AtomToken<_> = {
534
567
  key: string;
535
568
  type: `atom`;
536
569
  family?: FamilyMetadata;
570
+ __brand?: _;
537
571
  };
538
572
  type SelectorToken<_> = {
539
573
  key: string;
540
574
  type: `selector`;
541
575
  family?: FamilyMetadata;
576
+ __brand?: _;
542
577
  };
543
578
  type StateToken<T> = AtomToken<T> | SelectorToken<T>;
544
579
  type ReadonlySelectorToken<_> = {
545
580
  key: string;
546
581
  type: `readonly_selector`;
547
582
  family?: FamilyMetadata;
583
+ __brand?: _;
548
584
  };
549
585
  type FamilyMetadata = {
550
586
  key: string;
@@ -553,9 +589,10 @@ type FamilyMetadata = {
553
589
  type TransactionToken<_> = {
554
590
  key: string;
555
591
  type: `transaction`;
592
+ __brand?: _;
556
593
  };
557
594
  declare const getState: <T>(token: ReadonlySelectorToken<T> | StateToken<T>, store?: Store) => T;
558
595
  declare const setState: <T, New extends T>(token: StateToken<T>, value: New | ((oldValue: T) => New), store?: Store) => void;
559
596
  declare const isDefault: (token: ReadonlySelectorToken<unknown> | StateToken<unknown>, store?: Store) => boolean;
560
597
 
561
- export { AtomEffect, AtomFamily, AtomFamilyOptions, AtomOptions, AtomToken, Effectors, FamilyMetadata, LOG_LEVELS, Logger, Read, ReadonlySelectorFamily, ReadonlySelectorFamilyOptions, ReadonlySelectorOptions, ReadonlySelectorToken, ReadonlyTransactors, SelectorFamily, SelectorFamilyOptions, SelectorOptions, SelectorToken, Serializable, StateToken, StateUpdate, TimelineOptions, TimelineToken, Transaction, TransactionIO, TransactionOptions, TransactionToken, TransactionUpdateHandler, Transactors, UpdateHandler, Write, index as __INTERNAL__, atom, atomFamily, getState, isDefault, redo, runTransaction, selector, selectorFamily, setLogLevel, setState, subscribe, subscribeToTransaction, timeline, transaction, undo, useLogger, ƒn };
598
+ export { AtomEffect, AtomFamily, AtomFamilyOptions, AtomOptions, AtomToken, Effectors, FamilyMetadata, LOG_LEVELS, Logger, Read, ReadonlySelectorFamily, ReadonlySelectorFamilyOptions, ReadonlySelectorOptions, ReadonlySelectorToken, ReadonlyTransactors, SelectorFamily, SelectorFamilyOptions, SelectorOptions, SelectorToken, Serializable, Silo, StateToken, StateUpdate, Store, TimelineOptions, TimelineToken, Transaction, TransactionIO, TransactionOptions, TransactionToken, TransactionUpdateHandler, Transactors, UpdateHandler, Write, index as __INTERNAL__, atom, atomFamily, getState, isDefault, redo, runTransaction, selector, selectorFamily, setLogLevel, setState, silo, subscribe, subscribeToTransaction, timeline, transaction, undo, useLogger };
package/dist/index.js CHANGED
@@ -70,6 +70,7 @@ __export(src_exports, {
70
70
  selectorFamily: () => selectorFamily,
71
71
  setLogLevel: () => setLogLevel,
72
72
  setState: () => setState,
73
+ silo: () => silo,
73
74
  subscribe: () => subscribe,
74
75
  subscribeToTransaction: () => subscribeToTransaction,
75
76
  timeline: () => timeline,
@@ -79,6 +80,9 @@ __export(src_exports, {
79
80
  });
80
81
  module.exports = __toCommonJS(src_exports);
81
82
 
83
+ // ../anvl/src/string/capitalize.ts
84
+ var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
85
+
82
86
  // src/internal/index.ts
83
87
  var internal_exports = {};
84
88
  __export(internal_exports, {
@@ -152,9 +156,9 @@ function lookup(key, store) {
152
156
  return { key, type };
153
157
  }
154
158
  function withdraw(token, store) {
155
- var _a, _b, _c;
159
+ var _a, _b, _c, _d;
156
160
  const core = target(store);
157
- return (_c = (_b = (_a = import_hamt_plus.default.get(token.key, core.atoms)) != null ? _a : import_hamt_plus.default.get(token.key, core.selectors)) != null ? _b : import_hamt_plus.default.get(token.key, core.readonlySelectors)) != null ? _c : import_hamt_plus.default.get(token.key, core.transactions);
161
+ return (_d = (_c = (_b = (_a = import_hamt_plus.default.get(token.key, core.atoms)) != null ? _a : import_hamt_plus.default.get(token.key, core.selectors)) != null ? _b : import_hamt_plus.default.get(token.key, core.readonlySelectors)) != null ? _c : import_hamt_plus.default.get(token.key, core.transactions)) != null ? _d : null;
158
162
  }
159
163
  function deposit(state) {
160
164
  return __spreadValues({
@@ -246,7 +250,7 @@ var recordToEntries = (obj) => Object.entries(obj);
246
250
  var entriesToRecord = (entries) => Object.fromEntries(entries);
247
251
 
248
252
  // ../anvl/src/object/mapObject.ts
249
- var import_function = require("fp-ts/lib/function");
253
+ var import_function = require("fp-ts/function");
250
254
  var mapObject = (obj, fn) => (0, import_function.pipe)(
251
255
  obj,
252
256
  recordToEntries,
@@ -350,13 +354,19 @@ var EMPTY_RELATION_DATA = {
350
354
  a: `from`,
351
355
  b: `to`
352
356
  };
353
- var isRelationData = (isContent, a = `from`, b = `to`) => (input) => hasExactProperties({
354
- contents: isContent ? isRecord(import_string.isString, isContent) : hasExactProperties({}),
355
- relations: isRecord(import_string.isString, isArray(import_string.isString)),
356
- relationType: isRelationType,
357
- a: isLiteral(a),
358
- b: isLiteral(b)
359
- })(input);
357
+ var isRelationData = ({
358
+ from: a = `from`,
359
+ to: b = `to`,
360
+ isContent
361
+ } = {}) => (input) => {
362
+ return hasExactProperties({
363
+ contents: isContent ? isRecord(import_string.isString, isContent) : hasExactProperties({}),
364
+ relations: isRecord(import_string.isString, isArray(import_string.isString)),
365
+ relationType: isRelationType,
366
+ a: isLiteral(a),
367
+ b: isLiteral(b)
368
+ })(input);
369
+ };
360
370
 
361
371
  // ../anvl/src/join/get-related-ids.ts
362
372
  var getRelatedIds = (relationMap, id) => {
@@ -373,6 +383,21 @@ var getRelatedId = (relationMap, id) => {
373
383
  return relations[0];
374
384
  };
375
385
 
386
+ // ../anvl/src/join/make-json-interface.ts
387
+ var makeJsonInterface = (join, ...params) => {
388
+ const isContent = params[0];
389
+ const { a, b } = join;
390
+ const options = {
391
+ from: a,
392
+ to: b,
393
+ isContent
394
+ };
395
+ return {
396
+ toJson: (join2) => join2.toJSON(),
397
+ fromJson: (json) => Join.fromJSON(json, options)
398
+ };
399
+ };
400
+
376
401
  // ../anvl/src/join/relation-contents.ts
377
402
  var import_function6 = require("fp-ts/function");
378
403
 
@@ -563,11 +588,16 @@ var setRelations = (current, subject, relations) => {
563
588
  };
564
589
 
565
590
  // ../anvl/src/join/index.ts
566
- var Join = class {
591
+ var Join = class _Join {
567
592
  constructor(json) {
568
593
  this.a = `from`;
569
594
  this.b = `to`;
570
- Object.assign(this, __spreadValues(__spreadValues({}, EMPTY_RELATION_DATA), json));
595
+ this.makeJsonInterface = (...params) => {
596
+ return makeJsonInterface(this, ...params);
597
+ };
598
+ Object.assign(this, __spreadProps(__spreadValues(__spreadValues({}, EMPTY_RELATION_DATA), json), {
599
+ makeJsonInterface: this.makeJsonInterface
600
+ }));
571
601
  }
572
602
  toJSON() {
573
603
  return {
@@ -578,20 +608,20 @@ var Join = class {
578
608
  b: this.b
579
609
  };
580
610
  }
581
- static fromJSON(json, isContent = cannotExist, a = `from`, b = `to`) {
582
- const isValid = isRelationData(isContent, a, b)(json);
611
+ static fromJSON(json, options) {
612
+ const isValid = isRelationData(options)(json);
583
613
  if (isValid) {
584
- return new Join(json);
614
+ return new _Join(json);
585
615
  }
586
616
  throw new Error(
587
617
  `Saved JSON for this Join is invalid: ${JSON.stringify(json)}`
588
618
  );
589
619
  }
590
620
  from(newA) {
591
- return new Join(__spreadProps(__spreadValues({}, this), { a: newA }));
621
+ return new _Join(__spreadProps(__spreadValues({}, this), { a: newA }));
592
622
  }
593
623
  to(newB) {
594
- return new Join(__spreadProps(__spreadValues({}, this), { b: newB }));
624
+ return new _Join(__spreadProps(__spreadValues({}, this), { b: newB }));
595
625
  }
596
626
  getRelatedId(id) {
597
627
  return getRelatedId(this, id);
@@ -615,13 +645,13 @@ var Join = class {
615
645
  return getRelations(this, id);
616
646
  }
617
647
  setRelations(subject, relations) {
618
- return new Join(setRelations(this, subject, relations));
648
+ return new _Join(setRelations(this, subject, relations));
619
649
  }
620
650
  set(relation, ...rest) {
621
- return new Join(setRelationWithContent(this, relation, ...rest));
651
+ return new _Join(setRelationWithContent(this, relation, ...rest));
622
652
  }
623
653
  remove(relation) {
624
- return new Join(
654
+ return new _Join(
625
655
  removeRelation(this, relation)
626
656
  );
627
657
  }
@@ -807,18 +837,22 @@ var applyTransaction = (output, store) => {
807
837
  for (const { key, newValue } of atomUpdates) {
808
838
  const token = { key, type: `atom` };
809
839
  if (!import_hamt_plus4.default.has(token.key, store.valueMap)) {
810
- const atom2 = import_hamt_plus4.default.get(token.key, store.transactionStatus.core.atoms);
811
- store.atoms = import_hamt_plus4.default.set(atom2.key, atom2, store.atoms);
812
- store.valueMap = import_hamt_plus4.default.set(atom2.key, atom2.default, store.valueMap);
813
- (_c = store.config.logger) == null ? void 0 : _c.info(`\u{1F527}`, `add atom "${atom2.key}"`);
840
+ const newAtom = import_hamt_plus4.default.get(token.key, store.transactionStatus.core.atoms);
841
+ store.atoms = import_hamt_plus4.default.set(newAtom.key, newAtom, store.atoms);
842
+ store.valueMap = import_hamt_plus4.default.set(newAtom.key, newAtom.default, store.valueMap);
843
+ (_c = store.config.logger) == null ? void 0 : _c.info(`\u{1F527}`, `add atom "${newAtom.key}"`);
814
844
  }
815
- const state = withdraw(token, store);
816
- setState(state, newValue, store);
845
+ setState(token, newValue, store);
817
846
  }
818
847
  const myTransaction = withdraw(
819
848
  { key: store.transactionStatus.key, type: `transaction` },
820
849
  store
821
850
  );
851
+ if (myTransaction === null) {
852
+ throw new Error(
853
+ `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`
854
+ );
855
+ }
822
856
  myTransaction.subject.next({
823
857
  key: store.transactionStatus.key,
824
858
  atomUpdates,
@@ -834,6 +868,11 @@ var undoTransactionUpdate = (update, store) => {
834
868
  for (const { key, oldValue } of update.atomUpdates) {
835
869
  const token = { key, type: `atom` };
836
870
  const state = withdraw(token, store);
871
+ if (state === null) {
872
+ throw new Error(
873
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
874
+ );
875
+ }
837
876
  setState(state, oldValue, store);
838
877
  }
839
878
  };
@@ -843,6 +882,11 @@ var redoTransactionUpdate = (update, store) => {
843
882
  for (const { key, newValue } of update.atomUpdates) {
844
883
  const token = { key, type: `atom` };
845
884
  const state = withdraw(token, store);
885
+ if (state === null) {
886
+ throw new Error(
887
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
888
+ );
889
+ }
846
890
  setState(state, newValue, store);
847
891
  }
848
892
  };
@@ -1199,6 +1243,11 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1199
1243
  const core = target(store);
1200
1244
  const alreadyRegistered = core.selectorGraph.getRelations(selectorKey).some(({ source }) => source === dependency.key);
1201
1245
  const dependencyState = withdraw(dependency, store);
1246
+ if (dependencyState === null) {
1247
+ throw new Error(
1248
+ `State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1249
+ );
1250
+ }
1202
1251
  const dependencyValue = getState__INTERNAL(dependencyState, store);
1203
1252
  if (alreadyRegistered) {
1204
1253
  (_a = store.config.logger) == null ? void 0 : _a.info(
@@ -1223,6 +1272,11 @@ var registerSelector = (selectorKey, store = IMPLICIT.STORE) => ({
1223
1272
  },
1224
1273
  set: (stateToken, newValue) => {
1225
1274
  const state = withdraw(stateToken, store);
1275
+ if (state === null) {
1276
+ throw new Error(
1277
+ `State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1278
+ );
1279
+ }
1226
1280
  setState__INTERNAL(state, newValue, store);
1227
1281
  }
1228
1282
  });
@@ -1330,7 +1384,7 @@ var setAtomState = (atom2, next, store = IMPLICIT.STORE) => {
1330
1384
  const newValue = become(next)(oldValue);
1331
1385
  (_a = store.config.logger) == null ? void 0 : _a.info(`<< setting atom "${atom2.key}" to`, newValue);
1332
1386
  cacheValue(atom2.key, newValue, store);
1333
- if (isAtomDefault(atom2.key)) {
1387
+ if (isAtomDefault(atom2.key, store)) {
1334
1388
  markAtomAsNotDefault(atom2.key, store);
1335
1389
  }
1336
1390
  markDone(atom2.key, store);
@@ -1395,6 +1449,11 @@ var emitUpdate = (state, update, store) => {
1395
1449
  var subscribeToRootAtoms = (state, store) => {
1396
1450
  const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomToken) => {
1397
1451
  const atom2 = withdraw(atomToken, store);
1452
+ if (atom2 === null) {
1453
+ throw new Error(
1454
+ `Atom "${atomToken.key}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
1455
+ );
1456
+ }
1398
1457
  return atom2.subject.subscribe((atomChange) => {
1399
1458
  var _a, _b;
1400
1459
  (_a = store.config.logger) == null ? void 0 : _a.info(
@@ -1506,6 +1565,11 @@ function timeline__INTERNAL(options, store = IMPLICIT.STORE) {
1506
1565
  };
1507
1566
  const subscribeToAtom = (token2) => {
1508
1567
  const state = withdraw(token2, store);
1568
+ if (state === null) {
1569
+ throw new Error(
1570
+ `Cannot subscribe to atom "${token2.key}" because it has not been initialized in store "${store.config.name}"`
1571
+ );
1572
+ }
1509
1573
  state.subject.subscribe((update) => {
1510
1574
  var _a2, _b2, _c, _d, _e;
1511
1575
  const storeCurrentSelectorKey = store.operation.open && store.operation.token.type === `selector` ? store.operation.token.key : null;
@@ -1523,6 +1587,11 @@ function timeline__INTERNAL(options, store = IMPLICIT.STORE) {
1523
1587
  { key: storeCurrentTransactionKey, type: `transaction` },
1524
1588
  store
1525
1589
  );
1590
+ if (currentTransaction === null) {
1591
+ throw new Error(
1592
+ `Transaction "${storeCurrentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${storeCurrentTransactionKey}".`
1593
+ );
1594
+ }
1526
1595
  if (timelineData.transactionKey !== storeCurrentTransactionKey) {
1527
1596
  if (timelineData.transactionKey) {
1528
1597
  (_b2 = store.config.logger) == null ? void 0 : _b2.error(
@@ -1698,10 +1767,32 @@ function selectorFamily(options) {
1698
1767
  return selectorFamily__INTERNAL(options);
1699
1768
  }
1700
1769
 
1770
+ // src/silo.ts
1771
+ var silo = (name) => {
1772
+ const store = createStore(name);
1773
+ return {
1774
+ store,
1775
+ atom: (options) => atom__INTERNAL(options, void 0, store),
1776
+ atomFamily: (options) => atomFamily__INTERNAL(options, store),
1777
+ selector: (options) => selector__INTERNAL(options, void 0, store),
1778
+ selectorFamily: (options) => selectorFamily__INTERNAL(options, store),
1779
+ transaction: (options) => transaction__INTERNAL(options, store),
1780
+ timeline: (options) => timeline__INTERNAL(options, store),
1781
+ getState: (token) => getState(token, store),
1782
+ setState: (token, newValue) => setState(token, newValue, store),
1783
+ subscribe: (token, handler) => subscribe(token, handler, store)
1784
+ };
1785
+ };
1786
+
1701
1787
  // src/subscribe.ts
1702
1788
  var subscribe = (token, handleUpdate, store = IMPLICIT.STORE) => {
1703
1789
  var _a;
1704
1790
  const state = withdraw(token, store);
1791
+ if (state === null) {
1792
+ throw new Error(
1793
+ `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
1794
+ );
1795
+ }
1705
1796
  const subscription = state.subject.subscribe(handleUpdate);
1706
1797
  (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F440} subscribe to "${state.key}"`);
1707
1798
  const dependencySubscriptions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
@@ -1724,6 +1815,11 @@ var subscribe = (token, handleUpdate, store = IMPLICIT.STORE) => {
1724
1815
  var subscribeToTransaction = (token, handleUpdate, store = IMPLICIT.STORE) => {
1725
1816
  var _a;
1726
1817
  const tx = withdraw(token, store);
1818
+ if (tx === null) {
1819
+ throw new Error(
1820
+ `Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
1821
+ );
1822
+ }
1727
1823
  (_a = store.config.logger) == null ? void 0 : _a.info(`\u{1F440} subscribe to transaction "${token.key}"`);
1728
1824
  const subscription = tx.subject.subscribe(handleUpdate);
1729
1825
  const unsubscribe = () => {
@@ -1749,11 +1845,24 @@ var undo = (token) => {
1749
1845
  function transaction(options) {
1750
1846
  return transaction__INTERNAL(options);
1751
1847
  }
1752
- var runTransaction = (token, store = IMPLICIT.STORE) => (...parameters) => withdraw(token, store).run(...parameters);
1848
+ var runTransaction = (token, store = IMPLICIT.STORE) => (...parameters) => {
1849
+ const tx = withdraw(token, store);
1850
+ if (tx) {
1851
+ return tx.run(...parameters);
1852
+ }
1853
+ throw new Error(
1854
+ `Cannot run transaction "${token.key}": transaction not found in store "${store.config.name}".`
1855
+ );
1856
+ };
1753
1857
 
1754
1858
  // src/index.ts
1755
1859
  var getState = (token, store = IMPLICIT.STORE) => {
1756
1860
  const state = withdraw(token, store);
1861
+ if (state === null) {
1862
+ throw new Error(
1863
+ `${capitalize(token.type)} "${token.key}" not found in store "${store.config.name}".`
1864
+ );
1865
+ }
1757
1866
  return getState__INTERNAL(state, store);
1758
1867
  };
1759
1868
  var setState = (token, value, store = IMPLICIT.STORE) => {
@@ -1766,6 +1875,11 @@ var setState = (token, value, store = IMPLICIT.STORE) => {
1766
1875
  return;
1767
1876
  }
1768
1877
  const state = withdraw(token, store);
1878
+ if (state === null) {
1879
+ throw new Error(
1880
+ `${capitalize(token.type)} "${token.key}" not found in store "${store.config.name}".`
1881
+ );
1882
+ }
1769
1883
  setState__INTERNAL(state, value, store);
1770
1884
  closeOperation(store);
1771
1885
  };
@@ -1784,6 +1898,7 @@ var isDefault = (token, store = IMPLICIT.STORE) => token.type === `atom` ? isAto
1784
1898
  selectorFamily,
1785
1899
  setLogLevel,
1786
1900
  setState,
1901
+ silo,
1787
1902
  subscribe,
1788
1903
  subscribeToTransaction,
1789
1904
  timeline,