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/json/dist/index.cjs
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
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));
|
|
4
22
|
|
|
5
|
-
//
|
|
23
|
+
// ../anvl/src/json/json-interface.ts
|
|
6
24
|
var stringSetJsonInterface = {
|
|
7
25
|
toJson: (stringSet) => Array.from(stringSet),
|
|
8
26
|
fromJson: (json) => new Set(json)
|
|
9
27
|
};
|
|
10
28
|
|
|
11
|
-
//
|
|
29
|
+
// ../anvl/src/json/index.ts
|
|
12
30
|
var parseJson = (str) => JSON.parse(str);
|
|
13
31
|
var stringifyJson = (json) => JSON.stringify(json);
|
|
14
32
|
var JSON_TYPE_NAMES = [
|
|
@@ -28,7 +46,7 @@ var JSON_DEFAULTS = {
|
|
|
28
46
|
string: ``
|
|
29
47
|
};
|
|
30
48
|
|
|
31
|
-
//
|
|
49
|
+
// ../anvl/src/primitive/index.ts
|
|
32
50
|
var isString = (input) => {
|
|
33
51
|
return typeof input === `string`;
|
|
34
52
|
};
|
|
@@ -44,8 +62,1540 @@ var isNull = (input) => {
|
|
|
44
62
|
var isPrimitive = (input) => {
|
|
45
63
|
return isString(input) || isNumber(input) || isBoolean(input) || isNull(input);
|
|
46
64
|
};
|
|
47
|
-
|
|
48
|
-
|
|
65
|
+
|
|
66
|
+
// src/get-state.ts
|
|
67
|
+
function getState(token, store = IMPLICIT.STORE) {
|
|
68
|
+
var _a;
|
|
69
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
70
|
+
if (state === void 0) {
|
|
71
|
+
throw new NotFoundError(token, store);
|
|
72
|
+
}
|
|
73
|
+
return readOrComputeValue(state, store);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/logger.ts
|
|
77
|
+
var simpleLog = (logLevel) => (icon, tokenType, tokenKey, message, ...rest) => {
|
|
78
|
+
console[logLevel](`${icon} ${tokenType} "${tokenKey}" ${message}`, ...rest);
|
|
79
|
+
};
|
|
80
|
+
var simpleLogger = {
|
|
81
|
+
error: simpleLog(`error`),
|
|
82
|
+
info: simpleLog(`info`),
|
|
83
|
+
warn: simpleLog(`warn`)
|
|
84
|
+
};
|
|
85
|
+
var AtomIOLogger = class {
|
|
86
|
+
constructor(logLevel, filter, logger = simpleLogger) {
|
|
87
|
+
this.logLevel = logLevel;
|
|
88
|
+
this.filter = filter;
|
|
89
|
+
this.logger = logger;
|
|
90
|
+
this.error = (...args) => {
|
|
91
|
+
var _a, _b;
|
|
92
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== null) {
|
|
93
|
+
this.logger.error(...args);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
this.info = (...args) => {
|
|
97
|
+
var _a, _b;
|
|
98
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel === `info`) {
|
|
99
|
+
this.logger.info(...args);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
this.warn = (...args) => {
|
|
103
|
+
var _a, _b;
|
|
104
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== `error` && this.logLevel !== null) {
|
|
105
|
+
this.logger.warn(...args);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// src/set-state.ts
|
|
112
|
+
function setState(token, value, store = IMPLICIT.STORE) {
|
|
113
|
+
var _a;
|
|
114
|
+
const rejection = openOperation(token, store);
|
|
115
|
+
if (rejection) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
119
|
+
if (state === void 0) {
|
|
120
|
+
throw new NotFoundError(token, store);
|
|
121
|
+
}
|
|
122
|
+
setAtomOrSelector(state, value, store);
|
|
123
|
+
closeOperation(store);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// internal/src/future.ts
|
|
127
|
+
var Future = class extends Promise {
|
|
128
|
+
constructor(executor) {
|
|
129
|
+
super((resolve, reject) => {
|
|
130
|
+
const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
|
|
131
|
+
const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
|
|
132
|
+
if (typeof executor === `function`) {
|
|
133
|
+
executor(pass, fail);
|
|
134
|
+
} else {
|
|
135
|
+
executor.then(pass, fail);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
this.isCanceled = false;
|
|
139
|
+
}
|
|
140
|
+
cancel() {
|
|
141
|
+
this.isCanceled = true;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// internal/src/lineage.ts
|
|
146
|
+
function newest(scion) {
|
|
147
|
+
while (scion.child !== null) {
|
|
148
|
+
scion = scion.child;
|
|
149
|
+
}
|
|
150
|
+
return scion;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// internal/src/caching.ts
|
|
154
|
+
function cacheValue(key, value, subject, store) {
|
|
155
|
+
const target = newest(store);
|
|
156
|
+
const currentValue = target.valueMap.get(key);
|
|
157
|
+
if (currentValue instanceof Future) {
|
|
158
|
+
currentValue.cancel();
|
|
159
|
+
}
|
|
160
|
+
if (value instanceof Promise) {
|
|
161
|
+
const future = new Future(value);
|
|
162
|
+
newest(store).valueMap.set(key, future);
|
|
163
|
+
future.then((resolved) => {
|
|
164
|
+
if (future.isCanceled) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
cacheValue(key, resolved, subject, store);
|
|
168
|
+
subject.next({ newValue: resolved, oldValue: future });
|
|
169
|
+
}).catch((thrown) => {
|
|
170
|
+
if (thrown !== `canceled`) {
|
|
171
|
+
store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return future;
|
|
175
|
+
}
|
|
176
|
+
target.valueMap.set(key, value);
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
179
|
+
var readCachedValue = (key, store) => {
|
|
180
|
+
return newest(store).valueMap.get(key);
|
|
181
|
+
};
|
|
182
|
+
var isValueCached = (key, store) => {
|
|
183
|
+
return newest(store).valueMap.has(key);
|
|
184
|
+
};
|
|
185
|
+
var evictCachedValue = (key, store) => {
|
|
186
|
+
const core = newest(store);
|
|
187
|
+
const currentValue = core.valueMap.get(key);
|
|
188
|
+
if (currentValue instanceof Future) {
|
|
189
|
+
currentValue.cancel();
|
|
190
|
+
}
|
|
191
|
+
if (core.operation.open) {
|
|
192
|
+
core.operation.prev.set(key, currentValue);
|
|
193
|
+
}
|
|
194
|
+
core.valueMap.delete(key);
|
|
195
|
+
store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// internal/src/read-or-compute-value.ts
|
|
199
|
+
var readOrComputeValue = (state, store) => {
|
|
200
|
+
if (isValueCached(state.key, store)) {
|
|
201
|
+
store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
|
|
202
|
+
return readCachedValue(state.key, store);
|
|
203
|
+
}
|
|
204
|
+
if (state.type !== `atom`) {
|
|
205
|
+
store.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
|
|
206
|
+
return state.get();
|
|
207
|
+
}
|
|
208
|
+
const fallback = state.default instanceof Function ? state.default() : state.default;
|
|
209
|
+
store.logger.info(
|
|
210
|
+
`\u{1F481}`,
|
|
211
|
+
`atom`,
|
|
212
|
+
state.key,
|
|
213
|
+
`could not find cached value; using default`,
|
|
214
|
+
fallback
|
|
215
|
+
);
|
|
216
|
+
return state.default instanceof Function ? state.default() : state.default;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// internal/src/operation.ts
|
|
220
|
+
var openOperation = (token, store) => {
|
|
221
|
+
const target = newest(store);
|
|
222
|
+
if (target.operation.open) {
|
|
223
|
+
store.logger.error(
|
|
224
|
+
`\u274C`,
|
|
225
|
+
token.type,
|
|
226
|
+
token.key,
|
|
227
|
+
`failed to setState during a setState for "${target.operation.token.key}"`
|
|
228
|
+
);
|
|
229
|
+
return `rejection`;
|
|
230
|
+
}
|
|
231
|
+
target.operation = {
|
|
232
|
+
open: true,
|
|
233
|
+
done: /* @__PURE__ */ new Set(),
|
|
234
|
+
prev: /* @__PURE__ */ new Map(),
|
|
235
|
+
time: Date.now(),
|
|
236
|
+
token
|
|
237
|
+
};
|
|
238
|
+
store.logger.info(
|
|
239
|
+
`\u2B55`,
|
|
240
|
+
token.type,
|
|
241
|
+
token.key,
|
|
242
|
+
`operation start in store "${store.config.name}"${target.transactionMeta === null ? `` : ` ${target.transactionMeta.phase} "${target.transactionMeta.update.key}"`}`
|
|
243
|
+
);
|
|
244
|
+
};
|
|
245
|
+
var closeOperation = (store) => {
|
|
246
|
+
const target = newest(store);
|
|
247
|
+
if (target.operation.open) {
|
|
248
|
+
store.logger.info(
|
|
249
|
+
`\u{1F534}`,
|
|
250
|
+
target.operation.token.type,
|
|
251
|
+
target.operation.token.key,
|
|
252
|
+
`operation done in store "${store.config.name}"`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
target.operation = { open: false };
|
|
256
|
+
store.subject.operationStatus.next(target.operation);
|
|
257
|
+
};
|
|
258
|
+
var isDone = (key, store) => {
|
|
259
|
+
const target = newest(store);
|
|
260
|
+
if (!target.operation.open) {
|
|
261
|
+
store.logger.warn(
|
|
262
|
+
`\u{1F41E}`,
|
|
263
|
+
`unknown`,
|
|
264
|
+
key,
|
|
265
|
+
`isDone called outside of an operation. This is probably a bug.`
|
|
266
|
+
);
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
return target.operation.done.has(key);
|
|
270
|
+
};
|
|
271
|
+
var markDone = (key, store) => {
|
|
272
|
+
const target = newest(store);
|
|
273
|
+
if (!target.operation.open) {
|
|
274
|
+
store.logger.warn(
|
|
275
|
+
`\u{1F41E}`,
|
|
276
|
+
`unknown`,
|
|
277
|
+
key,
|
|
278
|
+
`markDone called outside of an operation. This is probably a bug.`
|
|
279
|
+
);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
target.operation.done.add(key);
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// internal/src/set-state/become.ts
|
|
286
|
+
var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
|
|
287
|
+
originalThing instanceof Function ? originalThing() : originalThing
|
|
288
|
+
) : nextVersionOfThing;
|
|
289
|
+
|
|
290
|
+
// internal/src/subject.ts
|
|
291
|
+
var Subject = class {
|
|
292
|
+
constructor() {
|
|
293
|
+
this.subscribers = /* @__PURE__ */ new Map();
|
|
294
|
+
}
|
|
295
|
+
subscribe(key, subscriber) {
|
|
296
|
+
this.subscribers.set(key, subscriber);
|
|
297
|
+
const unsubscribe = () => this.unsubscribe(key);
|
|
298
|
+
return unsubscribe;
|
|
299
|
+
}
|
|
300
|
+
unsubscribe(key) {
|
|
301
|
+
this.subscribers.delete(key);
|
|
302
|
+
}
|
|
303
|
+
next(value) {
|
|
304
|
+
for (const subscriber of this.subscribers.values()) {
|
|
305
|
+
subscriber(value);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
var StatefulSubject = class extends Subject {
|
|
310
|
+
constructor(initialState) {
|
|
311
|
+
super();
|
|
312
|
+
this.state = initialState;
|
|
313
|
+
}
|
|
314
|
+
next(value) {
|
|
315
|
+
this.state = value;
|
|
316
|
+
super.next(value);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// internal/src/set-state/copy-mutable-if-needed.ts
|
|
321
|
+
function copyMutableIfNeeded(atom, transform, origin, target) {
|
|
322
|
+
const originValue = origin.valueMap.get(atom.key);
|
|
323
|
+
const targetValue = target.valueMap.get(atom.key);
|
|
324
|
+
if (originValue === targetValue) {
|
|
325
|
+
origin.logger.info(`\u{1F4C3}`, `atom`, `${atom.key}`, `copying`);
|
|
326
|
+
const jsonValue = transform.toJson(originValue);
|
|
327
|
+
const copiedValue = transform.fromJson(jsonValue);
|
|
328
|
+
target.valueMap.set(atom.key, copiedValue);
|
|
329
|
+
new Tracker(atom, origin);
|
|
330
|
+
return copiedValue;
|
|
331
|
+
}
|
|
332
|
+
return targetValue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// internal/src/set-state/copy-mutable-in-transaction.ts
|
|
336
|
+
function copyMutableIfWithinTransaction(oldValue, atom, store) {
|
|
337
|
+
const target = newest(store);
|
|
338
|
+
const parent = target.parent;
|
|
339
|
+
if (parent !== null) {
|
|
340
|
+
if (`toJson` in atom && `fromJson` in atom) {
|
|
341
|
+
const copiedValue = copyMutableIfNeeded(atom, atom, parent, target);
|
|
342
|
+
return copiedValue;
|
|
343
|
+
}
|
|
344
|
+
if (`family` in atom) {
|
|
345
|
+
const family = parent.families.get(atom.family.key);
|
|
346
|
+
if (family && family.type === `atom_family`) {
|
|
347
|
+
const result = copyMutableFamilyMemberWithinTransaction(
|
|
348
|
+
atom,
|
|
349
|
+
family,
|
|
350
|
+
parent,
|
|
351
|
+
target
|
|
352
|
+
);
|
|
353
|
+
if (result) {
|
|
354
|
+
return result;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return oldValue;
|
|
360
|
+
}
|
|
361
|
+
function copyMutableFamilyMemberWithinTransaction(atom, family, origin, target) {
|
|
362
|
+
if (`toJson` in family && `fromJson` in family) {
|
|
363
|
+
const copyCreated = copyMutableIfNeeded(atom, family, origin, target);
|
|
364
|
+
return copyCreated;
|
|
365
|
+
}
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// internal/src/set-state/emit-update.ts
|
|
370
|
+
var emitUpdate = (state, update, store) => {
|
|
371
|
+
store.logger.info(
|
|
372
|
+
`\u{1F4E2}`,
|
|
373
|
+
state.type,
|
|
374
|
+
state.key,
|
|
375
|
+
`went (`,
|
|
376
|
+
update.oldValue,
|
|
377
|
+
`->`,
|
|
378
|
+
update.newValue,
|
|
379
|
+
`) subscribers:`,
|
|
380
|
+
state.subject.subscribers
|
|
381
|
+
);
|
|
382
|
+
state.subject.next(update);
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// internal/src/set-state/evict-downstream.ts
|
|
386
|
+
var evictDownStream = (atom, store) => {
|
|
387
|
+
const target = newest(store);
|
|
388
|
+
const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom.key);
|
|
389
|
+
store.logger.info(
|
|
390
|
+
`\u{1F9F9}`,
|
|
391
|
+
atom.type,
|
|
392
|
+
atom.key,
|
|
393
|
+
downstreamKeys ? `evicting ${downstreamKeys.size} states downstream:` : `no downstream states`,
|
|
394
|
+
downstreamKeys != null ? downstreamKeys : `to evict`
|
|
395
|
+
);
|
|
396
|
+
if (downstreamKeys) {
|
|
397
|
+
if (target.operation.open) {
|
|
398
|
+
store.logger.info(
|
|
399
|
+
`\u{1F9F9}`,
|
|
400
|
+
atom.type,
|
|
401
|
+
atom.key,
|
|
402
|
+
`[ ${[...target.operation.done].join(`, `)} ] already done`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
for (const key of downstreamKeys) {
|
|
406
|
+
if (isDone(key, store)) {
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
evictCachedValue(key, store);
|
|
410
|
+
markDone(key, store);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
// internal/src/set-state/stow-update.ts
|
|
416
|
+
function shouldUpdateBeStowed(key, update) {
|
|
417
|
+
if (isTransceiver(update.newValue)) {
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
if (key.includes(`\u{1F441}\u200D\u{1F5E8}`)) {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
425
|
+
var stowUpdate = (state, update, store) => {
|
|
426
|
+
const { key } = state;
|
|
427
|
+
const target = newest(store);
|
|
428
|
+
if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
|
|
429
|
+
store.logger.error(
|
|
430
|
+
`\u{1F41E}`,
|
|
431
|
+
`atom`,
|
|
432
|
+
key,
|
|
433
|
+
`stowUpdate called outside of a transaction. This is probably a bug.`
|
|
434
|
+
);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const shouldStow = shouldUpdateBeStowed(key, update);
|
|
438
|
+
if (!shouldStow) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const atomUpdate = __spreadValues({ key }, update);
|
|
442
|
+
if (state.family) {
|
|
443
|
+
atomUpdate.family = state.family;
|
|
444
|
+
}
|
|
445
|
+
target.transactionMeta.update.updates.push(atomUpdate);
|
|
446
|
+
store.logger.info(
|
|
447
|
+
`\u{1F4C1}`,
|
|
448
|
+
`atom`,
|
|
449
|
+
key,
|
|
450
|
+
`stowed (`,
|
|
451
|
+
update.oldValue,
|
|
452
|
+
`->`,
|
|
453
|
+
update.newValue,
|
|
454
|
+
`)`
|
|
455
|
+
);
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// internal/src/set-state/set-atom.ts
|
|
459
|
+
var setAtom = (atom, next, store) => {
|
|
460
|
+
const target = newest(store);
|
|
461
|
+
const oldValue = readOrComputeValue(atom, store);
|
|
462
|
+
let newValue = copyMutableIfWithinTransaction(oldValue, atom, store);
|
|
463
|
+
newValue = become(next)(newValue);
|
|
464
|
+
store.logger.info(`\u{1F4DD}`, `atom`, atom.key, `set to`, newValue);
|
|
465
|
+
newValue = cacheValue(atom.key, newValue, atom.subject, store);
|
|
466
|
+
if (isAtomDefault(atom.key, store)) {
|
|
467
|
+
markAtomAsNotDefault(atom.key, store);
|
|
468
|
+
}
|
|
469
|
+
markDone(atom.key, store);
|
|
470
|
+
evictDownStream(atom, store);
|
|
471
|
+
const update = { oldValue, newValue };
|
|
472
|
+
if (target.transactionMeta === null || target.transactionMeta.phase === `applying`) {
|
|
473
|
+
emitUpdate(atom, update, store);
|
|
474
|
+
} else {
|
|
475
|
+
stowUpdate(atom, update, store);
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// internal/src/set-state/set-atom-or-selector.ts
|
|
480
|
+
var setAtomOrSelector = (state, value, store) => {
|
|
481
|
+
if (state.type === `selector`) {
|
|
482
|
+
state.set(value);
|
|
483
|
+
} else {
|
|
484
|
+
setAtom(state, value, store);
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
// internal/src/store/deposit.ts
|
|
489
|
+
function deposit(state) {
|
|
490
|
+
const token = {
|
|
491
|
+
key: state.key,
|
|
492
|
+
type: state.type
|
|
493
|
+
};
|
|
494
|
+
if (`family` in state) {
|
|
495
|
+
token.family = state.family;
|
|
496
|
+
}
|
|
497
|
+
return token;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// ../rel8/junction/src/junction.ts
|
|
501
|
+
var Junction = class {
|
|
502
|
+
constructor(data, config) {
|
|
503
|
+
this.relations = /* @__PURE__ */ new Map();
|
|
504
|
+
this.contents = /* @__PURE__ */ new Map();
|
|
505
|
+
this.makeContentKey = (a, b) => `${a}:${b}`;
|
|
506
|
+
var _a, _b, _c, _d;
|
|
507
|
+
this.a = data.between[0];
|
|
508
|
+
this.b = data.between[1];
|
|
509
|
+
this.cardinality = data.cardinality;
|
|
510
|
+
if (!(config == null ? void 0 : config.externalStore)) {
|
|
511
|
+
this.relations = new Map((_a = data.relations) == null ? void 0 : _a.map(([a, b]) => [a, new Set(b)]));
|
|
512
|
+
this.contents = new Map(data.contents);
|
|
513
|
+
}
|
|
514
|
+
this.isContent = (_b = config == null ? void 0 : config.isContent) != null ? _b : null;
|
|
515
|
+
if (config == null ? void 0 : config.makeContentKey) {
|
|
516
|
+
this.makeContentKey = config.makeContentKey;
|
|
517
|
+
}
|
|
518
|
+
if (config == null ? void 0 : config.externalStore) {
|
|
519
|
+
const externalStore = config.externalStore;
|
|
520
|
+
this.has = (a, b) => externalStore.has(a, b);
|
|
521
|
+
this.addRelation = (a, b) => {
|
|
522
|
+
externalStore.addRelation(a, b);
|
|
523
|
+
};
|
|
524
|
+
this.deleteRelation = (a, b) => {
|
|
525
|
+
externalStore.deleteRelation(a, b);
|
|
526
|
+
};
|
|
527
|
+
this.replaceRelationsSafely = (a, bs) => {
|
|
528
|
+
externalStore.replaceRelationsSafely(a, bs);
|
|
529
|
+
};
|
|
530
|
+
this.replaceRelationsUnsafely = (a, bs) => {
|
|
531
|
+
externalStore.replaceRelationsUnsafely(a, bs);
|
|
532
|
+
};
|
|
533
|
+
this.getRelatedKeys = (key) => externalStore.getRelatedKeys(key);
|
|
534
|
+
if (externalStore.getContent) {
|
|
535
|
+
this.getContentInternal = (contentKey) => {
|
|
536
|
+
return externalStore.getContent(contentKey);
|
|
537
|
+
};
|
|
538
|
+
this.setContent = (contentKey, content) => {
|
|
539
|
+
externalStore.setContent(contentKey, content);
|
|
540
|
+
};
|
|
541
|
+
this.deleteContent = (contentKey) => {
|
|
542
|
+
externalStore.deleteContent(contentKey);
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
for (const [x, ys] of (_c = data.relations) != null ? _c : []) {
|
|
546
|
+
for (const y of ys)
|
|
547
|
+
this.addRelation(x, y);
|
|
548
|
+
}
|
|
549
|
+
for (const [contentKey, content] of (_d = data.contents) != null ? _d : []) {
|
|
550
|
+
this.setContent(contentKey, content);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
getRelatedKeys(key) {
|
|
555
|
+
return this.relations.get(key);
|
|
556
|
+
}
|
|
557
|
+
addRelation(a, b) {
|
|
558
|
+
let aRelations = this.relations.get(a);
|
|
559
|
+
let bRelations = this.relations.get(b);
|
|
560
|
+
if (aRelations) {
|
|
561
|
+
aRelations.add(b);
|
|
562
|
+
} else {
|
|
563
|
+
aRelations = /* @__PURE__ */ new Set([b]);
|
|
564
|
+
this.relations.set(a, aRelations);
|
|
565
|
+
}
|
|
566
|
+
if (bRelations) {
|
|
567
|
+
bRelations.add(a);
|
|
568
|
+
} else {
|
|
569
|
+
bRelations = /* @__PURE__ */ new Set([a]);
|
|
570
|
+
this.relations.set(b, bRelations);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
deleteRelation(a, b) {
|
|
574
|
+
const aRelations = this.relations.get(a);
|
|
575
|
+
if (aRelations) {
|
|
576
|
+
aRelations.delete(b);
|
|
577
|
+
if (aRelations.size === 0) {
|
|
578
|
+
this.relations.delete(a);
|
|
579
|
+
}
|
|
580
|
+
const bRelations = this.relations.get(b);
|
|
581
|
+
if (bRelations) {
|
|
582
|
+
bRelations.delete(a);
|
|
583
|
+
if (bRelations.size === 0) {
|
|
584
|
+
this.relations.delete(b);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
replaceRelationsUnsafely(a, bs) {
|
|
590
|
+
this.relations.set(a, new Set(bs));
|
|
591
|
+
for (const b of bs) {
|
|
592
|
+
const bRelations = /* @__PURE__ */ new Set([a]);
|
|
593
|
+
this.relations.set(b, bRelations);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
replaceRelationsSafely(a, bs) {
|
|
597
|
+
const aRelationsPrev = this.relations.get(a);
|
|
598
|
+
if (aRelationsPrev) {
|
|
599
|
+
for (const b of aRelationsPrev) {
|
|
600
|
+
const bRelations = this.relations.get(b);
|
|
601
|
+
if (bRelations) {
|
|
602
|
+
if (bRelations.size === 1) {
|
|
603
|
+
this.relations.delete(b);
|
|
604
|
+
} else {
|
|
605
|
+
bRelations.delete(a);
|
|
606
|
+
}
|
|
607
|
+
this.contents.delete(this.makeContentKey(a, b));
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
this.relations.set(a, new Set(bs));
|
|
612
|
+
for (const b of bs) {
|
|
613
|
+
let bRelations = this.relations.get(b);
|
|
614
|
+
if (bRelations) {
|
|
615
|
+
bRelations.add(a);
|
|
616
|
+
} else {
|
|
617
|
+
bRelations = /* @__PURE__ */ new Set([a]);
|
|
618
|
+
this.relations.set(b, bRelations);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
getContentInternal(contentKey) {
|
|
623
|
+
return this.contents.get(contentKey);
|
|
624
|
+
}
|
|
625
|
+
setContent(contentKey, content) {
|
|
626
|
+
this.contents.set(contentKey, content);
|
|
627
|
+
}
|
|
628
|
+
deleteContent(contentKey) {
|
|
629
|
+
this.contents.delete(contentKey);
|
|
630
|
+
}
|
|
631
|
+
toJSON() {
|
|
632
|
+
return {
|
|
633
|
+
between: [this.a, this.b],
|
|
634
|
+
cardinality: this.cardinality,
|
|
635
|
+
relations: [...this.relations.entries()].map(([a, b]) => [a, [...b]]),
|
|
636
|
+
contents: [...this.contents.entries()]
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
set(a, ...rest) {
|
|
640
|
+
var _a;
|
|
641
|
+
const b = typeof rest[0] === `string` ? rest[0] : a[this.b];
|
|
642
|
+
const content = ((_a = rest[1]) != null ? _a : typeof rest[0] === `string`) ? void 0 : rest[0];
|
|
643
|
+
a = typeof a === `string` ? a : a[this.a];
|
|
644
|
+
switch (this.cardinality) {
|
|
645
|
+
case `1:1`: {
|
|
646
|
+
const bPrev = this.getRelatedKey(a);
|
|
647
|
+
if (bPrev && bPrev !== b)
|
|
648
|
+
this.delete(bPrev, a);
|
|
649
|
+
}
|
|
650
|
+
case `1:n`: {
|
|
651
|
+
const aPrev = this.getRelatedKey(b);
|
|
652
|
+
if (aPrev && aPrev !== a)
|
|
653
|
+
this.delete(aPrev, b);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
if (content) {
|
|
657
|
+
const contentKey = this.makeContentKey(a, b);
|
|
658
|
+
this.setContent(contentKey, content);
|
|
659
|
+
}
|
|
660
|
+
this.addRelation(a, b);
|
|
661
|
+
return this;
|
|
662
|
+
}
|
|
663
|
+
delete(x, b) {
|
|
664
|
+
b = typeof b === `string` ? b : x[this.b];
|
|
665
|
+
const a = typeof x === `string` ? x : x[this.a];
|
|
666
|
+
if (a === void 0 && typeof b === `string`) {
|
|
667
|
+
const bRelations = this.getRelatedKeys(b);
|
|
668
|
+
if (bRelations) {
|
|
669
|
+
for (const a2 of bRelations) {
|
|
670
|
+
this.delete(a2, b);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
if (typeof a === `string` && b === void 0) {
|
|
675
|
+
const aRelations = this.getRelatedKeys(a);
|
|
676
|
+
if (aRelations) {
|
|
677
|
+
for (const b2 of aRelations) {
|
|
678
|
+
this.delete(a, b2);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (typeof a === `string` && typeof b === `string`) {
|
|
683
|
+
this.deleteRelation(a, b);
|
|
684
|
+
const contentKey = this.makeContentKey(a, b);
|
|
685
|
+
this.deleteContent(contentKey);
|
|
686
|
+
}
|
|
687
|
+
return this;
|
|
688
|
+
}
|
|
689
|
+
getRelatedKey(key) {
|
|
690
|
+
const relations = this.getRelatedKeys(key);
|
|
691
|
+
if (relations) {
|
|
692
|
+
if (relations.size > 1) {
|
|
693
|
+
console.warn(
|
|
694
|
+
`${relations.size} related keys were found for key "${key}": (${[
|
|
695
|
+
...relations
|
|
696
|
+
].map((k) => `"${k}"`).join(`, `)}). Only one related key was expected.`
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
for (const relation of relations) {
|
|
700
|
+
return relation;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
replaceRelations(a, relations, config) {
|
|
705
|
+
const hasContent = !Array.isArray(relations);
|
|
706
|
+
const bs = hasContent ? Object.keys(relations) : relations;
|
|
707
|
+
if (config == null ? void 0 : config.reckless) {
|
|
708
|
+
this.replaceRelationsUnsafely(a, bs);
|
|
709
|
+
} else {
|
|
710
|
+
this.replaceRelationsSafely(a, bs);
|
|
711
|
+
}
|
|
712
|
+
if (hasContent) {
|
|
713
|
+
for (const b of bs) {
|
|
714
|
+
const contentKey = this.makeContentKey(a, b);
|
|
715
|
+
const content = relations[b];
|
|
716
|
+
this.setContent(contentKey, content);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return this;
|
|
720
|
+
}
|
|
721
|
+
getContent(a, b) {
|
|
722
|
+
const contentKey = this.makeContentKey(a, b);
|
|
723
|
+
return this.getContentInternal(contentKey);
|
|
724
|
+
}
|
|
725
|
+
getRelationEntries(input) {
|
|
726
|
+
const a = input[this.a];
|
|
727
|
+
const b = input[this.b];
|
|
728
|
+
if (a !== void 0 && b === void 0) {
|
|
729
|
+
const aRelations = this.getRelatedKeys(a);
|
|
730
|
+
if (aRelations) {
|
|
731
|
+
return [...aRelations].map((b2) => {
|
|
732
|
+
var _a;
|
|
733
|
+
return [b2, (_a = this.getContent(a, b2)) != null ? _a : null];
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (a === void 0 && b !== void 0) {
|
|
738
|
+
const bRelations = this.getRelatedKeys(b);
|
|
739
|
+
if (bRelations) {
|
|
740
|
+
return [...bRelations].map((a2) => {
|
|
741
|
+
var _a;
|
|
742
|
+
return [a2, (_a = this.getContent(a2, b)) != null ? _a : null];
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
return [];
|
|
747
|
+
}
|
|
748
|
+
has(a, b) {
|
|
749
|
+
var _a;
|
|
750
|
+
if (b) {
|
|
751
|
+
const setA = this.getRelatedKeys(a);
|
|
752
|
+
return (_a = setA == null ? void 0 : setA.has(b)) != null ? _a : false;
|
|
753
|
+
}
|
|
754
|
+
return this.relations.has(a);
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
// internal/src/store/store.ts
|
|
759
|
+
var Store = class {
|
|
760
|
+
constructor(name, store = null) {
|
|
761
|
+
this.parent = null;
|
|
762
|
+
this.child = null;
|
|
763
|
+
this.valueMap = /* @__PURE__ */ new Map();
|
|
764
|
+
this.atoms = /* @__PURE__ */ new Map();
|
|
765
|
+
this.selectors = /* @__PURE__ */ new Map();
|
|
766
|
+
this.readonlySelectors = /* @__PURE__ */ new Map();
|
|
767
|
+
this.trackers = /* @__PURE__ */ new Map();
|
|
768
|
+
this.families = /* @__PURE__ */ new Map();
|
|
769
|
+
this.timelines = /* @__PURE__ */ new Map();
|
|
770
|
+
this.transactions = /* @__PURE__ */ new Map();
|
|
771
|
+
this.atomsThatAreDefault = /* @__PURE__ */ new Set();
|
|
772
|
+
this.timelineAtoms = new Junction({
|
|
773
|
+
between: [`timelineKey`, `atomKey`],
|
|
774
|
+
cardinality: `1:n`
|
|
775
|
+
});
|
|
776
|
+
this.selectorAtoms = new Junction({
|
|
777
|
+
between: [`selectorKey`, `atomKey`],
|
|
778
|
+
cardinality: `n:n`
|
|
779
|
+
});
|
|
780
|
+
this.selectorGraph = new Junction(
|
|
781
|
+
{
|
|
782
|
+
between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
|
|
783
|
+
cardinality: `n:n`
|
|
784
|
+
},
|
|
785
|
+
{
|
|
786
|
+
makeContentKey: (...keys) => keys.sort().join(`:`)
|
|
787
|
+
}
|
|
788
|
+
);
|
|
789
|
+
this.subject = {
|
|
790
|
+
atomCreation: new Subject(),
|
|
791
|
+
selectorCreation: new Subject(),
|
|
792
|
+
transactionCreation: new Subject(),
|
|
793
|
+
timelineCreation: new Subject(),
|
|
794
|
+
transactionApplying: new StatefulSubject(null),
|
|
795
|
+
operationStatus: new Subject()
|
|
796
|
+
};
|
|
797
|
+
this.operation = { open: false };
|
|
798
|
+
this.transactionMeta = null;
|
|
799
|
+
this.config = {
|
|
800
|
+
name: `IMPLICIT_STORE`
|
|
801
|
+
};
|
|
802
|
+
this.loggers = [
|
|
803
|
+
new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
|
|
804
|
+
];
|
|
805
|
+
this.logger = {
|
|
806
|
+
error: (...messages) => {
|
|
807
|
+
for (const logger of this.loggers)
|
|
808
|
+
logger.error(...messages);
|
|
809
|
+
},
|
|
810
|
+
info: (...messages) => {
|
|
811
|
+
for (const logger of this.loggers)
|
|
812
|
+
logger.info(...messages);
|
|
813
|
+
},
|
|
814
|
+
warn: (...messages) => {
|
|
815
|
+
for (const logger of this.loggers)
|
|
816
|
+
logger.warn(...messages);
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
if (store !== null) {
|
|
820
|
+
this.valueMap = new Map(store == null ? void 0 : store.valueMap);
|
|
821
|
+
this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
|
|
822
|
+
this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
|
|
823
|
+
this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
|
|
824
|
+
name
|
|
825
|
+
});
|
|
826
|
+
for (const [, atom] of store.atoms) {
|
|
827
|
+
atom.install(this);
|
|
828
|
+
}
|
|
829
|
+
for (const [, selector] of store.readonlySelectors) {
|
|
830
|
+
selector.install(this);
|
|
831
|
+
}
|
|
832
|
+
for (const [, selector] of store.selectors) {
|
|
833
|
+
selector.install(this);
|
|
834
|
+
}
|
|
835
|
+
for (const [, tx] of store.transactions) {
|
|
836
|
+
tx.install(this);
|
|
837
|
+
}
|
|
838
|
+
for (const [, timeline] of store.timelines) {
|
|
839
|
+
timeline.install(this);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
var IMPLICIT = {
|
|
845
|
+
STORE_INTERNAL: void 0,
|
|
846
|
+
get STORE() {
|
|
847
|
+
var _a;
|
|
848
|
+
return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
// internal/src/store/withdraw.ts
|
|
853
|
+
function withdraw(token, store) {
|
|
854
|
+
var _a, _b, _c, _d;
|
|
855
|
+
const target = newest(store);
|
|
856
|
+
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);
|
|
857
|
+
if (state) {
|
|
858
|
+
return state;
|
|
859
|
+
}
|
|
860
|
+
return void 0;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// internal/src/store/withdraw-new-family-member.ts
|
|
864
|
+
function withdrawNewFamilyMember(token, store) {
|
|
865
|
+
if (token.family) {
|
|
866
|
+
store.logger.info(
|
|
867
|
+
`\u{1F46A}`,
|
|
868
|
+
token.type,
|
|
869
|
+
token.key,
|
|
870
|
+
`creating new family member in store "${store.config.name}"`
|
|
871
|
+
);
|
|
872
|
+
const target = newest(store);
|
|
873
|
+
const family = target.families.get(token.family.key);
|
|
874
|
+
if (family) {
|
|
875
|
+
const jsonSubKey = JSON.parse(token.family.subKey);
|
|
876
|
+
family(jsonSubKey);
|
|
877
|
+
const state = withdraw(token, store);
|
|
878
|
+
return state;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return void 0;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// internal/src/keys.ts
|
|
885
|
+
var isAtomKey = (key, store) => newest(store).atoms.has(key);
|
|
886
|
+
var isSelectorKey = (key, store) => newest(store).selectors.has(key);
|
|
887
|
+
var isReadonlySelectorKey = (key, store) => newest(store).readonlySelectors.has(key);
|
|
888
|
+
var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
|
|
889
|
+
|
|
890
|
+
// internal/src/selector/get-selector-dependency-keys.ts
|
|
891
|
+
var getSelectorDependencyKeys = (key, store) => {
|
|
892
|
+
const sources = newest(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
|
|
893
|
+
return sources;
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
// internal/src/selector/trace-selector-atoms.ts
|
|
897
|
+
var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
|
|
898
|
+
const rootKeys = [];
|
|
899
|
+
const indirectDependencyKeys = getSelectorDependencyKeys(
|
|
900
|
+
directDependencyKey,
|
|
901
|
+
store
|
|
902
|
+
);
|
|
903
|
+
let depth = 0;
|
|
904
|
+
while (indirectDependencyKeys.length > 0) {
|
|
905
|
+
const indirectDependencyKey = indirectDependencyKeys.shift();
|
|
906
|
+
++depth;
|
|
907
|
+
if (depth > 99999) {
|
|
908
|
+
throw new Error(
|
|
909
|
+
`Maximum selector dependency depth exceeded (> 99999) in selector "${selectorKey}". This is likely due to a circular dependency.`
|
|
910
|
+
);
|
|
911
|
+
}
|
|
912
|
+
if (!isAtomKey(indirectDependencyKey, store)) {
|
|
913
|
+
indirectDependencyKeys.push(
|
|
914
|
+
...getSelectorDependencyKeys(indirectDependencyKey, store)
|
|
915
|
+
);
|
|
916
|
+
} else if (!rootKeys.includes(indirectDependencyKey)) {
|
|
917
|
+
rootKeys.push(indirectDependencyKey);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return rootKeys;
|
|
921
|
+
};
|
|
922
|
+
var traceAllSelectorAtoms = (selectorKey, store) => {
|
|
923
|
+
const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
|
|
924
|
+
return directDependencyKeys.flatMap(
|
|
925
|
+
(depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
|
|
926
|
+
);
|
|
927
|
+
};
|
|
928
|
+
|
|
929
|
+
// internal/src/selector/update-selector-atoms.ts
|
|
930
|
+
var updateSelectorAtoms = (selectorKey, dependency, store) => {
|
|
931
|
+
const target = newest(store);
|
|
932
|
+
if (dependency.type === `atom`) {
|
|
933
|
+
target.selectorAtoms.set({
|
|
934
|
+
selectorKey,
|
|
935
|
+
atomKey: dependency.key
|
|
936
|
+
});
|
|
937
|
+
store.logger.info(
|
|
938
|
+
`\u{1F50D}`,
|
|
939
|
+
`selector`,
|
|
940
|
+
selectorKey,
|
|
941
|
+
`discovers root atom "${dependency.key}"`
|
|
942
|
+
);
|
|
943
|
+
} else {
|
|
944
|
+
const rootKeys = traceSelectorAtoms(selectorKey, dependency.key, store);
|
|
945
|
+
store.logger.info(
|
|
946
|
+
`\u{1F50D}`,
|
|
947
|
+
`selector`,
|
|
948
|
+
selectorKey,
|
|
949
|
+
`discovers root atoms: [ ${rootKeys.map((key) => `"${key}"`).join(`, `)} ]`
|
|
950
|
+
);
|
|
951
|
+
for (const atomKey of rootKeys) {
|
|
952
|
+
target.selectorAtoms = target.selectorAtoms.set({
|
|
953
|
+
selectorKey,
|
|
954
|
+
atomKey
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
// internal/src/selector/register-selector.ts
|
|
961
|
+
var registerSelector = (selectorKey, store) => ({
|
|
962
|
+
get: (dependency) => {
|
|
963
|
+
const target = newest(store);
|
|
964
|
+
const alreadyRegistered = target.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
|
|
965
|
+
const dependencyState = withdraw(dependency, store);
|
|
966
|
+
if (dependencyState === void 0) {
|
|
967
|
+
throw new Error(
|
|
968
|
+
`State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
969
|
+
);
|
|
970
|
+
}
|
|
971
|
+
const dependencyValue = readOrComputeValue(dependencyState, store);
|
|
972
|
+
store.logger.info(
|
|
973
|
+
`\u{1F50C}`,
|
|
974
|
+
`selector`,
|
|
975
|
+
selectorKey,
|
|
976
|
+
`registers dependency ( "${dependency.key}" =`,
|
|
977
|
+
dependencyValue,
|
|
978
|
+
`)`
|
|
979
|
+
);
|
|
980
|
+
if (!alreadyRegistered) {
|
|
981
|
+
target.selectorGraph.set(
|
|
982
|
+
{
|
|
983
|
+
upstreamSelectorKey: dependency.key,
|
|
984
|
+
downstreamSelectorKey: selectorKey
|
|
985
|
+
},
|
|
986
|
+
{
|
|
987
|
+
source: dependency.key
|
|
988
|
+
}
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
updateSelectorAtoms(selectorKey, dependency, store);
|
|
992
|
+
return dependencyValue;
|
|
993
|
+
},
|
|
994
|
+
set: (stateToken, newValue) => {
|
|
995
|
+
const state = withdraw(stateToken, store);
|
|
996
|
+
if (state === void 0) {
|
|
997
|
+
throw new Error(
|
|
998
|
+
`State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
setAtomOrSelector(state, newValue, store);
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
// internal/src/selector/create-read-write-selector.ts
|
|
1006
|
+
var createReadWriteSelector = (options, family, store) => {
|
|
1007
|
+
const target = newest(store);
|
|
1008
|
+
const subject = new Subject();
|
|
1009
|
+
const { get, set } = registerSelector(options.key, store);
|
|
1010
|
+
const getSelf = () => {
|
|
1011
|
+
const value = options.get({ get });
|
|
1012
|
+
cacheValue(options.key, value, subject, store);
|
|
1013
|
+
return value;
|
|
1014
|
+
};
|
|
1015
|
+
const setSelf = (next) => {
|
|
1016
|
+
const oldValue = getSelf();
|
|
1017
|
+
const newValue = become(next)(oldValue);
|
|
1018
|
+
store.logger.info(
|
|
1019
|
+
`\u{1F4DD}`,
|
|
1020
|
+
`selector`,
|
|
1021
|
+
options.key,
|
|
1022
|
+
`set (`,
|
|
1023
|
+
oldValue,
|
|
1024
|
+
`->`,
|
|
1025
|
+
newValue,
|
|
1026
|
+
`)`
|
|
1027
|
+
);
|
|
1028
|
+
cacheValue(options.key, newValue, subject, store);
|
|
1029
|
+
markDone(options.key, store);
|
|
1030
|
+
if (target.transactionMeta === null) {
|
|
1031
|
+
subject.next({ newValue, oldValue });
|
|
1032
|
+
}
|
|
1033
|
+
options.set({ get, set }, newValue);
|
|
1034
|
+
};
|
|
1035
|
+
const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1036
|
+
subject,
|
|
1037
|
+
install: (s) => createSelector(options, family, s),
|
|
1038
|
+
get: getSelf,
|
|
1039
|
+
set: setSelf,
|
|
1040
|
+
type: `selector`
|
|
1041
|
+
}), family && { family });
|
|
1042
|
+
target.selectors.set(options.key, mySelector);
|
|
1043
|
+
const initialValue = getSelf();
|
|
1044
|
+
store.logger.info(`\u2728`, mySelector.type, mySelector.key, `=`, initialValue);
|
|
1045
|
+
const token = {
|
|
1046
|
+
key: options.key,
|
|
1047
|
+
type: `selector`
|
|
1048
|
+
};
|
|
1049
|
+
if (family) {
|
|
1050
|
+
token.family = family;
|
|
1051
|
+
}
|
|
1052
|
+
store.subject.selectorCreation.next(token);
|
|
1053
|
+
return token;
|
|
1054
|
+
};
|
|
1055
|
+
|
|
1056
|
+
// internal/src/selector/create-readonly-selector.ts
|
|
1057
|
+
var createReadonlySelector = (options, family, store) => {
|
|
1058
|
+
const target = newest(store);
|
|
1059
|
+
const subject = new Subject();
|
|
1060
|
+
const { get } = registerSelector(options.key, store);
|
|
1061
|
+
const getSelf = () => {
|
|
1062
|
+
const value = options.get({ get });
|
|
1063
|
+
cacheValue(options.key, value, subject, store);
|
|
1064
|
+
return value;
|
|
1065
|
+
};
|
|
1066
|
+
const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1067
|
+
subject,
|
|
1068
|
+
install: (s) => createSelector(options, family, s),
|
|
1069
|
+
get: getSelf,
|
|
1070
|
+
type: `readonly_selector`
|
|
1071
|
+
}), family && { family });
|
|
1072
|
+
target.readonlySelectors.set(options.key, readonlySelector);
|
|
1073
|
+
const initialValue = getSelf();
|
|
1074
|
+
store.logger.info(
|
|
1075
|
+
`\u2728`,
|
|
1076
|
+
readonlySelector.type,
|
|
1077
|
+
readonlySelector.key,
|
|
1078
|
+
`=`,
|
|
1079
|
+
initialValue
|
|
1080
|
+
);
|
|
1081
|
+
const token = {
|
|
1082
|
+
key: options.key,
|
|
1083
|
+
type: `readonly_selector`
|
|
1084
|
+
};
|
|
1085
|
+
if (family) {
|
|
1086
|
+
token.family = family;
|
|
1087
|
+
}
|
|
1088
|
+
store.subject.selectorCreation.next(token);
|
|
1089
|
+
return token;
|
|
1090
|
+
};
|
|
1091
|
+
|
|
1092
|
+
// internal/src/selector/create-selector.ts
|
|
1093
|
+
function createSelector(options, family, store) {
|
|
1094
|
+
const target = newest(store);
|
|
1095
|
+
const existingWritable = target.selectors.get(options.key);
|
|
1096
|
+
const existingReadonly = target.readonlySelectors.get(options.key);
|
|
1097
|
+
if (existingWritable || existingReadonly) {
|
|
1098
|
+
store.logger.error(
|
|
1099
|
+
`\u274C`,
|
|
1100
|
+
existingReadonly ? `readonly_selector` : `selector`,
|
|
1101
|
+
options.key,
|
|
1102
|
+
`Tried to create selector, but it already exists in the store. (Ignore if you are in development using hot module replacement.)`
|
|
1103
|
+
);
|
|
1104
|
+
}
|
|
1105
|
+
if (`set` in options) {
|
|
1106
|
+
return createReadWriteSelector(options, family, store);
|
|
1107
|
+
}
|
|
1108
|
+
return createReadonlySelector(options, family, store);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
// internal/src/selector/delete-selector.ts
|
|
1112
|
+
function deleteSelector(selectorToken, store) {
|
|
1113
|
+
const target = newest(store);
|
|
1114
|
+
const { key } = selectorToken;
|
|
1115
|
+
switch (selectorToken.type) {
|
|
1116
|
+
case `selector`:
|
|
1117
|
+
target.selectors.delete(key);
|
|
1118
|
+
break;
|
|
1119
|
+
case `readonly_selector`:
|
|
1120
|
+
target.readonlySelectors.delete(key);
|
|
1121
|
+
break;
|
|
1122
|
+
}
|
|
1123
|
+
target.valueMap.delete(key);
|
|
1124
|
+
target.selectorAtoms.delete(key);
|
|
1125
|
+
const downstreamTokens = target.selectorGraph.getRelationEntries({ upstreamSelectorKey: key }).filter(([_, { source }]) => source === key).map(
|
|
1126
|
+
([downstreamSelectorKey]) => {
|
|
1127
|
+
var _a;
|
|
1128
|
+
return (_a = target.selectors.get(downstreamSelectorKey)) != null ? _a : target.readonlySelectors.get(downstreamSelectorKey);
|
|
1129
|
+
}
|
|
1130
|
+
);
|
|
1131
|
+
for (const downstreamToken of downstreamTokens) {
|
|
1132
|
+
if (downstreamToken) {
|
|
1133
|
+
deleteSelector(downstreamToken, store);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
target.selectorGraph.delete(key);
|
|
1137
|
+
store.logger.info(`\u{1F525}`, selectorToken.type, `${key}`, `deleted`);
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// internal/src/subscribe/recall-state.ts
|
|
1141
|
+
var recallState = (state, store) => {
|
|
1142
|
+
const target = newest(store);
|
|
1143
|
+
if (!target.operation.open) {
|
|
1144
|
+
return target.valueMap.get(state.key);
|
|
1145
|
+
}
|
|
1146
|
+
return target.operation.prev.get(state.key);
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
// internal/src/subscribe/subscribe-to-root-atoms.ts
|
|
1150
|
+
var subscribeToRootAtoms = (state, store) => {
|
|
1151
|
+
const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
|
|
1152
|
+
const atom = store.atoms.get(atomKey);
|
|
1153
|
+
if (atom === void 0) {
|
|
1154
|
+
throw new Error(
|
|
1155
|
+
`Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
1158
|
+
return atom.subject.subscribe(
|
|
1159
|
+
`${state.type}:${state.key}`,
|
|
1160
|
+
(atomChange) => {
|
|
1161
|
+
store.logger.info(
|
|
1162
|
+
`\u{1F4E2}`,
|
|
1163
|
+
state.type,
|
|
1164
|
+
state.key,
|
|
1165
|
+
`root`,
|
|
1166
|
+
atomKey,
|
|
1167
|
+
`went`,
|
|
1168
|
+
atomChange.oldValue,
|
|
1169
|
+
`->`,
|
|
1170
|
+
atomChange.newValue
|
|
1171
|
+
);
|
|
1172
|
+
const oldValue = recallState(state, store);
|
|
1173
|
+
const newValue = readOrComputeValue(state, store);
|
|
1174
|
+
store.logger.info(
|
|
1175
|
+
`\u2728`,
|
|
1176
|
+
state.type,
|
|
1177
|
+
state.key,
|
|
1178
|
+
`went`,
|
|
1179
|
+
oldValue,
|
|
1180
|
+
`->`,
|
|
1181
|
+
newValue
|
|
1182
|
+
);
|
|
1183
|
+
state.subject.next({ newValue, oldValue });
|
|
1184
|
+
}
|
|
1185
|
+
);
|
|
1186
|
+
});
|
|
1187
|
+
return dependencySubscriptions;
|
|
1188
|
+
};
|
|
1189
|
+
|
|
1190
|
+
// internal/src/subscribe/subscribe-to-state.ts
|
|
1191
|
+
function subscribeToState(token, handleUpdate, key, store) {
|
|
1192
|
+
const state = withdraw(token, store);
|
|
1193
|
+
if (state === void 0) {
|
|
1194
|
+
throw new Error(
|
|
1195
|
+
`State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
1196
|
+
);
|
|
1197
|
+
}
|
|
1198
|
+
const unsubFunction = state.subject.subscribe(key, handleUpdate);
|
|
1199
|
+
store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
|
|
1200
|
+
const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
|
|
1201
|
+
const unsubscribe = dependencyUnsubFunctions === null ? () => {
|
|
1202
|
+
store.logger.info(
|
|
1203
|
+
`\u{1F648}`,
|
|
1204
|
+
state.type,
|
|
1205
|
+
state.key,
|
|
1206
|
+
`Removing subscription "${key}"`
|
|
1207
|
+
);
|
|
1208
|
+
unsubFunction();
|
|
1209
|
+
} : () => {
|
|
1210
|
+
store.logger.info(
|
|
1211
|
+
`\u{1F648}`,
|
|
1212
|
+
state.type,
|
|
1213
|
+
state.key,
|
|
1214
|
+
`Removing subscription "${key}"`
|
|
1215
|
+
);
|
|
1216
|
+
unsubFunction();
|
|
1217
|
+
for (const unsubFromDependency of dependencyUnsubFunctions) {
|
|
1218
|
+
unsubFromDependency();
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1221
|
+
return unsubscribe;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// internal/src/subscribe/subscribe-to-timeline.ts
|
|
1225
|
+
var subscribeToTimeline = (token, handleUpdate, key, store) => {
|
|
1226
|
+
const tl = withdraw(token, store);
|
|
1227
|
+
if (tl === void 0) {
|
|
1228
|
+
throw new Error(
|
|
1229
|
+
`Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
|
|
1230
|
+
);
|
|
1231
|
+
}
|
|
1232
|
+
store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
|
|
1233
|
+
const unsubscribe = tl.subject.subscribe(key, handleUpdate);
|
|
1234
|
+
return () => {
|
|
1235
|
+
store.logger.info(
|
|
1236
|
+
`\u{1F648}`,
|
|
1237
|
+
`timeline`,
|
|
1238
|
+
token.key,
|
|
1239
|
+
`Removing subscription "${key}" from timeline`
|
|
1240
|
+
);
|
|
1241
|
+
unsubscribe();
|
|
1242
|
+
};
|
|
1243
|
+
};
|
|
1244
|
+
|
|
1245
|
+
// internal/src/mutable/tracker.ts
|
|
1246
|
+
var Tracker = class {
|
|
1247
|
+
constructor(mutableState, store) {
|
|
1248
|
+
this.unsubscribeFromInnerValue = null;
|
|
1249
|
+
this.mutableState = mutableState;
|
|
1250
|
+
const target = newest(store);
|
|
1251
|
+
this.latestUpdateState = this.initializeState(mutableState, target);
|
|
1252
|
+
this.observeCore(mutableState, this.latestUpdateState, target);
|
|
1253
|
+
this.updateCore(mutableState, this.latestUpdateState, target);
|
|
1254
|
+
target.trackers.set(mutableState.key, this);
|
|
1255
|
+
}
|
|
1256
|
+
initializeState(mutableState, store) {
|
|
1257
|
+
const latestUpdateStateKey = `*${mutableState.key}`;
|
|
1258
|
+
deleteAtom({ type: `atom`, key: latestUpdateStateKey }, store);
|
|
1259
|
+
const familyMetaData = mutableState.family ? {
|
|
1260
|
+
key: `*${mutableState.family.key}`,
|
|
1261
|
+
subKey: mutableState.family.subKey
|
|
1262
|
+
} : void 0;
|
|
1263
|
+
const latestUpdateState = createAtom(
|
|
1264
|
+
{
|
|
1265
|
+
key: latestUpdateStateKey,
|
|
1266
|
+
default: null
|
|
1267
|
+
},
|
|
1268
|
+
familyMetaData,
|
|
1269
|
+
store
|
|
1270
|
+
);
|
|
1271
|
+
return latestUpdateState;
|
|
1272
|
+
}
|
|
1273
|
+
observeCore(mutableState, latestUpdateState, store) {
|
|
1274
|
+
const originalInnerValue = getState(mutableState, store);
|
|
1275
|
+
const target = newest(store);
|
|
1276
|
+
this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
|
|
1277
|
+
`tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
|
|
1278
|
+
(update) => {
|
|
1279
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1280
|
+
mutableState.key,
|
|
1281
|
+
() => {
|
|
1282
|
+
unsubscribe();
|
|
1283
|
+
setState(latestUpdateState, update, store);
|
|
1284
|
+
}
|
|
1285
|
+
);
|
|
1286
|
+
}
|
|
1287
|
+
);
|
|
1288
|
+
subscribeToState(
|
|
1289
|
+
mutableState,
|
|
1290
|
+
(update) => {
|
|
1291
|
+
var _a;
|
|
1292
|
+
if (update.newValue !== update.oldValue) {
|
|
1293
|
+
(_a = this.unsubscribeFromInnerValue) == null ? void 0 : _a.call(this);
|
|
1294
|
+
const target2 = newest(store);
|
|
1295
|
+
this.unsubscribeFromInnerValue = update.newValue.subscribe(
|
|
1296
|
+
`tracker:${store.config.name}:${target2.transactionMeta === null ? `main` : target2.transactionMeta.update.key}`,
|
|
1297
|
+
(update2) => {
|
|
1298
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1299
|
+
mutableState.key,
|
|
1300
|
+
() => {
|
|
1301
|
+
unsubscribe();
|
|
1302
|
+
setState(latestUpdateState, update2, store);
|
|
1303
|
+
}
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
},
|
|
1309
|
+
`${store.config.name}: tracker observing inner value`,
|
|
1310
|
+
store
|
|
1311
|
+
);
|
|
1312
|
+
}
|
|
1313
|
+
updateCore(mutableState, latestUpdateState, store) {
|
|
1314
|
+
subscribeToState(
|
|
1315
|
+
latestUpdateState,
|
|
1316
|
+
({ newValue, oldValue }) => {
|
|
1317
|
+
const timelineId = store.timelineAtoms.getRelatedKey(
|
|
1318
|
+
latestUpdateState.key
|
|
1319
|
+
);
|
|
1320
|
+
if (timelineId) {
|
|
1321
|
+
const timelineData = store.timelines.get(timelineId);
|
|
1322
|
+
if (timelineData == null ? void 0 : timelineData.timeTraveling) {
|
|
1323
|
+
const unsubscribe2 = subscribeToTimeline(
|
|
1324
|
+
{ key: timelineId, type: `timeline` },
|
|
1325
|
+
(update) => {
|
|
1326
|
+
unsubscribe2();
|
|
1327
|
+
setState(
|
|
1328
|
+
mutableState,
|
|
1329
|
+
(transceiver) => {
|
|
1330
|
+
if (update === `redo` && newValue) {
|
|
1331
|
+
transceiver.do(newValue);
|
|
1332
|
+
} else if (update === `undo` && oldValue) {
|
|
1333
|
+
transceiver.undo(oldValue);
|
|
1334
|
+
}
|
|
1335
|
+
return transceiver;
|
|
1336
|
+
},
|
|
1337
|
+
store
|
|
1338
|
+
);
|
|
1339
|
+
},
|
|
1340
|
+
`${mutableState.key}: tracker observing timeline`,
|
|
1341
|
+
store
|
|
1342
|
+
);
|
|
1343
|
+
return;
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1347
|
+
latestUpdateState.key,
|
|
1348
|
+
() => {
|
|
1349
|
+
unsubscribe();
|
|
1350
|
+
const mutable = getState(mutableState, store);
|
|
1351
|
+
const updateNumber = mutable.getUpdateNumber(newValue);
|
|
1352
|
+
const eventOffset = updateNumber - mutable.cacheUpdateNumber;
|
|
1353
|
+
if (newValue && eventOffset === 1) {
|
|
1354
|
+
setState(
|
|
1355
|
+
mutableState,
|
|
1356
|
+
(transceiver) => (transceiver.do(newValue), transceiver),
|
|
1357
|
+
store
|
|
1358
|
+
);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
);
|
|
1362
|
+
},
|
|
1363
|
+
`${store.config.name}: tracker observing latest update`,
|
|
1364
|
+
store
|
|
1365
|
+
);
|
|
1366
|
+
}
|
|
1367
|
+
};
|
|
1368
|
+
|
|
1369
|
+
// internal/src/mutable/create-mutable-atom.ts
|
|
1370
|
+
function createMutableAtom(options, store) {
|
|
1371
|
+
store.logger.info(
|
|
1372
|
+
`\u{1F527}`,
|
|
1373
|
+
`atom`,
|
|
1374
|
+
options.key,
|
|
1375
|
+
`creating in store "${store.config.name}"`
|
|
1376
|
+
);
|
|
1377
|
+
const coreState = createAtom(options, void 0, store);
|
|
1378
|
+
new Tracker(coreState, store);
|
|
1379
|
+
const jsonState = selectJson(coreState, options, store);
|
|
1380
|
+
const target = newest(store);
|
|
1381
|
+
subscribeToState(
|
|
1382
|
+
jsonState,
|
|
1383
|
+
() => {
|
|
1384
|
+
const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
|
|
1385
|
+
if (!trackerHasBeenInitialized) {
|
|
1386
|
+
new Tracker(coreState, store);
|
|
1387
|
+
}
|
|
1388
|
+
},
|
|
1389
|
+
`tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
|
|
1390
|
+
store
|
|
1391
|
+
);
|
|
1392
|
+
return coreState;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
// internal/src/families/create-readonly-selector-family.ts
|
|
1396
|
+
function createReadonlySelectorFamily(options, store) {
|
|
1397
|
+
const subject = new Subject();
|
|
1398
|
+
return Object.assign(
|
|
1399
|
+
(key) => {
|
|
1400
|
+
const target = newest(store);
|
|
1401
|
+
const subKey = stringifyJson(key);
|
|
1402
|
+
const family = { key: options.key, subKey };
|
|
1403
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1404
|
+
const existing = target.readonlySelectors.get(fullKey);
|
|
1405
|
+
if (existing) {
|
|
1406
|
+
return deposit(existing);
|
|
1407
|
+
}
|
|
1408
|
+
return createSelector(
|
|
1409
|
+
{
|
|
1410
|
+
key: fullKey,
|
|
1411
|
+
get: options.get(key)
|
|
1412
|
+
},
|
|
1413
|
+
family,
|
|
1414
|
+
store
|
|
1415
|
+
);
|
|
1416
|
+
},
|
|
1417
|
+
{
|
|
1418
|
+
key: options.key,
|
|
1419
|
+
type: `readonly_selector_family`,
|
|
1420
|
+
subject
|
|
1421
|
+
}
|
|
1422
|
+
);
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
// internal/src/families/create-selector-family.ts
|
|
1426
|
+
function createSelectorFamily(options, store) {
|
|
1427
|
+
const isReadonly = !(`set` in options);
|
|
1428
|
+
if (isReadonly) {
|
|
1429
|
+
return createReadonlySelectorFamily(options, store);
|
|
1430
|
+
}
|
|
1431
|
+
const target = newest(store);
|
|
1432
|
+
const subject = new Subject();
|
|
1433
|
+
const selectorFamily = Object.assign(
|
|
1434
|
+
(key) => {
|
|
1435
|
+
const subKey = stringifyJson(key);
|
|
1436
|
+
const family = { key: options.key, subKey };
|
|
1437
|
+
const fullKey = `${options.key}(${subKey})`;
|
|
1438
|
+
const existing = target.selectors.get(fullKey);
|
|
1439
|
+
if (existing) {
|
|
1440
|
+
return deposit(existing);
|
|
1441
|
+
}
|
|
1442
|
+
const token = createSelector(
|
|
1443
|
+
{
|
|
1444
|
+
key: fullKey,
|
|
1445
|
+
get: options.get(key),
|
|
1446
|
+
set: options.set(key)
|
|
1447
|
+
},
|
|
1448
|
+
family,
|
|
1449
|
+
store
|
|
1450
|
+
);
|
|
1451
|
+
subject.next(token);
|
|
1452
|
+
return token;
|
|
1453
|
+
},
|
|
1454
|
+
{
|
|
1455
|
+
key: options.key,
|
|
1456
|
+
type: `selector_family`
|
|
1457
|
+
}
|
|
1458
|
+
);
|
|
1459
|
+
target.families.set(options.key, selectorFamily);
|
|
1460
|
+
return selectorFamily;
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
// internal/src/mutable/transceiver.ts
|
|
1464
|
+
function isTransceiver(value) {
|
|
1465
|
+
return typeof value === `object` && value !== null && `do` in value && `undo` in value && `subscribe` in value;
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
// internal/src/atom/is-default.ts
|
|
1469
|
+
var isAtomDefault = (key, store) => {
|
|
1470
|
+
const core = newest(store);
|
|
1471
|
+
return core.atomsThatAreDefault.has(key);
|
|
1472
|
+
};
|
|
1473
|
+
var markAtomAsDefault = (key, store) => {
|
|
1474
|
+
const core = newest(store);
|
|
1475
|
+
core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key);
|
|
1476
|
+
};
|
|
1477
|
+
var markAtomAsNotDefault = (key, store) => {
|
|
1478
|
+
const core = newest(store);
|
|
1479
|
+
core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault);
|
|
1480
|
+
core.atomsThatAreDefault.delete(key);
|
|
1481
|
+
};
|
|
1482
|
+
|
|
1483
|
+
// internal/src/atom/create-atom.ts
|
|
1484
|
+
function createAtom(options, family, store) {
|
|
1485
|
+
store.logger.info(
|
|
1486
|
+
`\u{1F528}`,
|
|
1487
|
+
`atom`,
|
|
1488
|
+
options.key,
|
|
1489
|
+
`creating in store "${store.config.name}"`
|
|
1490
|
+
);
|
|
1491
|
+
const target = newest(store);
|
|
1492
|
+
const existing = target.atoms.get(options.key);
|
|
1493
|
+
if (existing) {
|
|
1494
|
+
store.logger.error(
|
|
1495
|
+
`\u274C`,
|
|
1496
|
+
`atom`,
|
|
1497
|
+
options.key,
|
|
1498
|
+
`Tried to create atom, but it already exists in the store.`,
|
|
1499
|
+
`(Ignore if you are in development using hot module replacement.)`
|
|
1500
|
+
);
|
|
1501
|
+
return deposit(existing);
|
|
1502
|
+
}
|
|
1503
|
+
const subject = new Subject();
|
|
1504
|
+
const newAtom = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1505
|
+
type: `atom`,
|
|
1506
|
+
install: (store2) => {
|
|
1507
|
+
store2.logger.info(
|
|
1508
|
+
`\u{1F6E0}\uFE0F`,
|
|
1509
|
+
`atom`,
|
|
1510
|
+
options.key,
|
|
1511
|
+
`installing in store "${store2.config.name}"`
|
|
1512
|
+
);
|
|
1513
|
+
return `mutable` in options ? createMutableAtom(options, store2) : createAtom(options, void 0, store2);
|
|
1514
|
+
},
|
|
1515
|
+
subject
|
|
1516
|
+
}), family && { family });
|
|
1517
|
+
let initialValue = options.default;
|
|
1518
|
+
if (options.default instanceof Function) {
|
|
1519
|
+
initialValue = options.default();
|
|
1520
|
+
}
|
|
1521
|
+
target.atoms.set(newAtom.key, newAtom);
|
|
1522
|
+
markAtomAsDefault(options.key, store);
|
|
1523
|
+
cacheValue(options.key, initialValue, subject, store);
|
|
1524
|
+
const token = deposit(newAtom);
|
|
1525
|
+
if (options.effects) {
|
|
1526
|
+
let effectIndex = 0;
|
|
1527
|
+
const cleanupFunctions = [];
|
|
1528
|
+
for (const effect of options.effects) {
|
|
1529
|
+
const cleanup = effect({
|
|
1530
|
+
setSelf: (next) => setState(token, next, store),
|
|
1531
|
+
onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
|
|
1532
|
+
});
|
|
1533
|
+
if (cleanup) {
|
|
1534
|
+
cleanupFunctions.push(cleanup);
|
|
1535
|
+
}
|
|
1536
|
+
++effectIndex;
|
|
1537
|
+
}
|
|
1538
|
+
newAtom.cleanup = () => {
|
|
1539
|
+
for (const cleanup of cleanupFunctions) {
|
|
1540
|
+
cleanup();
|
|
1541
|
+
}
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
store.subject.atomCreation.next(token);
|
|
1545
|
+
return token;
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
// internal/src/atom/delete-atom.ts
|
|
1549
|
+
function deleteAtom(atomToken, store) {
|
|
1550
|
+
var _a, _b;
|
|
1551
|
+
const target = newest(store);
|
|
1552
|
+
const { key } = atomToken;
|
|
1553
|
+
const atom = target.atoms.get(key);
|
|
1554
|
+
if (!atom) {
|
|
1555
|
+
store.logger.error(
|
|
1556
|
+
`\u274C`,
|
|
1557
|
+
`atom`,
|
|
1558
|
+
`${key}`,
|
|
1559
|
+
`Tried to delete atom, but it does not exist in the store.`
|
|
1560
|
+
);
|
|
1561
|
+
}
|
|
1562
|
+
(_a = atom == null ? void 0 : atom.cleanup) == null ? void 0 : _a.call(atom);
|
|
1563
|
+
target.atoms.delete(key);
|
|
1564
|
+
target.valueMap.delete(key);
|
|
1565
|
+
const selectorKeys = target.selectorAtoms.getRelatedKeys(key);
|
|
1566
|
+
if (selectorKeys) {
|
|
1567
|
+
for (const selectorKey of selectorKeys) {
|
|
1568
|
+
const token = (_b = target.selectors.get(selectorKey)) != null ? _b : target.readonlySelectors.get(selectorKey);
|
|
1569
|
+
if (token) {
|
|
1570
|
+
deleteSelector(token, store);
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
target.selectorAtoms.delete(key);
|
|
1575
|
+
target.atomsThatAreDefault.delete(key);
|
|
1576
|
+
target.timelineAtoms.delete(key);
|
|
1577
|
+
store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
// internal/src/not-found-error.ts
|
|
1581
|
+
var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
1582
|
+
function prettyPrintTokenType(token) {
|
|
1583
|
+
if (token.type === `readonly_selector`) {
|
|
1584
|
+
return `Readonly Selector`;
|
|
1585
|
+
}
|
|
1586
|
+
return capitalize(token.type);
|
|
1587
|
+
}
|
|
1588
|
+
var NotFoundError = class extends Error {
|
|
1589
|
+
constructor(token, store) {
|
|
1590
|
+
super(
|
|
1591
|
+
`${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
|
|
1592
|
+
);
|
|
1593
|
+
}
|
|
1594
|
+
};
|
|
1595
|
+
|
|
1596
|
+
// json/src/select-json.ts
|
|
1597
|
+
var selectJson = (atom, transform, store = IMPLICIT.STORE) => {
|
|
1598
|
+
return createSelector(
|
|
49
1599
|
{
|
|
50
1600
|
key: `${atom.key}:JSON`,
|
|
51
1601
|
get: ({ get }) => transform.toJson(get(atom)),
|
|
@@ -55,8 +1605,10 @@ var selectJson = (atom, transform, store = internal.IMPLICIT.STORE) => {
|
|
|
55
1605
|
store
|
|
56
1606
|
);
|
|
57
1607
|
};
|
|
58
|
-
|
|
59
|
-
|
|
1608
|
+
|
|
1609
|
+
// json/src/select-json-family.ts
|
|
1610
|
+
var selectJsonFamily = (atomFamily, transform, store = IMPLICIT.STORE) => {
|
|
1611
|
+
const jsonFamily = createSelectorFamily(
|
|
60
1612
|
{
|
|
61
1613
|
key: `${atomFamily.key}:JSON`,
|
|
62
1614
|
get: (key) => ({ get }) => transform.toJson(get(atomFamily(key))),
|