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/data/dist/index.cjs
CHANGED
|
@@ -1,28 +1,500 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var internal = require('atom.io/internal');
|
|
4
|
-
var atom_io = require('atom.io');
|
|
5
3
|
var setRtx = require('atom.io/transceivers/set-rtx');
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __defProps = Object.defineProperties;
|
|
7
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
|
|
25
|
+
// src/get-state.ts
|
|
26
|
+
function getState(token, store = IMPLICIT.STORE) {
|
|
27
|
+
var _a;
|
|
28
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
29
|
+
if (state === void 0) {
|
|
30
|
+
throw new NotFoundError(token, store);
|
|
31
|
+
}
|
|
32
|
+
return readOrComputeValue(state, store);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/logger.ts
|
|
36
|
+
var simpleLog = (logLevel) => (icon, tokenType, tokenKey, message, ...rest) => {
|
|
37
|
+
console[logLevel](`${icon} ${tokenType} "${tokenKey}" ${message}`, ...rest);
|
|
38
|
+
};
|
|
39
|
+
var simpleLogger = {
|
|
40
|
+
error: simpleLog(`error`),
|
|
41
|
+
info: simpleLog(`info`),
|
|
42
|
+
warn: simpleLog(`warn`)
|
|
43
|
+
};
|
|
44
|
+
var AtomIOLogger = class {
|
|
45
|
+
constructor(logLevel, filter, logger = simpleLogger) {
|
|
46
|
+
this.logLevel = logLevel;
|
|
47
|
+
this.filter = filter;
|
|
48
|
+
this.logger = logger;
|
|
49
|
+
this.error = (...args) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== null) {
|
|
52
|
+
this.logger.error(...args);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
this.info = (...args) => {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel === `info`) {
|
|
58
|
+
this.logger.info(...args);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
this.warn = (...args) => {
|
|
62
|
+
var _a, _b;
|
|
63
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== `error` && this.logLevel !== null) {
|
|
64
|
+
this.logger.warn(...args);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/set-state.ts
|
|
71
|
+
function setState(token, value, store = IMPLICIT.STORE) {
|
|
72
|
+
var _a;
|
|
73
|
+
const rejection = openOperation(token, store);
|
|
74
|
+
if (rejection) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
78
|
+
if (state === void 0) {
|
|
79
|
+
throw new NotFoundError(token, store);
|
|
80
|
+
}
|
|
81
|
+
setAtomOrSelector(state, value, store);
|
|
82
|
+
closeOperation(store);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// internal/src/future.ts
|
|
86
|
+
var Future = class extends Promise {
|
|
87
|
+
constructor(executor) {
|
|
88
|
+
super((resolve, reject) => {
|
|
89
|
+
const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
|
|
90
|
+
const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
|
|
91
|
+
if (typeof executor === `function`) {
|
|
92
|
+
executor(pass, fail);
|
|
93
|
+
} else {
|
|
94
|
+
executor.then(pass, fail);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
this.isCanceled = false;
|
|
98
|
+
}
|
|
99
|
+
cancel() {
|
|
100
|
+
this.isCanceled = true;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// internal/src/lineage.ts
|
|
105
|
+
function newest(scion) {
|
|
106
|
+
while (scion.child !== null) {
|
|
107
|
+
scion = scion.child;
|
|
108
|
+
}
|
|
109
|
+
return scion;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// internal/src/caching.ts
|
|
113
|
+
function cacheValue(key, value, subject, store) {
|
|
114
|
+
const target = newest(store);
|
|
115
|
+
const currentValue = target.valueMap.get(key);
|
|
116
|
+
if (currentValue instanceof Future) {
|
|
117
|
+
currentValue.cancel();
|
|
118
|
+
}
|
|
119
|
+
if (value instanceof Promise) {
|
|
120
|
+
const future = new Future(value);
|
|
121
|
+
newest(store).valueMap.set(key, future);
|
|
122
|
+
future.then((resolved) => {
|
|
123
|
+
if (future.isCanceled) {
|
|
124
|
+
return;
|
|
18
125
|
}
|
|
126
|
+
cacheValue(key, resolved, subject, store);
|
|
127
|
+
subject.next({ newValue: resolved, oldValue: future });
|
|
128
|
+
}).catch((thrown) => {
|
|
129
|
+
if (thrown !== `canceled`) {
|
|
130
|
+
store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
return future;
|
|
134
|
+
}
|
|
135
|
+
target.valueMap.set(key, value);
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
var readCachedValue = (key, store) => {
|
|
139
|
+
return newest(store).valueMap.get(key);
|
|
140
|
+
};
|
|
141
|
+
var isValueCached = (key, store) => {
|
|
142
|
+
return newest(store).valueMap.has(key);
|
|
143
|
+
};
|
|
144
|
+
var evictCachedValue = (key, store) => {
|
|
145
|
+
const core = newest(store);
|
|
146
|
+
const currentValue = core.valueMap.get(key);
|
|
147
|
+
if (currentValue instanceof Future) {
|
|
148
|
+
currentValue.cancel();
|
|
149
|
+
}
|
|
150
|
+
if (core.operation.open) {
|
|
151
|
+
core.operation.prev.set(key, currentValue);
|
|
152
|
+
}
|
|
153
|
+
core.valueMap.delete(key);
|
|
154
|
+
store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// ../anvl/src/json/index.ts
|
|
158
|
+
var parseJson = (str) => JSON.parse(str);
|
|
159
|
+
var stringifyJson = (json) => JSON.stringify(json);
|
|
160
|
+
|
|
161
|
+
// json/src/select-json.ts
|
|
162
|
+
var selectJson = (atom, transform, store = IMPLICIT.STORE) => {
|
|
163
|
+
return createSelector(
|
|
164
|
+
{
|
|
165
|
+
key: `${atom.key}:JSON`,
|
|
166
|
+
get: ({ get }) => transform.toJson(get(atom)),
|
|
167
|
+
set: ({ set }, newValue) => set(atom, transform.fromJson(newValue))
|
|
19
168
|
},
|
|
20
169
|
void 0,
|
|
21
170
|
store
|
|
22
171
|
);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// json/src/select-json-family.ts
|
|
175
|
+
var selectJsonFamily = (atomFamily, transform, store = IMPLICIT.STORE) => {
|
|
176
|
+
const jsonFamily = createSelectorFamily(
|
|
177
|
+
{
|
|
178
|
+
key: `${atomFamily.key}:JSON`,
|
|
179
|
+
get: (key) => ({ get }) => transform.toJson(get(atomFamily(key))),
|
|
180
|
+
set: (key) => ({ set }, newValue) => set(atomFamily(key), transform.fromJson(newValue))
|
|
181
|
+
},
|
|
182
|
+
store
|
|
183
|
+
);
|
|
184
|
+
atomFamily.subject.subscribe(
|
|
185
|
+
`store=${store.config.name}::json-selector-family`,
|
|
186
|
+
(token) => {
|
|
187
|
+
if (token.family) {
|
|
188
|
+
jsonFamily(parseJson(token.family.subKey));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
return jsonFamily;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// internal/src/read-or-compute-value.ts
|
|
196
|
+
var readOrComputeValue = (state, store) => {
|
|
197
|
+
if (isValueCached(state.key, store)) {
|
|
198
|
+
store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
|
|
199
|
+
return readCachedValue(state.key, store);
|
|
200
|
+
}
|
|
201
|
+
if (state.type !== `atom`) {
|
|
202
|
+
store.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
|
|
203
|
+
return state.get();
|
|
204
|
+
}
|
|
205
|
+
const fallback = state.default instanceof Function ? state.default() : state.default;
|
|
206
|
+
store.logger.info(
|
|
207
|
+
`\u{1F481}`,
|
|
208
|
+
`atom`,
|
|
209
|
+
state.key,
|
|
210
|
+
`could not find cached value; using default`,
|
|
211
|
+
fallback
|
|
212
|
+
);
|
|
213
|
+
return state.default instanceof Function ? state.default() : state.default;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
// internal/src/operation.ts
|
|
217
|
+
var openOperation = (token, store) => {
|
|
218
|
+
const target = newest(store);
|
|
219
|
+
if (target.operation.open) {
|
|
220
|
+
store.logger.error(
|
|
221
|
+
`\u274C`,
|
|
222
|
+
token.type,
|
|
223
|
+
token.key,
|
|
224
|
+
`failed to setState during a setState for "${target.operation.token.key}"`
|
|
225
|
+
);
|
|
226
|
+
return `rejection`;
|
|
227
|
+
}
|
|
228
|
+
target.operation = {
|
|
229
|
+
open: true,
|
|
230
|
+
done: /* @__PURE__ */ new Set(),
|
|
231
|
+
prev: /* @__PURE__ */ new Map(),
|
|
232
|
+
time: Date.now(),
|
|
233
|
+
token
|
|
234
|
+
};
|
|
235
|
+
store.logger.info(
|
|
236
|
+
`\u2B55`,
|
|
237
|
+
token.type,
|
|
238
|
+
token.key,
|
|
239
|
+
`operation start in store "${store.config.name}"${target.transactionMeta === null ? `` : ` ${target.transactionMeta.phase} "${target.transactionMeta.update.key}"`}`
|
|
240
|
+
);
|
|
241
|
+
};
|
|
242
|
+
var closeOperation = (store) => {
|
|
243
|
+
const target = newest(store);
|
|
244
|
+
if (target.operation.open) {
|
|
245
|
+
store.logger.info(
|
|
246
|
+
`\u{1F534}`,
|
|
247
|
+
target.operation.token.type,
|
|
248
|
+
target.operation.token.key,
|
|
249
|
+
`operation done in store "${store.config.name}"`
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
target.operation = { open: false };
|
|
253
|
+
store.subject.operationStatus.next(target.operation);
|
|
254
|
+
};
|
|
255
|
+
var isDone = (key, store) => {
|
|
256
|
+
const target = newest(store);
|
|
257
|
+
if (!target.operation.open) {
|
|
258
|
+
store.logger.warn(
|
|
259
|
+
`\u{1F41E}`,
|
|
260
|
+
`unknown`,
|
|
261
|
+
key,
|
|
262
|
+
`isDone called outside of an operation. This is probably a bug.`
|
|
263
|
+
);
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
return target.operation.done.has(key);
|
|
267
|
+
};
|
|
268
|
+
var markDone = (key, store) => {
|
|
269
|
+
const target = newest(store);
|
|
270
|
+
if (!target.operation.open) {
|
|
271
|
+
store.logger.warn(
|
|
272
|
+
`\u{1F41E}`,
|
|
273
|
+
`unknown`,
|
|
274
|
+
key,
|
|
275
|
+
`markDone called outside of an operation. This is probably a bug.`
|
|
276
|
+
);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
target.operation.done.add(key);
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// internal/src/set-state/become.ts
|
|
283
|
+
var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
|
|
284
|
+
originalThing instanceof Function ? originalThing() : originalThing
|
|
285
|
+
) : nextVersionOfThing;
|
|
286
|
+
|
|
287
|
+
// internal/src/subject.ts
|
|
288
|
+
var Subject = class {
|
|
289
|
+
constructor() {
|
|
290
|
+
this.subscribers = /* @__PURE__ */ new Map();
|
|
291
|
+
}
|
|
292
|
+
subscribe(key, subscriber) {
|
|
293
|
+
this.subscribers.set(key, subscriber);
|
|
294
|
+
const unsubscribe = () => this.unsubscribe(key);
|
|
295
|
+
return unsubscribe;
|
|
296
|
+
}
|
|
297
|
+
unsubscribe(key) {
|
|
298
|
+
this.subscribers.delete(key);
|
|
299
|
+
}
|
|
300
|
+
next(value) {
|
|
301
|
+
for (const subscriber of this.subscribers.values()) {
|
|
302
|
+
subscriber(value);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
var StatefulSubject = class extends Subject {
|
|
307
|
+
constructor(initialState) {
|
|
308
|
+
super();
|
|
309
|
+
this.state = initialState;
|
|
310
|
+
}
|
|
311
|
+
next(value) {
|
|
312
|
+
this.state = value;
|
|
313
|
+
super.next(value);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// internal/src/set-state/copy-mutable-if-needed.ts
|
|
318
|
+
function copyMutableIfNeeded(atom, transform, origin, target) {
|
|
319
|
+
const originValue = origin.valueMap.get(atom.key);
|
|
320
|
+
const targetValue = target.valueMap.get(atom.key);
|
|
321
|
+
if (originValue === targetValue) {
|
|
322
|
+
origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
|
|
323
|
+
const jsonValue = transform.toJson(originValue);
|
|
324
|
+
const copiedValue = transform.fromJson(jsonValue);
|
|
325
|
+
target.valueMap.set(atom.key, copiedValue);
|
|
326
|
+
new Tracker(atom, origin);
|
|
327
|
+
return copiedValue;
|
|
328
|
+
}
|
|
329
|
+
return targetValue;
|
|
23
330
|
}
|
|
24
331
|
|
|
25
|
-
//
|
|
332
|
+
// internal/src/set-state/copy-mutable-in-transaction.ts
|
|
333
|
+
function copyMutableIfWithinTransaction(oldValue, atom, store) {
|
|
334
|
+
const target = newest(store);
|
|
335
|
+
const parent = target.parent;
|
|
336
|
+
if (parent !== null) {
|
|
337
|
+
if (`toJson` in atom && `fromJson` in atom) {
|
|
338
|
+
const copiedValue = copyMutableIfNeeded(atom, atom, parent, target);
|
|
339
|
+
return copiedValue;
|
|
340
|
+
}
|
|
341
|
+
if (`family` in atom) {
|
|
342
|
+
const family = parent.families.get(atom.family.key);
|
|
343
|
+
if (family && family.type === `atom_family`) {
|
|
344
|
+
const result = copyMutableFamilyMemberWithinTransaction(
|
|
345
|
+
atom,
|
|
346
|
+
family,
|
|
347
|
+
parent,
|
|
348
|
+
target
|
|
349
|
+
);
|
|
350
|
+
if (result) {
|
|
351
|
+
return result;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return oldValue;
|
|
357
|
+
}
|
|
358
|
+
function copyMutableFamilyMemberWithinTransaction(atom, family, origin, target) {
|
|
359
|
+
if (`toJson` in family && `fromJson` in family) {
|
|
360
|
+
const copyCreated = copyMutableIfNeeded(atom, family, origin, target);
|
|
361
|
+
return copyCreated;
|
|
362
|
+
}
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// internal/src/set-state/emit-update.ts
|
|
367
|
+
var emitUpdate = (state, update, store) => {
|
|
368
|
+
store.logger.info(
|
|
369
|
+
`\u{1F4E2}`,
|
|
370
|
+
state.type,
|
|
371
|
+
state.key,
|
|
372
|
+
`went (`,
|
|
373
|
+
update.oldValue,
|
|
374
|
+
`->`,
|
|
375
|
+
update.newValue,
|
|
376
|
+
`) subscribers:`,
|
|
377
|
+
state.subject.subscribers
|
|
378
|
+
);
|
|
379
|
+
state.subject.next(update);
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// internal/src/set-state/evict-downstream.ts
|
|
383
|
+
var evictDownStream = (atom, store) => {
|
|
384
|
+
const target = newest(store);
|
|
385
|
+
const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom.key);
|
|
386
|
+
store.logger.info(
|
|
387
|
+
`\u{1F9F9}`,
|
|
388
|
+
atom.type,
|
|
389
|
+
atom.key,
|
|
390
|
+
downstreamKeys ? `evicting ${downstreamKeys.size} states downstream:` : `no downstream states`,
|
|
391
|
+
downstreamKeys != null ? downstreamKeys : `to evict`
|
|
392
|
+
);
|
|
393
|
+
if (downstreamKeys) {
|
|
394
|
+
if (target.operation.open) {
|
|
395
|
+
store.logger.info(
|
|
396
|
+
`\u{1F9F9}`,
|
|
397
|
+
atom.type,
|
|
398
|
+
atom.key,
|
|
399
|
+
`[ ${[...target.operation.done].join(`, `)} ] already done`
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
for (const key of downstreamKeys) {
|
|
403
|
+
if (isDone(key, store)) {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
evictCachedValue(key, store);
|
|
407
|
+
markDone(key, store);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
// internal/src/set-state/stow-update.ts
|
|
413
|
+
function shouldUpdateBeStowed(key, update) {
|
|
414
|
+
if (isTransceiver(update.newValue)) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
if (key.includes(`\u{1F441}\u200D\u{1F5E8}`)) {
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
var stowUpdate = (state, update, store) => {
|
|
423
|
+
const { key } = state;
|
|
424
|
+
const target = newest(store);
|
|
425
|
+
if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
|
|
426
|
+
store.logger.error(
|
|
427
|
+
`\u{1F41E}`,
|
|
428
|
+
`atom`,
|
|
429
|
+
key,
|
|
430
|
+
`stowUpdate called outside of a transaction. This is probably a bug.`
|
|
431
|
+
);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
const shouldStow = shouldUpdateBeStowed(key, update);
|
|
435
|
+
if (!shouldStow) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
const atomUpdate = __spreadValues({ key }, update);
|
|
439
|
+
if (state.family) {
|
|
440
|
+
atomUpdate.family = state.family;
|
|
441
|
+
}
|
|
442
|
+
target.transactionMeta.update.updates.push(atomUpdate);
|
|
443
|
+
store.logger.info(
|
|
444
|
+
`\u{1F4C1}`,
|
|
445
|
+
`atom`,
|
|
446
|
+
key,
|
|
447
|
+
`stowed (`,
|
|
448
|
+
update.oldValue,
|
|
449
|
+
`->`,
|
|
450
|
+
update.newValue,
|
|
451
|
+
`)`
|
|
452
|
+
);
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// internal/src/set-state/set-atom.ts
|
|
456
|
+
var setAtom = (atom, next, store) => {
|
|
457
|
+
const target = newest(store);
|
|
458
|
+
const oldValue = readOrComputeValue(atom, store);
|
|
459
|
+
let newValue = copyMutableIfWithinTransaction(oldValue, atom, store);
|
|
460
|
+
newValue = become(next)(newValue);
|
|
461
|
+
store.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
|
|
462
|
+
newValue = cacheValue(atom.key, newValue, atom.subject, store);
|
|
463
|
+
if (isAtomDefault(atom.key, store)) {
|
|
464
|
+
markAtomAsNotDefault(atom.key, store);
|
|
465
|
+
}
|
|
466
|
+
markDone(atom.key, store);
|
|
467
|
+
evictDownStream(atom, store);
|
|
468
|
+
const update = { oldValue, newValue };
|
|
469
|
+
if (target.transactionMeta === null || target.transactionMeta.phase === `applying`) {
|
|
470
|
+
emitUpdate(atom, update, store);
|
|
471
|
+
} else {
|
|
472
|
+
stowUpdate(atom, update, store);
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
// internal/src/set-state/set-atom-or-selector.ts
|
|
477
|
+
var setAtomOrSelector = (state, value, store) => {
|
|
478
|
+
if (state.type === `selector`) {
|
|
479
|
+
state.set(value);
|
|
480
|
+
} else {
|
|
481
|
+
setAtom(state, value, store);
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// internal/src/store/deposit.ts
|
|
486
|
+
function deposit(state) {
|
|
487
|
+
const token = {
|
|
488
|
+
key: state.key,
|
|
489
|
+
type: state.type
|
|
490
|
+
};
|
|
491
|
+
if (`family` in state) {
|
|
492
|
+
token.family = state.family;
|
|
493
|
+
}
|
|
494
|
+
return token;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// ../rel8/junction/src/junction.ts
|
|
26
498
|
var Junction = class {
|
|
27
499
|
constructor(data, config) {
|
|
28
500
|
this.relations = /* @__PURE__ */ new Map();
|
|
@@ -280,15 +752,960 @@ var Junction = class {
|
|
|
280
752
|
}
|
|
281
753
|
};
|
|
282
754
|
|
|
283
|
-
// src/
|
|
284
|
-
var
|
|
285
|
-
|
|
755
|
+
// internal/src/store/store.ts
|
|
756
|
+
var Store = class {
|
|
757
|
+
constructor(name, store = null) {
|
|
758
|
+
this.parent = null;
|
|
759
|
+
this.child = null;
|
|
760
|
+
this.valueMap = /* @__PURE__ */ new Map();
|
|
761
|
+
this.atoms = /* @__PURE__ */ new Map();
|
|
762
|
+
this.selectors = /* @__PURE__ */ new Map();
|
|
763
|
+
this.readonlySelectors = /* @__PURE__ */ new Map();
|
|
764
|
+
this.trackers = /* @__PURE__ */ new Map();
|
|
765
|
+
this.families = /* @__PURE__ */ new Map();
|
|
766
|
+
this.timelines = /* @__PURE__ */ new Map();
|
|
767
|
+
this.transactions = /* @__PURE__ */ new Map();
|
|
768
|
+
this.atomsThatAreDefault = /* @__PURE__ */ new Set();
|
|
769
|
+
this.timelineAtoms = new Junction({
|
|
770
|
+
between: [`timelineKey`, `atomKey`],
|
|
771
|
+
cardinality: `1:n`
|
|
772
|
+
});
|
|
773
|
+
this.selectorAtoms = new Junction({
|
|
774
|
+
between: [`selectorKey`, `atomKey`],
|
|
775
|
+
cardinality: `n:n`
|
|
776
|
+
});
|
|
777
|
+
this.selectorGraph = new Junction(
|
|
778
|
+
{
|
|
779
|
+
between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
|
|
780
|
+
cardinality: `n:n`
|
|
781
|
+
},
|
|
782
|
+
{
|
|
783
|
+
makeContentKey: (...keys) => keys.sort().join(`:`)
|
|
784
|
+
}
|
|
785
|
+
);
|
|
786
|
+
this.subject = {
|
|
787
|
+
atomCreation: new Subject(),
|
|
788
|
+
selectorCreation: new Subject(),
|
|
789
|
+
transactionCreation: new Subject(),
|
|
790
|
+
timelineCreation: new Subject(),
|
|
791
|
+
transactionApplying: new StatefulSubject(null),
|
|
792
|
+
operationStatus: new Subject()
|
|
793
|
+
};
|
|
794
|
+
this.operation = { open: false };
|
|
795
|
+
this.transactionMeta = null;
|
|
796
|
+
this.config = {
|
|
797
|
+
name: `IMPLICIT_STORE`
|
|
798
|
+
};
|
|
799
|
+
this.loggers = [
|
|
800
|
+
new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
|
|
801
|
+
];
|
|
802
|
+
this.logger = {
|
|
803
|
+
error: (...messages) => {
|
|
804
|
+
for (const logger of this.loggers)
|
|
805
|
+
logger.error(...messages);
|
|
806
|
+
},
|
|
807
|
+
info: (...messages) => {
|
|
808
|
+
for (const logger of this.loggers)
|
|
809
|
+
logger.info(...messages);
|
|
810
|
+
},
|
|
811
|
+
warn: (...messages) => {
|
|
812
|
+
for (const logger of this.loggers)
|
|
813
|
+
logger.warn(...messages);
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
if (store !== null) {
|
|
817
|
+
this.valueMap = new Map(store == null ? void 0 : store.valueMap);
|
|
818
|
+
this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
|
|
819
|
+
this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
|
|
820
|
+
this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
|
|
821
|
+
name
|
|
822
|
+
});
|
|
823
|
+
for (const [, atom] of store.atoms) {
|
|
824
|
+
atom.install(this);
|
|
825
|
+
}
|
|
826
|
+
for (const [, selector] of store.readonlySelectors) {
|
|
827
|
+
selector.install(this);
|
|
828
|
+
}
|
|
829
|
+
for (const [, selector] of store.selectors) {
|
|
830
|
+
selector.install(this);
|
|
831
|
+
}
|
|
832
|
+
for (const [, tx] of store.transactions) {
|
|
833
|
+
tx.install(this);
|
|
834
|
+
}
|
|
835
|
+
for (const [, timeline] of store.timelines) {
|
|
836
|
+
timeline.install(this);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
var IMPLICIT = {
|
|
842
|
+
STORE_INTERNAL: void 0,
|
|
843
|
+
get STORE() {
|
|
844
|
+
var _a;
|
|
845
|
+
return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
// internal/src/store/withdraw.ts
|
|
850
|
+
function withdraw(token, store) {
|
|
851
|
+
var _a, _b, _c, _d;
|
|
852
|
+
const target = newest(store);
|
|
853
|
+
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);
|
|
854
|
+
if (state) {
|
|
855
|
+
return state;
|
|
856
|
+
}
|
|
857
|
+
return void 0;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// internal/src/store/withdraw-new-family-member.ts
|
|
861
|
+
function withdrawNewFamilyMember(token, store) {
|
|
862
|
+
if (token.family) {
|
|
863
|
+
store.logger.info(
|
|
864
|
+
`\u{1F46A}`,
|
|
865
|
+
token.type,
|
|
866
|
+
token.key,
|
|
867
|
+
`creating new family member in store "${store.config.name}"`
|
|
868
|
+
);
|
|
869
|
+
const target = newest(store);
|
|
870
|
+
const family = target.families.get(token.family.key);
|
|
871
|
+
if (family) {
|
|
872
|
+
const jsonSubKey = JSON.parse(token.family.subKey);
|
|
873
|
+
family(jsonSubKey);
|
|
874
|
+
const state = withdraw(token, store);
|
|
875
|
+
return state;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
return void 0;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// internal/src/keys.ts
|
|
882
|
+
var isAtomKey = (key, store) => newest(store).atoms.has(key);
|
|
883
|
+
var isSelectorKey = (key, store) => newest(store).selectors.has(key);
|
|
884
|
+
var isReadonlySelectorKey = (key, store) => newest(store).readonlySelectors.has(key);
|
|
885
|
+
var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
|
|
886
|
+
|
|
887
|
+
// internal/src/selector/get-selector-dependency-keys.ts
|
|
888
|
+
var getSelectorDependencyKeys = (key, store) => {
|
|
889
|
+
const sources = newest(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
|
|
890
|
+
return sources;
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
// internal/src/selector/trace-selector-atoms.ts
|
|
894
|
+
var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
|
|
895
|
+
const rootKeys = [];
|
|
896
|
+
const indirectDependencyKeys = getSelectorDependencyKeys(
|
|
897
|
+
directDependencyKey,
|
|
898
|
+
store
|
|
899
|
+
);
|
|
900
|
+
let depth = 0;
|
|
901
|
+
while (indirectDependencyKeys.length > 0) {
|
|
902
|
+
const indirectDependencyKey = indirectDependencyKeys.shift();
|
|
903
|
+
++depth;
|
|
904
|
+
if (depth > 99999) {
|
|
905
|
+
throw new Error(
|
|
906
|
+
`Maximum selector dependency depth exceeded (> 99999) in selector "${selectorKey}". This is likely due to a circular dependency.`
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
if (!isAtomKey(indirectDependencyKey, store)) {
|
|
910
|
+
indirectDependencyKeys.push(
|
|
911
|
+
...getSelectorDependencyKeys(indirectDependencyKey, store)
|
|
912
|
+
);
|
|
913
|
+
} else if (!rootKeys.includes(indirectDependencyKey)) {
|
|
914
|
+
rootKeys.push(indirectDependencyKey);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
return rootKeys;
|
|
918
|
+
};
|
|
919
|
+
var traceAllSelectorAtoms = (selectorKey, store) => {
|
|
920
|
+
const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
|
|
921
|
+
return directDependencyKeys.flatMap(
|
|
922
|
+
(depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
|
|
923
|
+
);
|
|
924
|
+
};
|
|
925
|
+
|
|
926
|
+
// internal/src/selector/update-selector-atoms.ts
|
|
927
|
+
var updateSelectorAtoms = (selectorKey, dependency, store) => {
|
|
928
|
+
const target = newest(store);
|
|
929
|
+
if (dependency.type === `atom`) {
|
|
930
|
+
target.selectorAtoms.set({
|
|
931
|
+
selectorKey,
|
|
932
|
+
atomKey: dependency.key
|
|
933
|
+
});
|
|
934
|
+
store.logger.info(
|
|
935
|
+
`\u{1F50D}`,
|
|
936
|
+
`selector`,
|
|
937
|
+
selectorKey,
|
|
938
|
+
`discovers root atom "${dependency.key}"`
|
|
939
|
+
);
|
|
940
|
+
} else {
|
|
941
|
+
const rootKeys = traceSelectorAtoms(selectorKey, dependency.key, store);
|
|
942
|
+
store.logger.info(
|
|
943
|
+
`\u{1F50D}`,
|
|
944
|
+
`selector`,
|
|
945
|
+
selectorKey,
|
|
946
|
+
`discovers root atoms: [ ${rootKeys.map((key) => `"${key}"`).join(`, `)} ]`
|
|
947
|
+
);
|
|
948
|
+
for (const atomKey of rootKeys) {
|
|
949
|
+
target.selectorAtoms = target.selectorAtoms.set({
|
|
950
|
+
selectorKey,
|
|
951
|
+
atomKey
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
// internal/src/selector/register-selector.ts
|
|
958
|
+
var registerSelector = (selectorKey, store) => ({
|
|
959
|
+
get: (dependency) => {
|
|
960
|
+
const target = newest(store);
|
|
961
|
+
const alreadyRegistered = target.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
|
|
962
|
+
const dependencyState = withdraw(dependency, store);
|
|
963
|
+
if (dependencyState === void 0) {
|
|
964
|
+
throw new Error(
|
|
965
|
+
`State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
const dependencyValue = readOrComputeValue(dependencyState, store);
|
|
969
|
+
store.logger.info(
|
|
970
|
+
`\u{1F50C}`,
|
|
971
|
+
`selector`,
|
|
972
|
+
selectorKey,
|
|
973
|
+
`registers dependency ( "${dependency.key}" =`,
|
|
974
|
+
dependencyValue,
|
|
975
|
+
`)`
|
|
976
|
+
);
|
|
977
|
+
if (!alreadyRegistered) {
|
|
978
|
+
target.selectorGraph.set(
|
|
979
|
+
{
|
|
980
|
+
upstreamSelectorKey: dependency.key,
|
|
981
|
+
downstreamSelectorKey: selectorKey
|
|
982
|
+
},
|
|
983
|
+
{
|
|
984
|
+
source: dependency.key
|
|
985
|
+
}
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
updateSelectorAtoms(selectorKey, dependency, store);
|
|
989
|
+
return dependencyValue;
|
|
990
|
+
},
|
|
991
|
+
set: (stateToken, newValue) => {
|
|
992
|
+
const state = withdraw(stateToken, store);
|
|
993
|
+
if (state === void 0) {
|
|
994
|
+
throw new Error(
|
|
995
|
+
`State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
setAtomOrSelector(state, newValue, store);
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
1001
|
+
|
|
1002
|
+
// internal/src/selector/create-read-write-selector.ts
|
|
1003
|
+
var createReadWriteSelector = (options, family, store) => {
|
|
1004
|
+
const target = newest(store);
|
|
1005
|
+
const subject = new Subject();
|
|
1006
|
+
const { get, set } = registerSelector(options.key, store);
|
|
1007
|
+
const getSelf = () => {
|
|
1008
|
+
const value = options.get({ get });
|
|
1009
|
+
cacheValue(options.key, value, subject, store);
|
|
1010
|
+
return value;
|
|
1011
|
+
};
|
|
1012
|
+
const setSelf = (next) => {
|
|
1013
|
+
const oldValue = getSelf();
|
|
1014
|
+
const newValue = become(next)(oldValue);
|
|
1015
|
+
store.logger.info(
|
|
1016
|
+
`\u{1F4DD}`,
|
|
1017
|
+
`selector`,
|
|
1018
|
+
options.key,
|
|
1019
|
+
`set (`,
|
|
1020
|
+
oldValue,
|
|
1021
|
+
`->`,
|
|
1022
|
+
newValue,
|
|
1023
|
+
`)`
|
|
1024
|
+
);
|
|
1025
|
+
cacheValue(options.key, newValue, subject, store);
|
|
1026
|
+
markDone(options.key, store);
|
|
1027
|
+
if (target.transactionMeta === null) {
|
|
1028
|
+
subject.next({ newValue, oldValue });
|
|
1029
|
+
}
|
|
1030
|
+
options.set({ get, set }, newValue);
|
|
1031
|
+
};
|
|
1032
|
+
const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1033
|
+
subject,
|
|
1034
|
+
install: (s) => createSelector(options, family, s),
|
|
1035
|
+
get: getSelf,
|
|
1036
|
+
set: setSelf,
|
|
1037
|
+
type: `selector`
|
|
1038
|
+
}), family && { family });
|
|
1039
|
+
target.selectors.set(options.key, mySelector);
|
|
1040
|
+
const initialValue = getSelf();
|
|
1041
|
+
store.logger.info(`\u2728`, mySelector.type, mySelector.key, `=`, initialValue);
|
|
1042
|
+
const token = {
|
|
1043
|
+
key: options.key,
|
|
1044
|
+
type: `selector`
|
|
1045
|
+
};
|
|
1046
|
+
if (family) {
|
|
1047
|
+
token.family = family;
|
|
1048
|
+
}
|
|
1049
|
+
store.subject.selectorCreation.next(token);
|
|
1050
|
+
return token;
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
// internal/src/selector/create-readonly-selector.ts
|
|
1054
|
+
var createReadonlySelector = (options, family, store) => {
|
|
1055
|
+
const target = newest(store);
|
|
1056
|
+
const subject = new Subject();
|
|
1057
|
+
const { get } = registerSelector(options.key, store);
|
|
1058
|
+
const getSelf = () => {
|
|
1059
|
+
const value = options.get({ get });
|
|
1060
|
+
cacheValue(options.key, value, subject, store);
|
|
1061
|
+
return value;
|
|
1062
|
+
};
|
|
1063
|
+
const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1064
|
+
subject,
|
|
1065
|
+
install: (s) => createSelector(options, family, s),
|
|
1066
|
+
get: getSelf,
|
|
1067
|
+
type: `readonly_selector`
|
|
1068
|
+
}), family && { family });
|
|
1069
|
+
target.readonlySelectors.set(options.key, readonlySelector);
|
|
1070
|
+
const initialValue = getSelf();
|
|
1071
|
+
store.logger.info(
|
|
1072
|
+
`\u2728`,
|
|
1073
|
+
readonlySelector.type,
|
|
1074
|
+
readonlySelector.key,
|
|
1075
|
+
`=`,
|
|
1076
|
+
initialValue
|
|
1077
|
+
);
|
|
1078
|
+
const token = {
|
|
1079
|
+
key: options.key,
|
|
1080
|
+
type: `readonly_selector`
|
|
1081
|
+
};
|
|
1082
|
+
if (family) {
|
|
1083
|
+
token.family = family;
|
|
1084
|
+
}
|
|
1085
|
+
store.subject.selectorCreation.next(token);
|
|
1086
|
+
return token;
|
|
1087
|
+
};
|
|
1088
|
+
|
|
1089
|
+
// internal/src/selector/create-selector.ts
|
|
1090
|
+
function createSelector(options, family, store) {
|
|
1091
|
+
const target = newest(store);
|
|
1092
|
+
const existingWritable = target.selectors.get(options.key);
|
|
1093
|
+
const existingReadonly = target.readonlySelectors.get(options.key);
|
|
1094
|
+
if (existingWritable || existingReadonly) {
|
|
1095
|
+
store.logger.error(
|
|
1096
|
+
`\u274C`,
|
|
1097
|
+
existingReadonly ? `readonly_selector` : `selector`,
|
|
1098
|
+
options.key,
|
|
1099
|
+
`Tried to create selector, but it already exists in the store. (Ignore if you are in development using hot module replacement.)`
|
|
1100
|
+
);
|
|
1101
|
+
}
|
|
1102
|
+
if (`set` in options) {
|
|
1103
|
+
return createReadWriteSelector(options, family, store);
|
|
1104
|
+
}
|
|
1105
|
+
return createReadonlySelector(options, family, store);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// internal/src/selector/delete-selector.ts
|
|
1109
|
+
function deleteSelector(selectorToken, store) {
|
|
1110
|
+
const target = newest(store);
|
|
1111
|
+
const { key } = selectorToken;
|
|
1112
|
+
switch (selectorToken.type) {
|
|
1113
|
+
case `selector`:
|
|
1114
|
+
target.selectors.delete(key);
|
|
1115
|
+
break;
|
|
1116
|
+
case `readonly_selector`:
|
|
1117
|
+
target.readonlySelectors.delete(key);
|
|
1118
|
+
break;
|
|
1119
|
+
}
|
|
1120
|
+
target.valueMap.delete(key);
|
|
1121
|
+
target.selectorAtoms.delete(key);
|
|
1122
|
+
const downstreamTokens = target.selectorGraph.getRelationEntries({ upstreamSelectorKey: key }).filter(([_, { source }]) => source === key).map(
|
|
1123
|
+
([downstreamSelectorKey]) => {
|
|
1124
|
+
var _a;
|
|
1125
|
+
return (_a = target.selectors.get(downstreamSelectorKey)) != null ? _a : target.readonlySelectors.get(downstreamSelectorKey);
|
|
1126
|
+
}
|
|
1127
|
+
);
|
|
1128
|
+
for (const downstreamToken of downstreamTokens) {
|
|
1129
|
+
if (downstreamToken) {
|
|
1130
|
+
deleteSelector(downstreamToken, store);
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
target.selectorGraph.delete(key);
|
|
1134
|
+
store.logger.info(`\u{1F525}`, selectorToken.type, `${key}`, `deleted`);
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
// internal/src/subscribe/recall-state.ts
|
|
1138
|
+
var recallState = (state, store) => {
|
|
1139
|
+
const target = newest(store);
|
|
1140
|
+
if (!target.operation.open) {
|
|
1141
|
+
return target.valueMap.get(state.key);
|
|
1142
|
+
}
|
|
1143
|
+
return target.operation.prev.get(state.key);
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
// internal/src/subscribe/subscribe-to-root-atoms.ts
|
|
1147
|
+
var subscribeToRootAtoms = (state, store) => {
|
|
1148
|
+
const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
|
|
1149
|
+
const atom = store.atoms.get(atomKey);
|
|
1150
|
+
if (atom === void 0) {
|
|
1151
|
+
throw new Error(
|
|
1152
|
+
`Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
|
|
1153
|
+
);
|
|
1154
|
+
}
|
|
1155
|
+
return atom.subject.subscribe(
|
|
1156
|
+
`${state.type}:${state.key}`,
|
|
1157
|
+
(atomChange) => {
|
|
1158
|
+
store.logger.info(
|
|
1159
|
+
`\u{1F4E2}`,
|
|
1160
|
+
state.type,
|
|
1161
|
+
state.key,
|
|
1162
|
+
`root`,
|
|
1163
|
+
atomKey,
|
|
1164
|
+
`went`,
|
|
1165
|
+
atomChange.oldValue,
|
|
1166
|
+
`->`,
|
|
1167
|
+
atomChange.newValue
|
|
1168
|
+
);
|
|
1169
|
+
const oldValue = recallState(state, store);
|
|
1170
|
+
const newValue = readOrComputeValue(state, store);
|
|
1171
|
+
store.logger.info(
|
|
1172
|
+
`\u2728`,
|
|
1173
|
+
state.type,
|
|
1174
|
+
state.key,
|
|
1175
|
+
`went`,
|
|
1176
|
+
oldValue,
|
|
1177
|
+
`->`,
|
|
1178
|
+
newValue
|
|
1179
|
+
);
|
|
1180
|
+
state.subject.next({ newValue, oldValue });
|
|
1181
|
+
}
|
|
1182
|
+
);
|
|
1183
|
+
});
|
|
1184
|
+
return dependencySubscriptions;
|
|
1185
|
+
};
|
|
1186
|
+
|
|
1187
|
+
// internal/src/subscribe/subscribe-to-state.ts
|
|
1188
|
+
function subscribeToState(token, handleUpdate, key, store) {
|
|
1189
|
+
const state = withdraw(token, store);
|
|
1190
|
+
if (state === void 0) {
|
|
1191
|
+
throw new Error(
|
|
1192
|
+
`State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
1195
|
+
const unsubFunction = state.subject.subscribe(key, handleUpdate);
|
|
1196
|
+
store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
|
|
1197
|
+
const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
|
|
1198
|
+
const unsubscribe = dependencyUnsubFunctions === null ? () => {
|
|
1199
|
+
store.logger.info(
|
|
1200
|
+
`\u{1F648}`,
|
|
1201
|
+
state.type,
|
|
1202
|
+
state.key,
|
|
1203
|
+
`Removing subscription "${key}"`
|
|
1204
|
+
);
|
|
1205
|
+
unsubFunction();
|
|
1206
|
+
} : () => {
|
|
1207
|
+
store.logger.info(
|
|
1208
|
+
`\u{1F648}`,
|
|
1209
|
+
state.type,
|
|
1210
|
+
state.key,
|
|
1211
|
+
`Removing subscription "${key}"`
|
|
1212
|
+
);
|
|
1213
|
+
unsubFunction();
|
|
1214
|
+
for (const unsubFromDependency of dependencyUnsubFunctions) {
|
|
1215
|
+
unsubFromDependency();
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
return unsubscribe;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// internal/src/subscribe/subscribe-to-timeline.ts
|
|
1222
|
+
var subscribeToTimeline = (token, handleUpdate, key, store) => {
|
|
1223
|
+
const tl = withdraw(token, store);
|
|
1224
|
+
if (tl === void 0) {
|
|
1225
|
+
throw new Error(
|
|
1226
|
+
`Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
|
|
1230
|
+
const unsubscribe = tl.subject.subscribe(key, handleUpdate);
|
|
1231
|
+
return () => {
|
|
1232
|
+
store.logger.info(
|
|
1233
|
+
`\u{1F648}`,
|
|
1234
|
+
`timeline`,
|
|
1235
|
+
token.key,
|
|
1236
|
+
`Removing subscription "${key}" from timeline`
|
|
1237
|
+
);
|
|
1238
|
+
unsubscribe();
|
|
1239
|
+
};
|
|
1240
|
+
};
|
|
1241
|
+
|
|
1242
|
+
// internal/src/mutable/tracker.ts
|
|
1243
|
+
var Tracker = class {
|
|
1244
|
+
constructor(mutableState, store) {
|
|
1245
|
+
this.unsubscribeFromInnerValue = null;
|
|
1246
|
+
this.mutableState = mutableState;
|
|
1247
|
+
const target = newest(store);
|
|
1248
|
+
this.latestUpdateState = this.initializeState(mutableState, target);
|
|
1249
|
+
this.observeCore(mutableState, this.latestUpdateState, target);
|
|
1250
|
+
this.updateCore(mutableState, this.latestUpdateState, target);
|
|
1251
|
+
target.trackers.set(mutableState.key, this);
|
|
1252
|
+
}
|
|
1253
|
+
initializeState(mutableState, store) {
|
|
1254
|
+
const latestUpdateStateKey = `*${mutableState.key}`;
|
|
1255
|
+
deleteAtom({ type: `atom`, key: latestUpdateStateKey }, store);
|
|
1256
|
+
const familyMetaData = mutableState.family ? {
|
|
1257
|
+
key: `*${mutableState.family.key}`,
|
|
1258
|
+
subKey: mutableState.family.subKey
|
|
1259
|
+
} : void 0;
|
|
1260
|
+
const latestUpdateState = createAtom(
|
|
1261
|
+
{
|
|
1262
|
+
key: latestUpdateStateKey,
|
|
1263
|
+
default: null
|
|
1264
|
+
},
|
|
1265
|
+
familyMetaData,
|
|
1266
|
+
store
|
|
1267
|
+
);
|
|
1268
|
+
return latestUpdateState;
|
|
1269
|
+
}
|
|
1270
|
+
observeCore(mutableState, latestUpdateState, store) {
|
|
1271
|
+
const originalInnerValue = getState(mutableState, store);
|
|
1272
|
+
const target = newest(store);
|
|
1273
|
+
this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
|
|
1274
|
+
`tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
|
|
1275
|
+
(update) => {
|
|
1276
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1277
|
+
mutableState.key,
|
|
1278
|
+
() => {
|
|
1279
|
+
unsubscribe();
|
|
1280
|
+
setState(latestUpdateState, update, store);
|
|
1281
|
+
}
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
);
|
|
1285
|
+
subscribeToState(
|
|
1286
|
+
mutableState,
|
|
1287
|
+
(update) => {
|
|
1288
|
+
var _a;
|
|
1289
|
+
if (update.newValue !== update.oldValue) {
|
|
1290
|
+
(_a = this.unsubscribeFromInnerValue) == null ? void 0 : _a.call(this);
|
|
1291
|
+
const target2 = newest(store);
|
|
1292
|
+
this.unsubscribeFromInnerValue = update.newValue.subscribe(
|
|
1293
|
+
`tracker:${store.config.name}:${target2.transactionMeta === null ? `main` : target2.transactionMeta.update.key}`,
|
|
1294
|
+
(update2) => {
|
|
1295
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1296
|
+
mutableState.key,
|
|
1297
|
+
() => {
|
|
1298
|
+
unsubscribe();
|
|
1299
|
+
setState(latestUpdateState, update2, store);
|
|
1300
|
+
}
|
|
1301
|
+
);
|
|
1302
|
+
}
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
},
|
|
1306
|
+
`${store.config.name}: tracker observing inner value`,
|
|
1307
|
+
store
|
|
1308
|
+
);
|
|
1309
|
+
}
|
|
1310
|
+
updateCore(mutableState, latestUpdateState, store) {
|
|
1311
|
+
subscribeToState(
|
|
1312
|
+
latestUpdateState,
|
|
1313
|
+
({ newValue, oldValue }) => {
|
|
1314
|
+
const timelineId = store.timelineAtoms.getRelatedKey(
|
|
1315
|
+
latestUpdateState.key
|
|
1316
|
+
);
|
|
1317
|
+
if (timelineId) {
|
|
1318
|
+
const timelineData = store.timelines.get(timelineId);
|
|
1319
|
+
if (timelineData == null ? void 0 : timelineData.timeTraveling) {
|
|
1320
|
+
const unsubscribe2 = subscribeToTimeline(
|
|
1321
|
+
{ key: timelineId, type: `timeline` },
|
|
1322
|
+
(update) => {
|
|
1323
|
+
unsubscribe2();
|
|
1324
|
+
setState(
|
|
1325
|
+
mutableState,
|
|
1326
|
+
(transceiver) => {
|
|
1327
|
+
if (update === `redo` && newValue) {
|
|
1328
|
+
transceiver.do(newValue);
|
|
1329
|
+
} else if (update === `undo` && oldValue) {
|
|
1330
|
+
transceiver.undo(oldValue);
|
|
1331
|
+
}
|
|
1332
|
+
return transceiver;
|
|
1333
|
+
},
|
|
1334
|
+
store
|
|
1335
|
+
);
|
|
1336
|
+
},
|
|
1337
|
+
`${mutableState.key}: tracker observing timeline`,
|
|
1338
|
+
store
|
|
1339
|
+
);
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1344
|
+
latestUpdateState.key,
|
|
1345
|
+
() => {
|
|
1346
|
+
unsubscribe();
|
|
1347
|
+
const mutable = getState(mutableState, store);
|
|
1348
|
+
const updateNumber = mutable.getUpdateNumber(newValue);
|
|
1349
|
+
const eventOffset = updateNumber - mutable.cacheUpdateNumber;
|
|
1350
|
+
if (newValue && eventOffset === 1) {
|
|
1351
|
+
setState(
|
|
1352
|
+
mutableState,
|
|
1353
|
+
(transceiver) => (transceiver.do(newValue), transceiver),
|
|
1354
|
+
store
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
);
|
|
1359
|
+
},
|
|
1360
|
+
`${store.config.name}: tracker observing latest update`,
|
|
1361
|
+
store
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
};
|
|
1365
|
+
|
|
1366
|
+
// internal/src/mutable/create-mutable-atom.ts
|
|
1367
|
+
function createMutableAtom(options, store) {
|
|
1368
|
+
store.logger.info(
|
|
1369
|
+
`\u{1F527}`,
|
|
1370
|
+
`atom`,
|
|
1371
|
+
options.key,
|
|
1372
|
+
`creating in store "${store.config.name}"`
|
|
1373
|
+
);
|
|
1374
|
+
const coreState = createAtom(options, void 0, store);
|
|
1375
|
+
new Tracker(coreState, store);
|
|
1376
|
+
const jsonState = selectJson(coreState, options, store);
|
|
1377
|
+
const target = newest(store);
|
|
1378
|
+
subscribeToState(
|
|
1379
|
+
jsonState,
|
|
1380
|
+
() => {
|
|
1381
|
+
const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
|
|
1382
|
+
if (!trackerHasBeenInitialized) {
|
|
1383
|
+
new Tracker(coreState, store);
|
|
1384
|
+
}
|
|
1385
|
+
},
|
|
1386
|
+
`tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
|
|
1387
|
+
store
|
|
1388
|
+
);
|
|
1389
|
+
return coreState;
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
// internal/src/families/create-atom-family.ts
|
|
1393
|
+
function createAtomFamily(options, store) {
|
|
1394
|
+
const subject = new Subject();
|
|
1395
|
+
const atomFamily = Object.assign(
|
|
1396
|
+
(key) => {
|
|
1397
|
+
const subKey = stringifyJson(key);
|
|
1398
|
+
const family = { key: options.key, subKey };
|
|
1399
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1400
|
+
const existing = withdraw({ key: fullKey, type: `atom` }, store);
|
|
1401
|
+
let token;
|
|
1402
|
+
if (existing) {
|
|
1403
|
+
token = deposit(existing);
|
|
1404
|
+
} else {
|
|
1405
|
+
const individualOptions = {
|
|
1406
|
+
key: fullKey,
|
|
1407
|
+
default: options.default instanceof Function ? options.default(key) : options.default
|
|
1408
|
+
};
|
|
1409
|
+
if (options.effects) {
|
|
1410
|
+
individualOptions.effects = options.effects(key);
|
|
1411
|
+
}
|
|
1412
|
+
token = createAtom(individualOptions, family, store);
|
|
1413
|
+
subject.next(token);
|
|
1414
|
+
}
|
|
1415
|
+
return token;
|
|
1416
|
+
},
|
|
1417
|
+
{
|
|
1418
|
+
key: options.key,
|
|
1419
|
+
type: `atom_family`,
|
|
1420
|
+
subject
|
|
1421
|
+
}
|
|
1422
|
+
);
|
|
1423
|
+
const target = newest(store);
|
|
1424
|
+
target.families.set(options.key, atomFamily);
|
|
1425
|
+
return atomFamily;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
// internal/src/families/create-readonly-selector-family.ts
|
|
1429
|
+
function createReadonlySelectorFamily(options, store) {
|
|
1430
|
+
const subject = new Subject();
|
|
1431
|
+
return Object.assign(
|
|
1432
|
+
(key) => {
|
|
1433
|
+
const target = newest(store);
|
|
1434
|
+
const subKey = stringifyJson(key);
|
|
1435
|
+
const family = { key: options.key, subKey };
|
|
1436
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1437
|
+
const existing = target.readonlySelectors.get(fullKey);
|
|
1438
|
+
if (existing) {
|
|
1439
|
+
return deposit(existing);
|
|
1440
|
+
}
|
|
1441
|
+
return createSelector(
|
|
1442
|
+
{
|
|
1443
|
+
key: fullKey,
|
|
1444
|
+
get: options.get(key)
|
|
1445
|
+
},
|
|
1446
|
+
family,
|
|
1447
|
+
store
|
|
1448
|
+
);
|
|
1449
|
+
},
|
|
1450
|
+
{
|
|
1451
|
+
key: options.key,
|
|
1452
|
+
type: `readonly_selector_family`,
|
|
1453
|
+
subject
|
|
1454
|
+
}
|
|
1455
|
+
);
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
// internal/src/families/create-selector-family.ts
|
|
1459
|
+
function createSelectorFamily(options, store) {
|
|
1460
|
+
const isReadonly = !(`set` in options);
|
|
1461
|
+
if (isReadonly) {
|
|
1462
|
+
return createReadonlySelectorFamily(options, store);
|
|
1463
|
+
}
|
|
1464
|
+
const target = newest(store);
|
|
1465
|
+
const subject = new Subject();
|
|
1466
|
+
const selectorFamily = Object.assign(
|
|
1467
|
+
(key) => {
|
|
1468
|
+
const subKey = stringifyJson(key);
|
|
1469
|
+
const family = { key: options.key, subKey };
|
|
1470
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1471
|
+
const existing = target.selectors.get(fullKey);
|
|
1472
|
+
if (existing) {
|
|
1473
|
+
return deposit(existing);
|
|
1474
|
+
}
|
|
1475
|
+
const token = createSelector(
|
|
1476
|
+
{
|
|
1477
|
+
key: fullKey,
|
|
1478
|
+
get: options.get(key),
|
|
1479
|
+
set: options.set(key)
|
|
1480
|
+
},
|
|
1481
|
+
family,
|
|
1482
|
+
store
|
|
1483
|
+
);
|
|
1484
|
+
subject.next(token);
|
|
1485
|
+
return token;
|
|
1486
|
+
},
|
|
1487
|
+
{
|
|
1488
|
+
key: options.key,
|
|
1489
|
+
type: `selector_family`
|
|
1490
|
+
}
|
|
1491
|
+
);
|
|
1492
|
+
target.families.set(options.key, selectorFamily);
|
|
1493
|
+
return selectorFamily;
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
// internal/src/mutable/tracker-family.ts
|
|
1497
|
+
var FamilyTracker = class {
|
|
1498
|
+
constructor(findMutableState, store) {
|
|
1499
|
+
this.findLatestUpdateState = createAtomFamily(
|
|
1500
|
+
{
|
|
1501
|
+
key: `*${findMutableState.key}`,
|
|
1502
|
+
default: null
|
|
1503
|
+
},
|
|
1504
|
+
store
|
|
1505
|
+
);
|
|
1506
|
+
this.findMutableState = findMutableState;
|
|
1507
|
+
this.findMutableState.subject.subscribe(
|
|
1508
|
+
`store=${store.config.name}::tracker-atom-family`,
|
|
1509
|
+
(atomToken) => {
|
|
1510
|
+
if (atomToken.family) {
|
|
1511
|
+
const key = parseJson(atomToken.family.subKey);
|
|
1512
|
+
this.findLatestUpdateState(key);
|
|
1513
|
+
new Tracker(atomToken, store);
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
);
|
|
1517
|
+
this.findLatestUpdateState.subject.subscribe(
|
|
1518
|
+
`store=${store.config.name}::tracker-atom-family`,
|
|
1519
|
+
(atomToken) => {
|
|
1520
|
+
if (atomToken.family) {
|
|
1521
|
+
const key = parseJson(atomToken.family.subKey);
|
|
1522
|
+
const mutableAtomToken = this.findMutableState(key);
|
|
1523
|
+
new Tracker(mutableAtomToken, store);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
};
|
|
1529
|
+
|
|
1530
|
+
// internal/src/mutable/create-mutable-atom-family.ts
|
|
1531
|
+
function createMutableAtomFamily(options, store) {
|
|
1532
|
+
const coreFamily = Object.assign(
|
|
1533
|
+
createAtomFamily(options, store),
|
|
1534
|
+
options
|
|
1535
|
+
);
|
|
1536
|
+
selectJsonFamily(coreFamily, options);
|
|
1537
|
+
new FamilyTracker(coreFamily, store);
|
|
1538
|
+
return coreFamily;
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
// internal/src/mutable/get-json-family.ts
|
|
1542
|
+
var getJsonFamily = (mutableAtomFamily, store) => {
|
|
1543
|
+
const target = newest(store);
|
|
1544
|
+
const key = `${mutableAtomFamily.key}:JSON`;
|
|
1545
|
+
const jsonFamily = target.families.get(
|
|
1546
|
+
key
|
|
1547
|
+
);
|
|
1548
|
+
return jsonFamily;
|
|
1549
|
+
};
|
|
1550
|
+
|
|
1551
|
+
// internal/src/mutable/transceiver.ts
|
|
1552
|
+
function isTransceiver(value) {
|
|
1553
|
+
return typeof value === `object` && value !== null && `do` in value && `undo` in value && `subscribe` in value;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
// internal/src/atom/is-default.ts
|
|
1557
|
+
var isAtomDefault = (key, store) => {
|
|
1558
|
+
const core = newest(store);
|
|
1559
|
+
return core.atomsThatAreDefault.has(key);
|
|
1560
|
+
};
|
|
1561
|
+
var markAtomAsDefault = (key, store) => {
|
|
1562
|
+
const core = newest(store);
|
|
1563
|
+
core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key);
|
|
1564
|
+
};
|
|
1565
|
+
var markAtomAsNotDefault = (key, store) => {
|
|
1566
|
+
const core = newest(store);
|
|
1567
|
+
core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault);
|
|
1568
|
+
core.atomsThatAreDefault.delete(key);
|
|
1569
|
+
};
|
|
1570
|
+
|
|
1571
|
+
// internal/src/atom/create-atom.ts
|
|
1572
|
+
function createAtom(options, family, store) {
|
|
1573
|
+
store.logger.info(
|
|
1574
|
+
`\u{1F528}`,
|
|
1575
|
+
`atom`,
|
|
1576
|
+
options.key,
|
|
1577
|
+
`creating in store "${store.config.name}"`
|
|
1578
|
+
);
|
|
1579
|
+
const target = newest(store);
|
|
1580
|
+
const existing = target.atoms.get(options.key);
|
|
1581
|
+
if (existing) {
|
|
1582
|
+
store.logger.error(
|
|
1583
|
+
`\u274C`,
|
|
1584
|
+
`atom`,
|
|
1585
|
+
options.key,
|
|
1586
|
+
`Tried to create atom, but it already exists in the store.`,
|
|
1587
|
+
`(Ignore if you are in development using hot module replacement.)`
|
|
1588
|
+
);
|
|
1589
|
+
return deposit(existing);
|
|
1590
|
+
}
|
|
1591
|
+
const subject = new Subject();
|
|
1592
|
+
const newAtom = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1593
|
+
type: `atom`,
|
|
1594
|
+
install: (store2) => {
|
|
1595
|
+
store2.logger.info(
|
|
1596
|
+
`\u{1F6E0}\uFE0F`,
|
|
1597
|
+
`atom`,
|
|
1598
|
+
options.key,
|
|
1599
|
+
`installing in store "${store2.config.name}"`
|
|
1600
|
+
);
|
|
1601
|
+
return `mutable` in options ? createMutableAtom(options, store2) : createAtom(options, void 0, store2);
|
|
1602
|
+
},
|
|
1603
|
+
subject
|
|
1604
|
+
}), family && { family });
|
|
1605
|
+
let initialValue = options.default;
|
|
1606
|
+
if (options.default instanceof Function) {
|
|
1607
|
+
initialValue = options.default();
|
|
1608
|
+
}
|
|
1609
|
+
target.atoms.set(newAtom.key, newAtom);
|
|
1610
|
+
markAtomAsDefault(options.key, store);
|
|
1611
|
+
cacheValue(options.key, initialValue, subject, store);
|
|
1612
|
+
const token = deposit(newAtom);
|
|
1613
|
+
if (options.effects) {
|
|
1614
|
+
let effectIndex = 0;
|
|
1615
|
+
const cleanupFunctions = [];
|
|
1616
|
+
for (const effect of options.effects) {
|
|
1617
|
+
const cleanup = effect({
|
|
1618
|
+
setSelf: (next) => setState(token, next, store),
|
|
1619
|
+
onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
|
|
1620
|
+
});
|
|
1621
|
+
if (cleanup) {
|
|
1622
|
+
cleanupFunctions.push(cleanup);
|
|
1623
|
+
}
|
|
1624
|
+
++effectIndex;
|
|
1625
|
+
}
|
|
1626
|
+
newAtom.cleanup = () => {
|
|
1627
|
+
for (const cleanup of cleanupFunctions) {
|
|
1628
|
+
cleanup();
|
|
1629
|
+
}
|
|
1630
|
+
};
|
|
1631
|
+
}
|
|
1632
|
+
store.subject.atomCreation.next(token);
|
|
1633
|
+
return token;
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
// internal/src/atom/delete-atom.ts
|
|
1637
|
+
function deleteAtom(atomToken, store) {
|
|
1638
|
+
var _a, _b;
|
|
1639
|
+
const target = newest(store);
|
|
1640
|
+
const { key } = atomToken;
|
|
1641
|
+
const atom = target.atoms.get(key);
|
|
1642
|
+
if (!atom) {
|
|
1643
|
+
store.logger.error(
|
|
1644
|
+
`\u274C`,
|
|
1645
|
+
`atom`,
|
|
1646
|
+
`${key}`,
|
|
1647
|
+
`Tried to delete atom, but it does not exist in the store.`
|
|
1648
|
+
);
|
|
1649
|
+
}
|
|
1650
|
+
(_a = atom == null ? void 0 : atom.cleanup) == null ? void 0 : _a.call(atom);
|
|
1651
|
+
target.atoms.delete(key);
|
|
1652
|
+
target.valueMap.delete(key);
|
|
1653
|
+
const selectorKeys = target.selectorAtoms.getRelatedKeys(key);
|
|
1654
|
+
if (selectorKeys) {
|
|
1655
|
+
for (const selectorKey of selectorKeys) {
|
|
1656
|
+
const token = (_b = target.selectors.get(selectorKey)) != null ? _b : target.readonlySelectors.get(selectorKey);
|
|
1657
|
+
if (token) {
|
|
1658
|
+
deleteSelector(token, store);
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
target.selectorAtoms.delete(key);
|
|
1663
|
+
target.atomsThatAreDefault.delete(key);
|
|
1664
|
+
target.timelineAtoms.delete(key);
|
|
1665
|
+
store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
// internal/src/not-found-error.ts
|
|
1669
|
+
var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
1670
|
+
function prettyPrintTokenType(token) {
|
|
1671
|
+
if (token.type === `readonly_selector`) {
|
|
1672
|
+
return `Readonly Selector`;
|
|
1673
|
+
}
|
|
1674
|
+
return capitalize(token.type);
|
|
1675
|
+
}
|
|
1676
|
+
var NotFoundError = class extends Error {
|
|
1677
|
+
constructor(token, store) {
|
|
1678
|
+
super(
|
|
1679
|
+
`${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
|
|
1680
|
+
);
|
|
1681
|
+
}
|
|
1682
|
+
};
|
|
1683
|
+
|
|
1684
|
+
// data/src/dict.ts
|
|
1685
|
+
function dict(findState, index, store = IMPLICIT.STORE) {
|
|
1686
|
+
return createSelector(
|
|
1687
|
+
{
|
|
1688
|
+
key: `${findState.key}Dict`,
|
|
1689
|
+
get: ({ get }) => {
|
|
1690
|
+
const keys = get(index);
|
|
1691
|
+
return keys.reduce((acc, key) => {
|
|
1692
|
+
acc[key] = get(findState(key));
|
|
1693
|
+
return acc;
|
|
1694
|
+
}, {});
|
|
1695
|
+
}
|
|
1696
|
+
},
|
|
1697
|
+
void 0,
|
|
1698
|
+
store
|
|
1699
|
+
);
|
|
1700
|
+
}
|
|
1701
|
+
var TRANSACTORS = { get: getState, set: setState };
|
|
1702
|
+
function capitalize2(string) {
|
|
286
1703
|
return string[0].toUpperCase() + string.slice(1);
|
|
287
1704
|
}
|
|
288
|
-
function join(options, defaultContent, store =
|
|
1705
|
+
function join(options, defaultContent, store = IMPLICIT.STORE) {
|
|
289
1706
|
const a = options.between[0];
|
|
290
1707
|
const b = options.between[1];
|
|
291
|
-
const findRelatedKeysState =
|
|
1708
|
+
const findRelatedKeysState = createMutableAtomFamily(
|
|
292
1709
|
{
|
|
293
1710
|
key: `${options.key}/relatedKeys`,
|
|
294
1711
|
default: () => new setRtx.SetRTX(),
|
|
@@ -330,9 +1747,9 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
330
1747
|
}
|
|
331
1748
|
};
|
|
332
1749
|
const replaceRelationsSafely = (transactors, a2, bs) => {
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
335
|
-
for (const b2 of
|
|
1750
|
+
const aRelations = getRelatedKeys(transactors, a2);
|
|
1751
|
+
if (aRelations) {
|
|
1752
|
+
for (const b2 of aRelations) {
|
|
336
1753
|
const bKeys = getRelatedKeys(transactors, b2);
|
|
337
1754
|
if (bKeys) {
|
|
338
1755
|
bKeys.delete(a2);
|
|
@@ -380,7 +1797,7 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
380
1797
|
let externalStore;
|
|
381
1798
|
let findContentState;
|
|
382
1799
|
if (defaultContent) {
|
|
383
|
-
findContentState =
|
|
1800
|
+
findContentState = createAtomFamily(
|
|
384
1801
|
{
|
|
385
1802
|
key: `${options.key}/content`,
|
|
386
1803
|
default: defaultContent
|
|
@@ -413,7 +1830,7 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
413
1830
|
externalStore,
|
|
414
1831
|
makeContentKey: (...args) => args.sort().join(`:`)
|
|
415
1832
|
});
|
|
416
|
-
const createSingleKeyStateFamily = () =>
|
|
1833
|
+
const createSingleKeyStateFamily = () => createSelectorFamily(
|
|
417
1834
|
{
|
|
418
1835
|
key: `${options.key}/singleRelatedKey`,
|
|
419
1836
|
get: (key) => ({ get }) => {
|
|
@@ -425,8 +1842,8 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
425
1842
|
},
|
|
426
1843
|
store
|
|
427
1844
|
);
|
|
428
|
-
const getMultipleKeyStateFamily = () =>
|
|
429
|
-
const createSingleEntryStateFamily = () =>
|
|
1845
|
+
const getMultipleKeyStateFamily = () => getJsonFamily(findRelatedKeysState, store);
|
|
1846
|
+
const createSingleEntryStateFamily = () => createSelectorFamily(
|
|
430
1847
|
{
|
|
431
1848
|
key: `${options.key}/singleRelatedEntry`,
|
|
432
1849
|
get: (key) => ({ get }) => {
|
|
@@ -439,7 +1856,7 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
439
1856
|
},
|
|
440
1857
|
store
|
|
441
1858
|
);
|
|
442
|
-
const getMultipleEntryStateFamily = () =>
|
|
1859
|
+
const getMultipleEntryStateFamily = () => createSelectorFamily(
|
|
443
1860
|
{
|
|
444
1861
|
key: `${options.key}/multipleRelatedEntries`,
|
|
445
1862
|
get: (key) => ({ get }) => {
|
|
@@ -455,8 +1872,8 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
455
1872
|
switch (options.cardinality) {
|
|
456
1873
|
case `1:1`: {
|
|
457
1874
|
const findSingleRelatedKeyState = createSingleKeyStateFamily();
|
|
458
|
-
const stateKeyA = `${a}KeyOf${
|
|
459
|
-
const stateKeyB = `${b}KeyOf${
|
|
1875
|
+
const stateKeyA = `${a}KeyOf${capitalize2(b)}`;
|
|
1876
|
+
const stateKeyB = `${b}KeyOf${capitalize2(a)}`;
|
|
460
1877
|
const findStateBase = {
|
|
461
1878
|
[stateKeyA]: findSingleRelatedKeyState,
|
|
462
1879
|
[stateKeyB]: findSingleRelatedKeyState
|
|
@@ -464,8 +1881,8 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
464
1881
|
let findState;
|
|
465
1882
|
if (defaultContent) {
|
|
466
1883
|
const findSingleRelatedEntryState = createSingleEntryStateFamily();
|
|
467
|
-
const entriesStateKeyA = `${a}EntryOf${
|
|
468
|
-
const entriesStateKeyB = `${b}EntryOf${
|
|
1884
|
+
const entriesStateKeyA = `${a}EntryOf${capitalize2(b)}`;
|
|
1885
|
+
const entriesStateKeyB = `${b}EntryOf${capitalize2(a)}`;
|
|
469
1886
|
const findStateWithContent = {
|
|
470
1887
|
[entriesStateKeyA]: findSingleRelatedEntryState,
|
|
471
1888
|
[entriesStateKeyB]: findSingleRelatedEntryState
|
|
@@ -482,8 +1899,8 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
482
1899
|
case `1:n`: {
|
|
483
1900
|
const findSingleRelatedKeyState = createSingleKeyStateFamily();
|
|
484
1901
|
const findMultipleRelatedKeysState = getMultipleKeyStateFamily();
|
|
485
|
-
const stateKeyA = `${a}KeyOf${
|
|
486
|
-
const stateKeyB = `${b}KeysOf${
|
|
1902
|
+
const stateKeyA = `${a}KeyOf${capitalize2(b)}`;
|
|
1903
|
+
const stateKeyB = `${b}KeysOf${capitalize2(a)}`;
|
|
487
1904
|
const findStateBase = {
|
|
488
1905
|
[stateKeyA]: findSingleRelatedKeyState,
|
|
489
1906
|
[stateKeyB]: findMultipleRelatedKeysState
|
|
@@ -492,8 +1909,8 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
492
1909
|
if (defaultContent) {
|
|
493
1910
|
const findSingleRelatedEntryState = createSingleEntryStateFamily();
|
|
494
1911
|
const findMultipleRelatedEntriesState = getMultipleEntryStateFamily();
|
|
495
|
-
const entriesStateKeyA = `${a}EntryOf${
|
|
496
|
-
const entriesStateKeyB = `${b}EntriesOf${
|
|
1912
|
+
const entriesStateKeyA = `${a}EntryOf${capitalize2(b)}`;
|
|
1913
|
+
const entriesStateKeyB = `${b}EntriesOf${capitalize2(a)}`;
|
|
497
1914
|
const findStateWithContent = {
|
|
498
1915
|
[entriesStateKeyA]: findSingleRelatedEntryState,
|
|
499
1916
|
[entriesStateKeyB]: findMultipleRelatedEntriesState
|
|
@@ -509,8 +1926,8 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
509
1926
|
}
|
|
510
1927
|
case `n:n`: {
|
|
511
1928
|
const findMultipleRelatedKeysState = getMultipleKeyStateFamily();
|
|
512
|
-
const stateKeyA = `${a}KeysOf${
|
|
513
|
-
const stateKeyB = `${b}KeysOf${
|
|
1929
|
+
const stateKeyA = `${a}KeysOf${capitalize2(b)}`;
|
|
1930
|
+
const stateKeyB = `${b}KeysOf${capitalize2(a)}`;
|
|
514
1931
|
const findStateBase = {
|
|
515
1932
|
[stateKeyA]: findMultipleRelatedKeysState,
|
|
516
1933
|
[stateKeyB]: findMultipleRelatedKeysState
|
|
@@ -518,8 +1935,8 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
518
1935
|
let findState;
|
|
519
1936
|
if (defaultContent) {
|
|
520
1937
|
const findMultipleRelatedEntriesState = getMultipleEntryStateFamily();
|
|
521
|
-
const entriesStateKeyA = `${a}EntriesOf${
|
|
522
|
-
const entriesStateKeyB = `${b}EntriesOf${
|
|
1938
|
+
const entriesStateKeyA = `${a}EntriesOf${capitalize2(b)}`;
|
|
1939
|
+
const entriesStateKeyB = `${b}EntriesOf${capitalize2(a)}`;
|
|
523
1940
|
const findStateWithContent = {
|
|
524
1941
|
[entriesStateKeyA]: findMultipleRelatedEntriesState,
|
|
525
1942
|
[entriesStateKeyB]: findMultipleRelatedEntriesState
|
|
@@ -537,11 +1954,13 @@ function join(options, defaultContent, store = internal.IMPLICIT.STORE) {
|
|
|
537
1954
|
throw new Error(`Invalid cardinality: ${options.cardinality}`);
|
|
538
1955
|
}
|
|
539
1956
|
}
|
|
540
|
-
|
|
541
|
-
|
|
1957
|
+
|
|
1958
|
+
// data/src/struct.ts
|
|
1959
|
+
var capitalize3 = (str) => str[0].toUpperCase() + str.slice(1);
|
|
1960
|
+
function struct(options, store = IMPLICIT.STORE) {
|
|
542
1961
|
const atoms = Object.keys(options.default).reduce((acc, key) => {
|
|
543
|
-
const atomName = options.key +
|
|
544
|
-
acc[atomName] =
|
|
1962
|
+
const atomName = options.key + capitalize3(key) + `State`;
|
|
1963
|
+
acc[atomName] = createAtom(
|
|
545
1964
|
{
|
|
546
1965
|
key: `${options.key}.${key}`,
|
|
547
1966
|
default: options.default[key]
|
|
@@ -551,12 +1970,12 @@ function struct(options, store = internal.IMPLICIT.STORE) {
|
|
|
551
1970
|
);
|
|
552
1971
|
return acc;
|
|
553
1972
|
}, {});
|
|
554
|
-
const structState =
|
|
1973
|
+
const structState = createSelector(
|
|
555
1974
|
{
|
|
556
1975
|
key: options.key,
|
|
557
1976
|
get: ({ get }) => {
|
|
558
1977
|
return Object.keys(options.default).reduce((acc, key) => {
|
|
559
|
-
acc[key] = get(atoms[options.key +
|
|
1978
|
+
acc[key] = get(atoms[options.key + capitalize3(key) + `State`]);
|
|
560
1979
|
return acc;
|
|
561
1980
|
}, {});
|
|
562
1981
|
}
|
|
@@ -566,21 +1985,23 @@ function struct(options, store = internal.IMPLICIT.STORE) {
|
|
|
566
1985
|
);
|
|
567
1986
|
return [atoms, structState];
|
|
568
1987
|
}
|
|
569
|
-
|
|
570
|
-
|
|
1988
|
+
|
|
1989
|
+
// data/src/struct-family.ts
|
|
1990
|
+
var capitalize4 = (str) => str[0].toUpperCase() + str.slice(1);
|
|
1991
|
+
var nameFamily = (topKey, subKey) => `find` + capitalize4(topKey) + capitalize4(subKey) + `State`;
|
|
571
1992
|
function structFamily(options) {
|
|
572
1993
|
const atoms = Object.keys(options.default).reduce((acc, subKey) => {
|
|
573
1994
|
const atomFamilyName = nameFamily(options.key, subKey);
|
|
574
|
-
acc[atomFamilyName] =
|
|
1995
|
+
acc[atomFamilyName] = createAtomFamily(
|
|
575
1996
|
{
|
|
576
1997
|
key: `${options.key}.${subKey}`,
|
|
577
1998
|
default: options.default[subKey]
|
|
578
1999
|
},
|
|
579
|
-
|
|
2000
|
+
IMPLICIT.STORE
|
|
580
2001
|
);
|
|
581
2002
|
return acc;
|
|
582
2003
|
}, {});
|
|
583
|
-
const findStructState =
|
|
2004
|
+
const findStructState = createSelectorFamily(
|
|
584
2005
|
{
|
|
585
2006
|
key: options.key,
|
|
586
2007
|
get: (id) => ({ get }) => {
|
|
@@ -592,12 +2013,12 @@ function structFamily(options) {
|
|
|
592
2013
|
}, {});
|
|
593
2014
|
}
|
|
594
2015
|
},
|
|
595
|
-
|
|
2016
|
+
IMPLICIT.STORE
|
|
596
2017
|
);
|
|
597
2018
|
return [atoms, findStructState];
|
|
598
2019
|
}
|
|
599
2020
|
|
|
600
|
-
// src/until.ts
|
|
2021
|
+
// data/src/until.ts
|
|
601
2022
|
function until(loadable, fallback) {
|
|
602
2023
|
if (loadable instanceof Promise) {
|
|
603
2024
|
return fallback;
|