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