atom.io 0.15.5 → 0.16.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 (134) hide show
  1. package/data/dist/index.cjs +20 -22
  2. package/data/dist/index.cjs.map +1 -1
  3. package/data/dist/index.d.ts +6 -6
  4. package/data/dist/index.js +21 -23
  5. package/data/dist/index.js.map +1 -1
  6. package/data/src/dict.ts +6 -7
  7. package/data/src/join.ts +23 -23
  8. package/data/src/struct-family.ts +2 -2
  9. package/data/src/struct.ts +4 -5
  10. package/dist/index.cjs +12 -5
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.ts +67 -60
  13. package/dist/index.js +13 -6
  14. package/dist/index.js.map +1 -1
  15. package/internal/dist/index.cjs +912 -886
  16. package/internal/dist/index.cjs.map +1 -1
  17. package/internal/dist/index.d.ts +157 -162
  18. package/internal/dist/index.js +487 -460
  19. package/internal/dist/index.js.map +1 -1
  20. package/internal/src/atom/create-regular-atom.ts +9 -12
  21. package/internal/src/atom/create-standalone-atom.ts +33 -0
  22. package/internal/src/atom/delete-atom.ts +5 -2
  23. package/internal/src/atom/index.ts +1 -16
  24. package/internal/src/atom/is-default.ts +0 -1
  25. package/internal/src/caching.ts +6 -3
  26. package/internal/src/families/create-atom-family.ts +11 -7
  27. package/internal/src/families/create-readonly-selector-family.ts +4 -3
  28. package/internal/src/families/create-regular-atom-family.ts +12 -27
  29. package/internal/src/families/create-selector-family.ts +13 -49
  30. package/internal/src/families/create-writable-selector-family.ts +51 -0
  31. package/internal/src/index.ts +44 -3
  32. package/internal/src/lineage.ts +0 -7
  33. package/internal/src/mutable/create-mutable-atom-family.ts +61 -15
  34. package/internal/src/mutable/create-mutable-atom.ts +70 -25
  35. package/internal/src/mutable/get-json-family.ts +4 -5
  36. package/internal/src/mutable/get-json-token.ts +6 -3
  37. package/internal/src/mutable/get-update-token.ts +3 -3
  38. package/internal/src/mutable/index.ts +1 -7
  39. package/internal/src/mutable/is-mutable.ts +6 -7
  40. package/internal/src/mutable/tracker-family.ts +4 -4
  41. package/internal/src/mutable/tracker.ts +6 -6
  42. package/internal/src/read-or-compute-value.ts +6 -3
  43. package/internal/src/selector/create-readonly-selector.ts +2 -3
  44. package/internal/src/selector/create-standalone-selector.ts +32 -0
  45. package/internal/src/selector/{create-read-write-selector.ts → create-writable-selector.ts} +12 -9
  46. package/internal/src/selector/delete-selector.ts +2 -2
  47. package/internal/src/selector/index.ts +3 -1
  48. package/internal/src/selector/trace-selector-atoms.ts +3 -2
  49. package/internal/src/selector/update-selector-atoms.ts +1 -1
  50. package/internal/src/set-state/copy-mutable-if-needed.ts +5 -6
  51. package/internal/src/set-state/copy-mutable-in-transaction.ts +4 -36
  52. package/internal/src/set-state/emit-update.ts +2 -3
  53. package/internal/src/set-state/evict-downstream.ts +1 -1
  54. package/internal/src/set-state/set-atom-or-selector.ts +3 -3
  55. package/internal/src/set-state/set-atom.ts +8 -4
  56. package/internal/src/set-state/stow-update.ts +1 -1
  57. package/internal/src/store/deposit.ts +25 -13
  58. package/internal/src/store/store.ts +21 -21
  59. package/internal/src/store/withdraw-new-family-member.ts +16 -9
  60. package/internal/src/store/withdraw.ts +43 -19
  61. package/internal/src/subscribe/recall-state.ts +2 -6
  62. package/internal/src/subscribe/subscribe-to-root-atoms.ts +39 -41
  63. package/internal/src/subscribe/subscribe-to-state.ts +3 -1
  64. package/internal/src/timeline/add-atom-to-timeline.ts +5 -5
  65. package/internal/src/timeline/create-timeline.ts +19 -22
  66. package/introspection/dist/index.cjs +4 -8
  67. package/introspection/dist/index.cjs.map +1 -1
  68. package/introspection/dist/index.d.ts +5 -5
  69. package/introspection/dist/index.js +5 -9
  70. package/introspection/dist/index.js.map +1 -1
  71. package/introspection/src/attach-atom-index.ts +4 -5
  72. package/introspection/src/attach-selector-index.ts +4 -5
  73. package/introspection/src/attach-timeline-index.ts +6 -3
  74. package/introspection/src/attach-transaction-index.ts +6 -3
  75. package/introspection/src/index.ts +9 -5
  76. package/json/dist/index.cjs +3 -4
  77. package/json/dist/index.cjs.map +1 -1
  78. package/json/dist/index.d.ts +4 -3
  79. package/json/dist/index.js +4 -5
  80. package/json/dist/index.js.map +1 -1
  81. package/json/src/select-json-family.ts +24 -4
  82. package/json/src/select-json.ts +3 -4
  83. package/package.json +2 -2
  84. package/react-devtools/dist/index.cjs.map +1 -1
  85. package/react-devtools/dist/index.d.ts +9 -9
  86. package/react-devtools/dist/index.js.map +1 -1
  87. package/react-devtools/src/StateIndex.tsx +18 -10
  88. package/react-devtools/src/TimelineIndex.tsx +6 -2
  89. package/react-devtools/src/TransactionIndex.tsx +2 -2
  90. package/realtime-client/dist/index.cjs +27 -13
  91. package/realtime-client/dist/index.cjs.map +1 -1
  92. package/realtime-client/dist/index.d.ts +6 -4
  93. package/realtime-client/dist/index.js +20 -7
  94. package/realtime-client/dist/index.js.map +1 -1
  95. package/realtime-client/src/index.ts +4 -3
  96. package/realtime-client/src/server-action.ts +2 -55
  97. package/realtime-client/src/sync-server-action.ts +75 -0
  98. package/realtime-react/dist/index.cjs +22 -13
  99. package/realtime-react/dist/index.cjs.map +1 -1
  100. package/realtime-react/dist/index.d.ts +3 -1
  101. package/realtime-react/dist/index.js +20 -12
  102. package/realtime-react/dist/index.js.map +1 -1
  103. package/realtime-react/src/index.ts +1 -0
  104. package/realtime-react/src/use-server-action.ts +2 -4
  105. package/realtime-react/src/use-sync-server-action.ts +19 -0
  106. package/realtime-server/dist/index.cjs +83 -81
  107. package/realtime-server/dist/index.cjs.map +1 -1
  108. package/realtime-server/dist/index.d.ts +8 -8
  109. package/realtime-server/dist/index.js +58 -56
  110. package/realtime-server/dist/index.js.map +1 -1
  111. package/realtime-server/src/index.ts +15 -1
  112. package/realtime-server/src/{hook-composition/receive-transaction.ts → realtime-action-receiver.ts} +6 -2
  113. package/realtime-server/src/{hook-composition/sync-transaction.ts → realtime-action-synchronizer.ts} +10 -43
  114. package/realtime-server/src/{hook-composition/expose-family.ts → realtime-family-provider.ts} +9 -23
  115. package/realtime-server/src/{hook-composition/expose-mutable-family.ts → realtime-mutable-family-provider.ts} +4 -4
  116. package/realtime-server/src/{hook-composition/expose-mutable.ts → realtime-mutable-provider.ts} +4 -5
  117. package/realtime-server/src/realtime-server-store.ts +39 -0
  118. package/realtime-server/src/{hook-composition/expose-single.ts → realtime-state-provider.ts} +7 -8
  119. package/realtime-server/src/{hook-composition/receive-state.ts → realtime-state-receiver.ts} +7 -4
  120. package/src/atom.ts +39 -24
  121. package/src/find-state.ts +20 -19
  122. package/src/index.ts +41 -28
  123. package/src/logger.ts +1 -0
  124. package/src/selector.ts +31 -16
  125. package/src/silo.ts +45 -6
  126. package/src/subscribe.ts +1 -0
  127. package/src/validators.ts +35 -25
  128. package/internal/src/atom/create-atom.ts +0 -21
  129. package/internal/src/mutable/get-update-family.ts +0 -23
  130. package/internal/src/selector/create-selector.ts +0 -65
  131. package/realtime-server/src/hook-composition/index.ts +0 -15
  132. /package/realtime-client/src/{pull.ts → pull-state.ts} +0 -0
  133. /package/realtime-client/src/{push.ts → push-state.ts} +0 -0
  134. /package/realtime-client/src/{realtime-state.ts → realtime-client-store.ts} +0 -0
@@ -1,7 +1,7 @@
1
1
  import { Junction } from '../../dist/chunk-NYTGCPHB.js';
2
2
  import { __spreadValues, __spreadProps } from '../../dist/chunk-PZLG2HP3.js';
3
- import { selectJson, stringifyJson, parseJson, selectJsonFamily } from 'atom.io/json';
4
3
  import { AtomIOLogger, findInStore, getState, setState, runTransaction } from 'atom.io';
4
+ import { selectJson, stringifyJson, parseJson, selectJsonFamily } from 'atom.io/json';
5
5
  import { withdraw as withdraw$1 } from 'atom.io/internal';
6
6
 
7
7
  // internal/src/lineage.ts
@@ -11,11 +11,214 @@ function newest(scion) {
11
11
  }
12
12
  return scion;
13
13
  }
14
- function eldest(scion) {
15
- while (scion.parent !== null) {
16
- scion = scion.parent;
14
+
15
+ // internal/src/store/deposit.ts
16
+ function deposit(state) {
17
+ const token = {
18
+ key: state.key,
19
+ type: state.type
20
+ };
21
+ if (`family` in state) {
22
+ token.family = state.family;
17
23
  }
18
- return scion;
24
+ return token;
25
+ }
26
+
27
+ // internal/src/subject.ts
28
+ var Subject = class {
29
+ constructor() {
30
+ this.subscribers = /* @__PURE__ */ new Map();
31
+ }
32
+ subscribe(key, subscriber) {
33
+ this.subscribers.set(key, subscriber);
34
+ const unsubscribe = () => this.unsubscribe(key);
35
+ return unsubscribe;
36
+ }
37
+ unsubscribe(key) {
38
+ this.subscribers.delete(key);
39
+ }
40
+ next(value) {
41
+ const subscribers = this.subscribers.values();
42
+ for (const subscriber of subscribers) {
43
+ subscriber(value);
44
+ }
45
+ }
46
+ };
47
+ var StatefulSubject = class extends Subject {
48
+ constructor(initialState) {
49
+ super();
50
+ this.state = initialState;
51
+ }
52
+ next(value) {
53
+ this.state = value;
54
+ super.next(value);
55
+ }
56
+ };
57
+
58
+ // internal/src/store/store.ts
59
+ var Store = class {
60
+ constructor(name, store = null) {
61
+ this.parent = null;
62
+ this.child = null;
63
+ this.valueMap = /* @__PURE__ */ new Map();
64
+ this.atoms = /* @__PURE__ */ new Map();
65
+ this.selectors = /* @__PURE__ */ new Map();
66
+ this.readonlySelectors = /* @__PURE__ */ new Map();
67
+ this.trackers = /* @__PURE__ */ new Map();
68
+ this.families = /* @__PURE__ */ new Map();
69
+ this.timelines = /* @__PURE__ */ new Map();
70
+ this.transactions = /* @__PURE__ */ new Map();
71
+ this.atomsThatAreDefault = /* @__PURE__ */ new Set();
72
+ this.timelineAtoms = new Junction({
73
+ between: [`timelineKey`, `atomKey`],
74
+ cardinality: `1:n`
75
+ });
76
+ this.selectorAtoms = new Junction({
77
+ between: [`selectorKey`, `atomKey`],
78
+ cardinality: `n:n`
79
+ });
80
+ this.selectorGraph = new Junction(
81
+ {
82
+ between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
83
+ cardinality: `n:n`
84
+ },
85
+ {
86
+ makeContentKey: (...keys) => keys.sort().join(`:`)
87
+ }
88
+ );
89
+ this.on = {
90
+ atomCreation: new Subject(),
91
+ selectorCreation: new Subject(),
92
+ transactionCreation: new Subject(),
93
+ timelineCreation: new Subject(),
94
+ transactionApplying: new StatefulSubject(null),
95
+ operationClose: new Subject()
96
+ };
97
+ this.operation = { open: false };
98
+ this.transactionMeta = null;
99
+ this.config = {
100
+ name: `IMPLICIT_STORE`
101
+ };
102
+ this.loggers = [
103
+ new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
104
+ ];
105
+ this.logger = {
106
+ error: (...messages) => {
107
+ for (const logger of this.loggers)
108
+ logger.error(...messages);
109
+ },
110
+ info: (...messages) => {
111
+ for (const logger of this.loggers)
112
+ logger.info(...messages);
113
+ },
114
+ warn: (...messages) => {
115
+ for (const logger of this.loggers)
116
+ logger.warn(...messages);
117
+ }
118
+ };
119
+ if (store !== null) {
120
+ this.valueMap = new Map(store == null ? void 0 : store.valueMap);
121
+ this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
122
+ this.transactionMeta = null;
123
+ this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
124
+ name
125
+ });
126
+ for (const [, family] of store.families) {
127
+ family.install(this);
128
+ }
129
+ const mutableHelpers = /* @__PURE__ */ new Set();
130
+ for (const [, atom] of store.atoms) {
131
+ if (mutableHelpers.has(atom.key)) {
132
+ continue;
133
+ }
134
+ atom.install(this);
135
+ if (atom.type === `mutable_atom`) {
136
+ const originalJsonToken = getJsonToken(atom);
137
+ const originalUpdateToken = getUpdateToken(atom);
138
+ mutableHelpers.add(originalJsonToken.key);
139
+ mutableHelpers.add(originalUpdateToken.key);
140
+ }
141
+ }
142
+ for (const [, selector] of store.readonlySelectors) {
143
+ selector.install(this);
144
+ }
145
+ for (const [, selector] of store.selectors) {
146
+ if (mutableHelpers.has(selector.key)) {
147
+ continue;
148
+ }
149
+ selector.install(this);
150
+ }
151
+ for (const [, tx] of store.transactions) {
152
+ tx.install(this);
153
+ }
154
+ for (const [, timeline] of store.timelines) {
155
+ timeline.install(this);
156
+ }
157
+ }
158
+ }
159
+ };
160
+ var IMPLICIT = {
161
+ STORE_INTERNAL: void 0,
162
+ get STORE() {
163
+ var _a;
164
+ return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
165
+ }
166
+ };
167
+ var clearStore = (store) => {
168
+ const { config } = store;
169
+ Object.assign(store, new Store(config.name));
170
+ store.config = config;
171
+ };
172
+
173
+ // internal/src/store/withdraw.ts
174
+ function withdraw(token, store) {
175
+ let withdrawn;
176
+ let target = store;
177
+ while (target !== null) {
178
+ switch (token.type) {
179
+ case `atom`:
180
+ case `mutable_atom`:
181
+ withdrawn = target.atoms.get(token.key);
182
+ break;
183
+ case `selector`:
184
+ withdrawn = target.selectors.get(token.key);
185
+ break;
186
+ case `readonly_selector`:
187
+ withdrawn = target.readonlySelectors.get(token.key);
188
+ break;
189
+ case `timeline`:
190
+ withdrawn = target.timelines.get(token.key);
191
+ break;
192
+ case `transaction`:
193
+ withdrawn = target.transactions.get(token.key);
194
+ break;
195
+ }
196
+ if (withdrawn) {
197
+ return withdrawn;
198
+ }
199
+ target = target.child;
200
+ }
201
+ }
202
+
203
+ // internal/src/store/withdraw-new-family-member.ts
204
+ function withdrawNewFamilyMember(token, store) {
205
+ if (token.family) {
206
+ store.logger.info(
207
+ `\u{1F46A}`,
208
+ token.type,
209
+ token.key,
210
+ `creating new family member in store "${store.config.name}"`
211
+ );
212
+ const target = newest(store);
213
+ const family = target.families.get(token.family.key);
214
+ if (family) {
215
+ const jsonSubKey = JSON.parse(token.family.subKey);
216
+ family(jsonSubKey);
217
+ const state = withdraw(token, store);
218
+ return state;
219
+ }
220
+ }
221
+ return void 0;
19
222
  }
20
223
 
21
224
  // internal/src/future.ts
@@ -38,13 +241,13 @@ var Future = class extends Promise {
38
241
  };
39
242
 
40
243
  // internal/src/set-state/copy-mutable-if-needed.ts
41
- function copyMutableIfNeeded(atom, transform, origin, target) {
244
+ function copyMutableIfNeeded(atom, origin, target) {
42
245
  const originValue = origin.valueMap.get(atom.key);
43
246
  const targetValue = target.valueMap.get(atom.key);
44
247
  if (originValue === targetValue) {
45
248
  origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
46
- const jsonValue = transform.toJson(originValue);
47
- const copiedValue = transform.fromJson(jsonValue);
249
+ const jsonValue = atom.toJson(originValue);
250
+ const copiedValue = atom.fromJson(jsonValue);
48
251
  target.valueMap.set(atom.key, copiedValue);
49
252
  new Tracker(atom, origin);
50
253
  return copiedValue;
@@ -57,34 +260,13 @@ function copyMutableIfWithinTransaction(oldValue, atom, store) {
57
260
  const target = newest(store);
58
261
  const parent = target.parent;
59
262
  if (parent !== null) {
60
- if (`family` in atom) {
61
- const family = parent.families.get(atom.family.key);
62
- if (family && family.type === `atom_family`) {
63
- const result = copyMutableFamilyMemberWithinTransaction(
64
- atom,
65
- family,
66
- parent,
67
- target
68
- );
69
- if (result) {
70
- return result;
71
- }
72
- }
73
- }
74
- if (`toJson` in atom && `fromJson` in atom) {
75
- const copiedValue = copyMutableIfNeeded(atom, atom, parent, target);
263
+ if (atom.type === `mutable_atom`) {
264
+ const copiedValue = copyMutableIfNeeded(atom, parent, target);
76
265
  return copiedValue;
77
266
  }
78
267
  }
79
268
  return oldValue;
80
269
  }
81
- function copyMutableFamilyMemberWithinTransaction(atom, family, origin, target) {
82
- if (`toJson` in family && `fromJson` in family) {
83
- const copyCreated = copyMutableIfNeeded(atom, family, origin, target);
84
- return copyCreated;
85
- }
86
- return null;
87
- }
88
270
 
89
271
  // internal/src/caching.ts
90
272
  function cacheValue(key, value, subject, target) {
@@ -136,7 +318,7 @@ var readOrComputeValue = (state, target) => {
136
318
  target.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
137
319
  return readCachedValue(state, target);
138
320
  }
139
- if (state.type !== `atom`) {
321
+ if (state.type !== `atom` && state.type !== `mutable_atom`) {
140
322
  target.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
141
323
  return state.get();
142
324
  }
@@ -151,6 +333,11 @@ var readOrComputeValue = (state, target) => {
151
333
  return state.default instanceof Function ? state.default() : state.default;
152
334
  };
153
335
 
336
+ // internal/src/set-state/become.ts
337
+ var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
338
+ originalThing instanceof Function ? originalThing() : originalThing
339
+ ) : nextVersionOfThing;
340
+
154
341
  // internal/src/operation.ts
155
342
  var openOperation = (token, store) => {
156
343
  if (store.operation.open) {
@@ -213,42 +400,6 @@ var markDone = (key, store) => {
213
400
  store.operation.done.add(key);
214
401
  };
215
402
 
216
- // internal/src/set-state/become.ts
217
- var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
218
- originalThing instanceof Function ? originalThing() : originalThing
219
- ) : nextVersionOfThing;
220
-
221
- // internal/src/subject.ts
222
- var Subject = class {
223
- constructor() {
224
- this.subscribers = /* @__PURE__ */ new Map();
225
- }
226
- subscribe(key, subscriber) {
227
- this.subscribers.set(key, subscriber);
228
- const unsubscribe = () => this.unsubscribe(key);
229
- return unsubscribe;
230
- }
231
- unsubscribe(key) {
232
- this.subscribers.delete(key);
233
- }
234
- next(value) {
235
- const subscribers = this.subscribers.values();
236
- for (const subscriber of subscribers) {
237
- subscriber(value);
238
- }
239
- }
240
- };
241
- var StatefulSubject = class extends Subject {
242
- constructor(initialState) {
243
- super();
244
- this.state = initialState;
245
- }
246
- next(value) {
247
- this.state = value;
248
- super.next(value);
249
- }
250
- };
251
-
252
403
  // internal/src/set-state/emit-update.ts
253
404
  var emitUpdate = (state, update, store) => {
254
405
  store.logger.info(
@@ -336,215 +487,44 @@ var stowUpdate = (state, update, store) => {
336
487
  `)`
337
488
  );
338
489
  };
339
-
340
- // internal/src/set-state/set-atom.ts
341
- var setAtom = (atom, next, target) => {
342
- const oldValue = readOrComputeValue(atom, target);
343
- let newValue = copyMutableIfWithinTransaction(oldValue, atom, target);
344
- newValue = become(next)(newValue);
345
- target.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
346
- newValue = cacheValue(atom.key, newValue, atom.subject, target);
347
- if (isAtomDefault(atom.key, target)) {
348
- markAtomAsNotDefault(atom.key, target);
349
- }
350
- markDone(atom.key, target);
351
- evictDownStream(atom, target);
352
- const update = { oldValue, newValue };
353
- if (target.transactionMeta === null) {
354
- emitUpdate(atom, update, target);
355
- } else if (target.transactionMeta.phase === `applying` && target.parent) {
356
- emitUpdate(atom, update, target.parent);
357
- } else {
358
- stowUpdate(atom, update, target);
359
- }
360
- };
361
-
362
- // internal/src/set-state/set-atom-or-selector.ts
363
- var setAtomOrSelector = (state, value, store) => {
364
- switch (state.type) {
365
- case `atom`:
366
- setAtom(state, value, store);
367
- break;
368
- case `selector`:
369
- state.set(value);
370
- break;
371
- }
372
- };
373
-
374
- // internal/src/store/deposit.ts
375
- function deposit(state) {
376
- const token = {
377
- key: state.key,
378
- type: state.type
379
- };
380
- if (`family` in state) {
381
- token.family = state.family;
382
- }
383
- return token;
384
- }
385
- var Store = class {
386
- constructor(name, store = null) {
387
- this.parent = null;
388
- this.child = null;
389
- this.valueMap = /* @__PURE__ */ new Map();
390
- this.atoms = /* @__PURE__ */ new Map();
391
- this.selectors = /* @__PURE__ */ new Map();
392
- this.readonlySelectors = /* @__PURE__ */ new Map();
393
- this.trackers = /* @__PURE__ */ new Map();
394
- this.families = /* @__PURE__ */ new Map();
395
- this.timelines = /* @__PURE__ */ new Map();
396
- this.transactions = /* @__PURE__ */ new Map();
397
- this.atomsThatAreDefault = /* @__PURE__ */ new Set();
398
- this.timelineAtoms = new Junction({
399
- between: [`timelineKey`, `atomKey`],
400
- cardinality: `1:n`
401
- });
402
- this.selectorAtoms = new Junction({
403
- between: [`selectorKey`, `atomKey`],
404
- cardinality: `n:n`
405
- });
406
- this.selectorGraph = new Junction(
407
- {
408
- between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
409
- cardinality: `n:n`
410
- },
411
- {
412
- makeContentKey: (...keys) => keys.sort().join(`:`)
413
- }
414
- );
415
- this.on = {
416
- atomCreation: new Subject(),
417
- selectorCreation: new Subject(),
418
- transactionCreation: new Subject(),
419
- timelineCreation: new Subject(),
420
- transactionApplying: new StatefulSubject(null),
421
- operationClose: new Subject()
422
- };
423
- this.operation = { open: false };
424
- this.transactionMeta = null;
425
- this.config = {
426
- name: `IMPLICIT_STORE`
427
- };
428
- this.loggers = [
429
- new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
430
- ];
431
- this.logger = {
432
- error: (...messages) => {
433
- for (const logger of this.loggers)
434
- logger.error(...messages);
435
- },
436
- info: (...messages) => {
437
- for (const logger of this.loggers)
438
- logger.info(...messages);
439
- },
440
- warn: (...messages) => {
441
- for (const logger of this.loggers)
442
- logger.warn(...messages);
443
- }
444
- };
445
- if (store !== null) {
446
- this.valueMap = new Map(store == null ? void 0 : store.valueMap);
447
- this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
448
- this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
449
- this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
450
- name
451
- });
452
- for (const [, family] of store.families) {
453
- family.install(this);
454
- }
455
- const mutableHelpers = /* @__PURE__ */ new Set();
456
- for (const [, atom] of store.atoms) {
457
- if (mutableHelpers.has(atom.key)) {
458
- continue;
459
- }
460
- atom.install(this);
461
- if (`mutable` in atom) {
462
- const originalJsonToken = getJsonToken(atom);
463
- const originalUpdateToken = getUpdateToken(atom);
464
- mutableHelpers.add(originalJsonToken.key);
465
- mutableHelpers.add(originalUpdateToken.key);
466
- }
467
- }
468
- for (const [, selector] of store.readonlySelectors) {
469
- selector.install(this);
470
- }
471
- for (const [, selector] of store.selectors) {
472
- if (mutableHelpers.has(selector.key)) {
473
- continue;
474
- }
475
- selector.install(this);
476
- }
477
- for (const [, tx] of store.transactions) {
478
- tx.install(this);
479
- }
480
- for (const [, timeline] of store.timelines) {
481
- timeline.install(this);
482
- }
483
- }
484
- }
485
- };
486
- var IMPLICIT = {
487
- STORE_INTERNAL: void 0,
488
- get STORE() {
489
- var _a;
490
- return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
491
- }
492
- };
493
- var clearStore = (store) => {
494
- const { config } = store;
495
- Object.assign(store, new Store(config.name));
496
- store.config = config;
497
- };
498
-
499
- // internal/src/store/withdraw.ts
500
- function withdraw(token, store) {
501
- let withdrawn;
502
- let target = store;
503
- while (target !== null) {
504
- switch (token.type) {
505
- case `atom`:
506
- withdrawn = target.atoms.get(token.key);
507
- break;
508
- case `selector`:
509
- withdrawn = target.selectors.get(token.key);
510
- break;
511
- case `readonly_selector`:
512
- withdrawn = target.readonlySelectors.get(token.key);
513
- break;
514
- case `timeline`:
515
- withdrawn = target.timelines.get(token.key);
516
- break;
517
- case `transaction`:
518
- withdrawn = target.transactions.get(token.key);
519
- break;
520
- }
521
- if (withdrawn) {
522
- return withdrawn;
490
+
491
+ // internal/src/set-state/set-atom.ts
492
+ var setAtom = (atom, next, target) => {
493
+ const oldValue = readOrComputeValue(atom, target);
494
+ let newValue = copyMutableIfWithinTransaction(oldValue, atom, target);
495
+ newValue = become(next)(newValue);
496
+ target.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
497
+ newValue = cacheValue(atom.key, newValue, atom.subject, target);
498
+ if (isAtomDefault(atom.key, target)) {
499
+ markAtomAsNotDefault(atom.key, target);
500
+ }
501
+ markDone(atom.key, target);
502
+ evictDownStream(atom, target);
503
+ const update = { oldValue, newValue };
504
+ if (target.transactionMeta === null) {
505
+ emitUpdate(atom, update, target);
506
+ } else if (target.on.transactionApplying && target.parent) {
507
+ stowUpdate(atom, update, target);
508
+ if (atom.key.startsWith(`*`)) {
509
+ const mutableKey = atom.key.slice(1);
510
+ const mutable = target.valueMap.get(mutableKey);
511
+ mutable.do(update.newValue);
523
512
  }
524
- target = target.child;
525
513
  }
526
- }
514
+ };
527
515
 
528
- // internal/src/store/withdraw-new-family-member.ts
529
- function withdrawNewFamilyMember(token, store) {
530
- if (token.family) {
531
- store.logger.info(
532
- `\u{1F46A}`,
533
- token.type,
534
- token.key,
535
- `creating new family member in store "${store.config.name}"`
536
- );
537
- const target = newest(store);
538
- const family = target.families.get(token.family.key);
539
- if (family) {
540
- const jsonSubKey = JSON.parse(token.family.subKey);
541
- family(jsonSubKey);
542
- const state = withdraw(token, store);
543
- return state;
544
- }
516
+ // internal/src/set-state/set-atom-or-selector.ts
517
+ var setAtomOrSelector = (state, value, store) => {
518
+ switch (state.type) {
519
+ case `atom`:
520
+ case `mutable_atom`:
521
+ setAtom(state, value, store);
522
+ break;
523
+ case `selector`:
524
+ state.set(value);
525
+ break;
545
526
  }
546
- return void 0;
547
- }
527
+ };
548
528
 
549
529
  // internal/src/keys.ts
550
530
  var isAtomKey = (key, store) => newest(store).atoms.has(key);
@@ -584,7 +564,8 @@ var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
584
564
  }
585
565
  return rootKeys;
586
566
  };
587
- var traceAllSelectorAtoms = (selectorKey, store) => {
567
+ var traceAllSelectorAtoms = (selector, store) => {
568
+ const selectorKey = selector.key;
588
569
  const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
589
570
  return directDependencyKeys.flatMap(
590
571
  (depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
@@ -594,7 +575,7 @@ var traceAllSelectorAtoms = (selectorKey, store) => {
594
575
  // internal/src/selector/update-selector-atoms.ts
595
576
  var updateSelectorAtoms = (selectorKey, dependency, store) => {
596
577
  const target = newest(store);
597
- if (dependency.type === `atom`) {
578
+ if (dependency.type === `atom` || dependency.type === `mutable_atom`) {
598
579
  target.selectorAtoms.set({
599
580
  selectorKey,
600
581
  atomKey: dependency.key
@@ -665,8 +646,44 @@ var registerSelector = (selectorKey, store) => ({
665
646
  find: (token, key) => findInStore(token, key, store)
666
647
  });
667
648
 
668
- // internal/src/selector/create-read-write-selector.ts
669
- var createReadWriteSelector = (options, family, store) => {
649
+ // internal/src/selector/create-readonly-selector.ts
650
+ var createReadonlySelector = (options, family, store) => {
651
+ const target = newest(store);
652
+ const subject = new Subject();
653
+ const { get, find } = registerSelector(options.key, store);
654
+ const getSelf = () => {
655
+ const value = options.get({ get, find });
656
+ cacheValue(options.key, value, subject, store);
657
+ return value;
658
+ };
659
+ const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
660
+ subject,
661
+ install: (s) => createReadonlySelector(options, family, s),
662
+ get: getSelf,
663
+ type: `readonly_selector`
664
+ }), family && { family });
665
+ target.readonlySelectors.set(options.key, readonlySelector);
666
+ const initialValue = getSelf();
667
+ store.logger.info(
668
+ `\u2728`,
669
+ readonlySelector.type,
670
+ readonlySelector.key,
671
+ `=`,
672
+ initialValue
673
+ );
674
+ const token = {
675
+ key: options.key,
676
+ type: `readonly_selector`
677
+ };
678
+ if (family) {
679
+ token.family = family;
680
+ }
681
+ store.on.selectorCreation.next(token);
682
+ return token;
683
+ };
684
+
685
+ // internal/src/selector/create-writable-selector.ts
686
+ var createWritableSelector = (options, family, store) => {
670
687
  const target = newest(store);
671
688
  const subject = new Subject();
672
689
  const transactors = registerSelector(options.key, store);
@@ -699,7 +716,7 @@ var createReadWriteSelector = (options, family, store) => {
699
716
  };
700
717
  const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
701
718
  subject,
702
- install: (s) => createSelector(options, family, s),
719
+ install: (s) => createWritableSelector(options, family, s),
703
720
  get: getSelf,
704
721
  set: setSelf,
705
722
  type: `selector`
@@ -718,59 +735,13 @@ var createReadWriteSelector = (options, family, store) => {
718
735
  return token;
719
736
  };
720
737
 
721
- // internal/src/selector/create-readonly-selector.ts
722
- var createReadonlySelector = (options, family, store) => {
723
- const target = newest(store);
724
- const subject = new Subject();
725
- const { get, find } = registerSelector(options.key, store);
726
- const getSelf = () => {
727
- const value = options.get({ get, find });
728
- cacheValue(options.key, value, subject, store);
729
- return value;
730
- };
731
- const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
732
- subject,
733
- install: (s) => createSelector(options, family, s),
734
- get: getSelf,
735
- type: `readonly_selector`
736
- }), family && { family });
737
- target.readonlySelectors.set(options.key, readonlySelector);
738
- const initialValue = getSelf();
739
- store.logger.info(
740
- `\u2728`,
741
- readonlySelector.type,
742
- readonlySelector.key,
743
- `=`,
744
- initialValue
745
- );
746
- const token = {
747
- key: options.key,
748
- type: `readonly_selector`
749
- };
750
- if (family) {
751
- token.family = family;
752
- }
753
- store.on.selectorCreation.next(token);
754
- return token;
755
- };
756
-
757
- // internal/src/selector/create-selector.ts
758
- function createSelector(options, family, store) {
759
- const target = newest(store);
760
- const existingWritable = target.selectors.get(options.key);
761
- const existingReadonly = target.readonlySelectors.get(options.key);
762
- if (existingWritable || existingReadonly) {
763
- store.logger.error(
764
- `\u274C`,
765
- existingReadonly ? `readonly_selector` : `selector`,
766
- options.key,
767
- `Tried to create selector, but it already exists in the store.`
768
- );
738
+ // internal/src/selector/create-standalone-selector.ts
739
+ function createStandaloneSelector(options, store) {
740
+ const isWritable = `set` in options;
741
+ if (isWritable) {
742
+ return createWritableSelector(options, void 0, store);
769
743
  }
770
- if (`set` in options) {
771
- return createReadWriteSelector(options, family, store);
772
- }
773
- return createReadonlySelector(options, family, store);
744
+ return createReadonlySelector(options, void 0, store);
774
745
  }
775
746
 
776
747
  // internal/src/selector/delete-selector.ts
@@ -812,44 +783,46 @@ var recallState = (state, store) => {
812
783
  };
813
784
 
814
785
  // internal/src/subscribe/subscribe-to-root-atoms.ts
815
- var subscribeToRootAtoms = (state, store) => {
786
+ var subscribeToRootAtoms = (selector, store) => {
816
787
  const target = newest(store);
817
- const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
818
- const atom = target.atoms.get(atomKey);
819
- if (atom === void 0) {
820
- throw new Error(
821
- `Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
822
- );
823
- }
824
- return atom.subject.subscribe(
825
- `${state.type}:${state.key}`,
826
- (atomChange) => {
827
- store.logger.info(
828
- `\u{1F4E2}`,
829
- state.type,
830
- state.key,
831
- `root`,
832
- atomKey,
833
- `went`,
834
- atomChange.oldValue,
835
- `->`,
836
- atomChange.newValue
837
- );
838
- const oldValue = recallState(state, target);
839
- const newValue = readOrComputeValue(state, target);
840
- store.logger.info(
841
- `\u2728`,
842
- state.type,
843
- state.key,
844
- `went`,
845
- oldValue,
846
- `->`,
847
- newValue
788
+ const dependencySubscriptions = traceAllSelectorAtoms(selector, store).map(
789
+ (atomKey) => {
790
+ const atom = target.atoms.get(atomKey);
791
+ if (atom === void 0) {
792
+ throw new Error(
793
+ `Atom "${atomKey}", a dependency of selector "${selector.key}", not found in store "${store.config.name}".`
848
794
  );
849
- state.subject.next({ newValue, oldValue });
850
795
  }
851
- );
852
- });
796
+ return atom.subject.subscribe(
797
+ `${selector.type}:${selector.key}`,
798
+ (atomChange) => {
799
+ store.logger.info(
800
+ `\u{1F4E2}`,
801
+ selector.type,
802
+ selector.key,
803
+ `root`,
804
+ atomKey,
805
+ `went`,
806
+ atomChange.oldValue,
807
+ `->`,
808
+ atomChange.newValue
809
+ );
810
+ const oldValue = recallState(selector, target);
811
+ const newValue = readOrComputeValue(selector, target);
812
+ store.logger.info(
813
+ `\u2728`,
814
+ selector.type,
815
+ selector.key,
816
+ `went`,
817
+ oldValue,
818
+ `->`,
819
+ newValue
820
+ );
821
+ selector.subject.next({ newValue, oldValue });
822
+ }
823
+ );
824
+ }
825
+ );
853
826
  return dependencySubscriptions;
854
827
  };
855
828
 
@@ -864,7 +837,7 @@ function subscribeToState(token, handleUpdate, key, store) {
864
837
  }
865
838
  const unsubFunction = state.subject.subscribe(key, handleUpdate);
866
839
  store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
867
- const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
840
+ const dependencyUnsubFunctions = state.type !== `atom` && state.type !== `mutable_atom` ? subscribeToRootAtoms(state, store) : null;
868
841
  const unsubscribe = dependencyUnsubFunctions === null ? () => {
869
842
  store.logger.info(
870
843
  `\u{1F648}`,
@@ -1075,27 +1048,67 @@ var Tracker = class {
1075
1048
  // internal/src/mutable/create-mutable-atom.ts
1076
1049
  function createMutableAtom(options, family, store) {
1077
1050
  store.logger.info(
1078
- `\u{1F527}`,
1051
+ `\u{1F528}`,
1079
1052
  `atom`,
1080
1053
  options.key,
1081
1054
  `creating in store "${store.config.name}"`
1082
1055
  );
1083
- const coreState = createRegularAtom(options, family, store);
1084
- new Tracker(coreState, store);
1085
- const jsonState = selectJson(coreState, options, store);
1086
1056
  const target = newest(store);
1087
- subscribeToState(
1088
- jsonState,
1089
- () => {
1090
- const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
1091
- if (!trackerHasBeenInitialized) {
1092
- new Tracker(coreState, store);
1093
- }
1057
+ const existing = target.atoms.get(options.key);
1058
+ if (existing && existing.type === `mutable_atom`) {
1059
+ store.logger.error(
1060
+ `\u274C`,
1061
+ `atom`,
1062
+ options.key,
1063
+ `Tried to create atom, but it already exists in the store.`
1064
+ );
1065
+ return deposit(existing);
1066
+ }
1067
+ const subject = new Subject();
1068
+ const newAtom = __spreadProps(__spreadValues({}, options), {
1069
+ type: `mutable_atom`,
1070
+ install: (store2) => {
1071
+ store2.logger.info(
1072
+ `\u{1F6E0}\uFE0F`,
1073
+ `atom`,
1074
+ options.key,
1075
+ `installing in store "${store2.config.name}"`
1076
+ );
1077
+ return createMutableAtom(options, family, store2);
1094
1078
  },
1095
- `tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
1096
- store
1097
- );
1098
- return coreState;
1079
+ subject
1080
+ });
1081
+ if (family) {
1082
+ newAtom.family = family;
1083
+ }
1084
+ const initialValue = options.default();
1085
+ target.atoms.set(newAtom.key, newAtom);
1086
+ markAtomAsDefault(options.key, store);
1087
+ cacheValue(options.key, initialValue, subject, target);
1088
+ const token = deposit(newAtom);
1089
+ if (options.effects) {
1090
+ let effectIndex = 0;
1091
+ const cleanupFunctions = [];
1092
+ for (const effect of options.effects) {
1093
+ const cleanup = effect({
1094
+ setSelf: (next) => setState(token, next, store),
1095
+ onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
1096
+ });
1097
+ if (cleanup) {
1098
+ cleanupFunctions.push(cleanup);
1099
+ }
1100
+ ++effectIndex;
1101
+ }
1102
+ newAtom.cleanup = () => {
1103
+ for (const cleanup of cleanupFunctions) {
1104
+ cleanup();
1105
+ }
1106
+ };
1107
+ }
1108
+ new Tracker(token, store);
1109
+ selectJson(token, options, store);
1110
+ store.on.atomCreation.next(token);
1111
+ return token;
1099
1112
  }
1100
1113
  function createRegularAtomFamily(options, store) {
1101
1114
  const subject = new Subject();
@@ -1116,16 +1129,7 @@ function createRegularAtomFamily(options, store) {
1116
1129
  if (options.effects) {
1117
1130
  individualOptions.effects = options.effects(key);
1118
1131
  }
1119
- if (`mutable` in options) {
1120
- const mutableOptions = __spreadProps(__spreadValues({}, individualOptions), {
1121
- mutable: true,
1122
- toJson: options.toJson,
1123
- fromJson: options.fromJson
1124
- });
1125
- token = createMutableAtom(mutableOptions, family, store);
1126
- } else {
1127
- token = createRegularAtom(individualOptions, family, store);
1128
- }
1132
+ token = createRegularAtom(individualOptions, family, store);
1129
1133
  subject.next(token);
1130
1134
  }
1131
1135
  return token;
@@ -1137,9 +1141,6 @@ function createRegularAtomFamily(options, store) {
1137
1141
  install: (store2) => createRegularAtomFamily(options, store2)
1138
1142
  }
1139
1143
  );
1140
- if (`mutable` in options && typeof options.mutable === `boolean`) {
1141
- Object.assign(atomFamily, { mutable: options.mutable });
1142
- }
1143
1144
  const target = newest(store);
1144
1145
  target.families.set(options.key, atomFamily);
1145
1146
  return atomFamily;
@@ -1147,7 +1148,8 @@ function createRegularAtomFamily(options, store) {
1147
1148
 
1148
1149
  // internal/src/families/create-atom-family.ts
1149
1150
  function createAtomFamily(options, store) {
1150
- if (`mutable` in options) {
1151
+ const isMutable2 = `mutable` in options;
1152
+ if (isMutable2) {
1151
1153
  return createMutableAtomFamily(options, store);
1152
1154
  }
1153
1155
  return createRegularAtomFamily(options, store);
@@ -1164,7 +1166,7 @@ function createReadonlySelectorFamily(options, store) {
1164
1166
  if (existing) {
1165
1167
  return deposit(existing);
1166
1168
  }
1167
- return createSelector(
1169
+ return createReadonlySelector(
1168
1170
  {
1169
1171
  key: fullKey,
1170
1172
  get: options.get(key)
@@ -1183,23 +1185,18 @@ function createReadonlySelectorFamily(options, store) {
1183
1185
  store.families.set(options.key, readonlySelectorFamily);
1184
1186
  return readonlySelectorFamily;
1185
1187
  }
1186
- function createSelectorFamily(options, store) {
1187
- const isReadonly = !(`set` in options);
1188
- if (isReadonly) {
1189
- return createReadonlySelectorFamily(options, store);
1190
- }
1191
- const target = newest(store);
1188
+ function createWritableSelectorFamily(options, store) {
1192
1189
  const subject = new Subject();
1193
1190
  const selectorFamily = Object.assign(
1194
1191
  (key) => {
1195
1192
  const subKey = stringifyJson(key);
1196
1193
  const family = { key: options.key, subKey };
1197
1194
  const fullKey = `${options.key}(${subKey})`;
1198
- const existing = target.selectors.get(fullKey);
1195
+ const existing = store.selectors.get(fullKey);
1199
1196
  if (existing) {
1200
1197
  return deposit(existing);
1201
1198
  }
1202
- const token = createSelector(
1199
+ const token = createWritableSelector(
1203
1200
  {
1204
1201
  key: fullKey,
1205
1202
  get: options.get(key),
@@ -1215,13 +1212,22 @@ function createSelectorFamily(options, store) {
1215
1212
  key: options.key,
1216
1213
  type: `selector_family`,
1217
1214
  subject,
1218
- install: (store2) => createSelectorFamily(options, store2)
1215
+ install: (store2) => createWritableSelectorFamily(options, store2)
1219
1216
  }
1220
1217
  );
1221
- target.families.set(options.key, selectorFamily);
1218
+ store.families.set(options.key, selectorFamily);
1222
1219
  return selectorFamily;
1223
1220
  }
1224
1221
 
1222
+ // internal/src/families/create-selector-family.ts
1223
+ function createSelectorFamily(options, store) {
1224
+ const isWritable = `set` in options;
1225
+ if (isWritable) {
1226
+ return createWritableSelectorFamily(options, store);
1227
+ }
1228
+ return createReadonlySelectorFamily(options, store);
1229
+ }
1230
+
1225
1231
  // internal/src/mutable/tracker-family.ts
1226
1232
  var FamilyTracker = class {
1227
1233
  constructor(findMutableState, store) {
@@ -1258,29 +1264,63 @@ var FamilyTracker = class {
1258
1264
 
1259
1265
  // internal/src/mutable/create-mutable-atom-family.ts
1260
1266
  function createMutableAtomFamily(options, store) {
1261
- const coreFamily = Object.assign(
1262
- createRegularAtomFamily(options, store),
1263
- options
1267
+ const subject = new Subject();
1268
+ const atomFamily = Object.assign(
1269
+ (key) => {
1270
+ const subKey = stringifyJson(key);
1271
+ const family = { key: options.key, subKey };
1272
+ const fullKey = `${options.key}(${subKey})`;
1273
+ const existing = withdraw({ key: fullKey, type: `mutable_atom` }, store);
1274
+ let token;
1275
+ if (existing) {
1276
+ token = deposit(existing);
1277
+ } else {
1278
+ const individualOptions = {
1279
+ key: fullKey,
1280
+ default: () => options.default(key),
1281
+ toJson: options.toJson,
1282
+ fromJson: options.fromJson,
1283
+ mutable: true
1284
+ };
1285
+ if (options.effects) {
1286
+ individualOptions.effects = options.effects(key);
1287
+ }
1288
+ token = createMutableAtom(individualOptions, family, store);
1289
+ subject.next(token);
1290
+ }
1291
+ return token;
1292
+ },
1293
+ {
1294
+ key: options.key,
1295
+ type: `mutable_atom_family`,
1296
+ subject,
1297
+ install: (store2) => createMutableAtomFamily(options, store2),
1298
+ toJson: options.toJson,
1299
+ fromJson: options.fromJson
1300
+ }
1264
1301
  );
1265
- selectJsonFamily(coreFamily, options);
1266
- new FamilyTracker(coreFamily, store);
1267
- return coreFamily;
1302
+ const target = newest(store);
1303
+ target.families.set(options.key, atomFamily);
1304
+ selectJsonFamily(atomFamily, options, store);
1305
+ new FamilyTracker(atomFamily, store);
1306
+ return atomFamily;
1268
1307
  }
1269
1308
 
1270
1309
  // internal/src/mutable/get-json-family.ts
1271
1310
  var getJsonFamily = (mutableAtomFamily, store) => {
1272
1311
  const target = newest(store);
1273
1312
  const key = `${mutableAtomFamily.key}:JSON`;
1274
- const jsonFamily = target.families.get(
1275
- key
1276
- );
1313
+ const jsonFamily = target.families.get(key);
1277
1314
  return jsonFamily;
1278
1315
  };
1279
1316
 
1280
1317
  // internal/src/mutable/get-json-token.ts
1281
1318
  var getJsonToken = (mutableAtomToken) => {
1282
1319
  const key = mutableAtomToken.family ? `${mutableAtomToken.family.key}:JSON(${mutableAtomToken.family.subKey})` : `${mutableAtomToken.key}:JSON`;
1283
- const jsonToken = { type: `selector`, key };
1320
+ const jsonToken = {
1321
+ type: `selector`,
1322
+ key
1323
+ };
1284
1324
  if (mutableAtomToken.family) {
1285
1325
  jsonToken.family = {
1286
1326
  key: `${mutableAtomToken.family.key}:JSON`,
@@ -1303,13 +1343,10 @@ var getUpdateToken = (mutableAtomToken) => {
1303
1343
  return updateToken;
1304
1344
  };
1305
1345
 
1306
- // internal/src/mutable/get-update-family.ts
1307
- var getUpdateFamily = (mutableAtomFamily, store) => {
1308
- const target = newest(store);
1309
- const key = `*${mutableAtomFamily.key}`;
1310
- const updateFamily = target.families.get(key);
1311
- return updateFamily;
1312
- };
1346
+ // internal/src/mutable/is-mutable.ts
1347
+ function isMutable(atomOrTokenOrFamily) {
1348
+ return atomOrTokenOrFamily.type === `mutable_atom` || atomOrTokenOrFamily.type === `mutable_atom_family`;
1349
+ }
1313
1350
 
1314
1351
  // internal/src/mutable/transceiver.ts
1315
1352
  function isTransceiver(value) {
@@ -1341,7 +1378,7 @@ function createRegularAtom(options, family, store) {
1341
1378
  );
1342
1379
  const target = newest(store);
1343
1380
  const existing = target.atoms.get(options.key);
1344
- if (existing) {
1381
+ if (existing && existing.type === `atom`) {
1345
1382
  store.logger.error(
1346
1383
  `\u274C`,
1347
1384
  `atom`,
@@ -1360,7 +1397,7 @@ function createRegularAtom(options, family, store) {
1360
1397
  options.key,
1361
1398
  `installing in store "${store2.config.name}"`
1362
1399
  );
1363
- return `mutable` in options ? createMutableAtom(options, family, store2) : createRegularAtom(options, family, store2);
1400
+ return createRegularAtom(options, family, store2);
1364
1401
  },
1365
1402
  subject
1366
1403
  });
@@ -1398,12 +1435,13 @@ function createRegularAtom(options, family, store) {
1398
1435
  return token;
1399
1436
  }
1400
1437
 
1401
- // internal/src/atom/create-atom.ts
1402
- function createAtom(options, family, store) {
1403
- if (`mutable` in options) {
1404
- return createMutableAtom(options, family, store);
1438
+ // internal/src/atom/create-standalone-atom.ts
1439
+ function createStandaloneAtom(options, store) {
1440
+ const isMutable2 = `mutable` in options;
1441
+ if (isMutable2) {
1442
+ return createMutableAtom(options, void 0, store);
1405
1443
  }
1406
- return createRegularAtom(options, family, store);
1444
+ return createRegularAtom(options, void 0, store);
1407
1445
  }
1408
1446
 
1409
1447
  // internal/src/atom/delete-atom.ts
@@ -1524,9 +1562,8 @@ var NotFoundError = class extends Error {
1524
1562
  // internal/src/timeline/add-atom-to-timeline.ts
1525
1563
  var addAtomToTimeline = (atomToken, tl, store) => {
1526
1564
  let maybeAtom = withdraw(atomToken, store);
1527
- if (maybeAtom == null ? void 0 : maybeAtom.mutable) {
1528
- console.log(`adding transceiver to timeline`, atomToken.key);
1529
- const updateToken = getUpdateToken(atomToken);
1565
+ if ((maybeAtom == null ? void 0 : maybeAtom.type) === `mutable_atom`) {
1566
+ const updateToken = getUpdateToken(maybeAtom);
1530
1567
  maybeAtom = withdraw(updateToken, store);
1531
1568
  }
1532
1569
  const atom = maybeAtom;
@@ -1535,6 +1572,7 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1535
1572
  `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
1536
1573
  );
1537
1574
  }
1575
+ store.timelineAtoms.set({ atomKey: atom.key, timelineKey: tl.key });
1538
1576
  atom.subject.subscribe(`timeline`, (update) => {
1539
1577
  var _a, _b, _c, _d, _e, _f;
1540
1578
  const target = newest(store);
@@ -1719,14 +1757,6 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1719
1757
  });
1720
1758
  };
1721
1759
 
1722
- // internal/src/mutable/is-mutable.ts
1723
- function isMutable(atomOrTokenOrFamily) {
1724
- if (`mutable` in atomOrTokenOrFamily) {
1725
- return atomOrTokenOrFamily.mutable;
1726
- }
1727
- return false;
1728
- }
1729
-
1730
1760
  // internal/src/timeline/create-timeline.ts
1731
1761
  function createTimeline(options, store, data) {
1732
1762
  var _a, _b;
@@ -1749,27 +1779,15 @@ function createTimeline(options, store, data) {
1749
1779
  const target = newest(store);
1750
1780
  for (const tokenOrFamily of options.atoms) {
1751
1781
  let atomKey = tokenOrFamily.key;
1752
- const existingTimelineKey = target.timelineAtoms.getRelatedKey(atomKey);
1753
- if (existingTimelineKey) {
1754
- store.logger.error(
1755
- `\u274C`,
1756
- `timeline`,
1757
- options.key,
1758
- `Failed to add atom "${atomKey}" because it already belongs to timeline "${existingTimelineKey}"`
1759
- );
1760
- continue;
1761
- }
1762
- if (tokenOrFamily.type === `atom_family`) {
1763
- let family = tokenOrFamily;
1764
- if (isMutable(family)) {
1765
- family = getUpdateFamily(family, store);
1766
- atomKey = family.key;
1767
- }
1782
+ if (tokenOrFamily.type === `atom_family` || tokenOrFamily.type === `mutable_atom_family`) {
1783
+ const family = tokenOrFamily;
1784
+ const familyKey = family.key;
1785
+ target.timelineAtoms.set({ atomKey: familyKey, timelineKey });
1768
1786
  family.subject.subscribe(`timeline:${options.key}`, (token2) => {
1769
1787
  addAtomToTimeline(token2, tl, store);
1770
1788
  });
1771
1789
  for (const atom of target.atoms.values()) {
1772
- if (((_b = atom.family) == null ? void 0 : _b.key) === family.key) {
1790
+ if (((_b = atom.family) == null ? void 0 : _b.key) === familyKey) {
1773
1791
  addAtomToTimeline(atom, tl, store);
1774
1792
  }
1775
1793
  }
@@ -1812,9 +1830,18 @@ function createTimeline(options, store, data) {
1812
1830
  continue;
1813
1831
  }
1814
1832
  }
1833
+ const existingTimelineKey = target.timelineAtoms.getRelatedKey(atomKey);
1834
+ if (existingTimelineKey) {
1835
+ store.logger.error(
1836
+ `\u274C`,
1837
+ `timeline`,
1838
+ options.key,
1839
+ `Failed to add atom "${atomKey}" because it already belongs to timeline "${existingTimelineKey}"`
1840
+ );
1841
+ continue;
1842
+ }
1815
1843
  addAtomToTimeline(atom, tl, store);
1816
1844
  }
1817
- target.timelineAtoms.set({ atomKey, timelineKey });
1818
1845
  }
1819
1846
  store.timelines.set(options.key, tl);
1820
1847
  const token = {
@@ -2056,6 +2083,6 @@ function createTransaction(options, store) {
2056
2083
  // internal/src/transaction/index.ts
2057
2084
  var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
2058
2085
 
2059
- export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, StatefulSubject, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, addAtomToTimeline, applyTransaction, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtom, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelector, createSelectorFamily, createTimeline, createTransaction, deleteAtom, deleteSelector, deposit, eldest, evictCachedValue, getEnvironmentData, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateFamily, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isDone, isReadonlySelectorKey, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
2086
+ export { FamilyTracker, Future, IMPLICIT, LazyMap, NotFoundError, StatefulSubject, Store, Subject, TRANSACTION_PHASES, Tracker, abortTransaction, addAtomToTimeline, applyTransaction, become, buildTransaction, cacheValue, clearStore, closeOperation, createAtomFamily, createMutableAtom, createMutableAtomFamily, createReadonlySelector, createReadonlySelectorFamily, createRegularAtom, createRegularAtomFamily, createSelectorFamily, createStandaloneAtom, createStandaloneSelector, createTimeline, createTransaction, createWritableSelector, deleteAtom, deleteSelector, deposit, evictCachedValue, getEnvironmentData, getJsonFamily, getJsonToken, getSelectorDependencyKeys, getUpdateToken, ingestAtomUpdate, ingestSelectorUpdate, ingestTransactionUpdate, isAtomDefault, isAtomKey, isDone, isMutable, isReadonlySelectorKey, isSelectorKey, isStateKey, isTransceiver, markAtomAsDefault, markAtomAsNotDefault, markDone, newest, openOperation, readCachedValue, readOrComputeValue, registerSelector, setAtomOrSelector, subscribeToRootAtoms, subscribeToState, subscribeToTimeline, subscribeToTransaction, timeTravel, traceAllSelectorAtoms, traceSelectorAtoms, updateSelectorAtoms, withdraw, withdrawNewFamilyMember };
2060
2087
  //# sourceMappingURL=out.js.map
2061
2088
  //# sourceMappingURL=index.js.map