atom.io 0.15.6 → 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 +910 -887
  16. package/internal/dist/index.cjs.map +1 -1
  17. package/internal/dist/index.d.ts +157 -162
  18. package/internal/dist/index.js +482 -458
  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 +1 -2
  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(
@@ -341,213 +492,39 @@ var stowUpdate = (state, update, store) => {
341
492
  var setAtom = (atom, next, target) => {
342
493
  const oldValue = readOrComputeValue(atom, target);
343
494
  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.on.transactionApplying && target.parent) {
356
- stowUpdate(atom, update, target);
357
- if (atom.key.startsWith(`*`)) {
358
- const mutableKey = atom.key.slice(1);
359
- const mutable = target.valueMap.get(mutableKey);
360
- mutable.do(update.newValue);
361
- }
362
- }
363
- };
364
-
365
- // internal/src/set-state/set-atom-or-selector.ts
366
- var setAtomOrSelector = (state, value, store) => {
367
- switch (state.type) {
368
- case `atom`:
369
- setAtom(state, value, store);
370
- break;
371
- case `selector`:
372
- state.set(value);
373
- break;
374
- }
375
- };
376
-
377
- // internal/src/store/deposit.ts
378
- function deposit(state) {
379
- const token = {
380
- key: state.key,
381
- type: state.type
382
- };
383
- if (`family` in state) {
384
- token.family = state.family;
385
- }
386
- return token;
387
- }
388
- var Store = class {
389
- constructor(name, store = null) {
390
- this.parent = null;
391
- this.child = null;
392
- this.valueMap = /* @__PURE__ */ new Map();
393
- this.atoms = /* @__PURE__ */ new Map();
394
- this.selectors = /* @__PURE__ */ new Map();
395
- this.readonlySelectors = /* @__PURE__ */ new Map();
396
- this.trackers = /* @__PURE__ */ new Map();
397
- this.families = /* @__PURE__ */ new Map();
398
- this.timelines = /* @__PURE__ */ new Map();
399
- this.transactions = /* @__PURE__ */ new Map();
400
- this.atomsThatAreDefault = /* @__PURE__ */ new Set();
401
- this.timelineAtoms = new Junction({
402
- between: [`timelineKey`, `atomKey`],
403
- cardinality: `1:n`
404
- });
405
- this.selectorAtoms = new Junction({
406
- between: [`selectorKey`, `atomKey`],
407
- cardinality: `n:n`
408
- });
409
- this.selectorGraph = new Junction(
410
- {
411
- between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
412
- cardinality: `n:n`
413
- },
414
- {
415
- makeContentKey: (...keys) => keys.sort().join(`:`)
416
- }
417
- );
418
- this.on = {
419
- atomCreation: new Subject(),
420
- selectorCreation: new Subject(),
421
- transactionCreation: new Subject(),
422
- timelineCreation: new Subject(),
423
- transactionApplying: new StatefulSubject(null),
424
- operationClose: new Subject()
425
- };
426
- this.operation = { open: false };
427
- this.transactionMeta = null;
428
- this.config = {
429
- name: `IMPLICIT_STORE`
430
- };
431
- this.loggers = [
432
- new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
433
- ];
434
- this.logger = {
435
- error: (...messages) => {
436
- for (const logger of this.loggers)
437
- logger.error(...messages);
438
- },
439
- info: (...messages) => {
440
- for (const logger of this.loggers)
441
- logger.info(...messages);
442
- },
443
- warn: (...messages) => {
444
- for (const logger of this.loggers)
445
- logger.warn(...messages);
446
- }
447
- };
448
- if (store !== null) {
449
- this.valueMap = new Map(store == null ? void 0 : store.valueMap);
450
- this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
451
- this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
452
- this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
453
- name
454
- });
455
- for (const [, family] of store.families) {
456
- family.install(this);
457
- }
458
- const mutableHelpers = /* @__PURE__ */ new Set();
459
- for (const [, atom] of store.atoms) {
460
- if (mutableHelpers.has(atom.key)) {
461
- continue;
462
- }
463
- atom.install(this);
464
- if (`mutable` in atom) {
465
- const originalJsonToken = getJsonToken(atom);
466
- const originalUpdateToken = getUpdateToken(atom);
467
- mutableHelpers.add(originalJsonToken.key);
468
- mutableHelpers.add(originalUpdateToken.key);
469
- }
470
- }
471
- for (const [, selector] of store.readonlySelectors) {
472
- selector.install(this);
473
- }
474
- for (const [, selector] of store.selectors) {
475
- if (mutableHelpers.has(selector.key)) {
476
- continue;
477
- }
478
- selector.install(this);
479
- }
480
- for (const [, tx] of store.transactions) {
481
- tx.install(this);
482
- }
483
- for (const [, timeline] of store.timelines) {
484
- timeline.install(this);
485
- }
486
- }
487
- }
488
- };
489
- var IMPLICIT = {
490
- STORE_INTERNAL: void 0,
491
- get STORE() {
492
- var _a;
493
- return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
494
- }
495
- };
496
- var clearStore = (store) => {
497
- const { config } = store;
498
- Object.assign(store, new Store(config.name));
499
- store.config = config;
500
- };
501
-
502
- // internal/src/store/withdraw.ts
503
- function withdraw(token, store) {
504
- let withdrawn;
505
- let target = store;
506
- while (target !== null) {
507
- switch (token.type) {
508
- case `atom`:
509
- withdrawn = target.atoms.get(token.key);
510
- break;
511
- case `selector`:
512
- withdrawn = target.selectors.get(token.key);
513
- break;
514
- case `readonly_selector`:
515
- withdrawn = target.readonlySelectors.get(token.key);
516
- break;
517
- case `timeline`:
518
- withdrawn = target.timelines.get(token.key);
519
- break;
520
- case `transaction`:
521
- withdrawn = target.transactions.get(token.key);
522
- break;
523
- }
524
- if (withdrawn) {
525
- return withdrawn;
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);
526
512
  }
527
- target = target.child;
528
513
  }
529
- }
514
+ };
530
515
 
531
- // internal/src/store/withdraw-new-family-member.ts
532
- function withdrawNewFamilyMember(token, store) {
533
- if (token.family) {
534
- store.logger.info(
535
- `\u{1F46A}`,
536
- token.type,
537
- token.key,
538
- `creating new family member in store "${store.config.name}"`
539
- );
540
- const target = newest(store);
541
- const family = target.families.get(token.family.key);
542
- if (family) {
543
- const jsonSubKey = JSON.parse(token.family.subKey);
544
- family(jsonSubKey);
545
- const state = withdraw(token, store);
546
- return state;
547
- }
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;
548
526
  }
549
- return void 0;
550
- }
527
+ };
551
528
 
552
529
  // internal/src/keys.ts
553
530
  var isAtomKey = (key, store) => newest(store).atoms.has(key);
@@ -587,7 +564,8 @@ var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
587
564
  }
588
565
  return rootKeys;
589
566
  };
590
- var traceAllSelectorAtoms = (selectorKey, store) => {
567
+ var traceAllSelectorAtoms = (selector, store) => {
568
+ const selectorKey = selector.key;
591
569
  const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
592
570
  return directDependencyKeys.flatMap(
593
571
  (depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
@@ -597,7 +575,7 @@ var traceAllSelectorAtoms = (selectorKey, store) => {
597
575
  // internal/src/selector/update-selector-atoms.ts
598
576
  var updateSelectorAtoms = (selectorKey, dependency, store) => {
599
577
  const target = newest(store);
600
- if (dependency.type === `atom`) {
578
+ if (dependency.type === `atom` || dependency.type === `mutable_atom`) {
601
579
  target.selectorAtoms.set({
602
580
  selectorKey,
603
581
  atomKey: dependency.key
@@ -668,8 +646,44 @@ var registerSelector = (selectorKey, store) => ({
668
646
  find: (token, key) => findInStore(token, key, store)
669
647
  });
670
648
 
671
- // internal/src/selector/create-read-write-selector.ts
672
- 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) => {
673
687
  const target = newest(store);
674
688
  const subject = new Subject();
675
689
  const transactors = registerSelector(options.key, store);
@@ -702,7 +716,7 @@ var createReadWriteSelector = (options, family, store) => {
702
716
  };
703
717
  const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
704
718
  subject,
705
- install: (s) => createSelector(options, family, s),
719
+ install: (s) => createWritableSelector(options, family, s),
706
720
  get: getSelf,
707
721
  set: setSelf,
708
722
  type: `selector`
@@ -721,59 +735,13 @@ var createReadWriteSelector = (options, family, store) => {
721
735
  return token;
722
736
  };
723
737
 
724
- // internal/src/selector/create-readonly-selector.ts
725
- var createReadonlySelector = (options, family, store) => {
726
- const target = newest(store);
727
- const subject = new Subject();
728
- const { get, find } = registerSelector(options.key, store);
729
- const getSelf = () => {
730
- const value = options.get({ get, find });
731
- cacheValue(options.key, value, subject, store);
732
- return value;
733
- };
734
- const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
735
- subject,
736
- install: (s) => createSelector(options, family, s),
737
- get: getSelf,
738
- type: `readonly_selector`
739
- }), family && { family });
740
- target.readonlySelectors.set(options.key, readonlySelector);
741
- const initialValue = getSelf();
742
- store.logger.info(
743
- `\u2728`,
744
- readonlySelector.type,
745
- readonlySelector.key,
746
- `=`,
747
- initialValue
748
- );
749
- const token = {
750
- key: options.key,
751
- type: `readonly_selector`
752
- };
753
- if (family) {
754
- token.family = family;
755
- }
756
- store.on.selectorCreation.next(token);
757
- return token;
758
- };
759
-
760
- // internal/src/selector/create-selector.ts
761
- function createSelector(options, family, store) {
762
- const target = newest(store);
763
- const existingWritable = target.selectors.get(options.key);
764
- const existingReadonly = target.readonlySelectors.get(options.key);
765
- if (existingWritable || existingReadonly) {
766
- store.logger.error(
767
- `\u274C`,
768
- existingReadonly ? `readonly_selector` : `selector`,
769
- options.key,
770
- `Tried to create selector, but it already exists in the store.`
771
- );
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);
772
743
  }
773
- if (`set` in options) {
774
- return createReadWriteSelector(options, family, store);
775
- }
776
- return createReadonlySelector(options, family, store);
744
+ return createReadonlySelector(options, void 0, store);
777
745
  }
778
746
 
779
747
  // internal/src/selector/delete-selector.ts
@@ -815,44 +783,46 @@ var recallState = (state, store) => {
815
783
  };
816
784
 
817
785
  // internal/src/subscribe/subscribe-to-root-atoms.ts
818
- var subscribeToRootAtoms = (state, store) => {
786
+ var subscribeToRootAtoms = (selector, store) => {
819
787
  const target = newest(store);
820
- const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
821
- const atom = target.atoms.get(atomKey);
822
- if (atom === void 0) {
823
- throw new Error(
824
- `Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
825
- );
826
- }
827
- return atom.subject.subscribe(
828
- `${state.type}:${state.key}`,
829
- (atomChange) => {
830
- store.logger.info(
831
- `\u{1F4E2}`,
832
- state.type,
833
- state.key,
834
- `root`,
835
- atomKey,
836
- `went`,
837
- atomChange.oldValue,
838
- `->`,
839
- atomChange.newValue
840
- );
841
- const oldValue = recallState(state, target);
842
- const newValue = readOrComputeValue(state, target);
843
- store.logger.info(
844
- `\u2728`,
845
- state.type,
846
- state.key,
847
- `went`,
848
- oldValue,
849
- `->`,
850
- 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}".`
851
794
  );
852
- state.subject.next({ newValue, oldValue });
853
795
  }
854
- );
855
- });
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
+ );
856
826
  return dependencySubscriptions;
857
827
  };
858
828
 
@@ -867,7 +837,7 @@ function subscribeToState(token, handleUpdate, key, store) {
867
837
  }
868
838
  const unsubFunction = state.subject.subscribe(key, handleUpdate);
869
839
  store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
870
- const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
840
+ const dependencyUnsubFunctions = state.type !== `atom` && state.type !== `mutable_atom` ? subscribeToRootAtoms(state, store) : null;
871
841
  const unsubscribe = dependencyUnsubFunctions === null ? () => {
872
842
  store.logger.info(
873
843
  `\u{1F648}`,
@@ -1078,27 +1048,67 @@ var Tracker = class {
1078
1048
  // internal/src/mutable/create-mutable-atom.ts
1079
1049
  function createMutableAtom(options, family, store) {
1080
1050
  store.logger.info(
1081
- `\u{1F527}`,
1051
+ `\u{1F528}`,
1082
1052
  `atom`,
1083
1053
  options.key,
1084
1054
  `creating in store "${store.config.name}"`
1085
1055
  );
1086
- const coreState = createRegularAtom(options, family, store);
1087
- new Tracker(coreState, store);
1088
- const jsonState = selectJson(coreState, options, store);
1089
1056
  const target = newest(store);
1090
- subscribeToState(
1091
- jsonState,
1092
- () => {
1093
- const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
1094
- if (!trackerHasBeenInitialized) {
1095
- new Tracker(coreState, store);
1096
- }
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);
1097
1078
  },
1098
- `tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
1099
- store
1100
- );
1101
- 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;
1102
1112
  }
1103
1113
  function createRegularAtomFamily(options, store) {
1104
1114
  const subject = new Subject();
@@ -1119,16 +1129,7 @@ function createRegularAtomFamily(options, store) {
1119
1129
  if (options.effects) {
1120
1130
  individualOptions.effects = options.effects(key);
1121
1131
  }
1122
- if (`mutable` in options) {
1123
- const mutableOptions = __spreadProps(__spreadValues({}, individualOptions), {
1124
- mutable: true,
1125
- toJson: options.toJson,
1126
- fromJson: options.fromJson
1127
- });
1128
- token = createMutableAtom(mutableOptions, family, store);
1129
- } else {
1130
- token = createRegularAtom(individualOptions, family, store);
1131
- }
1132
+ token = createRegularAtom(individualOptions, family, store);
1132
1133
  subject.next(token);
1133
1134
  }
1134
1135
  return token;
@@ -1140,9 +1141,6 @@ function createRegularAtomFamily(options, store) {
1140
1141
  install: (store2) => createRegularAtomFamily(options, store2)
1141
1142
  }
1142
1143
  );
1143
- if (`mutable` in options && typeof options.mutable === `boolean`) {
1144
- Object.assign(atomFamily, { mutable: options.mutable });
1145
- }
1146
1144
  const target = newest(store);
1147
1145
  target.families.set(options.key, atomFamily);
1148
1146
  return atomFamily;
@@ -1150,7 +1148,8 @@ function createRegularAtomFamily(options, store) {
1150
1148
 
1151
1149
  // internal/src/families/create-atom-family.ts
1152
1150
  function createAtomFamily(options, store) {
1153
- if (`mutable` in options) {
1151
+ const isMutable2 = `mutable` in options;
1152
+ if (isMutable2) {
1154
1153
  return createMutableAtomFamily(options, store);
1155
1154
  }
1156
1155
  return createRegularAtomFamily(options, store);
@@ -1167,7 +1166,7 @@ function createReadonlySelectorFamily(options, store) {
1167
1166
  if (existing) {
1168
1167
  return deposit(existing);
1169
1168
  }
1170
- return createSelector(
1169
+ return createReadonlySelector(
1171
1170
  {
1172
1171
  key: fullKey,
1173
1172
  get: options.get(key)
@@ -1186,23 +1185,18 @@ function createReadonlySelectorFamily(options, store) {
1186
1185
  store.families.set(options.key, readonlySelectorFamily);
1187
1186
  return readonlySelectorFamily;
1188
1187
  }
1189
- function createSelectorFamily(options, store) {
1190
- const isReadonly = !(`set` in options);
1191
- if (isReadonly) {
1192
- return createReadonlySelectorFamily(options, store);
1193
- }
1194
- const target = newest(store);
1188
+ function createWritableSelectorFamily(options, store) {
1195
1189
  const subject = new Subject();
1196
1190
  const selectorFamily = Object.assign(
1197
1191
  (key) => {
1198
1192
  const subKey = stringifyJson(key);
1199
1193
  const family = { key: options.key, subKey };
1200
1194
  const fullKey = `${options.key}(${subKey})`;
1201
- const existing = target.selectors.get(fullKey);
1195
+ const existing = store.selectors.get(fullKey);
1202
1196
  if (existing) {
1203
1197
  return deposit(existing);
1204
1198
  }
1205
- const token = createSelector(
1199
+ const token = createWritableSelector(
1206
1200
  {
1207
1201
  key: fullKey,
1208
1202
  get: options.get(key),
@@ -1218,13 +1212,22 @@ function createSelectorFamily(options, store) {
1218
1212
  key: options.key,
1219
1213
  type: `selector_family`,
1220
1214
  subject,
1221
- install: (store2) => createSelectorFamily(options, store2)
1215
+ install: (store2) => createWritableSelectorFamily(options, store2)
1222
1216
  }
1223
1217
  );
1224
- target.families.set(options.key, selectorFamily);
1218
+ store.families.set(options.key, selectorFamily);
1225
1219
  return selectorFamily;
1226
1220
  }
1227
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
+
1228
1231
  // internal/src/mutable/tracker-family.ts
1229
1232
  var FamilyTracker = class {
1230
1233
  constructor(findMutableState, store) {
@@ -1261,29 +1264,63 @@ var FamilyTracker = class {
1261
1264
 
1262
1265
  // internal/src/mutable/create-mutable-atom-family.ts
1263
1266
  function createMutableAtomFamily(options, store) {
1264
- const coreFamily = Object.assign(
1265
- createRegularAtomFamily(options, store),
1266
- 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
+ }
1267
1301
  );
1268
- selectJsonFamily(coreFamily, options);
1269
- new FamilyTracker(coreFamily, store);
1270
- 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;
1271
1307
  }
1272
1308
 
1273
1309
  // internal/src/mutable/get-json-family.ts
1274
1310
  var getJsonFamily = (mutableAtomFamily, store) => {
1275
1311
  const target = newest(store);
1276
1312
  const key = `${mutableAtomFamily.key}:JSON`;
1277
- const jsonFamily = target.families.get(
1278
- key
1279
- );
1313
+ const jsonFamily = target.families.get(key);
1280
1314
  return jsonFamily;
1281
1315
  };
1282
1316
 
1283
1317
  // internal/src/mutable/get-json-token.ts
1284
1318
  var getJsonToken = (mutableAtomToken) => {
1285
1319
  const key = mutableAtomToken.family ? `${mutableAtomToken.family.key}:JSON(${mutableAtomToken.family.subKey})` : `${mutableAtomToken.key}:JSON`;
1286
- const jsonToken = { type: `selector`, key };
1320
+ const jsonToken = {
1321
+ type: `selector`,
1322
+ key
1323
+ };
1287
1324
  if (mutableAtomToken.family) {
1288
1325
  jsonToken.family = {
1289
1326
  key: `${mutableAtomToken.family.key}:JSON`,
@@ -1306,13 +1343,10 @@ var getUpdateToken = (mutableAtomToken) => {
1306
1343
  return updateToken;
1307
1344
  };
1308
1345
 
1309
- // internal/src/mutable/get-update-family.ts
1310
- var getUpdateFamily = (mutableAtomFamily, store) => {
1311
- const target = newest(store);
1312
- const key = `*${mutableAtomFamily.key}`;
1313
- const updateFamily = target.families.get(key);
1314
- return updateFamily;
1315
- };
1346
+ // internal/src/mutable/is-mutable.ts
1347
+ function isMutable(atomOrTokenOrFamily) {
1348
+ return atomOrTokenOrFamily.type === `mutable_atom` || atomOrTokenOrFamily.type === `mutable_atom_family`;
1349
+ }
1316
1350
 
1317
1351
  // internal/src/mutable/transceiver.ts
1318
1352
  function isTransceiver(value) {
@@ -1344,7 +1378,7 @@ function createRegularAtom(options, family, store) {
1344
1378
  );
1345
1379
  const target = newest(store);
1346
1380
  const existing = target.atoms.get(options.key);
1347
- if (existing) {
1381
+ if (existing && existing.type === `atom`) {
1348
1382
  store.logger.error(
1349
1383
  `\u274C`,
1350
1384
  `atom`,
@@ -1363,7 +1397,7 @@ function createRegularAtom(options, family, store) {
1363
1397
  options.key,
1364
1398
  `installing in store "${store2.config.name}"`
1365
1399
  );
1366
- return `mutable` in options ? createMutableAtom(options, family, store2) : createRegularAtom(options, family, store2);
1400
+ return createRegularAtom(options, family, store2);
1367
1401
  },
1368
1402
  subject
1369
1403
  });
@@ -1401,12 +1435,13 @@ function createRegularAtom(options, family, store) {
1401
1435
  return token;
1402
1436
  }
1403
1437
 
1404
- // internal/src/atom/create-atom.ts
1405
- function createAtom(options, family, store) {
1406
- if (`mutable` in options) {
1407
- 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);
1408
1443
  }
1409
- return createRegularAtom(options, family, store);
1444
+ return createRegularAtom(options, void 0, store);
1410
1445
  }
1411
1446
 
1412
1447
  // internal/src/atom/delete-atom.ts
@@ -1527,9 +1562,8 @@ var NotFoundError = class extends Error {
1527
1562
  // internal/src/timeline/add-atom-to-timeline.ts
1528
1563
  var addAtomToTimeline = (atomToken, tl, store) => {
1529
1564
  let maybeAtom = withdraw(atomToken, store);
1530
- if (maybeAtom == null ? void 0 : maybeAtom.mutable) {
1531
- console.log(`adding transceiver to timeline`, atomToken.key);
1532
- const updateToken = getUpdateToken(atomToken);
1565
+ if ((maybeAtom == null ? void 0 : maybeAtom.type) === `mutable_atom`) {
1566
+ const updateToken = getUpdateToken(maybeAtom);
1533
1567
  maybeAtom = withdraw(updateToken, store);
1534
1568
  }
1535
1569
  const atom = maybeAtom;
@@ -1538,6 +1572,7 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1538
1572
  `Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
1539
1573
  );
1540
1574
  }
1575
+ store.timelineAtoms.set({ atomKey: atom.key, timelineKey: tl.key });
1541
1576
  atom.subject.subscribe(`timeline`, (update) => {
1542
1577
  var _a, _b, _c, _d, _e, _f;
1543
1578
  const target = newest(store);
@@ -1722,14 +1757,6 @@ var addAtomToTimeline = (atomToken, tl, store) => {
1722
1757
  });
1723
1758
  };
1724
1759
 
1725
- // internal/src/mutable/is-mutable.ts
1726
- function isMutable(atomOrTokenOrFamily) {
1727
- if (`mutable` in atomOrTokenOrFamily) {
1728
- return atomOrTokenOrFamily.mutable;
1729
- }
1730
- return false;
1731
- }
1732
-
1733
1760
  // internal/src/timeline/create-timeline.ts
1734
1761
  function createTimeline(options, store, data) {
1735
1762
  var _a, _b;
@@ -1752,27 +1779,15 @@ function createTimeline(options, store, data) {
1752
1779
  const target = newest(store);
1753
1780
  for (const tokenOrFamily of options.atoms) {
1754
1781
  let atomKey = tokenOrFamily.key;
1755
- const existingTimelineKey = target.timelineAtoms.getRelatedKey(atomKey);
1756
- if (existingTimelineKey) {
1757
- store.logger.error(
1758
- `\u274C`,
1759
- `timeline`,
1760
- options.key,
1761
- `Failed to add atom "${atomKey}" because it already belongs to timeline "${existingTimelineKey}"`
1762
- );
1763
- continue;
1764
- }
1765
- if (tokenOrFamily.type === `atom_family`) {
1766
- let family = tokenOrFamily;
1767
- if (isMutable(family)) {
1768
- family = getUpdateFamily(family, store);
1769
- atomKey = family.key;
1770
- }
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 });
1771
1786
  family.subject.subscribe(`timeline:${options.key}`, (token2) => {
1772
1787
  addAtomToTimeline(token2, tl, store);
1773
1788
  });
1774
1789
  for (const atom of target.atoms.values()) {
1775
- if (((_b = atom.family) == null ? void 0 : _b.key) === family.key) {
1790
+ if (((_b = atom.family) == null ? void 0 : _b.key) === familyKey) {
1776
1791
  addAtomToTimeline(atom, tl, store);
1777
1792
  }
1778
1793
  }
@@ -1815,9 +1830,18 @@ function createTimeline(options, store, data) {
1815
1830
  continue;
1816
1831
  }
1817
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
+ }
1818
1843
  addAtomToTimeline(atom, tl, store);
1819
1844
  }
1820
- target.timelineAtoms.set({ atomKey, timelineKey });
1821
1845
  }
1822
1846
  store.timelines.set(options.key, tl);
1823
1847
  const token = {
@@ -2059,6 +2083,6 @@ function createTransaction(options, store) {
2059
2083
  // internal/src/transaction/index.ts
2060
2084
  var TRANSACTION_PHASES = [`idle`, `building`, `applying`];
2061
2085
 
2062
- 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 };
2063
2087
  //# sourceMappingURL=out.js.map
2064
2088
  //# sourceMappingURL=index.js.map