atom.io 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/dist/index.cjs +1473 -52
- package/data/dist/index.cjs.map +1 -1
- package/data/dist/index.d.cts +315 -16
- package/data/dist/index.js +10 -267
- package/data/dist/index.js.map +1 -1
- package/data/package.json +5 -5
- package/data/src/join.ts +3 -3
- package/dist/chunk-5VJ77LZE.js +176 -0
- package/dist/chunk-5VJ77LZE.js.map +1 -0
- package/dist/chunk-CK7GNCU5.js +309 -0
- package/dist/chunk-CK7GNCU5.js.map +1 -0
- package/dist/chunk-KW7RA7IM.js +67 -0
- package/dist/chunk-KW7RA7IM.js.map +1 -0
- package/dist/chunk-LFXB7Y6M.js +68 -0
- package/dist/chunk-LFXB7Y6M.js.map +1 -0
- package/dist/chunk-NYCVSXQB.js +181 -0
- package/dist/chunk-NYCVSXQB.js.map +1 -0
- package/dist/chunk-TE3ZSTQ6.js +2516 -0
- package/dist/chunk-TE3ZSTQ6.js.map +1 -0
- package/dist/index.cjs +2244 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +268 -21
- package/dist/index.js +1 -152
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/internal/dist/index.cjs +208 -79
- package/internal/dist/index.cjs.map +1 -1
- package/internal/dist/index.d.cts +226 -27
- package/internal/dist/index.js +1 -2240
- package/internal/dist/index.js.map +1 -1
- package/internal/package.json +5 -5
- package/introspection/dist/index.cjs +1618 -46
- package/introspection/dist/index.cjs.map +1 -1
- package/introspection/dist/index.d.cts +374 -4
- package/introspection/dist/index.js +2 -316
- package/introspection/dist/index.js.map +1 -1
- package/introspection/package.json +5 -5
- package/json/dist/index.cjs +1560 -8
- package/json/dist/index.cjs.map +1 -1
- package/json/dist/index.d.cts +371 -7
- package/json/dist/index.js +1 -76
- package/json/dist/index.js.map +1 -1
- package/json/package.json +5 -5
- package/package.json +5 -4
- package/react/dist/index.cjs +1604 -14
- package/react/dist/index.cjs.map +1 -1
- package/react/dist/index.d.cts +375 -4
- package/react/dist/index.js +2 -68
- package/react/dist/index.js.map +1 -1
- package/react/package.json +5 -5
- package/react-devtools/dist/index.cjs +2268 -198
- package/react-devtools/dist/index.cjs.map +1 -1
- package/react-devtools/dist/index.css +2 -2
- package/react-devtools/dist/index.d.cts +148 -24
- package/react-devtools/dist/index.js +57 -297
- package/react-devtools/dist/index.js.map +1 -1
- package/react-devtools/package.json +5 -5
- package/realtime-client/dist/index.cjs +1585 -35
- package/realtime-client/dist/index.cjs.map +1 -1
- package/realtime-client/dist/index.d.cts +382 -12
- package/realtime-client/dist/index.js +2 -165
- package/realtime-client/dist/index.js.map +1 -1
- package/realtime-client/package.json +5 -5
- package/realtime-react/dist/index.cjs +1782 -35
- package/realtime-react/dist/index.cjs.map +1 -1
- package/realtime-react/dist/index.d.cts +49 -9
- package/realtime-react/dist/index.js +4 -66
- package/realtime-react/dist/index.js.map +1 -1
- package/realtime-react/package.json +5 -5
- package/realtime-server/dist/index.cjs +1604 -58
- package/realtime-server/dist/index.cjs.map +1 -1
- package/realtime-server/dist/index.d.cts +391 -11
- package/realtime-server/dist/index.js +27 -21
- package/realtime-server/dist/index.js.map +1 -1
- package/realtime-server/package.json +6 -6
- package/realtime-testing/dist/index.cjs +2352 -16
- package/realtime-testing/dist/index.cjs.map +1 -1
- package/realtime-testing/dist/index.d.cts +486 -3
- package/realtime-testing/dist/index.js +10 -31
- package/realtime-testing/dist/index.js.map +1 -1
- package/realtime-testing/package.json +5 -5
- package/transceivers/set-rtx/dist/index.cjs +31 -10
- package/transceivers/set-rtx/dist/index.cjs.map +1 -1
- package/transceivers/set-rtx/dist/index.d.cts +29 -3
- package/transceivers/set-rtx/dist/index.js +2 -3
- package/transceivers/set-rtx/dist/index.js.map +1 -1
- package/transceivers/set-rtx/package.json +6 -6
- package/data/dist/index.d.ts +0 -158
- package/data/dist/metafile-cjs.json +0 -1
- package/data/dist/metafile-esm.json +0 -1
- package/dist/index.d.ts +0 -243
- package/dist/metafile-esm.json +0 -1
- package/internal/dist/index.d.ts +0 -421
- package/internal/dist/metafile-cjs.json +0 -1
- package/internal/dist/metafile-esm.json +0 -1
- package/introspection/dist/index.d.ts +0 -24
- package/introspection/dist/metafile-cjs.json +0 -1
- package/introspection/dist/metafile-esm.json +0 -1
- package/json/dist/index.d.ts +0 -51
- package/json/dist/metafile-cjs.json +0 -1
- package/json/dist/metafile-esm.json +0 -1
- package/react/dist/index.d.ts +0 -23
- package/react/dist/metafile-cjs.json +0 -1
- package/react/dist/metafile-esm.json +0 -1
- package/react-devtools/dist/index.d.ts +0 -341
- package/react-devtools/dist/metafile-cjs.json +0 -1
- package/react-devtools/dist/metafile-esm.json +0 -1
- package/realtime-client/dist/index.d.ts +0 -22
- package/realtime-client/dist/metafile-cjs.json +0 -1
- package/realtime-client/dist/metafile-esm.json +0 -1
- package/realtime-react/dist/index.d.ts +0 -27
- package/realtime-react/dist/metafile-cjs.json +0 -1
- package/realtime-react/dist/metafile-esm.json +0 -1
- package/realtime-server/dist/index.d.ts +0 -25
- package/realtime-server/dist/metafile-cjs.json +0 -1
- package/realtime-server/dist/metafile-esm.json +0 -1
- package/realtime-testing/dist/index.d.ts +0 -49
- package/realtime-testing/dist/metafile-cjs.json +0 -1
- package/realtime-testing/dist/metafile-esm.json +0 -1
- package/transceivers/set-rtx/dist/index.d.ts +0 -40
- package/transceivers/set-rtx/dist/metafile-cjs.json +0 -1
- package/transceivers/set-rtx/dist/metafile-esm.json +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,58 +1,2235 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
+
|
|
23
|
+
// internal/src/future.ts
|
|
24
|
+
var Future = class extends Promise {
|
|
25
|
+
constructor(executor) {
|
|
26
|
+
super((resolve, reject) => {
|
|
27
|
+
const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
|
|
28
|
+
const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
|
|
29
|
+
if (typeof executor === `function`) {
|
|
30
|
+
executor(pass, fail);
|
|
31
|
+
} else {
|
|
32
|
+
executor.then(pass, fail);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
this.isCanceled = false;
|
|
36
|
+
}
|
|
37
|
+
cancel() {
|
|
38
|
+
this.isCanceled = true;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// internal/src/lineage.ts
|
|
43
|
+
function newest(scion) {
|
|
44
|
+
while (scion.child !== null) {
|
|
45
|
+
scion = scion.child;
|
|
46
|
+
}
|
|
47
|
+
return scion;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// internal/src/caching.ts
|
|
51
|
+
function cacheValue(key, value, subject, store) {
|
|
52
|
+
const target = newest(store);
|
|
53
|
+
const currentValue = target.valueMap.get(key);
|
|
54
|
+
if (currentValue instanceof Future) {
|
|
55
|
+
currentValue.cancel();
|
|
56
|
+
}
|
|
57
|
+
if (value instanceof Promise) {
|
|
58
|
+
const future = new Future(value);
|
|
59
|
+
newest(store).valueMap.set(key, future);
|
|
60
|
+
future.then((resolved) => {
|
|
61
|
+
if (future.isCanceled) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
cacheValue(key, resolved, subject, store);
|
|
65
|
+
subject.next({ newValue: resolved, oldValue: future });
|
|
66
|
+
}).catch((thrown) => {
|
|
67
|
+
if (thrown !== `canceled`) {
|
|
68
|
+
store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return future;
|
|
72
|
+
}
|
|
73
|
+
target.valueMap.set(key, value);
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
var readCachedValue = (key, store) => {
|
|
77
|
+
return newest(store).valueMap.get(key);
|
|
78
|
+
};
|
|
79
|
+
var isValueCached = (key, store) => {
|
|
80
|
+
return newest(store).valueMap.has(key);
|
|
81
|
+
};
|
|
82
|
+
var evictCachedValue = (key, store) => {
|
|
83
|
+
const core = newest(store);
|
|
84
|
+
const currentValue = core.valueMap.get(key);
|
|
85
|
+
if (currentValue instanceof Future) {
|
|
86
|
+
currentValue.cancel();
|
|
87
|
+
}
|
|
88
|
+
if (core.operation.open) {
|
|
89
|
+
core.operation.prev.set(key, currentValue);
|
|
90
|
+
}
|
|
91
|
+
core.valueMap.delete(key);
|
|
92
|
+
store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// ../anvl/src/json/index.ts
|
|
96
|
+
var parseJson = (str) => JSON.parse(str);
|
|
97
|
+
var stringifyJson = (json) => JSON.stringify(json);
|
|
98
|
+
|
|
99
|
+
// json/src/select-json.ts
|
|
100
|
+
var selectJson = (atom2, transform, store = IMPLICIT.STORE) => {
|
|
101
|
+
return createSelector(
|
|
102
|
+
{
|
|
103
|
+
key: `${atom2.key}:JSON`,
|
|
104
|
+
get: ({ get }) => transform.toJson(get(atom2)),
|
|
105
|
+
set: ({ set }, newValue) => set(atom2, transform.fromJson(newValue))
|
|
106
|
+
},
|
|
107
|
+
void 0,
|
|
108
|
+
store
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// json/src/select-json-family.ts
|
|
113
|
+
var selectJsonFamily = (atomFamily2, transform, store = IMPLICIT.STORE) => {
|
|
114
|
+
const jsonFamily = createSelectorFamily(
|
|
115
|
+
{
|
|
116
|
+
key: `${atomFamily2.key}:JSON`,
|
|
117
|
+
get: (key) => ({ get }) => transform.toJson(get(atomFamily2(key))),
|
|
118
|
+
set: (key) => ({ set }, newValue) => set(atomFamily2(key), transform.fromJson(newValue))
|
|
119
|
+
},
|
|
120
|
+
store
|
|
121
|
+
);
|
|
122
|
+
atomFamily2.subject.subscribe(
|
|
123
|
+
`store=${store.config.name}::json-selector-family`,
|
|
124
|
+
(token) => {
|
|
125
|
+
if (token.family) {
|
|
126
|
+
jsonFamily(parseJson(token.family.subKey));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
return jsonFamily;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// internal/src/read-or-compute-value.ts
|
|
134
|
+
var readOrComputeValue = (state, store) => {
|
|
135
|
+
if (isValueCached(state.key, store)) {
|
|
136
|
+
store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
|
|
137
|
+
return readCachedValue(state.key, store);
|
|
138
|
+
}
|
|
139
|
+
if (state.type !== `atom`) {
|
|
140
|
+
store.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
|
|
141
|
+
return state.get();
|
|
142
|
+
}
|
|
143
|
+
const fallback = state.default instanceof Function ? state.default() : state.default;
|
|
144
|
+
store.logger.info(
|
|
145
|
+
`\u{1F481}`,
|
|
146
|
+
`atom`,
|
|
147
|
+
state.key,
|
|
148
|
+
`could not find cached value; using default`,
|
|
149
|
+
fallback
|
|
150
|
+
);
|
|
151
|
+
return state.default instanceof Function ? state.default() : state.default;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// internal/src/operation.ts
|
|
155
|
+
var openOperation = (token, store) => {
|
|
156
|
+
const target = newest(store);
|
|
157
|
+
if (target.operation.open) {
|
|
158
|
+
store.logger.error(
|
|
159
|
+
`\u274C`,
|
|
160
|
+
token.type,
|
|
161
|
+
token.key,
|
|
162
|
+
`failed to setState during a setState for "${target.operation.token.key}"`
|
|
163
|
+
);
|
|
164
|
+
return `rejection`;
|
|
165
|
+
}
|
|
166
|
+
target.operation = {
|
|
167
|
+
open: true,
|
|
168
|
+
done: /* @__PURE__ */ new Set(),
|
|
169
|
+
prev: /* @__PURE__ */ new Map(),
|
|
170
|
+
time: Date.now(),
|
|
171
|
+
token
|
|
172
|
+
};
|
|
173
|
+
store.logger.info(
|
|
174
|
+
`\u2B55`,
|
|
175
|
+
token.type,
|
|
176
|
+
token.key,
|
|
177
|
+
`operation start in store "${store.config.name}"${target.transactionMeta === null ? `` : ` ${target.transactionMeta.phase} "${target.transactionMeta.update.key}"`}`
|
|
178
|
+
);
|
|
179
|
+
};
|
|
180
|
+
var closeOperation = (store) => {
|
|
181
|
+
const target = newest(store);
|
|
182
|
+
if (target.operation.open) {
|
|
183
|
+
store.logger.info(
|
|
184
|
+
`\u{1F534}`,
|
|
185
|
+
target.operation.token.type,
|
|
186
|
+
target.operation.token.key,
|
|
187
|
+
`operation done in store "${store.config.name}"`
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
target.operation = { open: false };
|
|
191
|
+
store.subject.operationStatus.next(target.operation);
|
|
192
|
+
};
|
|
193
|
+
var isDone = (key, store) => {
|
|
194
|
+
const target = newest(store);
|
|
195
|
+
if (!target.operation.open) {
|
|
196
|
+
store.logger.warn(
|
|
197
|
+
`\u{1F41E}`,
|
|
198
|
+
`unknown`,
|
|
199
|
+
key,
|
|
200
|
+
`isDone called outside of an operation. This is probably a bug.`
|
|
201
|
+
);
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
return target.operation.done.has(key);
|
|
205
|
+
};
|
|
206
|
+
var markDone = (key, store) => {
|
|
207
|
+
const target = newest(store);
|
|
208
|
+
if (!target.operation.open) {
|
|
209
|
+
store.logger.warn(
|
|
210
|
+
`\u{1F41E}`,
|
|
211
|
+
`unknown`,
|
|
212
|
+
key,
|
|
213
|
+
`markDone called outside of an operation. This is probably a bug.`
|
|
214
|
+
);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
target.operation.done.add(key);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// internal/src/set-state/become.ts
|
|
221
|
+
var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
|
|
222
|
+
originalThing instanceof Function ? originalThing() : originalThing
|
|
223
|
+
) : nextVersionOfThing;
|
|
224
|
+
|
|
225
|
+
// internal/src/subject.ts
|
|
226
|
+
var Subject = class {
|
|
227
|
+
constructor() {
|
|
228
|
+
this.subscribers = /* @__PURE__ */ new Map();
|
|
229
|
+
}
|
|
230
|
+
subscribe(key, subscriber) {
|
|
231
|
+
this.subscribers.set(key, subscriber);
|
|
232
|
+
const unsubscribe = () => this.unsubscribe(key);
|
|
233
|
+
return unsubscribe;
|
|
234
|
+
}
|
|
235
|
+
unsubscribe(key) {
|
|
236
|
+
this.subscribers.delete(key);
|
|
237
|
+
}
|
|
238
|
+
next(value) {
|
|
239
|
+
for (const subscriber of this.subscribers.values()) {
|
|
240
|
+
subscriber(value);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
var StatefulSubject = class extends Subject {
|
|
245
|
+
constructor(initialState) {
|
|
246
|
+
super();
|
|
247
|
+
this.state = initialState;
|
|
248
|
+
}
|
|
249
|
+
next(value) {
|
|
250
|
+
this.state = value;
|
|
251
|
+
super.next(value);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// internal/src/set-state/copy-mutable-if-needed.ts
|
|
256
|
+
function copyMutableIfNeeded(atom2, transform, origin, target) {
|
|
257
|
+
const originValue = origin.valueMap.get(atom2.key);
|
|
258
|
+
const targetValue = target.valueMap.get(atom2.key);
|
|
259
|
+
if (originValue === targetValue) {
|
|
260
|
+
origin.logger.info(`\u{1F4C3}`, `atom`, `${atom2.key}`, `copying`);
|
|
261
|
+
const jsonValue = transform.toJson(originValue);
|
|
262
|
+
const copiedValue = transform.fromJson(jsonValue);
|
|
263
|
+
target.valueMap.set(atom2.key, copiedValue);
|
|
264
|
+
new Tracker(atom2, origin);
|
|
265
|
+
return copiedValue;
|
|
266
|
+
}
|
|
267
|
+
return targetValue;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// internal/src/set-state/copy-mutable-in-transaction.ts
|
|
271
|
+
function copyMutableIfWithinTransaction(oldValue, atom2, store) {
|
|
272
|
+
const target = newest(store);
|
|
273
|
+
const parent = target.parent;
|
|
274
|
+
if (parent !== null) {
|
|
275
|
+
if (`toJson` in atom2 && `fromJson` in atom2) {
|
|
276
|
+
const copiedValue = copyMutableIfNeeded(atom2, atom2, parent, target);
|
|
277
|
+
return copiedValue;
|
|
278
|
+
}
|
|
279
|
+
if (`family` in atom2) {
|
|
280
|
+
const family = parent.families.get(atom2.family.key);
|
|
281
|
+
if (family && family.type === `atom_family`) {
|
|
282
|
+
const result = copyMutableFamilyMemberWithinTransaction(
|
|
283
|
+
atom2,
|
|
284
|
+
family,
|
|
285
|
+
parent,
|
|
286
|
+
target
|
|
287
|
+
);
|
|
288
|
+
if (result) {
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return oldValue;
|
|
295
|
+
}
|
|
296
|
+
function copyMutableFamilyMemberWithinTransaction(atom2, family, origin, target) {
|
|
297
|
+
if (`toJson` in family && `fromJson` in family) {
|
|
298
|
+
const copyCreated = copyMutableIfNeeded(atom2, family, origin, target);
|
|
299
|
+
return copyCreated;
|
|
300
|
+
}
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// internal/src/set-state/emit-update.ts
|
|
305
|
+
var emitUpdate = (state, update, store) => {
|
|
306
|
+
store.logger.info(
|
|
307
|
+
`\u{1F4E2}`,
|
|
308
|
+
state.type,
|
|
309
|
+
state.key,
|
|
310
|
+
`went (`,
|
|
311
|
+
update.oldValue,
|
|
312
|
+
`->`,
|
|
313
|
+
update.newValue,
|
|
314
|
+
`) subscribers:`,
|
|
315
|
+
state.subject.subscribers
|
|
316
|
+
);
|
|
317
|
+
state.subject.next(update);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// internal/src/set-state/evict-downstream.ts
|
|
321
|
+
var evictDownStream = (atom2, store) => {
|
|
322
|
+
const target = newest(store);
|
|
323
|
+
const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom2.key);
|
|
324
|
+
store.logger.info(
|
|
325
|
+
`\u{1F9F9}`,
|
|
326
|
+
atom2.type,
|
|
327
|
+
atom2.key,
|
|
328
|
+
downstreamKeys ? `evicting ${downstreamKeys.size} states downstream:` : `no downstream states`,
|
|
329
|
+
downstreamKeys != null ? downstreamKeys : `to evict`
|
|
330
|
+
);
|
|
331
|
+
if (downstreamKeys) {
|
|
332
|
+
if (target.operation.open) {
|
|
333
|
+
store.logger.info(
|
|
334
|
+
`\u{1F9F9}`,
|
|
335
|
+
atom2.type,
|
|
336
|
+
atom2.key,
|
|
337
|
+
`[ ${[...target.operation.done].join(`, `)} ] already done`
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
for (const key of downstreamKeys) {
|
|
341
|
+
if (isDone(key, store)) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
evictCachedValue(key, store);
|
|
345
|
+
markDone(key, store);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// internal/src/set-state/stow-update.ts
|
|
351
|
+
function shouldUpdateBeStowed(key, update) {
|
|
352
|
+
if (isTransceiver(update.newValue)) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
if (key.includes(`\u{1F441}\u200D\u{1F5E8}`)) {
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
var stowUpdate = (state, update, store) => {
|
|
361
|
+
const { key } = state;
|
|
362
|
+
const target = newest(store);
|
|
363
|
+
if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
|
|
364
|
+
store.logger.error(
|
|
365
|
+
`\u{1F41E}`,
|
|
366
|
+
`atom`,
|
|
367
|
+
key,
|
|
368
|
+
`stowUpdate called outside of a transaction. This is probably a bug.`
|
|
369
|
+
);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const shouldStow = shouldUpdateBeStowed(key, update);
|
|
373
|
+
if (!shouldStow) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const atomUpdate = __spreadValues({ key }, update);
|
|
377
|
+
if (state.family) {
|
|
378
|
+
atomUpdate.family = state.family;
|
|
379
|
+
}
|
|
380
|
+
target.transactionMeta.update.updates.push(atomUpdate);
|
|
381
|
+
store.logger.info(
|
|
382
|
+
`\u{1F4C1}`,
|
|
383
|
+
`atom`,
|
|
384
|
+
key,
|
|
385
|
+
`stowed (`,
|
|
386
|
+
update.oldValue,
|
|
387
|
+
`->`,
|
|
388
|
+
update.newValue,
|
|
389
|
+
`)`
|
|
390
|
+
);
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// internal/src/set-state/set-atom.ts
|
|
394
|
+
var setAtom = (atom2, next, store) => {
|
|
395
|
+
const target = newest(store);
|
|
396
|
+
const oldValue = readOrComputeValue(atom2, store);
|
|
397
|
+
let newValue = copyMutableIfWithinTransaction(oldValue, atom2, store);
|
|
398
|
+
newValue = become(next)(newValue);
|
|
399
|
+
store.logger.info(`\u{1F4DD}`, `atom`, atom2.key, `set to`, newValue);
|
|
400
|
+
newValue = cacheValue(atom2.key, newValue, atom2.subject, store);
|
|
401
|
+
if (isAtomDefault(atom2.key, store)) {
|
|
402
|
+
markAtomAsNotDefault(atom2.key, store);
|
|
403
|
+
}
|
|
404
|
+
markDone(atom2.key, store);
|
|
405
|
+
evictDownStream(atom2, store);
|
|
406
|
+
const update = { oldValue, newValue };
|
|
407
|
+
if (target.transactionMeta === null || target.transactionMeta.phase === `applying`) {
|
|
408
|
+
emitUpdate(atom2, update, store);
|
|
409
|
+
} else {
|
|
410
|
+
stowUpdate(atom2, update, store);
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
// internal/src/set-state/set-atom-or-selector.ts
|
|
415
|
+
var setAtomOrSelector = (state, value, store) => {
|
|
416
|
+
if (state.type === `selector`) {
|
|
417
|
+
state.set(value);
|
|
418
|
+
} else {
|
|
419
|
+
setAtom(state, value, store);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
// internal/src/store/deposit.ts
|
|
424
|
+
function deposit(state) {
|
|
425
|
+
const token = {
|
|
426
|
+
key: state.key,
|
|
427
|
+
type: state.type
|
|
428
|
+
};
|
|
429
|
+
if (`family` in state) {
|
|
430
|
+
token.family = state.family;
|
|
431
|
+
}
|
|
432
|
+
return token;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// ../rel8/junction/src/junction.ts
|
|
436
|
+
var Junction = class {
|
|
437
|
+
constructor(data, config) {
|
|
438
|
+
this.relations = /* @__PURE__ */ new Map();
|
|
439
|
+
this.contents = /* @__PURE__ */ new Map();
|
|
440
|
+
this.makeContentKey = (a, b) => `${a}:${b}`;
|
|
441
|
+
var _a, _b, _c, _d;
|
|
442
|
+
this.a = data.between[0];
|
|
443
|
+
this.b = data.between[1];
|
|
444
|
+
this.cardinality = data.cardinality;
|
|
445
|
+
if (!(config == null ? void 0 : config.externalStore)) {
|
|
446
|
+
this.relations = new Map((_a = data.relations) == null ? void 0 : _a.map(([a, b]) => [a, new Set(b)]));
|
|
447
|
+
this.contents = new Map(data.contents);
|
|
448
|
+
}
|
|
449
|
+
this.isContent = (_b = config == null ? void 0 : config.isContent) != null ? _b : null;
|
|
450
|
+
if (config == null ? void 0 : config.makeContentKey) {
|
|
451
|
+
this.makeContentKey = config.makeContentKey;
|
|
452
|
+
}
|
|
453
|
+
if (config == null ? void 0 : config.externalStore) {
|
|
454
|
+
const externalStore = config.externalStore;
|
|
455
|
+
this.has = (a, b) => externalStore.has(a, b);
|
|
456
|
+
this.addRelation = (a, b) => {
|
|
457
|
+
externalStore.addRelation(a, b);
|
|
458
|
+
};
|
|
459
|
+
this.deleteRelation = (a, b) => {
|
|
460
|
+
externalStore.deleteRelation(a, b);
|
|
461
|
+
};
|
|
462
|
+
this.replaceRelationsSafely = (a, bs) => {
|
|
463
|
+
externalStore.replaceRelationsSafely(a, bs);
|
|
464
|
+
};
|
|
465
|
+
this.replaceRelationsUnsafely = (a, bs) => {
|
|
466
|
+
externalStore.replaceRelationsUnsafely(a, bs);
|
|
467
|
+
};
|
|
468
|
+
this.getRelatedKeys = (key) => externalStore.getRelatedKeys(key);
|
|
469
|
+
if (externalStore.getContent) {
|
|
470
|
+
this.getContentInternal = (contentKey) => {
|
|
471
|
+
return externalStore.getContent(contentKey);
|
|
472
|
+
};
|
|
473
|
+
this.setContent = (contentKey, content) => {
|
|
474
|
+
externalStore.setContent(contentKey, content);
|
|
475
|
+
};
|
|
476
|
+
this.deleteContent = (contentKey) => {
|
|
477
|
+
externalStore.deleteContent(contentKey);
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
for (const [x, ys] of (_c = data.relations) != null ? _c : []) {
|
|
481
|
+
for (const y of ys)
|
|
482
|
+
this.addRelation(x, y);
|
|
483
|
+
}
|
|
484
|
+
for (const [contentKey, content] of (_d = data.contents) != null ? _d : []) {
|
|
485
|
+
this.setContent(contentKey, content);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
getRelatedKeys(key) {
|
|
490
|
+
return this.relations.get(key);
|
|
491
|
+
}
|
|
492
|
+
addRelation(a, b) {
|
|
493
|
+
let aRelations = this.relations.get(a);
|
|
494
|
+
let bRelations = this.relations.get(b);
|
|
495
|
+
if (aRelations) {
|
|
496
|
+
aRelations.add(b);
|
|
497
|
+
} else {
|
|
498
|
+
aRelations = /* @__PURE__ */ new Set([b]);
|
|
499
|
+
this.relations.set(a, aRelations);
|
|
500
|
+
}
|
|
501
|
+
if (bRelations) {
|
|
502
|
+
bRelations.add(a);
|
|
503
|
+
} else {
|
|
504
|
+
bRelations = /* @__PURE__ */ new Set([a]);
|
|
505
|
+
this.relations.set(b, bRelations);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
deleteRelation(a, b) {
|
|
509
|
+
const aRelations = this.relations.get(a);
|
|
510
|
+
if (aRelations) {
|
|
511
|
+
aRelations.delete(b);
|
|
512
|
+
if (aRelations.size === 0) {
|
|
513
|
+
this.relations.delete(a);
|
|
514
|
+
}
|
|
515
|
+
const bRelations = this.relations.get(b);
|
|
516
|
+
if (bRelations) {
|
|
517
|
+
bRelations.delete(a);
|
|
518
|
+
if (bRelations.size === 0) {
|
|
519
|
+
this.relations.delete(b);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
replaceRelationsUnsafely(a, bs) {
|
|
525
|
+
this.relations.set(a, new Set(bs));
|
|
526
|
+
for (const b of bs) {
|
|
527
|
+
const bRelations = /* @__PURE__ */ new Set([a]);
|
|
528
|
+
this.relations.set(b, bRelations);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
replaceRelationsSafely(a, bs) {
|
|
532
|
+
const aRelationsPrev = this.relations.get(a);
|
|
533
|
+
if (aRelationsPrev) {
|
|
534
|
+
for (const b of aRelationsPrev) {
|
|
535
|
+
const bRelations = this.relations.get(b);
|
|
536
|
+
if (bRelations) {
|
|
537
|
+
if (bRelations.size === 1) {
|
|
538
|
+
this.relations.delete(b);
|
|
539
|
+
} else {
|
|
540
|
+
bRelations.delete(a);
|
|
541
|
+
}
|
|
542
|
+
this.contents.delete(this.makeContentKey(a, b));
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
this.relations.set(a, new Set(bs));
|
|
547
|
+
for (const b of bs) {
|
|
548
|
+
let bRelations = this.relations.get(b);
|
|
549
|
+
if (bRelations) {
|
|
550
|
+
bRelations.add(a);
|
|
551
|
+
} else {
|
|
552
|
+
bRelations = /* @__PURE__ */ new Set([a]);
|
|
553
|
+
this.relations.set(b, bRelations);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
getContentInternal(contentKey) {
|
|
558
|
+
return this.contents.get(contentKey);
|
|
559
|
+
}
|
|
560
|
+
setContent(contentKey, content) {
|
|
561
|
+
this.contents.set(contentKey, content);
|
|
562
|
+
}
|
|
563
|
+
deleteContent(contentKey) {
|
|
564
|
+
this.contents.delete(contentKey);
|
|
565
|
+
}
|
|
566
|
+
toJSON() {
|
|
567
|
+
return {
|
|
568
|
+
between: [this.a, this.b],
|
|
569
|
+
cardinality: this.cardinality,
|
|
570
|
+
relations: [...this.relations.entries()].map(([a, b]) => [a, [...b]]),
|
|
571
|
+
contents: [...this.contents.entries()]
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
set(a, ...rest) {
|
|
575
|
+
var _a;
|
|
576
|
+
const b = typeof rest[0] === `string` ? rest[0] : a[this.b];
|
|
577
|
+
const content = ((_a = rest[1]) != null ? _a : typeof rest[0] === `string`) ? void 0 : rest[0];
|
|
578
|
+
a = typeof a === `string` ? a : a[this.a];
|
|
579
|
+
switch (this.cardinality) {
|
|
580
|
+
case `1:1`: {
|
|
581
|
+
const bPrev = this.getRelatedKey(a);
|
|
582
|
+
if (bPrev && bPrev !== b)
|
|
583
|
+
this.delete(bPrev, a);
|
|
584
|
+
}
|
|
585
|
+
case `1:n`: {
|
|
586
|
+
const aPrev = this.getRelatedKey(b);
|
|
587
|
+
if (aPrev && aPrev !== a)
|
|
588
|
+
this.delete(aPrev, b);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (content) {
|
|
592
|
+
const contentKey = this.makeContentKey(a, b);
|
|
593
|
+
this.setContent(contentKey, content);
|
|
594
|
+
}
|
|
595
|
+
this.addRelation(a, b);
|
|
596
|
+
return this;
|
|
597
|
+
}
|
|
598
|
+
delete(x, b) {
|
|
599
|
+
b = typeof b === `string` ? b : x[this.b];
|
|
600
|
+
const a = typeof x === `string` ? x : x[this.a];
|
|
601
|
+
if (a === void 0 && typeof b === `string`) {
|
|
602
|
+
const bRelations = this.getRelatedKeys(b);
|
|
603
|
+
if (bRelations) {
|
|
604
|
+
for (const a2 of bRelations) {
|
|
605
|
+
this.delete(a2, b);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
if (typeof a === `string` && b === void 0) {
|
|
610
|
+
const aRelations = this.getRelatedKeys(a);
|
|
611
|
+
if (aRelations) {
|
|
612
|
+
for (const b2 of aRelations) {
|
|
613
|
+
this.delete(a, b2);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
if (typeof a === `string` && typeof b === `string`) {
|
|
618
|
+
this.deleteRelation(a, b);
|
|
619
|
+
const contentKey = this.makeContentKey(a, b);
|
|
620
|
+
this.deleteContent(contentKey);
|
|
621
|
+
}
|
|
622
|
+
return this;
|
|
623
|
+
}
|
|
624
|
+
getRelatedKey(key) {
|
|
625
|
+
const relations = this.getRelatedKeys(key);
|
|
626
|
+
if (relations) {
|
|
627
|
+
if (relations.size > 1) {
|
|
628
|
+
console.warn(
|
|
629
|
+
`${relations.size} related keys were found for key "${key}": (${[
|
|
630
|
+
...relations
|
|
631
|
+
].map((k) => `"${k}"`).join(`, `)}). Only one related key was expected.`
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
for (const relation of relations) {
|
|
635
|
+
return relation;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
replaceRelations(a, relations, config) {
|
|
640
|
+
const hasContent = !Array.isArray(relations);
|
|
641
|
+
const bs = hasContent ? Object.keys(relations) : relations;
|
|
642
|
+
if (config == null ? void 0 : config.reckless) {
|
|
643
|
+
this.replaceRelationsUnsafely(a, bs);
|
|
644
|
+
} else {
|
|
645
|
+
this.replaceRelationsSafely(a, bs);
|
|
646
|
+
}
|
|
647
|
+
if (hasContent) {
|
|
648
|
+
for (const b of bs) {
|
|
649
|
+
const contentKey = this.makeContentKey(a, b);
|
|
650
|
+
const content = relations[b];
|
|
651
|
+
this.setContent(contentKey, content);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return this;
|
|
655
|
+
}
|
|
656
|
+
getContent(a, b) {
|
|
657
|
+
const contentKey = this.makeContentKey(a, b);
|
|
658
|
+
return this.getContentInternal(contentKey);
|
|
659
|
+
}
|
|
660
|
+
getRelationEntries(input) {
|
|
661
|
+
const a = input[this.a];
|
|
662
|
+
const b = input[this.b];
|
|
663
|
+
if (a !== void 0 && b === void 0) {
|
|
664
|
+
const aRelations = this.getRelatedKeys(a);
|
|
665
|
+
if (aRelations) {
|
|
666
|
+
return [...aRelations].map((b2) => {
|
|
667
|
+
var _a;
|
|
668
|
+
return [b2, (_a = this.getContent(a, b2)) != null ? _a : null];
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
if (a === void 0 && b !== void 0) {
|
|
673
|
+
const bRelations = this.getRelatedKeys(b);
|
|
674
|
+
if (bRelations) {
|
|
675
|
+
return [...bRelations].map((a2) => {
|
|
676
|
+
var _a;
|
|
677
|
+
return [a2, (_a = this.getContent(a2, b)) != null ? _a : null];
|
|
15
678
|
});
|
|
16
679
|
}
|
|
680
|
+
}
|
|
681
|
+
return [];
|
|
682
|
+
}
|
|
683
|
+
has(a, b) {
|
|
684
|
+
var _a;
|
|
685
|
+
if (b) {
|
|
686
|
+
const setA = this.getRelatedKeys(a);
|
|
687
|
+
return (_a = setA == null ? void 0 : setA.has(b)) != null ? _a : false;
|
|
688
|
+
}
|
|
689
|
+
return this.relations.has(a);
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
// internal/src/store/store.ts
|
|
694
|
+
var Store = class {
|
|
695
|
+
constructor(name, store = null) {
|
|
696
|
+
this.parent = null;
|
|
697
|
+
this.child = null;
|
|
698
|
+
this.valueMap = /* @__PURE__ */ new Map();
|
|
699
|
+
this.atoms = /* @__PURE__ */ new Map();
|
|
700
|
+
this.selectors = /* @__PURE__ */ new Map();
|
|
701
|
+
this.readonlySelectors = /* @__PURE__ */ new Map();
|
|
702
|
+
this.trackers = /* @__PURE__ */ new Map();
|
|
703
|
+
this.families = /* @__PURE__ */ new Map();
|
|
704
|
+
this.timelines = /* @__PURE__ */ new Map();
|
|
705
|
+
this.transactions = /* @__PURE__ */ new Map();
|
|
706
|
+
this.atomsThatAreDefault = /* @__PURE__ */ new Set();
|
|
707
|
+
this.timelineAtoms = new Junction({
|
|
708
|
+
between: [`timelineKey`, `atomKey`],
|
|
709
|
+
cardinality: `1:n`
|
|
710
|
+
});
|
|
711
|
+
this.selectorAtoms = new Junction({
|
|
712
|
+
between: [`selectorKey`, `atomKey`],
|
|
713
|
+
cardinality: `n:n`
|
|
17
714
|
});
|
|
715
|
+
this.selectorGraph = new Junction(
|
|
716
|
+
{
|
|
717
|
+
between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
|
|
718
|
+
cardinality: `n:n`
|
|
719
|
+
},
|
|
720
|
+
{
|
|
721
|
+
makeContentKey: (...keys) => keys.sort().join(`:`)
|
|
722
|
+
}
|
|
723
|
+
);
|
|
724
|
+
this.subject = {
|
|
725
|
+
atomCreation: new Subject(),
|
|
726
|
+
selectorCreation: new Subject(),
|
|
727
|
+
transactionCreation: new Subject(),
|
|
728
|
+
timelineCreation: new Subject(),
|
|
729
|
+
transactionApplying: new StatefulSubject(null),
|
|
730
|
+
operationStatus: new Subject()
|
|
731
|
+
};
|
|
732
|
+
this.operation = { open: false };
|
|
733
|
+
this.transactionMeta = null;
|
|
734
|
+
this.config = {
|
|
735
|
+
name: `IMPLICIT_STORE`
|
|
736
|
+
};
|
|
737
|
+
this.loggers = [
|
|
738
|
+
new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
|
|
739
|
+
];
|
|
740
|
+
this.logger = {
|
|
741
|
+
error: (...messages) => {
|
|
742
|
+
for (const logger of this.loggers)
|
|
743
|
+
logger.error(...messages);
|
|
744
|
+
},
|
|
745
|
+
info: (...messages) => {
|
|
746
|
+
for (const logger of this.loggers)
|
|
747
|
+
logger.info(...messages);
|
|
748
|
+
},
|
|
749
|
+
warn: (...messages) => {
|
|
750
|
+
for (const logger of this.loggers)
|
|
751
|
+
logger.warn(...messages);
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
if (store !== null) {
|
|
755
|
+
this.valueMap = new Map(store == null ? void 0 : store.valueMap);
|
|
756
|
+
this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
|
|
757
|
+
this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
|
|
758
|
+
this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
|
|
759
|
+
name
|
|
760
|
+
});
|
|
761
|
+
for (const [, atom2] of store.atoms) {
|
|
762
|
+
atom2.install(this);
|
|
763
|
+
}
|
|
764
|
+
for (const [, selector2] of store.readonlySelectors) {
|
|
765
|
+
selector2.install(this);
|
|
766
|
+
}
|
|
767
|
+
for (const [, selector2] of store.selectors) {
|
|
768
|
+
selector2.install(this);
|
|
769
|
+
}
|
|
770
|
+
for (const [, tx] of store.transactions) {
|
|
771
|
+
tx.install(this);
|
|
772
|
+
}
|
|
773
|
+
for (const [, timeline2] of store.timelines) {
|
|
774
|
+
timeline2.install(this);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
var IMPLICIT = {
|
|
780
|
+
STORE_INTERNAL: void 0,
|
|
781
|
+
get STORE() {
|
|
782
|
+
var _a;
|
|
783
|
+
return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
|
|
18
784
|
}
|
|
19
|
-
|
|
20
|
-
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
// internal/src/store/withdraw.ts
|
|
788
|
+
function withdraw(token, store) {
|
|
789
|
+
var _a, _b, _c, _d;
|
|
790
|
+
const target = newest(store);
|
|
791
|
+
const state = (_d = (_c = (_b = (_a = target.atoms.get(token.key)) != null ? _a : target.selectors.get(token.key)) != null ? _b : target.readonlySelectors.get(token.key)) != null ? _c : target.transactions.get(token.key)) != null ? _d : target.timelines.get(token.key);
|
|
792
|
+
if (state) {
|
|
793
|
+
return state;
|
|
794
|
+
}
|
|
795
|
+
return void 0;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// internal/src/store/withdraw-new-family-member.ts
|
|
799
|
+
function withdrawNewFamilyMember(token, store) {
|
|
800
|
+
if (token.family) {
|
|
801
|
+
store.logger.info(
|
|
802
|
+
`\u{1F46A}`,
|
|
803
|
+
token.type,
|
|
804
|
+
token.key,
|
|
805
|
+
`creating new family member in store "${store.config.name}"`
|
|
806
|
+
);
|
|
807
|
+
const target = newest(store);
|
|
808
|
+
const family = target.families.get(token.family.key);
|
|
809
|
+
if (family) {
|
|
810
|
+
const jsonSubKey = JSON.parse(token.family.subKey);
|
|
811
|
+
family(jsonSubKey);
|
|
812
|
+
const state = withdraw(token, store);
|
|
813
|
+
return state;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
return void 0;
|
|
21
817
|
}
|
|
22
818
|
|
|
23
|
-
|
|
819
|
+
// internal/src/keys.ts
|
|
820
|
+
var isAtomKey = (key, store) => newest(store).atoms.has(key);
|
|
821
|
+
var isSelectorKey = (key, store) => newest(store).selectors.has(key);
|
|
822
|
+
var isReadonlySelectorKey = (key, store) => newest(store).readonlySelectors.has(key);
|
|
823
|
+
var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
|
|
824
|
+
|
|
825
|
+
// internal/src/selector/get-selector-dependency-keys.ts
|
|
826
|
+
var getSelectorDependencyKeys = (key, store) => {
|
|
827
|
+
const sources = newest(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
|
|
828
|
+
return sources;
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
// internal/src/selector/trace-selector-atoms.ts
|
|
832
|
+
var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
|
|
833
|
+
const rootKeys = [];
|
|
834
|
+
const indirectDependencyKeys = getSelectorDependencyKeys(
|
|
835
|
+
directDependencyKey,
|
|
836
|
+
store
|
|
837
|
+
);
|
|
838
|
+
let depth = 0;
|
|
839
|
+
while (indirectDependencyKeys.length > 0) {
|
|
840
|
+
const indirectDependencyKey = indirectDependencyKeys.shift();
|
|
841
|
+
++depth;
|
|
842
|
+
if (depth > 99999) {
|
|
843
|
+
throw new Error(
|
|
844
|
+
`Maximum selector dependency depth exceeded (> 99999) in selector "${selectorKey}". This is likely due to a circular dependency.`
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
if (!isAtomKey(indirectDependencyKey, store)) {
|
|
848
|
+
indirectDependencyKeys.push(
|
|
849
|
+
...getSelectorDependencyKeys(indirectDependencyKey, store)
|
|
850
|
+
);
|
|
851
|
+
} else if (!rootKeys.includes(indirectDependencyKey)) {
|
|
852
|
+
rootKeys.push(indirectDependencyKey);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
return rootKeys;
|
|
856
|
+
};
|
|
857
|
+
var traceAllSelectorAtoms = (selectorKey, store) => {
|
|
858
|
+
const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
|
|
859
|
+
return directDependencyKeys.flatMap(
|
|
860
|
+
(depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
|
|
861
|
+
);
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
// internal/src/selector/update-selector-atoms.ts
|
|
865
|
+
var updateSelectorAtoms = (selectorKey, dependency, store) => {
|
|
866
|
+
const target = newest(store);
|
|
867
|
+
if (dependency.type === `atom`) {
|
|
868
|
+
target.selectorAtoms.set({
|
|
869
|
+
selectorKey,
|
|
870
|
+
atomKey: dependency.key
|
|
871
|
+
});
|
|
872
|
+
store.logger.info(
|
|
873
|
+
`\u{1F50D}`,
|
|
874
|
+
`selector`,
|
|
875
|
+
selectorKey,
|
|
876
|
+
`discovers root atom "${dependency.key}"`
|
|
877
|
+
);
|
|
878
|
+
} else {
|
|
879
|
+
const rootKeys = traceSelectorAtoms(selectorKey, dependency.key, store);
|
|
880
|
+
store.logger.info(
|
|
881
|
+
`\u{1F50D}`,
|
|
882
|
+
`selector`,
|
|
883
|
+
selectorKey,
|
|
884
|
+
`discovers root atoms: [ ${rootKeys.map((key) => `"${key}"`).join(`, `)} ]`
|
|
885
|
+
);
|
|
886
|
+
for (const atomKey of rootKeys) {
|
|
887
|
+
target.selectorAtoms = target.selectorAtoms.set({
|
|
888
|
+
selectorKey,
|
|
889
|
+
atomKey
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
// internal/src/selector/register-selector.ts
|
|
896
|
+
var registerSelector = (selectorKey, store) => ({
|
|
897
|
+
get: (dependency) => {
|
|
898
|
+
const target = newest(store);
|
|
899
|
+
const alreadyRegistered = target.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
|
|
900
|
+
const dependencyState = withdraw(dependency, store);
|
|
901
|
+
if (dependencyState === void 0) {
|
|
902
|
+
throw new Error(
|
|
903
|
+
`State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
const dependencyValue = readOrComputeValue(dependencyState, store);
|
|
907
|
+
store.logger.info(
|
|
908
|
+
`\u{1F50C}`,
|
|
909
|
+
`selector`,
|
|
910
|
+
selectorKey,
|
|
911
|
+
`registers dependency ( "${dependency.key}" =`,
|
|
912
|
+
dependencyValue,
|
|
913
|
+
`)`
|
|
914
|
+
);
|
|
915
|
+
if (!alreadyRegistered) {
|
|
916
|
+
target.selectorGraph.set(
|
|
917
|
+
{
|
|
918
|
+
upstreamSelectorKey: dependency.key,
|
|
919
|
+
downstreamSelectorKey: selectorKey
|
|
920
|
+
},
|
|
921
|
+
{
|
|
922
|
+
source: dependency.key
|
|
923
|
+
}
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
updateSelectorAtoms(selectorKey, dependency, store);
|
|
927
|
+
return dependencyValue;
|
|
928
|
+
},
|
|
929
|
+
set: (stateToken, newValue) => {
|
|
930
|
+
const state = withdraw(stateToken, store);
|
|
931
|
+
if (state === void 0) {
|
|
932
|
+
throw new Error(
|
|
933
|
+
`State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
setAtomOrSelector(state, newValue, store);
|
|
937
|
+
}
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
// internal/src/selector/create-read-write-selector.ts
|
|
941
|
+
var createReadWriteSelector = (options, family, store) => {
|
|
942
|
+
const target = newest(store);
|
|
943
|
+
const subject = new Subject();
|
|
944
|
+
const { get, set } = registerSelector(options.key, store);
|
|
945
|
+
const getSelf = () => {
|
|
946
|
+
const value = options.get({ get });
|
|
947
|
+
cacheValue(options.key, value, subject, store);
|
|
948
|
+
return value;
|
|
949
|
+
};
|
|
950
|
+
const setSelf = (next) => {
|
|
951
|
+
const oldValue = getSelf();
|
|
952
|
+
const newValue = become(next)(oldValue);
|
|
953
|
+
store.logger.info(
|
|
954
|
+
`\u{1F4DD}`,
|
|
955
|
+
`selector`,
|
|
956
|
+
options.key,
|
|
957
|
+
`set (`,
|
|
958
|
+
oldValue,
|
|
959
|
+
`->`,
|
|
960
|
+
newValue,
|
|
961
|
+
`)`
|
|
962
|
+
);
|
|
963
|
+
cacheValue(options.key, newValue, subject, store);
|
|
964
|
+
markDone(options.key, store);
|
|
965
|
+
if (target.transactionMeta === null) {
|
|
966
|
+
subject.next({ newValue, oldValue });
|
|
967
|
+
}
|
|
968
|
+
options.set({ get, set }, newValue);
|
|
969
|
+
};
|
|
970
|
+
const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
971
|
+
subject,
|
|
972
|
+
install: (s) => createSelector(options, family, s),
|
|
973
|
+
get: getSelf,
|
|
974
|
+
set: setSelf,
|
|
975
|
+
type: `selector`
|
|
976
|
+
}), family && { family });
|
|
977
|
+
target.selectors.set(options.key, mySelector);
|
|
978
|
+
const initialValue = getSelf();
|
|
979
|
+
store.logger.info(`\u2728`, mySelector.type, mySelector.key, `=`, initialValue);
|
|
980
|
+
const token = {
|
|
981
|
+
key: options.key,
|
|
982
|
+
type: `selector`
|
|
983
|
+
};
|
|
984
|
+
if (family) {
|
|
985
|
+
token.family = family;
|
|
986
|
+
}
|
|
987
|
+
store.subject.selectorCreation.next(token);
|
|
988
|
+
return token;
|
|
989
|
+
};
|
|
990
|
+
|
|
991
|
+
// internal/src/selector/create-readonly-selector.ts
|
|
992
|
+
var createReadonlySelector = (options, family, store) => {
|
|
993
|
+
const target = newest(store);
|
|
994
|
+
const subject = new Subject();
|
|
995
|
+
const { get } = registerSelector(options.key, store);
|
|
996
|
+
const getSelf = () => {
|
|
997
|
+
const value = options.get({ get });
|
|
998
|
+
cacheValue(options.key, value, subject, store);
|
|
999
|
+
return value;
|
|
1000
|
+
};
|
|
1001
|
+
const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1002
|
+
subject,
|
|
1003
|
+
install: (s) => createSelector(options, family, s),
|
|
1004
|
+
get: getSelf,
|
|
1005
|
+
type: `readonly_selector`
|
|
1006
|
+
}), family && { family });
|
|
1007
|
+
target.readonlySelectors.set(options.key, readonlySelector);
|
|
1008
|
+
const initialValue = getSelf();
|
|
1009
|
+
store.logger.info(
|
|
1010
|
+
`\u2728`,
|
|
1011
|
+
readonlySelector.type,
|
|
1012
|
+
readonlySelector.key,
|
|
1013
|
+
`=`,
|
|
1014
|
+
initialValue
|
|
1015
|
+
);
|
|
1016
|
+
const token = {
|
|
1017
|
+
key: options.key,
|
|
1018
|
+
type: `readonly_selector`
|
|
1019
|
+
};
|
|
1020
|
+
if (family) {
|
|
1021
|
+
token.family = family;
|
|
1022
|
+
}
|
|
1023
|
+
store.subject.selectorCreation.next(token);
|
|
1024
|
+
return token;
|
|
1025
|
+
};
|
|
1026
|
+
|
|
1027
|
+
// internal/src/selector/create-selector.ts
|
|
1028
|
+
function createSelector(options, family, store) {
|
|
1029
|
+
const target = newest(store);
|
|
1030
|
+
const existingWritable = target.selectors.get(options.key);
|
|
1031
|
+
const existingReadonly = target.readonlySelectors.get(options.key);
|
|
1032
|
+
if (existingWritable || existingReadonly) {
|
|
1033
|
+
store.logger.error(
|
|
1034
|
+
`\u274C`,
|
|
1035
|
+
existingReadonly ? `readonly_selector` : `selector`,
|
|
1036
|
+
options.key,
|
|
1037
|
+
`Tried to create selector, but it already exists in the store. (Ignore if you are in development using hot module replacement.)`
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
if (`set` in options) {
|
|
1041
|
+
return createReadWriteSelector(options, family, store);
|
|
1042
|
+
}
|
|
1043
|
+
return createReadonlySelector(options, family, store);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
// internal/src/selector/delete-selector.ts
|
|
1047
|
+
function deleteSelector(selectorToken, store) {
|
|
1048
|
+
const target = newest(store);
|
|
1049
|
+
const { key } = selectorToken;
|
|
1050
|
+
switch (selectorToken.type) {
|
|
1051
|
+
case `selector`:
|
|
1052
|
+
target.selectors.delete(key);
|
|
1053
|
+
break;
|
|
1054
|
+
case `readonly_selector`:
|
|
1055
|
+
target.readonlySelectors.delete(key);
|
|
1056
|
+
break;
|
|
1057
|
+
}
|
|
1058
|
+
target.valueMap.delete(key);
|
|
1059
|
+
target.selectorAtoms.delete(key);
|
|
1060
|
+
const downstreamTokens = target.selectorGraph.getRelationEntries({ upstreamSelectorKey: key }).filter(([_, { source }]) => source === key).map(
|
|
1061
|
+
([downstreamSelectorKey]) => {
|
|
1062
|
+
var _a;
|
|
1063
|
+
return (_a = target.selectors.get(downstreamSelectorKey)) != null ? _a : target.readonlySelectors.get(downstreamSelectorKey);
|
|
1064
|
+
}
|
|
1065
|
+
);
|
|
1066
|
+
for (const downstreamToken of downstreamTokens) {
|
|
1067
|
+
if (downstreamToken) {
|
|
1068
|
+
deleteSelector(downstreamToken, store);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
target.selectorGraph.delete(key);
|
|
1072
|
+
store.logger.info(`\u{1F525}`, selectorToken.type, `${key}`, `deleted`);
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// internal/src/subscribe/recall-state.ts
|
|
1076
|
+
var recallState = (state, store) => {
|
|
1077
|
+
const target = newest(store);
|
|
1078
|
+
if (!target.operation.open) {
|
|
1079
|
+
return target.valueMap.get(state.key);
|
|
1080
|
+
}
|
|
1081
|
+
return target.operation.prev.get(state.key);
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
// internal/src/subscribe/subscribe-to-root-atoms.ts
|
|
1085
|
+
var subscribeToRootAtoms = (state, store) => {
|
|
1086
|
+
const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
|
|
1087
|
+
const atom2 = store.atoms.get(atomKey);
|
|
1088
|
+
if (atom2 === void 0) {
|
|
1089
|
+
throw new Error(
|
|
1090
|
+
`Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
|
|
1091
|
+
);
|
|
1092
|
+
}
|
|
1093
|
+
return atom2.subject.subscribe(
|
|
1094
|
+
`${state.type}:${state.key}`,
|
|
1095
|
+
(atomChange) => {
|
|
1096
|
+
store.logger.info(
|
|
1097
|
+
`\u{1F4E2}`,
|
|
1098
|
+
state.type,
|
|
1099
|
+
state.key,
|
|
1100
|
+
`root`,
|
|
1101
|
+
atomKey,
|
|
1102
|
+
`went`,
|
|
1103
|
+
atomChange.oldValue,
|
|
1104
|
+
`->`,
|
|
1105
|
+
atomChange.newValue
|
|
1106
|
+
);
|
|
1107
|
+
const oldValue = recallState(state, store);
|
|
1108
|
+
const newValue = readOrComputeValue(state, store);
|
|
1109
|
+
store.logger.info(
|
|
1110
|
+
`\u2728`,
|
|
1111
|
+
state.type,
|
|
1112
|
+
state.key,
|
|
1113
|
+
`went`,
|
|
1114
|
+
oldValue,
|
|
1115
|
+
`->`,
|
|
1116
|
+
newValue
|
|
1117
|
+
);
|
|
1118
|
+
state.subject.next({ newValue, oldValue });
|
|
1119
|
+
}
|
|
1120
|
+
);
|
|
1121
|
+
});
|
|
1122
|
+
return dependencySubscriptions;
|
|
1123
|
+
};
|
|
1124
|
+
|
|
1125
|
+
// internal/src/subscribe/subscribe-to-state.ts
|
|
1126
|
+
function subscribeToState(token, handleUpdate, key, store) {
|
|
1127
|
+
const state = withdraw(token, store);
|
|
1128
|
+
if (state === void 0) {
|
|
1129
|
+
throw new Error(
|
|
1130
|
+
`State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
const unsubFunction = state.subject.subscribe(key, handleUpdate);
|
|
1134
|
+
store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
|
|
1135
|
+
const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
|
|
1136
|
+
const unsubscribe = dependencyUnsubFunctions === null ? () => {
|
|
1137
|
+
store.logger.info(
|
|
1138
|
+
`\u{1F648}`,
|
|
1139
|
+
state.type,
|
|
1140
|
+
state.key,
|
|
1141
|
+
`Removing subscription "${key}"`
|
|
1142
|
+
);
|
|
1143
|
+
unsubFunction();
|
|
1144
|
+
} : () => {
|
|
1145
|
+
store.logger.info(
|
|
1146
|
+
`\u{1F648}`,
|
|
1147
|
+
state.type,
|
|
1148
|
+
state.key,
|
|
1149
|
+
`Removing subscription "${key}"`
|
|
1150
|
+
);
|
|
1151
|
+
unsubFunction();
|
|
1152
|
+
for (const unsubFromDependency of dependencyUnsubFunctions) {
|
|
1153
|
+
unsubFromDependency();
|
|
1154
|
+
}
|
|
1155
|
+
};
|
|
1156
|
+
return unsubscribe;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
// internal/src/subscribe/subscribe-to-timeline.ts
|
|
1160
|
+
var subscribeToTimeline = (token, handleUpdate, key, store) => {
|
|
1161
|
+
const tl = withdraw(token, store);
|
|
1162
|
+
if (tl === void 0) {
|
|
1163
|
+
throw new Error(
|
|
1164
|
+
`Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
|
|
1168
|
+
const unsubscribe = tl.subject.subscribe(key, handleUpdate);
|
|
1169
|
+
return () => {
|
|
1170
|
+
store.logger.info(
|
|
1171
|
+
`\u{1F648}`,
|
|
1172
|
+
`timeline`,
|
|
1173
|
+
token.key,
|
|
1174
|
+
`Removing subscription "${key}" from timeline`
|
|
1175
|
+
);
|
|
1176
|
+
unsubscribe();
|
|
1177
|
+
};
|
|
1178
|
+
};
|
|
1179
|
+
|
|
1180
|
+
// internal/src/subscribe/subscribe-to-transaction.ts
|
|
1181
|
+
var subscribeToTransaction = (token, handleUpdate, key, store) => {
|
|
1182
|
+
const tx = withdraw(token, store);
|
|
1183
|
+
if (tx === void 0) {
|
|
1184
|
+
throw new Error(
|
|
1185
|
+
`Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
store.logger.info(
|
|
1189
|
+
`\u{1F440}`,
|
|
1190
|
+
`transaction`,
|
|
1191
|
+
token.key,
|
|
1192
|
+
`Adding subscription "${key}"`
|
|
1193
|
+
);
|
|
1194
|
+
const unsubscribe = tx.subject.subscribe(key, handleUpdate);
|
|
1195
|
+
return () => {
|
|
1196
|
+
store.logger.info(
|
|
1197
|
+
`\u{1F648}`,
|
|
1198
|
+
`transaction`,
|
|
1199
|
+
token.key,
|
|
1200
|
+
`Removing subscription "${key}"`
|
|
1201
|
+
);
|
|
1202
|
+
unsubscribe();
|
|
1203
|
+
};
|
|
1204
|
+
};
|
|
1205
|
+
|
|
1206
|
+
// internal/src/mutable/tracker.ts
|
|
1207
|
+
var Tracker = class {
|
|
1208
|
+
constructor(mutableState, store) {
|
|
1209
|
+
this.unsubscribeFromInnerValue = null;
|
|
1210
|
+
this.mutableState = mutableState;
|
|
1211
|
+
const target = newest(store);
|
|
1212
|
+
this.latestUpdateState = this.initializeState(mutableState, target);
|
|
1213
|
+
this.observeCore(mutableState, this.latestUpdateState, target);
|
|
1214
|
+
this.updateCore(mutableState, this.latestUpdateState, target);
|
|
1215
|
+
target.trackers.set(mutableState.key, this);
|
|
1216
|
+
}
|
|
1217
|
+
initializeState(mutableState, store) {
|
|
1218
|
+
const latestUpdateStateKey = `*${mutableState.key}`;
|
|
1219
|
+
deleteAtom({ type: `atom`, key: latestUpdateStateKey }, store);
|
|
1220
|
+
const familyMetaData = mutableState.family ? {
|
|
1221
|
+
key: `*${mutableState.family.key}`,
|
|
1222
|
+
subKey: mutableState.family.subKey
|
|
1223
|
+
} : void 0;
|
|
1224
|
+
const latestUpdateState = createAtom(
|
|
1225
|
+
{
|
|
1226
|
+
key: latestUpdateStateKey,
|
|
1227
|
+
default: null
|
|
1228
|
+
},
|
|
1229
|
+
familyMetaData,
|
|
1230
|
+
store
|
|
1231
|
+
);
|
|
1232
|
+
return latestUpdateState;
|
|
1233
|
+
}
|
|
1234
|
+
observeCore(mutableState, latestUpdateState, store) {
|
|
1235
|
+
const originalInnerValue = getState(mutableState, store);
|
|
1236
|
+
const target = newest(store);
|
|
1237
|
+
this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
|
|
1238
|
+
`tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
|
|
1239
|
+
(update) => {
|
|
1240
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1241
|
+
mutableState.key,
|
|
1242
|
+
() => {
|
|
1243
|
+
unsubscribe();
|
|
1244
|
+
setState(latestUpdateState, update, store);
|
|
1245
|
+
}
|
|
1246
|
+
);
|
|
1247
|
+
}
|
|
1248
|
+
);
|
|
1249
|
+
subscribeToState(
|
|
1250
|
+
mutableState,
|
|
1251
|
+
(update) => {
|
|
1252
|
+
var _a;
|
|
1253
|
+
if (update.newValue !== update.oldValue) {
|
|
1254
|
+
(_a = this.unsubscribeFromInnerValue) == null ? void 0 : _a.call(this);
|
|
1255
|
+
const target2 = newest(store);
|
|
1256
|
+
this.unsubscribeFromInnerValue = update.newValue.subscribe(
|
|
1257
|
+
`tracker:${store.config.name}:${target2.transactionMeta === null ? `main` : target2.transactionMeta.update.key}`,
|
|
1258
|
+
(update2) => {
|
|
1259
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1260
|
+
mutableState.key,
|
|
1261
|
+
() => {
|
|
1262
|
+
unsubscribe();
|
|
1263
|
+
setState(latestUpdateState, update2, store);
|
|
1264
|
+
}
|
|
1265
|
+
);
|
|
1266
|
+
}
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
},
|
|
1270
|
+
`${store.config.name}: tracker observing inner value`,
|
|
1271
|
+
store
|
|
1272
|
+
);
|
|
1273
|
+
}
|
|
1274
|
+
updateCore(mutableState, latestUpdateState, store) {
|
|
1275
|
+
subscribeToState(
|
|
1276
|
+
latestUpdateState,
|
|
1277
|
+
({ newValue, oldValue }) => {
|
|
1278
|
+
const timelineId = store.timelineAtoms.getRelatedKey(
|
|
1279
|
+
latestUpdateState.key
|
|
1280
|
+
);
|
|
1281
|
+
if (timelineId) {
|
|
1282
|
+
const timelineData = store.timelines.get(timelineId);
|
|
1283
|
+
if (timelineData == null ? void 0 : timelineData.timeTraveling) {
|
|
1284
|
+
const unsubscribe2 = subscribeToTimeline(
|
|
1285
|
+
{ key: timelineId, type: `timeline` },
|
|
1286
|
+
(update) => {
|
|
1287
|
+
unsubscribe2();
|
|
1288
|
+
setState(
|
|
1289
|
+
mutableState,
|
|
1290
|
+
(transceiver) => {
|
|
1291
|
+
if (update === `redo` && newValue) {
|
|
1292
|
+
transceiver.do(newValue);
|
|
1293
|
+
} else if (update === `undo` && oldValue) {
|
|
1294
|
+
transceiver.undo(oldValue);
|
|
1295
|
+
}
|
|
1296
|
+
return transceiver;
|
|
1297
|
+
},
|
|
1298
|
+
store
|
|
1299
|
+
);
|
|
1300
|
+
},
|
|
1301
|
+
`${mutableState.key}: tracker observing timeline`,
|
|
1302
|
+
store
|
|
1303
|
+
);
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1308
|
+
latestUpdateState.key,
|
|
1309
|
+
() => {
|
|
1310
|
+
unsubscribe();
|
|
1311
|
+
const mutable = getState(mutableState, store);
|
|
1312
|
+
const updateNumber = mutable.getUpdateNumber(newValue);
|
|
1313
|
+
const eventOffset = updateNumber - mutable.cacheUpdateNumber;
|
|
1314
|
+
if (newValue && eventOffset === 1) {
|
|
1315
|
+
setState(
|
|
1316
|
+
mutableState,
|
|
1317
|
+
(transceiver) => (transceiver.do(newValue), transceiver),
|
|
1318
|
+
store
|
|
1319
|
+
);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
);
|
|
1323
|
+
},
|
|
1324
|
+
`${store.config.name}: tracker observing latest update`,
|
|
1325
|
+
store
|
|
1326
|
+
);
|
|
1327
|
+
}
|
|
1328
|
+
};
|
|
1329
|
+
|
|
1330
|
+
// internal/src/mutable/create-mutable-atom.ts
|
|
1331
|
+
function createMutableAtom(options, store) {
|
|
1332
|
+
store.logger.info(
|
|
1333
|
+
`\u{1F527}`,
|
|
1334
|
+
`atom`,
|
|
1335
|
+
options.key,
|
|
1336
|
+
`creating in store "${store.config.name}"`
|
|
1337
|
+
);
|
|
1338
|
+
const coreState = createAtom(options, void 0, store);
|
|
1339
|
+
new Tracker(coreState, store);
|
|
1340
|
+
const jsonState = selectJson(coreState, options, store);
|
|
1341
|
+
const target = newest(store);
|
|
1342
|
+
subscribeToState(
|
|
1343
|
+
jsonState,
|
|
1344
|
+
() => {
|
|
1345
|
+
const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
|
|
1346
|
+
if (!trackerHasBeenInitialized) {
|
|
1347
|
+
new Tracker(coreState, store);
|
|
1348
|
+
}
|
|
1349
|
+
},
|
|
1350
|
+
`tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
|
|
1351
|
+
store
|
|
1352
|
+
);
|
|
1353
|
+
return coreState;
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
// internal/src/families/create-atom-family.ts
|
|
1357
|
+
function createAtomFamily(options, store) {
|
|
1358
|
+
const subject = new Subject();
|
|
1359
|
+
const atomFamily2 = Object.assign(
|
|
1360
|
+
(key) => {
|
|
1361
|
+
const subKey = stringifyJson(key);
|
|
1362
|
+
const family = { key: options.key, subKey };
|
|
1363
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1364
|
+
const existing = withdraw({ key: fullKey, type: `atom` }, store);
|
|
1365
|
+
let token;
|
|
1366
|
+
if (existing) {
|
|
1367
|
+
token = deposit(existing);
|
|
1368
|
+
} else {
|
|
1369
|
+
const individualOptions = {
|
|
1370
|
+
key: fullKey,
|
|
1371
|
+
default: options.default instanceof Function ? options.default(key) : options.default
|
|
1372
|
+
};
|
|
1373
|
+
if (options.effects) {
|
|
1374
|
+
individualOptions.effects = options.effects(key);
|
|
1375
|
+
}
|
|
1376
|
+
token = createAtom(individualOptions, family, store);
|
|
1377
|
+
subject.next(token);
|
|
1378
|
+
}
|
|
1379
|
+
return token;
|
|
1380
|
+
},
|
|
1381
|
+
{
|
|
1382
|
+
key: options.key,
|
|
1383
|
+
type: `atom_family`,
|
|
1384
|
+
subject
|
|
1385
|
+
}
|
|
1386
|
+
);
|
|
1387
|
+
const target = newest(store);
|
|
1388
|
+
target.families.set(options.key, atomFamily2);
|
|
1389
|
+
return atomFamily2;
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
// internal/src/families/create-readonly-selector-family.ts
|
|
1393
|
+
function createReadonlySelectorFamily(options, store) {
|
|
1394
|
+
const subject = new Subject();
|
|
1395
|
+
return Object.assign(
|
|
1396
|
+
(key) => {
|
|
1397
|
+
const target = newest(store);
|
|
1398
|
+
const subKey = stringifyJson(key);
|
|
1399
|
+
const family = { key: options.key, subKey };
|
|
1400
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1401
|
+
const existing = target.readonlySelectors.get(fullKey);
|
|
1402
|
+
if (existing) {
|
|
1403
|
+
return deposit(existing);
|
|
1404
|
+
}
|
|
1405
|
+
return createSelector(
|
|
1406
|
+
{
|
|
1407
|
+
key: fullKey,
|
|
1408
|
+
get: options.get(key)
|
|
1409
|
+
},
|
|
1410
|
+
family,
|
|
1411
|
+
store
|
|
1412
|
+
);
|
|
1413
|
+
},
|
|
1414
|
+
{
|
|
1415
|
+
key: options.key,
|
|
1416
|
+
type: `readonly_selector_family`,
|
|
1417
|
+
subject
|
|
1418
|
+
}
|
|
1419
|
+
);
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
// internal/src/families/create-selector-family.ts
|
|
1423
|
+
function createSelectorFamily(options, store) {
|
|
1424
|
+
const isReadonly = !(`set` in options);
|
|
1425
|
+
if (isReadonly) {
|
|
1426
|
+
return createReadonlySelectorFamily(options, store);
|
|
1427
|
+
}
|
|
1428
|
+
const target = newest(store);
|
|
1429
|
+
const subject = new Subject();
|
|
1430
|
+
const selectorFamily2 = Object.assign(
|
|
1431
|
+
(key) => {
|
|
1432
|
+
const subKey = stringifyJson(key);
|
|
1433
|
+
const family = { key: options.key, subKey };
|
|
1434
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1435
|
+
const existing = target.selectors.get(fullKey);
|
|
1436
|
+
if (existing) {
|
|
1437
|
+
return deposit(existing);
|
|
1438
|
+
}
|
|
1439
|
+
const token = createSelector(
|
|
1440
|
+
{
|
|
1441
|
+
key: fullKey,
|
|
1442
|
+
get: options.get(key),
|
|
1443
|
+
set: options.set(key)
|
|
1444
|
+
},
|
|
1445
|
+
family,
|
|
1446
|
+
store
|
|
1447
|
+
);
|
|
1448
|
+
subject.next(token);
|
|
1449
|
+
return token;
|
|
1450
|
+
},
|
|
1451
|
+
{
|
|
1452
|
+
key: options.key,
|
|
1453
|
+
type: `selector_family`
|
|
1454
|
+
}
|
|
1455
|
+
);
|
|
1456
|
+
target.families.set(options.key, selectorFamily2);
|
|
1457
|
+
return selectorFamily2;
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
// internal/src/mutable/tracker-family.ts
|
|
1461
|
+
var FamilyTracker = class {
|
|
1462
|
+
constructor(findMutableState, store) {
|
|
1463
|
+
this.findLatestUpdateState = createAtomFamily(
|
|
1464
|
+
{
|
|
1465
|
+
key: `*${findMutableState.key}`,
|
|
1466
|
+
default: null
|
|
1467
|
+
},
|
|
1468
|
+
store
|
|
1469
|
+
);
|
|
1470
|
+
this.findMutableState = findMutableState;
|
|
1471
|
+
this.findMutableState.subject.subscribe(
|
|
1472
|
+
`store=${store.config.name}::tracker-atom-family`,
|
|
1473
|
+
(atomToken) => {
|
|
1474
|
+
if (atomToken.family) {
|
|
1475
|
+
const key = parseJson(atomToken.family.subKey);
|
|
1476
|
+
this.findLatestUpdateState(key);
|
|
1477
|
+
new Tracker(atomToken, store);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
);
|
|
1481
|
+
this.findLatestUpdateState.subject.subscribe(
|
|
1482
|
+
`store=${store.config.name}::tracker-atom-family`,
|
|
1483
|
+
(atomToken) => {
|
|
1484
|
+
if (atomToken.family) {
|
|
1485
|
+
const key = parseJson(atomToken.family.subKey);
|
|
1486
|
+
const mutableAtomToken = this.findMutableState(key);
|
|
1487
|
+
new Tracker(mutableAtomToken, store);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
);
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
|
|
1494
|
+
// internal/src/mutable/create-mutable-atom-family.ts
|
|
1495
|
+
function createMutableAtomFamily(options, store) {
|
|
1496
|
+
const coreFamily = Object.assign(
|
|
1497
|
+
createAtomFamily(options, store),
|
|
1498
|
+
options
|
|
1499
|
+
);
|
|
1500
|
+
selectJsonFamily(coreFamily, options);
|
|
1501
|
+
new FamilyTracker(coreFamily, store);
|
|
1502
|
+
return coreFamily;
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
// internal/src/mutable/transceiver.ts
|
|
1506
|
+
function isTransceiver(value) {
|
|
1507
|
+
return typeof value === `object` && value !== null && `do` in value && `undo` in value && `subscribe` in value;
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
// internal/src/atom/is-default.ts
|
|
1511
|
+
var isAtomDefault = (key, store) => {
|
|
1512
|
+
const core = newest(store);
|
|
1513
|
+
return core.atomsThatAreDefault.has(key);
|
|
1514
|
+
};
|
|
1515
|
+
var markAtomAsDefault = (key, store) => {
|
|
1516
|
+
const core = newest(store);
|
|
1517
|
+
core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key);
|
|
1518
|
+
};
|
|
1519
|
+
var markAtomAsNotDefault = (key, store) => {
|
|
1520
|
+
const core = newest(store);
|
|
1521
|
+
core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault);
|
|
1522
|
+
core.atomsThatAreDefault.delete(key);
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
// internal/src/atom/create-atom.ts
|
|
1526
|
+
function createAtom(options, family, store) {
|
|
1527
|
+
store.logger.info(
|
|
1528
|
+
`\u{1F528}`,
|
|
1529
|
+
`atom`,
|
|
1530
|
+
options.key,
|
|
1531
|
+
`creating in store "${store.config.name}"`
|
|
1532
|
+
);
|
|
1533
|
+
const target = newest(store);
|
|
1534
|
+
const existing = target.atoms.get(options.key);
|
|
1535
|
+
if (existing) {
|
|
1536
|
+
store.logger.error(
|
|
1537
|
+
`\u274C`,
|
|
1538
|
+
`atom`,
|
|
1539
|
+
options.key,
|
|
1540
|
+
`Tried to create atom, but it already exists in the store.`,
|
|
1541
|
+
`(Ignore if you are in development using hot module replacement.)`
|
|
1542
|
+
);
|
|
1543
|
+
return deposit(existing);
|
|
1544
|
+
}
|
|
1545
|
+
const subject = new Subject();
|
|
1546
|
+
const newAtom = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1547
|
+
type: `atom`,
|
|
1548
|
+
install: (store2) => {
|
|
1549
|
+
store2.logger.info(
|
|
1550
|
+
`\u{1F6E0}\uFE0F`,
|
|
1551
|
+
`atom`,
|
|
1552
|
+
options.key,
|
|
1553
|
+
`installing in store "${store2.config.name}"`
|
|
1554
|
+
);
|
|
1555
|
+
return `mutable` in options ? createMutableAtom(options, store2) : createAtom(options, void 0, store2);
|
|
1556
|
+
},
|
|
1557
|
+
subject
|
|
1558
|
+
}), family && { family });
|
|
1559
|
+
let initialValue = options.default;
|
|
1560
|
+
if (options.default instanceof Function) {
|
|
1561
|
+
initialValue = options.default();
|
|
1562
|
+
}
|
|
1563
|
+
target.atoms.set(newAtom.key, newAtom);
|
|
1564
|
+
markAtomAsDefault(options.key, store);
|
|
1565
|
+
cacheValue(options.key, initialValue, subject, store);
|
|
1566
|
+
const token = deposit(newAtom);
|
|
1567
|
+
if (options.effects) {
|
|
1568
|
+
let effectIndex = 0;
|
|
1569
|
+
const cleanupFunctions = [];
|
|
1570
|
+
for (const effect of options.effects) {
|
|
1571
|
+
const cleanup = effect({
|
|
1572
|
+
setSelf: (next) => setState(token, next, store),
|
|
1573
|
+
onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
|
|
1574
|
+
});
|
|
1575
|
+
if (cleanup) {
|
|
1576
|
+
cleanupFunctions.push(cleanup);
|
|
1577
|
+
}
|
|
1578
|
+
++effectIndex;
|
|
1579
|
+
}
|
|
1580
|
+
newAtom.cleanup = () => {
|
|
1581
|
+
for (const cleanup of cleanupFunctions) {
|
|
1582
|
+
cleanup();
|
|
1583
|
+
}
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
store.subject.atomCreation.next(token);
|
|
1587
|
+
return token;
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
// internal/src/atom/delete-atom.ts
|
|
1591
|
+
function deleteAtom(atomToken, store) {
|
|
1592
|
+
var _a, _b;
|
|
1593
|
+
const target = newest(store);
|
|
1594
|
+
const { key } = atomToken;
|
|
1595
|
+
const atom2 = target.atoms.get(key);
|
|
1596
|
+
if (!atom2) {
|
|
1597
|
+
store.logger.error(
|
|
1598
|
+
`\u274C`,
|
|
1599
|
+
`atom`,
|
|
1600
|
+
`${key}`,
|
|
1601
|
+
`Tried to delete atom, but it does not exist in the store.`
|
|
1602
|
+
);
|
|
1603
|
+
}
|
|
1604
|
+
(_a = atom2 == null ? void 0 : atom2.cleanup) == null ? void 0 : _a.call(atom2);
|
|
1605
|
+
target.atoms.delete(key);
|
|
1606
|
+
target.valueMap.delete(key);
|
|
1607
|
+
const selectorKeys = target.selectorAtoms.getRelatedKeys(key);
|
|
1608
|
+
if (selectorKeys) {
|
|
1609
|
+
for (const selectorKey of selectorKeys) {
|
|
1610
|
+
const token = (_b = target.selectors.get(selectorKey)) != null ? _b : target.readonlySelectors.get(selectorKey);
|
|
1611
|
+
if (token) {
|
|
1612
|
+
deleteSelector(token, store);
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
target.selectorAtoms.delete(key);
|
|
1617
|
+
target.atomsThatAreDefault.delete(key);
|
|
1618
|
+
target.timelineAtoms.delete(key);
|
|
1619
|
+
store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
// internal/src/lazy-map.ts
|
|
1623
|
+
var LazyMap = class extends Map {
|
|
1624
|
+
constructor(source) {
|
|
1625
|
+
super();
|
|
1626
|
+
this.source = source;
|
|
1627
|
+
this.deleted = /* @__PURE__ */ new Set();
|
|
1628
|
+
}
|
|
1629
|
+
get(key) {
|
|
1630
|
+
const has = super.has(key);
|
|
1631
|
+
if (has) {
|
|
1632
|
+
return super.get(key);
|
|
1633
|
+
}
|
|
1634
|
+
if (!this.deleted.has(key) && this.source.has(key)) {
|
|
1635
|
+
const value = this.source.get(key);
|
|
1636
|
+
return value;
|
|
1637
|
+
}
|
|
1638
|
+
return void 0;
|
|
1639
|
+
}
|
|
1640
|
+
set(key, value) {
|
|
1641
|
+
this.deleted.delete(key);
|
|
1642
|
+
return super.set(key, value);
|
|
1643
|
+
}
|
|
1644
|
+
hasOwn(key) {
|
|
1645
|
+
return super.has(key);
|
|
1646
|
+
}
|
|
1647
|
+
has(key) {
|
|
1648
|
+
return !this.deleted.has(key) && (super.has(key) || this.source.has(key));
|
|
1649
|
+
}
|
|
1650
|
+
delete(key) {
|
|
1651
|
+
this.deleted.add(key);
|
|
1652
|
+
return super.delete(key);
|
|
1653
|
+
}
|
|
1654
|
+
};
|
|
1655
|
+
|
|
1656
|
+
// internal/src/not-found-error.ts
|
|
1657
|
+
var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
1658
|
+
function prettyPrintTokenType(token) {
|
|
1659
|
+
if (token.type === `readonly_selector`) {
|
|
1660
|
+
return `Readonly Selector`;
|
|
1661
|
+
}
|
|
1662
|
+
return capitalize(token.type);
|
|
1663
|
+
}
|
|
1664
|
+
var NotFoundError = class extends Error {
|
|
1665
|
+
constructor(token, store) {
|
|
1666
|
+
super(
|
|
1667
|
+
`${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
|
|
1668
|
+
);
|
|
1669
|
+
}
|
|
1670
|
+
};
|
|
1671
|
+
|
|
1672
|
+
// internal/src/timeline/add-atom-to-timeline.ts
|
|
1673
|
+
var addAtomToTimeline = (atomToken, tl, store) => {
|
|
1674
|
+
const atom2 = withdraw(atomToken, store);
|
|
1675
|
+
if (atom2 === void 0) {
|
|
1676
|
+
throw new Error(
|
|
1677
|
+
`Cannot subscribe to atom "${atomToken.key}" because it has not been initialized in store "${store.config.name}"`
|
|
1678
|
+
);
|
|
1679
|
+
}
|
|
1680
|
+
atom2.subject.subscribe(`timeline`, (update) => {
|
|
1681
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1682
|
+
const target = newest(store);
|
|
1683
|
+
const currentSelectorKey = store.operation.open && store.operation.token.type === `selector` ? store.operation.token.key : null;
|
|
1684
|
+
const currentSelectorTime = store.operation.open && store.operation.token.type === `selector` ? store.operation.time : null;
|
|
1685
|
+
const currentTransactionKey = (_a = target.subject.transactionApplying.state) == null ? void 0 : _a.update.key;
|
|
1686
|
+
const currentTransactionTime = (_b = target.subject.transactionApplying.state) == null ? void 0 : _b.time;
|
|
1687
|
+
store.logger.info(
|
|
1688
|
+
`\u23F3`,
|
|
1689
|
+
`timeline`,
|
|
1690
|
+
tl.key,
|
|
1691
|
+
`atom`,
|
|
1692
|
+
atomToken.key,
|
|
1693
|
+
`went`,
|
|
1694
|
+
update.oldValue,
|
|
1695
|
+
`->`,
|
|
1696
|
+
update.newValue,
|
|
1697
|
+
currentTransactionKey ? `in transaction "${currentTransactionKey}"` : currentSelectorKey ? `in selector "${currentSelectorKey}"` : ``
|
|
1698
|
+
);
|
|
1699
|
+
if (tl.timeTraveling === null) {
|
|
1700
|
+
if (tl.selectorTime && tl.selectorTime !== currentSelectorTime) {
|
|
1701
|
+
const mostRecentUpdate = tl.history.at(-1);
|
|
1702
|
+
if (mostRecentUpdate === void 0) {
|
|
1703
|
+
throw new Error(
|
|
1704
|
+
`Timeline "${tl.key}" has a selectorTime, but no history. This is most likely a bug in AtomIO.`
|
|
1705
|
+
);
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
if (currentTransactionKey) {
|
|
1709
|
+
const currentTransaction = withdraw(
|
|
1710
|
+
{ key: currentTransactionKey, type: `transaction` },
|
|
1711
|
+
store
|
|
1712
|
+
);
|
|
1713
|
+
if (currentTransaction === void 0) {
|
|
1714
|
+
throw new Error(
|
|
1715
|
+
`Transaction "${currentTransactionKey}" not found in store "${store.config.name}". This is surprising, because we are in the application phase of "${currentTransactionKey}".`
|
|
1716
|
+
);
|
|
1717
|
+
}
|
|
1718
|
+
if (tl.transactionKey !== currentTransactionKey) {
|
|
1719
|
+
if (tl.transactionKey) {
|
|
1720
|
+
store.logger.error(
|
|
1721
|
+
`\u{1F41E}`,
|
|
1722
|
+
`timeline`,
|
|
1723
|
+
tl.key,
|
|
1724
|
+
`unable to resolve transaction "${tl.transactionKey}. This is probably a bug in AtomIO.`
|
|
1725
|
+
);
|
|
1726
|
+
}
|
|
1727
|
+
tl.transactionKey = currentTransactionKey;
|
|
1728
|
+
const unsubscribe = currentTransaction.subject.subscribe(
|
|
1729
|
+
`timeline:${tl.key}`,
|
|
1730
|
+
(update2) => {
|
|
1731
|
+
var _a2, _b2;
|
|
1732
|
+
unsubscribe();
|
|
1733
|
+
if (tl.timeTraveling === null && currentTransactionTime) {
|
|
1734
|
+
if (tl.at !== tl.history.length) {
|
|
1735
|
+
tl.history.splice(tl.at);
|
|
1736
|
+
}
|
|
1737
|
+
const filterUpdates = (updates2) => updates2.filter((updateFromTx) => {
|
|
1738
|
+
const target2 = newest(store);
|
|
1739
|
+
if (`updates` in updateFromTx) {
|
|
1740
|
+
return true;
|
|
1741
|
+
}
|
|
1742
|
+
const atomOrFamilyKeys = target2.timelineAtoms.getRelatedKeys(tl.key);
|
|
1743
|
+
return atomOrFamilyKeys ? [...atomOrFamilyKeys].some(
|
|
1744
|
+
(key) => {
|
|
1745
|
+
var _a3;
|
|
1746
|
+
return key === updateFromTx.key || key === ((_a3 = updateFromTx.family) == null ? void 0 : _a3.key);
|
|
1747
|
+
}
|
|
1748
|
+
) : false;
|
|
1749
|
+
}).map((updateFromTx) => {
|
|
1750
|
+
if (`updates` in updateFromTx) {
|
|
1751
|
+
return __spreadProps(__spreadValues({}, updateFromTx), {
|
|
1752
|
+
updates: filterUpdates(updateFromTx.updates)
|
|
1753
|
+
});
|
|
1754
|
+
}
|
|
1755
|
+
return updateFromTx;
|
|
1756
|
+
});
|
|
1757
|
+
const updates = filterUpdates(update2.updates);
|
|
1758
|
+
const timelineTransactionUpdate = __spreadProps(__spreadValues({
|
|
1759
|
+
type: `transaction_update`,
|
|
1760
|
+
timestamp: currentTransactionTime
|
|
1761
|
+
}, update2), {
|
|
1762
|
+
updates
|
|
1763
|
+
});
|
|
1764
|
+
const willCapture = (_b2 = (_a2 = tl.shouldCapture) == null ? void 0 : _a2.call(tl, timelineTransactionUpdate, tl)) != null ? _b2 : true;
|
|
1765
|
+
if (willCapture) {
|
|
1766
|
+
tl.history.push(timelineTransactionUpdate);
|
|
1767
|
+
tl.at = tl.history.length;
|
|
1768
|
+
tl.subject.next(timelineTransactionUpdate);
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
tl.transactionKey = null;
|
|
1772
|
+
store.logger.info(
|
|
1773
|
+
`\u231B`,
|
|
1774
|
+
`timeline`,
|
|
1775
|
+
tl.key,
|
|
1776
|
+
`got a transaction_update "${update2.key}"`
|
|
1777
|
+
);
|
|
1778
|
+
}
|
|
1779
|
+
);
|
|
1780
|
+
}
|
|
1781
|
+
} else if (currentSelectorKey && currentSelectorTime) {
|
|
1782
|
+
let latestUpdate = tl.history.at(-1);
|
|
1783
|
+
if (currentSelectorTime !== tl.selectorTime) {
|
|
1784
|
+
latestUpdate = {
|
|
1785
|
+
type: `selector_update`,
|
|
1786
|
+
timestamp: currentSelectorTime,
|
|
1787
|
+
key: currentSelectorKey,
|
|
1788
|
+
atomUpdates: []
|
|
1789
|
+
};
|
|
1790
|
+
latestUpdate.atomUpdates.push(__spreadValues({
|
|
1791
|
+
key: atom2.key,
|
|
1792
|
+
type: `atom_update`
|
|
1793
|
+
}, update));
|
|
1794
|
+
if (tl.at !== tl.history.length) {
|
|
1795
|
+
tl.history.splice(tl.at);
|
|
1796
|
+
}
|
|
1797
|
+
tl.history.push(latestUpdate);
|
|
1798
|
+
store.logger.info(
|
|
1799
|
+
`\u231B`,
|
|
1800
|
+
`timeline`,
|
|
1801
|
+
tl.key,
|
|
1802
|
+
`got a selector_update "${currentSelectorKey}" with`,
|
|
1803
|
+
latestUpdate.atomUpdates.map((atomUpdate) => atomUpdate.key)
|
|
1804
|
+
);
|
|
1805
|
+
tl.at = tl.history.length;
|
|
1806
|
+
tl.selectorTime = currentSelectorTime;
|
|
1807
|
+
} else {
|
|
1808
|
+
if ((latestUpdate == null ? void 0 : latestUpdate.type) === `selector_update`) {
|
|
1809
|
+
latestUpdate.atomUpdates.push(__spreadValues({
|
|
1810
|
+
key: atom2.key,
|
|
1811
|
+
type: `atom_update`
|
|
1812
|
+
}, update));
|
|
1813
|
+
store.logger.info(
|
|
1814
|
+
`\u231B`,
|
|
1815
|
+
`timeline`,
|
|
1816
|
+
tl.key,
|
|
1817
|
+
`set selector_update "${currentSelectorKey}" to`,
|
|
1818
|
+
latestUpdate == null ? void 0 : latestUpdate.atomUpdates.map((atomUpdate) => atomUpdate.key)
|
|
1819
|
+
);
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
if (latestUpdate) {
|
|
1823
|
+
const willCaptureSelectorUpdate = (_d = (_c = tl.shouldCapture) == null ? void 0 : _c.call(tl, latestUpdate, tl)) != null ? _d : true;
|
|
1824
|
+
if (willCaptureSelectorUpdate) {
|
|
1825
|
+
tl.subject.next(latestUpdate);
|
|
1826
|
+
} else {
|
|
1827
|
+
tl.history.pop();
|
|
1828
|
+
tl.at = tl.history.length;
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
} else {
|
|
1832
|
+
const timestamp = Date.now();
|
|
1833
|
+
tl.selectorTime = null;
|
|
1834
|
+
if (tl.at !== tl.history.length) {
|
|
1835
|
+
tl.history.splice(tl.at);
|
|
1836
|
+
}
|
|
1837
|
+
const atomUpdate = {
|
|
1838
|
+
type: `atom_update`,
|
|
1839
|
+
timestamp,
|
|
1840
|
+
key: atom2.key,
|
|
1841
|
+
oldValue: update.oldValue,
|
|
1842
|
+
newValue: update.newValue
|
|
1843
|
+
};
|
|
1844
|
+
if (atom2.family) {
|
|
1845
|
+
atomUpdate.family = atom2.family;
|
|
1846
|
+
}
|
|
1847
|
+
const willCapture = (_f = (_e = tl.shouldCapture) == null ? void 0 : _e.call(tl, atomUpdate, tl)) != null ? _f : true;
|
|
1848
|
+
store.logger.info(
|
|
1849
|
+
`\u231B`,
|
|
1850
|
+
`timeline`,
|
|
1851
|
+
tl.key,
|
|
1852
|
+
`got an atom_update to "${atom2.key}"`
|
|
1853
|
+
);
|
|
1854
|
+
if (willCapture) {
|
|
1855
|
+
tl.history.push(atomUpdate);
|
|
1856
|
+
tl.at = tl.history.length;
|
|
1857
|
+
tl.subject.next(atomUpdate);
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
});
|
|
1862
|
+
};
|
|
1863
|
+
|
|
1864
|
+
// internal/src/timeline/create-timeline.ts
|
|
1865
|
+
function createTimeline(options, store, data) {
|
|
1866
|
+
var _a, _b;
|
|
1867
|
+
const tl = __spreadProps(__spreadValues({
|
|
1868
|
+
type: `timeline`,
|
|
1869
|
+
key: options.key,
|
|
1870
|
+
at: 0,
|
|
1871
|
+
timeTraveling: null,
|
|
1872
|
+
selectorTime: null,
|
|
1873
|
+
transactionKey: null
|
|
1874
|
+
}, data), {
|
|
1875
|
+
history: (_a = data == null ? void 0 : data.history.map((update) => __spreadValues({}, update))) != null ? _a : [],
|
|
1876
|
+
install: (store2) => createTimeline(options, store2, tl),
|
|
1877
|
+
subject: new Subject()
|
|
1878
|
+
});
|
|
1879
|
+
if (options.shouldCapture) {
|
|
1880
|
+
tl.shouldCapture = options.shouldCapture;
|
|
1881
|
+
}
|
|
1882
|
+
const target = newest(store);
|
|
1883
|
+
for (const tokenOrFamily of options.atoms) {
|
|
1884
|
+
const timelineKey = target.timelineAtoms.getRelatedKey(tokenOrFamily.key);
|
|
1885
|
+
if (timelineKey) {
|
|
1886
|
+
store.logger.error(
|
|
1887
|
+
`\u274C`,
|
|
1888
|
+
`timeline`,
|
|
1889
|
+
options.key,
|
|
1890
|
+
`Failed to add atom "${tokenOrFamily.key}" because it already belongs to timeline "${timelineKey}"`
|
|
1891
|
+
);
|
|
1892
|
+
continue;
|
|
1893
|
+
}
|
|
1894
|
+
if (tokenOrFamily.type === `atom_family`) {
|
|
1895
|
+
const family = tokenOrFamily;
|
|
1896
|
+
family.subject.subscribe(`timeline:${options.key}`, (token2) => {
|
|
1897
|
+
addAtomToTimeline(token2, tl, store);
|
|
1898
|
+
});
|
|
1899
|
+
for (const atom2 of target.atoms.values()) {
|
|
1900
|
+
if (((_b = atom2.family) == null ? void 0 : _b.key) === family.key) {
|
|
1901
|
+
addAtomToTimeline(atom2, tl, store);
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
} else {
|
|
1905
|
+
const token2 = tokenOrFamily;
|
|
1906
|
+
if (`family` in token2 && token2.family) {
|
|
1907
|
+
const familyTimelineKey = target.timelineAtoms.getRelatedKey(
|
|
1908
|
+
token2.family.key
|
|
1909
|
+
);
|
|
1910
|
+
if (familyTimelineKey) {
|
|
1911
|
+
store.logger.error(
|
|
1912
|
+
`\u274C`,
|
|
1913
|
+
`timeline`,
|
|
1914
|
+
options.key,
|
|
1915
|
+
`Failed to add atom "${token2.key}" because its family "${token2.family.key}" already belongs to timeline "${familyTimelineKey}"`
|
|
1916
|
+
);
|
|
1917
|
+
continue;
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
addAtomToTimeline(token2, tl, store);
|
|
1921
|
+
}
|
|
1922
|
+
target.timelineAtoms = target.timelineAtoms.set({
|
|
1923
|
+
atomKey: tokenOrFamily.key,
|
|
1924
|
+
timelineKey: options.key
|
|
1925
|
+
});
|
|
1926
|
+
}
|
|
1927
|
+
store.timelines.set(options.key, tl);
|
|
1928
|
+
const token = {
|
|
1929
|
+
key: options.key,
|
|
1930
|
+
type: `timeline`
|
|
1931
|
+
};
|
|
1932
|
+
store.subject.timelineCreation.next(token);
|
|
1933
|
+
return token;
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
// internal/src/timeline/time-travel.ts
|
|
1937
|
+
var timeTravel = (direction, token, store) => {
|
|
1938
|
+
const action = direction === `forward` ? `redo` : `undo`;
|
|
1939
|
+
store.logger.info(
|
|
1940
|
+
direction === `forward` ? `\u23E9` : `\u23EA`,
|
|
1941
|
+
`timeline`,
|
|
1942
|
+
token.key,
|
|
1943
|
+
action
|
|
1944
|
+
);
|
|
1945
|
+
const timelineData = store.timelines.get(token.key);
|
|
1946
|
+
if (!timelineData) {
|
|
1947
|
+
store.logger.error(
|
|
1948
|
+
`\u{1F41E}`,
|
|
1949
|
+
`timeline`,
|
|
1950
|
+
token.key,
|
|
1951
|
+
`Failed to ${action}. This timeline has not been initialized.`
|
|
1952
|
+
);
|
|
1953
|
+
return;
|
|
1954
|
+
}
|
|
1955
|
+
if (direction === `forward` && timelineData.at === timelineData.history.length || direction === `backward` && timelineData.at === 0) {
|
|
1956
|
+
store.logger.warn(
|
|
1957
|
+
`\u{1F481}`,
|
|
1958
|
+
`timeline`,
|
|
1959
|
+
token.key,
|
|
1960
|
+
`Failed to ${action} at the ${direction === `forward` ? `end` : `beginning`} of timeline "${token.key}". There is nothing to ${action}.`
|
|
1961
|
+
);
|
|
1962
|
+
return;
|
|
1963
|
+
}
|
|
1964
|
+
timelineData.timeTraveling = direction === `forward` ? `into_future` : `into_past`;
|
|
1965
|
+
if (direction === `backward`) {
|
|
1966
|
+
--timelineData.at;
|
|
1967
|
+
}
|
|
1968
|
+
const update = timelineData.history[timelineData.at];
|
|
1969
|
+
const updateValues = (atomUpdate) => {
|
|
1970
|
+
const { key, newValue, oldValue } = atomUpdate;
|
|
1971
|
+
const value = direction === `forward` ? newValue : oldValue;
|
|
1972
|
+
setState({ key, type: `atom` }, value, store);
|
|
1973
|
+
};
|
|
1974
|
+
const updateValuesFromTransactionUpdate = (transactionUpdate) => {
|
|
1975
|
+
const updates = direction === `forward` ? transactionUpdate.updates : [...transactionUpdate.updates].reverse();
|
|
1976
|
+
for (const updateFromTransaction of updates) {
|
|
1977
|
+
if (`newValue` in updateFromTransaction) {
|
|
1978
|
+
updateValues(updateFromTransaction);
|
|
1979
|
+
} else {
|
|
1980
|
+
updateValuesFromTransactionUpdate(updateFromTransaction);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
};
|
|
1984
|
+
switch (update.type) {
|
|
1985
|
+
case `atom_update`: {
|
|
1986
|
+
updateValues(update);
|
|
1987
|
+
break;
|
|
1988
|
+
}
|
|
1989
|
+
case `selector_update`: {
|
|
1990
|
+
const updates = direction === `forward` ? update.atomUpdates : [...update.atomUpdates].reverse();
|
|
1991
|
+
for (const atomUpdate of updates) {
|
|
1992
|
+
updateValues(atomUpdate);
|
|
1993
|
+
}
|
|
1994
|
+
break;
|
|
1995
|
+
}
|
|
1996
|
+
case `transaction_update`: {
|
|
1997
|
+
updateValuesFromTransactionUpdate(update);
|
|
1998
|
+
break;
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
if (direction === `forward`) {
|
|
2002
|
+
++timelineData.at;
|
|
2003
|
+
}
|
|
2004
|
+
timelineData.subject.next(action);
|
|
2005
|
+
timelineData.timeTraveling = null;
|
|
2006
|
+
store.logger.info(
|
|
2007
|
+
`\u23F9\uFE0F`,
|
|
2008
|
+
`timeline`,
|
|
2009
|
+
token.key,
|
|
2010
|
+
`"${token.key}" is now at ${timelineData.at} / ${timelineData.history.length}`
|
|
2011
|
+
);
|
|
2012
|
+
};
|
|
2013
|
+
|
|
2014
|
+
// internal/src/transaction/abort-transaction.ts
|
|
2015
|
+
var abortTransaction = (store) => {
|
|
2016
|
+
const target = newest(store);
|
|
2017
|
+
if (target.transactionMeta === null || target.parent === null) {
|
|
2018
|
+
store.logger.warn(
|
|
2019
|
+
`\u{1F41E}`,
|
|
2020
|
+
`transaction`,
|
|
2021
|
+
`???`,
|
|
2022
|
+
`abortTransaction called outside of a transaction. This is probably a bug in AtomIO.`
|
|
2023
|
+
);
|
|
2024
|
+
return;
|
|
2025
|
+
}
|
|
2026
|
+
store.logger.info(
|
|
2027
|
+
`\u{1FA82}`,
|
|
2028
|
+
`transaction`,
|
|
2029
|
+
target.transactionMeta.update.key,
|
|
2030
|
+
`Aborting transaction`
|
|
2031
|
+
);
|
|
2032
|
+
target.parent.child = null;
|
|
2033
|
+
};
|
|
2034
|
+
|
|
2035
|
+
// internal/src/transaction/apply-transaction.ts
|
|
2036
|
+
function ingestAtomUpdate(update, parent, child) {
|
|
2037
|
+
var _a, _b, _c;
|
|
2038
|
+
const { key, newValue } = update;
|
|
2039
|
+
const token = { key, type: `atom` };
|
|
2040
|
+
if (!parent.valueMap.has(token.key)) {
|
|
2041
|
+
if (token.family) {
|
|
2042
|
+
const family = parent.families.get(token.family.key);
|
|
2043
|
+
if (family) {
|
|
2044
|
+
family(token.family.subKey);
|
|
2045
|
+
}
|
|
2046
|
+
} else {
|
|
2047
|
+
const newAtom = child.atoms.get(token.key);
|
|
2048
|
+
if (!newAtom) {
|
|
2049
|
+
throw new Error(
|
|
2050
|
+
`Absurd Error: Atom "${token.key}" not found while copying updates from transaction "${(_a = child.transactionMeta) == null ? void 0 : _a.update.key}" to store "${parent.config.name}"`
|
|
2051
|
+
);
|
|
2052
|
+
}
|
|
2053
|
+
parent.atoms.set(newAtom.key, newAtom);
|
|
2054
|
+
parent.valueMap.set(newAtom.key, newAtom.default);
|
|
2055
|
+
parent.logger.info(
|
|
2056
|
+
`\u{1F528}`,
|
|
2057
|
+
`transaction`,
|
|
2058
|
+
(_c = (_b = child.transactionMeta) == null ? void 0 : _b.update.key) != null ? _c : `???`,
|
|
2059
|
+
`Adding atom "${newAtom.key}"`
|
|
2060
|
+
);
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
setState(token, newValue, parent);
|
|
2064
|
+
}
|
|
2065
|
+
function ingestTransactionUpdate(transactionUpdate, parent, child) {
|
|
2066
|
+
for (const update of transactionUpdate.updates) {
|
|
2067
|
+
if (`newValue` in update) {
|
|
2068
|
+
ingestAtomUpdate(update, parent, child);
|
|
2069
|
+
} else {
|
|
2070
|
+
ingestTransactionUpdate(update, parent, child);
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
var applyTransaction = (output, store) => {
|
|
2075
|
+
var _a;
|
|
2076
|
+
const child = newest(store);
|
|
2077
|
+
const { parent } = child;
|
|
2078
|
+
if (parent === null || child.transactionMeta === null || ((_a = child.transactionMeta) == null ? void 0 : _a.phase) !== `building`) {
|
|
2079
|
+
store.logger.warn(
|
|
2080
|
+
`\u{1F41E}`,
|
|
2081
|
+
`transaction`,
|
|
2082
|
+
`???`,
|
|
2083
|
+
`applyTransaction called outside of a transaction. This is probably a bug in AtomIO.`
|
|
2084
|
+
);
|
|
2085
|
+
return;
|
|
2086
|
+
}
|
|
2087
|
+
child.transactionMeta.phase = `applying`;
|
|
2088
|
+
child.transactionMeta.update.output = output;
|
|
2089
|
+
parent.child = null;
|
|
2090
|
+
parent.subject.transactionApplying.next(child.transactionMeta);
|
|
2091
|
+
const { updates } = child.transactionMeta.update;
|
|
2092
|
+
store.logger.info(
|
|
2093
|
+
`\u{1F6C4}`,
|
|
2094
|
+
`transaction`,
|
|
2095
|
+
child.transactionMeta.update.key,
|
|
2096
|
+
`Applying transaction with ${updates.length} updates:`,
|
|
2097
|
+
updates
|
|
2098
|
+
);
|
|
2099
|
+
if (parent.transactionMeta === null) {
|
|
2100
|
+
ingestTransactionUpdate(child.transactionMeta.update, parent, child);
|
|
2101
|
+
const myTransaction = withdraw(
|
|
2102
|
+
{ key: child.transactionMeta.update.key, type: `transaction` },
|
|
2103
|
+
store
|
|
2104
|
+
);
|
|
2105
|
+
myTransaction == null ? void 0 : myTransaction.subject.next(child.transactionMeta.update);
|
|
2106
|
+
store.logger.info(
|
|
2107
|
+
`\u{1F6EC}`,
|
|
2108
|
+
`transaction`,
|
|
2109
|
+
child.transactionMeta.update.key,
|
|
2110
|
+
`Finished applying transaction.`
|
|
2111
|
+
);
|
|
2112
|
+
} else {
|
|
2113
|
+
ingestTransactionUpdate(child.transactionMeta.update, parent, child);
|
|
2114
|
+
parent.transactionMeta.update.updates.push(child.transactionMeta.update);
|
|
2115
|
+
}
|
|
2116
|
+
parent.subject.transactionApplying.next(null);
|
|
2117
|
+
};
|
|
2118
|
+
|
|
2119
|
+
// internal/src/transaction/build-transaction.ts
|
|
2120
|
+
var buildTransaction = (key, params, store) => {
|
|
2121
|
+
const parent = newest(store);
|
|
2122
|
+
parent.child = {
|
|
2123
|
+
parent,
|
|
2124
|
+
child: null,
|
|
2125
|
+
subject: parent.subject,
|
|
2126
|
+
loggers: parent.loggers,
|
|
2127
|
+
logger: parent.logger,
|
|
2128
|
+
config: parent.config,
|
|
2129
|
+
transactionMeta: {
|
|
2130
|
+
phase: `building`,
|
|
2131
|
+
time: Date.now(),
|
|
2132
|
+
update: {
|
|
2133
|
+
key,
|
|
2134
|
+
updates: [],
|
|
2135
|
+
params,
|
|
2136
|
+
output: void 0
|
|
2137
|
+
}
|
|
2138
|
+
},
|
|
2139
|
+
atoms: new LazyMap(parent.atoms),
|
|
2140
|
+
atomsThatAreDefault: new Set(parent.atomsThatAreDefault),
|
|
2141
|
+
families: new LazyMap(parent.families),
|
|
2142
|
+
operation: { open: false },
|
|
2143
|
+
readonlySelectors: new LazyMap(parent.readonlySelectors),
|
|
2144
|
+
timelines: new LazyMap(parent.timelines),
|
|
2145
|
+
timelineAtoms: new Junction(parent.timelineAtoms.toJSON()),
|
|
2146
|
+
trackers: /* @__PURE__ */ new Map(),
|
|
2147
|
+
transactions: new LazyMap(parent.transactions),
|
|
2148
|
+
selectorAtoms: new Junction(parent.selectorAtoms.toJSON()),
|
|
2149
|
+
selectorGraph: new Junction(parent.selectorGraph.toJSON(), {
|
|
2150
|
+
makeContentKey: (...keys) => keys.sort().join(`:`)
|
|
2151
|
+
}),
|
|
2152
|
+
selectors: new LazyMap(parent.selectors),
|
|
2153
|
+
valueMap: new LazyMap(parent.valueMap)
|
|
2154
|
+
};
|
|
2155
|
+
store.logger.info(
|
|
2156
|
+
`\u{1F6EB}`,
|
|
2157
|
+
`transaction`,
|
|
2158
|
+
key,
|
|
2159
|
+
`Building transaction with params:`,
|
|
2160
|
+
params
|
|
2161
|
+
);
|
|
2162
|
+
};
|
|
2163
|
+
|
|
2164
|
+
// internal/src/transaction/create-transaction.ts
|
|
2165
|
+
function createTransaction(options, store) {
|
|
2166
|
+
const newTransaction = {
|
|
2167
|
+
key: options.key,
|
|
2168
|
+
type: `transaction`,
|
|
2169
|
+
run: (...params) => {
|
|
2170
|
+
buildTransaction(options.key, params, store);
|
|
2171
|
+
try {
|
|
2172
|
+
const output = options.do(
|
|
2173
|
+
{
|
|
2174
|
+
get: (token2) => getState(token2, store),
|
|
2175
|
+
set: (token2, value) => setState(token2, value, store),
|
|
2176
|
+
run: (token2) => runTransaction(token2, store)
|
|
2177
|
+
},
|
|
2178
|
+
...params
|
|
2179
|
+
);
|
|
2180
|
+
applyTransaction(output, store);
|
|
2181
|
+
return output;
|
|
2182
|
+
} catch (thrown) {
|
|
2183
|
+
abortTransaction(store);
|
|
2184
|
+
store.logger.warn(`\u{1F4A5}`, `transaction`, options.key, `caught:`, thrown);
|
|
2185
|
+
throw thrown;
|
|
2186
|
+
}
|
|
2187
|
+
},
|
|
2188
|
+
install: (store2) => createTransaction(options, store2),
|
|
2189
|
+
subject: new Subject()
|
|
2190
|
+
};
|
|
2191
|
+
const target = newest(store);
|
|
2192
|
+
target.transactions.set(newTransaction.key, newTransaction);
|
|
2193
|
+
const token = deposit(newTransaction);
|
|
2194
|
+
store.subject.transactionCreation.next(token);
|
|
2195
|
+
return token;
|
|
2196
|
+
}
|
|
24
2197
|
|
|
25
2198
|
// src/atom.ts
|
|
26
2199
|
function atom(options) {
|
|
27
2200
|
if (`mutable` in options) {
|
|
28
|
-
return
|
|
2201
|
+
return createMutableAtom(options, IMPLICIT.STORE);
|
|
29
2202
|
}
|
|
30
|
-
return
|
|
2203
|
+
return createAtom(options, void 0, IMPLICIT.STORE);
|
|
31
2204
|
}
|
|
32
2205
|
function atomFamily(options) {
|
|
33
2206
|
if (`mutable` in options) {
|
|
34
|
-
return
|
|
2207
|
+
return createMutableAtomFamily(options, IMPLICIT.STORE);
|
|
35
2208
|
}
|
|
36
|
-
return
|
|
2209
|
+
return createAtomFamily(options, IMPLICIT.STORE);
|
|
37
2210
|
}
|
|
38
|
-
|
|
2211
|
+
|
|
2212
|
+
// src/dispose.ts
|
|
2213
|
+
function dispose(token, store = IMPLICIT.STORE) {
|
|
39
2214
|
switch (token.type) {
|
|
40
2215
|
case `atom`:
|
|
41
|
-
|
|
2216
|
+
deleteAtom(token, store);
|
|
42
2217
|
break;
|
|
43
2218
|
case `selector`:
|
|
44
2219
|
case `readonly_selector`:
|
|
45
|
-
|
|
2220
|
+
deleteSelector(token, store);
|
|
46
2221
|
break;
|
|
47
2222
|
}
|
|
48
2223
|
}
|
|
49
|
-
|
|
2224
|
+
|
|
2225
|
+
// src/get-state.ts
|
|
2226
|
+
function getState(token, store = IMPLICIT.STORE) {
|
|
50
2227
|
var _a;
|
|
51
|
-
const state = (_a =
|
|
2228
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
52
2229
|
if (state === void 0) {
|
|
53
|
-
throw new
|
|
2230
|
+
throw new NotFoundError(token, store);
|
|
54
2231
|
}
|
|
55
|
-
return
|
|
2232
|
+
return readOrComputeValue(state, store);
|
|
56
2233
|
}
|
|
57
2234
|
|
|
58
2235
|
// src/logger.ts
|
|
@@ -90,78 +2267,90 @@ var AtomIOLogger = class {
|
|
|
90
2267
|
};
|
|
91
2268
|
}
|
|
92
2269
|
};
|
|
2270
|
+
|
|
2271
|
+
// src/selector.ts
|
|
93
2272
|
function selector(options) {
|
|
94
|
-
return
|
|
2273
|
+
return createSelector(options, void 0, IMPLICIT.STORE);
|
|
95
2274
|
}
|
|
96
2275
|
function selectorFamily(options) {
|
|
97
|
-
return
|
|
2276
|
+
return createSelectorFamily(options, IMPLICIT.STORE);
|
|
98
2277
|
}
|
|
99
|
-
|
|
2278
|
+
|
|
2279
|
+
// src/set-state.ts
|
|
2280
|
+
function setState(token, value, store = IMPLICIT.STORE) {
|
|
100
2281
|
var _a;
|
|
101
|
-
const rejection =
|
|
2282
|
+
const rejection = openOperation(token, store);
|
|
102
2283
|
if (rejection) {
|
|
103
2284
|
return;
|
|
104
2285
|
}
|
|
105
|
-
const state = (_a =
|
|
2286
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
106
2287
|
if (state === void 0) {
|
|
107
|
-
throw new
|
|
2288
|
+
throw new NotFoundError(token, store);
|
|
108
2289
|
}
|
|
109
|
-
|
|
110
|
-
|
|
2290
|
+
setAtomOrSelector(state, value, store);
|
|
2291
|
+
closeOperation(store);
|
|
111
2292
|
}
|
|
2293
|
+
|
|
2294
|
+
// src/silo.ts
|
|
112
2295
|
var Silo = class {
|
|
113
2296
|
constructor(name, fromStore = null) {
|
|
114
|
-
const s = new
|
|
2297
|
+
const s = new Store(name, fromStore);
|
|
115
2298
|
this.store = s;
|
|
116
2299
|
this.atom = (options) => {
|
|
117
2300
|
if (`mutable` in options) {
|
|
118
|
-
return
|
|
2301
|
+
return createMutableAtom(options, s);
|
|
119
2302
|
}
|
|
120
|
-
return
|
|
2303
|
+
return createAtom(options, void 0, s);
|
|
121
2304
|
};
|
|
122
2305
|
this.atomFamily = (options) => {
|
|
123
2306
|
if (`mutable` in options) {
|
|
124
|
-
return
|
|
2307
|
+
return createMutableAtomFamily(options, s);
|
|
125
2308
|
}
|
|
126
|
-
return
|
|
2309
|
+
return createAtomFamily(options, s);
|
|
127
2310
|
};
|
|
128
|
-
this.selector = (options) =>
|
|
129
|
-
this.selectorFamily = (options) =>
|
|
130
|
-
this.transaction = (options) =>
|
|
131
|
-
this.timeline = (options) =>
|
|
2311
|
+
this.selector = (options) => createSelector(options, void 0, s);
|
|
2312
|
+
this.selectorFamily = (options) => createSelectorFamily(options, s);
|
|
2313
|
+
this.transaction = (options) => createTransaction(options, s);
|
|
2314
|
+
this.timeline = (options) => createTimeline(options, s);
|
|
132
2315
|
this.getState = (token) => getState(token, s);
|
|
133
2316
|
this.setState = (token, newValue) => setState(token, newValue, s);
|
|
134
2317
|
this.subscribe = (token, handler, key) => subscribe(token, handler, key, s);
|
|
135
|
-
this.undo = (token) =>
|
|
136
|
-
this.redo = (token) =>
|
|
2318
|
+
this.undo = (token) => timeTravel(`backward`, token, s);
|
|
2319
|
+
this.redo = (token) => timeTravel(`forward`, token, s);
|
|
137
2320
|
}
|
|
138
2321
|
};
|
|
139
|
-
|
|
2322
|
+
|
|
2323
|
+
// src/subscribe.ts
|
|
2324
|
+
function subscribe(token, handleUpdate, key = Math.random().toString(36).slice(2), store = IMPLICIT.STORE) {
|
|
140
2325
|
switch (token.type) {
|
|
141
2326
|
case `atom`:
|
|
142
2327
|
case `readonly_selector`:
|
|
143
2328
|
case `selector`:
|
|
144
|
-
return
|
|
2329
|
+
return subscribeToState(token, handleUpdate, key, store);
|
|
145
2330
|
case `transaction`:
|
|
146
|
-
return
|
|
2331
|
+
return subscribeToTransaction(token, handleUpdate, key, store);
|
|
147
2332
|
case `timeline`:
|
|
148
|
-
return
|
|
2333
|
+
return subscribeToTimeline(token, handleUpdate, key, store);
|
|
149
2334
|
}
|
|
150
2335
|
}
|
|
2336
|
+
|
|
2337
|
+
// src/timeline.ts
|
|
151
2338
|
var timeline = (options) => {
|
|
152
|
-
return
|
|
2339
|
+
return createTimeline(options, IMPLICIT.STORE);
|
|
153
2340
|
};
|
|
154
2341
|
var redo = (timeline2) => {
|
|
155
|
-
|
|
2342
|
+
timeTravel(`forward`, timeline2, IMPLICIT.STORE);
|
|
156
2343
|
};
|
|
157
2344
|
var undo = (timeline2) => {
|
|
158
|
-
|
|
2345
|
+
timeTravel(`backward`, timeline2, IMPLICIT.STORE);
|
|
159
2346
|
};
|
|
2347
|
+
|
|
2348
|
+
// src/transaction.ts
|
|
160
2349
|
function transaction(options) {
|
|
161
|
-
return
|
|
2350
|
+
return createTransaction(options, IMPLICIT.STORE);
|
|
162
2351
|
}
|
|
163
|
-
var runTransaction = (token, store =
|
|
164
|
-
const tx =
|
|
2352
|
+
var runTransaction = (token, store = IMPLICIT.STORE) => (...parameters) => {
|
|
2353
|
+
const tx = withdraw(token, store);
|
|
165
2354
|
if (tx) {
|
|
166
2355
|
return tx.run(...parameters);
|
|
167
2356
|
}
|