@capybara-devtools/core 0.2.1 → 0.3.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/README.md +2 -2
- package/dist/client.d.ts +3 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +9 -4
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/state/context.d.ts +102 -0
- package/dist/plugins/state/context.d.ts.map +1 -0
- package/dist/plugins/state/context.js +765 -0
- package/dist/plugins/state/context.js.map +1 -0
- package/dist/plugins/state/engine.d.ts +71 -0
- package/dist/plugins/state/engine.d.ts.map +1 -0
- package/dist/plugins/state/engine.js +710 -0
- package/dist/plugins/state/engine.js.map +1 -0
- package/dist/plugins/state/index.d.ts +59 -0
- package/dist/plugins/state/index.d.ts.map +1 -0
- package/dist/plugins/state/index.js +52 -0
- package/dist/plugins/state/index.js.map +1 -0
- package/dist/plugins/state/redux.d.ts +43 -0
- package/dist/plugins/state/redux.d.ts.map +1 -0
- package/dist/plugins/state/redux.js +580 -0
- package/dist/plugins/state/redux.js.map +1 -0
- package/dist/plugins/state/registry.d.ts +22 -0
- package/dist/plugins/state/registry.d.ts.map +1 -0
- package/dist/plugins/state/registry.js +109 -0
- package/dist/plugins/state/registry.js.map +1 -0
- package/dist/plugins/state/swr.d.ts +49 -0
- package/dist/plugins/state/swr.d.ts.map +1 -0
- package/dist/plugins/state/swr.js +359 -0
- package/dist/plugins/state/swr.js.map +1 -0
- package/dist/plugins/state/tanstack.d.ts +31 -0
- package/dist/plugins/state/tanstack.d.ts.map +1 -0
- package/dist/plugins/state/tanstack.js +461 -0
- package/dist/plugins/state/tanstack.js.map +1 -0
- package/dist/plugins/state/zustand.d.ts +21 -0
- package/dist/plugins/state/zustand.d.ts.map +1 -0
- package/dist/plugins/state/zustand.js +70 -0
- package/dist/plugins/state/zustand.js.map +1 -0
- package/package.json +11 -2
- package/src/client.ts +12 -6
- package/src/index.ts +19 -0
- package/src/plugins/state/context.ts +831 -0
- package/src/plugins/state/engine.ts +739 -0
- package/src/plugins/state/index.ts +112 -0
- package/src/plugins/state/redux.ts +609 -0
- package/src/plugins/state/registry.ts +126 -0
- package/src/plugins/state/swr.ts +375 -0
- package/src/plugins/state/tanstack.ts +488 -0
- package/src/plugins/state/zustand.ts +82 -0
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diffs two states by reference: an unchanged reference skips its whole branch, so the cost follows what
|
|
3
|
+
* changed. Only plain objects and arrays are walked, arrays by index; anything else is a leaf.
|
|
4
|
+
*/
|
|
5
|
+
export function diffState(prev, next, limits, serialize) {
|
|
6
|
+
const ABSENT = {};
|
|
7
|
+
let ops = [];
|
|
8
|
+
let opCount = 0;
|
|
9
|
+
let nodes = 0;
|
|
10
|
+
const same = (a, b) => a === b || (a !== a && b !== b);
|
|
11
|
+
// Prototype checks rather than instanceof, so objects from another realm still count as plain.
|
|
12
|
+
const plain = (v) => {
|
|
13
|
+
if (v === null || typeof v !== 'object' || Array.isArray(v))
|
|
14
|
+
return false;
|
|
15
|
+
const proto = Object.getPrototypeOf(v);
|
|
16
|
+
return proto === null || Object.getPrototypeOf(proto) === null;
|
|
17
|
+
};
|
|
18
|
+
const has = (obj, key) => Object.prototype.propertyIsEnumerable.call(obj, key);
|
|
19
|
+
const read = (obj, key) => {
|
|
20
|
+
try {
|
|
21
|
+
const value = obj[key];
|
|
22
|
+
return limits.hideFunctions && typeof value === 'function' ? ABSENT : value;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return ABSENT;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const push = (op, path, key, before, after) => {
|
|
29
|
+
opCount++;
|
|
30
|
+
if (ops.length >= limits.maxOps)
|
|
31
|
+
return;
|
|
32
|
+
const out = { op, path: key === null ? path : [...path, key] };
|
|
33
|
+
if (op !== 'del')
|
|
34
|
+
out.value = serialize(after, limits.preview);
|
|
35
|
+
if (op !== 'add')
|
|
36
|
+
out.prev = serialize(before, limits.preview);
|
|
37
|
+
ops.push(out);
|
|
38
|
+
};
|
|
39
|
+
const walk = (a, b, path, depth) => {
|
|
40
|
+
const arrays = Array.isArray(a) && Array.isArray(b);
|
|
41
|
+
if ((!arrays && !(plain(a) && plain(b))) || depth >= limits.maxDepth || nodes >= limits.maxNodes) {
|
|
42
|
+
push('set', path, null, a, b);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
nodes++;
|
|
46
|
+
if (arrays) {
|
|
47
|
+
const before = a;
|
|
48
|
+
const after = b;
|
|
49
|
+
const shared = Math.min(before.length, after.length);
|
|
50
|
+
for (let i = 0; i < shared; i++) {
|
|
51
|
+
if (!same(before[i], after[i]))
|
|
52
|
+
walk(before[i], after[i], [...path, i], depth + 1);
|
|
53
|
+
}
|
|
54
|
+
for (let i = shared; i < after.length; i++)
|
|
55
|
+
push('add', path, i, undefined, after[i]);
|
|
56
|
+
for (let i = shared; i < before.length; i++)
|
|
57
|
+
push('del', path, i, before[i], undefined);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const before = a;
|
|
61
|
+
const after = b;
|
|
62
|
+
for (const key of Object.keys(after)) {
|
|
63
|
+
const y = read(after, key);
|
|
64
|
+
if (y === ABSENT)
|
|
65
|
+
continue;
|
|
66
|
+
const x = has(before, key) ? read(before, key) : ABSENT;
|
|
67
|
+
if (x === ABSENT)
|
|
68
|
+
push('add', path, key, undefined, y);
|
|
69
|
+
else if (!same(x, y))
|
|
70
|
+
walk(x, y, [...path, key], depth + 1);
|
|
71
|
+
}
|
|
72
|
+
for (const key of Object.keys(before)) {
|
|
73
|
+
if (has(after, key) && read(after, key) !== ABSENT)
|
|
74
|
+
continue;
|
|
75
|
+
const x = read(before, key);
|
|
76
|
+
if (x !== ABSENT)
|
|
77
|
+
push('del', path, key, x, undefined);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
if (!same(prev, next)) {
|
|
81
|
+
try {
|
|
82
|
+
walk(prev, next, [], 0);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// A proxy or exotic object threw midway; report the whole state as replaced instead.
|
|
86
|
+
ops = [];
|
|
87
|
+
opCount = 0;
|
|
88
|
+
push('set', [], null, prev, next);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return { ops, opCount };
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Follows `path` from `root`: keys into objects and arrays, keys into Maps, positions into Sets.
|
|
95
|
+
* `found` is false when some step does not exist, as opposed to a value that is `undefined`.
|
|
96
|
+
*/
|
|
97
|
+
export function readPath(root, path) {
|
|
98
|
+
let current = root;
|
|
99
|
+
for (const key of path) {
|
|
100
|
+
if (current === null || (typeof current !== 'object' && typeof current !== 'function')) {
|
|
101
|
+
return { found: false, value: undefined };
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
if (current instanceof Map) {
|
|
105
|
+
let mapKey = key;
|
|
106
|
+
let hit = current.has(key);
|
|
107
|
+
// Paths come through JSON, so a numeric Map key may arrive as a string and the other way round.
|
|
108
|
+
for (const k of hit ? [] : current.keys()) {
|
|
109
|
+
if (String(k) === String(key)) {
|
|
110
|
+
mapKey = k;
|
|
111
|
+
hit = true;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!hit)
|
|
116
|
+
return { found: false, value: undefined };
|
|
117
|
+
current = current.get(mapKey);
|
|
118
|
+
}
|
|
119
|
+
else if (current instanceof Set || Array.isArray(current)) {
|
|
120
|
+
const index = Number(key);
|
|
121
|
+
const size = Array.isArray(current) ? current.length : current.size;
|
|
122
|
+
if (!Number.isInteger(index) || index < 0 || index >= size)
|
|
123
|
+
return { found: false, value: undefined };
|
|
124
|
+
current = Array.isArray(current) ? current[index] : [...current][index];
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
if (!Object.prototype.hasOwnProperty.call(current, key))
|
|
128
|
+
return { found: false, value: undefined };
|
|
129
|
+
current = current[key];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return { found: false, value: undefined };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { found: true, value: current };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Turns what drivers report into `state.*` events and answers reads. Listeners only queue a reference or
|
|
140
|
+
* mark a key; diffing, serializing and emitting wait for a timer, and happen only while connected.
|
|
141
|
+
*/
|
|
142
|
+
export function createStateEngine(emit, options, serialize, diffState, readPath) {
|
|
143
|
+
const RECORD_DELAY = 100;
|
|
144
|
+
const ENTRY_DELAY = 200;
|
|
145
|
+
const LOW_ENTRY_DELAY = 1_000;
|
|
146
|
+
/** Plugins hear nothing when the connection comes back, so waiting changes look for it this often. */
|
|
147
|
+
const OFFLINE_POLL = 1_000;
|
|
148
|
+
/** References kept per store between flushes; past it the newest replaces the last one. */
|
|
149
|
+
const QUEUE_LIMIT = 200;
|
|
150
|
+
/** Summaries one store remembers or lists: room for TanStack Query's 1000 queries and 200 mutations. */
|
|
151
|
+
const ENTRY_LIMIT = 1_200;
|
|
152
|
+
/** Summaries per `state.entries` event; after a reset the `entries` command lists the rest. */
|
|
153
|
+
const ENTRIES_PER_EVENT = 200;
|
|
154
|
+
/** Same cap as network bodies. */
|
|
155
|
+
const MAX_READ = 128 * 1024;
|
|
156
|
+
const PREVIEW = { maxDepth: 3, maxKeys: 20, maxItems: 20, maxString: 200, maxNodes: 200 };
|
|
157
|
+
// Tried in order until a read fits in MAX_READ.
|
|
158
|
+
const READS = [
|
|
159
|
+
{ maxDepth: 8, maxKeys: 500, maxItems: 500, maxString: 10_000, maxNodes: 20_000 },
|
|
160
|
+
{ maxDepth: 5, maxKeys: 200, maxItems: 200, maxString: 2_000, maxNodes: 5_000 },
|
|
161
|
+
{ maxDepth: 3, maxKeys: 100, maxItems: 100, maxString: 500, maxNodes: 1_000 },
|
|
162
|
+
{ maxDepth: 1, maxKeys: 50, maxItems: 50, maxString: 200, maxNodes: 100 },
|
|
163
|
+
];
|
|
164
|
+
const hideFunctions = options.hideFunctions !== false;
|
|
165
|
+
const LIMITS = { maxOps: 100, maxDepth: 12, maxNodes: 5_000, preview: PREVIEW, hideFunctions };
|
|
166
|
+
const perSecond = Math.max(1, Math.floor(options.maxActionsPerSecond ?? 30));
|
|
167
|
+
const denylist = (options.actionsDenylist ?? []).map((pattern) => typeof pattern === 'string' ? pattern : new RegExp(pattern.source, pattern.flags));
|
|
168
|
+
const stores = new Map();
|
|
169
|
+
// Keyed by driver, so a stop function, which may hold on to the store, never outlives it.
|
|
170
|
+
const stops = new WeakMap();
|
|
171
|
+
let timer = null;
|
|
172
|
+
let timerAt = Infinity;
|
|
173
|
+
let offline = false;
|
|
174
|
+
let disposed = false;
|
|
175
|
+
const message = (error) => (error instanceof Error ? error.message : String(error));
|
|
176
|
+
// Weak, so the engine never keeps a store alive; whoever registered the driver decides that.
|
|
177
|
+
const hold = (driver) => typeof WeakRef === 'function' ? new WeakRef(driver) : { deref: () => driver };
|
|
178
|
+
const dropFunctions = (value) => {
|
|
179
|
+
const visit = (v) => {
|
|
180
|
+
if (v === null || typeof v !== 'object')
|
|
181
|
+
return;
|
|
182
|
+
if (Array.isArray(v)) {
|
|
183
|
+
for (const item of v)
|
|
184
|
+
visit(item);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const node = v;
|
|
188
|
+
switch (node.$t) {
|
|
189
|
+
case undefined:
|
|
190
|
+
strip(node);
|
|
191
|
+
break;
|
|
192
|
+
case 'object':
|
|
193
|
+
case 'error':
|
|
194
|
+
if (node.props)
|
|
195
|
+
strip(node.props);
|
|
196
|
+
break;
|
|
197
|
+
case 'array':
|
|
198
|
+
for (const item of node.items)
|
|
199
|
+
visit(item);
|
|
200
|
+
break;
|
|
201
|
+
case 'map':
|
|
202
|
+
for (const pair of node.entries)
|
|
203
|
+
visit(pair[1]);
|
|
204
|
+
break;
|
|
205
|
+
case 'set':
|
|
206
|
+
for (const item of node.values)
|
|
207
|
+
visit(item);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
const strip = (props) => {
|
|
212
|
+
for (const key of Object.keys(props)) {
|
|
213
|
+
const child = props[key];
|
|
214
|
+
if (child !== null && typeof child === 'object' && child.$t === 'function')
|
|
215
|
+
delete props[key];
|
|
216
|
+
else
|
|
217
|
+
visit(props[key]);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
visit(value);
|
|
221
|
+
return value;
|
|
222
|
+
};
|
|
223
|
+
const shown = (value, profile) => {
|
|
224
|
+
const out = serialize(value, profile);
|
|
225
|
+
return hideFunctions ? dropFunctions(out) : out;
|
|
226
|
+
};
|
|
227
|
+
const clean = (t, state) => {
|
|
228
|
+
if (!options.sanitizeState)
|
|
229
|
+
return state;
|
|
230
|
+
try {
|
|
231
|
+
return options.sanitizeState(state, t.info);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
// Never fall back to the state the sanitizer was meant to filter.
|
|
235
|
+
return `[sanitizeState threw: ${message(error)}]`;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const actionType = (action) => {
|
|
239
|
+
if (typeof action === 'string')
|
|
240
|
+
return action;
|
|
241
|
+
try {
|
|
242
|
+
const type = action?.type;
|
|
243
|
+
return typeof type === 'string' ? type : type === undefined ? 'anonymous' : String(type);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
return 'anonymous';
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
const hidden = (action) => {
|
|
250
|
+
if (denylist.length === 0 || action === undefined)
|
|
251
|
+
return false;
|
|
252
|
+
const type = actionType(action);
|
|
253
|
+
return denylist.some((pattern) => {
|
|
254
|
+
if (typeof pattern === 'string')
|
|
255
|
+
return pattern === type;
|
|
256
|
+
pattern.lastIndex = 0;
|
|
257
|
+
return pattern.test(type);
|
|
258
|
+
});
|
|
259
|
+
};
|
|
260
|
+
/** The type, and a preview of the rest of the action (payload, meta, error). */
|
|
261
|
+
const describeAction = (t, action) => {
|
|
262
|
+
let sanitized = action;
|
|
263
|
+
if (options.sanitizeAction) {
|
|
264
|
+
try {
|
|
265
|
+
sanitized = options.sanitizeAction(action, t.info);
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return { type: actionType(action) };
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const type = actionType(sanitized);
|
|
272
|
+
if (sanitized === null || typeof sanitized !== 'object')
|
|
273
|
+
return { type };
|
|
274
|
+
const rest = {};
|
|
275
|
+
let any = false;
|
|
276
|
+
try {
|
|
277
|
+
for (const key of Object.keys(sanitized)) {
|
|
278
|
+
if (key === 'type')
|
|
279
|
+
continue;
|
|
280
|
+
rest[key] = sanitized[key];
|
|
281
|
+
any = true;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
return { type };
|
|
286
|
+
}
|
|
287
|
+
return any ? { type, payload: shown(rest, PREVIEW) } : { type };
|
|
288
|
+
};
|
|
289
|
+
const wake = (delay) => {
|
|
290
|
+
if (disposed)
|
|
291
|
+
return;
|
|
292
|
+
const wait = Math.max(0, delay);
|
|
293
|
+
const at = Date.now() + wait;
|
|
294
|
+
if (timer !== null) {
|
|
295
|
+
if (timerAt <= at)
|
|
296
|
+
return;
|
|
297
|
+
clearTimeout(timer);
|
|
298
|
+
}
|
|
299
|
+
timerAt = at;
|
|
300
|
+
timer = setTimeout(flush, wait);
|
|
301
|
+
};
|
|
302
|
+
const record = (id, state, action, cause) => {
|
|
303
|
+
try {
|
|
304
|
+
const t = stores.get(id);
|
|
305
|
+
if (!t)
|
|
306
|
+
return;
|
|
307
|
+
const change = { state, action, cause: cause ?? (action === undefined ? 'set' : 'action'), skipped: 0, ts: Date.now() };
|
|
308
|
+
const queue = t.queue;
|
|
309
|
+
if (queue.length < QUEUE_LIMIT) {
|
|
310
|
+
queue.push(change);
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
change.skipped = queue[queue.length - 1].skipped + 1;
|
|
314
|
+
queue[queue.length - 1] = change;
|
|
315
|
+
}
|
|
316
|
+
wake(offline ? OFFLINE_POLL : RECORD_DELAY);
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
// never throw into the app's store
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
// Bounded: past the cap, start over with a reset rather than remember more keys.
|
|
323
|
+
const resync = (t) => {
|
|
324
|
+
t.dirty.clear();
|
|
325
|
+
t.removed.clear();
|
|
326
|
+
t.sent.clear();
|
|
327
|
+
t.reset = true;
|
|
328
|
+
};
|
|
329
|
+
const entriesSoon = (t, delay) => {
|
|
330
|
+
const at = Date.now() + delay;
|
|
331
|
+
if (at < t.entriesDue)
|
|
332
|
+
t.entriesDue = at;
|
|
333
|
+
wake(offline ? OFFLINE_POLL : t.entriesDue - Date.now());
|
|
334
|
+
};
|
|
335
|
+
const dirty = (id, key, low) => {
|
|
336
|
+
try {
|
|
337
|
+
const t = stores.get(id);
|
|
338
|
+
if (!t)
|
|
339
|
+
return;
|
|
340
|
+
t.removed.delete(key);
|
|
341
|
+
t.dirty.add(key);
|
|
342
|
+
if (t.dirty.size > ENTRY_LIMIT)
|
|
343
|
+
resync(t);
|
|
344
|
+
entriesSoon(t, low ? LOW_ENTRY_DELAY : ENTRY_DELAY);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
// never throw into the app's cache
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
const removed = (id, key) => {
|
|
351
|
+
try {
|
|
352
|
+
const t = stores.get(id);
|
|
353
|
+
if (!t)
|
|
354
|
+
return;
|
|
355
|
+
t.dirty.delete(key);
|
|
356
|
+
t.removed.add(key);
|
|
357
|
+
if (t.removed.size > ENTRY_LIMIT)
|
|
358
|
+
resync(t);
|
|
359
|
+
entriesSoon(t, ENTRY_DELAY);
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
// never throw into the app's cache
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
/** Drops denylisted changes; what they changed goes out with the next record, diffed from the last one sent. */
|
|
366
|
+
const visible = (t) => {
|
|
367
|
+
if (denylist.length === 0)
|
|
368
|
+
return t.queue;
|
|
369
|
+
const out = [];
|
|
370
|
+
for (const change of t.queue) {
|
|
371
|
+
if (hidden(change.action)) {
|
|
372
|
+
t.carry += change.skipped;
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
change.skipped += t.carry;
|
|
376
|
+
t.carry = 0;
|
|
377
|
+
out.push(change);
|
|
378
|
+
}
|
|
379
|
+
return out;
|
|
380
|
+
};
|
|
381
|
+
const fold = (changes) => {
|
|
382
|
+
if (changes.length <= 1)
|
|
383
|
+
return changes;
|
|
384
|
+
const last = changes[changes.length - 1];
|
|
385
|
+
for (let i = 0; i < changes.length - 1; i++)
|
|
386
|
+
last.skipped += changes[i].skipped + 1;
|
|
387
|
+
return [last];
|
|
388
|
+
};
|
|
389
|
+
const send = (t, change) => {
|
|
390
|
+
const next = clean(t, change.state);
|
|
391
|
+
const { ops, opCount } = t.based
|
|
392
|
+
? diffState(t.base, next, LIMITS, shown)
|
|
393
|
+
: { ops: [{ op: 'set', path: [], value: shown(next, PREVIEW) }], opCount: 1 };
|
|
394
|
+
t.raw = change.state;
|
|
395
|
+
t.base = next;
|
|
396
|
+
t.based = true;
|
|
397
|
+
if (ops.length === 0 && change.action === undefined && change.skipped === 0 && change.cause !== 'init')
|
|
398
|
+
return;
|
|
399
|
+
const payload = { store: t.info.id, rev: ++t.rev, cause: change.cause, ops };
|
|
400
|
+
if (change.action !== undefined)
|
|
401
|
+
payload.action = describeAction(t, change.action);
|
|
402
|
+
if (opCount > ops.length)
|
|
403
|
+
payload.opCount = opCount;
|
|
404
|
+
if (change.skipped > 0)
|
|
405
|
+
payload.skipped = change.skipped;
|
|
406
|
+
t.windowCount++;
|
|
407
|
+
emit('state.change', payload, change.ts);
|
|
408
|
+
};
|
|
409
|
+
const flushChanges = (t, live, now) => {
|
|
410
|
+
if (t.queue.length === 0)
|
|
411
|
+
return;
|
|
412
|
+
const queue = visible(t);
|
|
413
|
+
// Offline, nothing is diffed: changes only pile up as a count on the newest one.
|
|
414
|
+
if (!live) {
|
|
415
|
+
t.queue = fold(queue);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (now - t.windowStart >= 1_000) {
|
|
419
|
+
t.windowStart = now;
|
|
420
|
+
t.windowCount = 0;
|
|
421
|
+
}
|
|
422
|
+
const room = perSecond - t.windowCount;
|
|
423
|
+
if (room <= 0) {
|
|
424
|
+
t.queue = fold(queue);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
t.queue = [];
|
|
428
|
+
// The last record of the window carries everything past the limit, so the newest state still goes out.
|
|
429
|
+
const batch = queue.length > room ? [...queue.slice(0, room - 1), ...fold(queue.slice(room - 1))] : queue;
|
|
430
|
+
for (const change of batch)
|
|
431
|
+
send(t, change);
|
|
432
|
+
};
|
|
433
|
+
const summarize = (driver, key) => {
|
|
434
|
+
try {
|
|
435
|
+
const summary = driver.summarize?.(key);
|
|
436
|
+
return summary && typeof summary === 'object' ? summary : undefined;
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
const flushEntries = (t, live, now) => {
|
|
443
|
+
if (!live || now < t.entriesDue)
|
|
444
|
+
return;
|
|
445
|
+
t.entriesDue = Infinity;
|
|
446
|
+
const driver = t.driver.deref();
|
|
447
|
+
const store = t.info.id;
|
|
448
|
+
if (!driver || typeof driver.keys !== 'function') {
|
|
449
|
+
t.dirty.clear();
|
|
450
|
+
t.removed.clear();
|
|
451
|
+
t.reset = false;
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (t.reset) {
|
|
455
|
+
const upserts = [];
|
|
456
|
+
const sent = new Map();
|
|
457
|
+
for (const key of driver.keys()) {
|
|
458
|
+
if (upserts.length >= ENTRIES_PER_EVENT)
|
|
459
|
+
break;
|
|
460
|
+
const summary = summarize(driver, key);
|
|
461
|
+
if (!summary)
|
|
462
|
+
continue;
|
|
463
|
+
upserts.push(summary);
|
|
464
|
+
sent.set(key, JSON.stringify(summary));
|
|
465
|
+
}
|
|
466
|
+
t.reset = false;
|
|
467
|
+
t.dirty.clear();
|
|
468
|
+
t.removed.clear();
|
|
469
|
+
t.sent = sent;
|
|
470
|
+
emit('state.entries', { store, upserts, removed: [], reset: true });
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const upserts = [];
|
|
474
|
+
const gone = [...t.removed];
|
|
475
|
+
for (const key of gone)
|
|
476
|
+
t.sent.delete(key);
|
|
477
|
+
for (const key of t.dirty) {
|
|
478
|
+
const summary = summarize(driver, key);
|
|
479
|
+
if (!summary) {
|
|
480
|
+
if (t.sent.delete(key))
|
|
481
|
+
gone.push(key);
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
const json = JSON.stringify(summary);
|
|
485
|
+
if (t.sent.get(key) === json)
|
|
486
|
+
continue;
|
|
487
|
+
// Re-inserted, so the key sent longest ago is the one forgotten when the map is full.
|
|
488
|
+
t.sent.delete(key);
|
|
489
|
+
t.sent.set(key, json);
|
|
490
|
+
if (t.sent.size > ENTRY_LIMIT)
|
|
491
|
+
t.sent.delete(t.sent.keys().next().value);
|
|
492
|
+
upserts.push(summary);
|
|
493
|
+
}
|
|
494
|
+
t.dirty.clear();
|
|
495
|
+
t.removed.clear();
|
|
496
|
+
for (let i = 0; i < upserts.length || (i === 0 && gone.length > 0); i += ENTRIES_PER_EVENT) {
|
|
497
|
+
emit('state.entries', { store, upserts: upserts.slice(i, i + ENTRIES_PER_EVENT), removed: i === 0 ? gone : [] });
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
const flush = () => {
|
|
501
|
+
timer = null;
|
|
502
|
+
timerAt = Infinity;
|
|
503
|
+
if (disposed)
|
|
504
|
+
return;
|
|
505
|
+
let live = false;
|
|
506
|
+
try {
|
|
507
|
+
live = options.connected() === true;
|
|
508
|
+
}
|
|
509
|
+
catch {
|
|
510
|
+
// counts as offline
|
|
511
|
+
}
|
|
512
|
+
offline = !live;
|
|
513
|
+
const now = Date.now();
|
|
514
|
+
let next = Infinity;
|
|
515
|
+
for (const t of [...stores.values()]) {
|
|
516
|
+
try {
|
|
517
|
+
flushChanges(t, live, now);
|
|
518
|
+
}
|
|
519
|
+
catch {
|
|
520
|
+
t.queue = [];
|
|
521
|
+
}
|
|
522
|
+
try {
|
|
523
|
+
flushEntries(t, live, now);
|
|
524
|
+
}
|
|
525
|
+
catch {
|
|
526
|
+
t.dirty.clear();
|
|
527
|
+
t.removed.clear();
|
|
528
|
+
}
|
|
529
|
+
if (t.queue.length > 0)
|
|
530
|
+
next = Math.min(next, live ? t.windowStart + 1_000 : now + OFFLINE_POLL);
|
|
531
|
+
if (t.dirty.size > 0 || t.removed.size > 0 || t.reset)
|
|
532
|
+
next = Math.min(next, live ? t.entriesDue : now + OFFLINE_POLL);
|
|
533
|
+
}
|
|
534
|
+
if (next < Infinity)
|
|
535
|
+
wake(next - now);
|
|
536
|
+
};
|
|
537
|
+
const need = (id) => {
|
|
538
|
+
const t = typeof id === 'string' ? stores.get(id) : undefined;
|
|
539
|
+
if (!t)
|
|
540
|
+
throw new Error(`Unknown store "${String(id)}"`);
|
|
541
|
+
const driver = t.driver.deref();
|
|
542
|
+
if (!driver) {
|
|
543
|
+
untrack(t.info.id);
|
|
544
|
+
throw new Error(`${t.info.label} is no longer in the app`);
|
|
545
|
+
}
|
|
546
|
+
return { t, driver };
|
|
547
|
+
};
|
|
548
|
+
const at = (root, path) => {
|
|
549
|
+
const keys = Array.isArray(path) ? path : [];
|
|
550
|
+
const found = readPath(root, keys);
|
|
551
|
+
if (!found.found)
|
|
552
|
+
throw new Error(`Nothing at ${JSON.stringify(keys)}`);
|
|
553
|
+
return found.value;
|
|
554
|
+
};
|
|
555
|
+
const fit = (value) => {
|
|
556
|
+
for (let i = 0; i < READS.length; i++) {
|
|
557
|
+
const out = shown(value, READS[i]);
|
|
558
|
+
if (JSON.stringify(out).length <= MAX_READ)
|
|
559
|
+
return { value: out, truncated: i > 0 };
|
|
560
|
+
}
|
|
561
|
+
return { value: { $t: 'limit', name: Array.isArray(value) ? `Array(${value.length})` : 'Object' }, truncated: true };
|
|
562
|
+
};
|
|
563
|
+
const read = (id, path) => {
|
|
564
|
+
const { t, driver } = need(id);
|
|
565
|
+
if (typeof driver.getState !== 'function') {
|
|
566
|
+
throw new Error(`${t.info.label} has entries instead of a state; list them with "entries"`);
|
|
567
|
+
}
|
|
568
|
+
const raw = driver.getState();
|
|
569
|
+
const state = options.sanitizeState ? options.sanitizeState(raw, t.info) : raw;
|
|
570
|
+
return { rev: t.rev, pending: raw !== t.raw, ...fit(at(state, path)) };
|
|
571
|
+
};
|
|
572
|
+
const entries = (id) => {
|
|
573
|
+
const { t, driver } = need(id);
|
|
574
|
+
if (typeof driver.keys !== 'function')
|
|
575
|
+
throw new Error(`${t.info.label} has no entries`);
|
|
576
|
+
const list = [];
|
|
577
|
+
let total = 0;
|
|
578
|
+
for (const key of driver.keys()) {
|
|
579
|
+
total++;
|
|
580
|
+
if (list.length >= ENTRY_LIMIT)
|
|
581
|
+
continue;
|
|
582
|
+
const summary = summarize(driver, key);
|
|
583
|
+
if (summary)
|
|
584
|
+
list.push(summary);
|
|
585
|
+
}
|
|
586
|
+
return { entries: list, total };
|
|
587
|
+
};
|
|
588
|
+
const entry = (id, key, path) => {
|
|
589
|
+
const { t, driver } = need(id);
|
|
590
|
+
if (typeof driver.keys !== 'function')
|
|
591
|
+
throw new Error(`${t.info.label} has no entries`);
|
|
592
|
+
const summary = summarize(driver, String(key));
|
|
593
|
+
if (!summary)
|
|
594
|
+
return null;
|
|
595
|
+
const data = typeof driver.data === 'function' ? driver.data(String(key)) : undefined;
|
|
596
|
+
const { value, truncated } = fit(at(data, path));
|
|
597
|
+
return { summary, data: value, truncated };
|
|
598
|
+
};
|
|
599
|
+
const act = (id, key, op) => {
|
|
600
|
+
try {
|
|
601
|
+
const { t, driver } = need(id);
|
|
602
|
+
if (typeof driver.act !== 'function' || !(t.info.ops ?? []).includes(op)) {
|
|
603
|
+
throw new Error(`${t.info.label} does not support "${String(op)}"`);
|
|
604
|
+
}
|
|
605
|
+
return Promise.resolve(driver.act(String(key), op));
|
|
606
|
+
}
|
|
607
|
+
catch (error) {
|
|
608
|
+
return Promise.reject(error);
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
const stopDriver = (t) => {
|
|
612
|
+
const driver = t.driver.deref();
|
|
613
|
+
const stop = driver && stops.get(driver);
|
|
614
|
+
if (!driver || !stop)
|
|
615
|
+
return;
|
|
616
|
+
stops.delete(driver);
|
|
617
|
+
try {
|
|
618
|
+
stop();
|
|
619
|
+
}
|
|
620
|
+
catch {
|
|
621
|
+
// the store may be half torn down already
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
const untrack = (id, note) => {
|
|
625
|
+
const t = stores.get(id);
|
|
626
|
+
if (!t)
|
|
627
|
+
return;
|
|
628
|
+
stores.delete(id);
|
|
629
|
+
stopDriver(t);
|
|
630
|
+
const info = { ...t.info, disposed: true };
|
|
631
|
+
if (note)
|
|
632
|
+
info.note = note;
|
|
633
|
+
emit('state.store', { store: info });
|
|
634
|
+
};
|
|
635
|
+
const sink = { record, dirty, removed };
|
|
636
|
+
const track = (id, driver) => {
|
|
637
|
+
if (disposed || stores.has(id))
|
|
638
|
+
return;
|
|
639
|
+
let t;
|
|
640
|
+
try {
|
|
641
|
+
t = {
|
|
642
|
+
info: { ...driver.info, id },
|
|
643
|
+
driver: hold(driver),
|
|
644
|
+
rev: 0,
|
|
645
|
+
raw: undefined,
|
|
646
|
+
base: undefined,
|
|
647
|
+
based: false,
|
|
648
|
+
queue: [],
|
|
649
|
+
carry: 0,
|
|
650
|
+
windowStart: 0,
|
|
651
|
+
windowCount: 0,
|
|
652
|
+
dirty: new Set(),
|
|
653
|
+
removed: new Set(),
|
|
654
|
+
sent: new Map(),
|
|
655
|
+
reset: false,
|
|
656
|
+
entriesDue: Infinity,
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
catch {
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
stores.set(id, t);
|
|
663
|
+
emit('state.store', { store: t.info });
|
|
664
|
+
try {
|
|
665
|
+
if (typeof driver.getState === 'function') {
|
|
666
|
+
try {
|
|
667
|
+
t.raw = driver.getState();
|
|
668
|
+
t.base = clean(t, t.raw);
|
|
669
|
+
t.based = true;
|
|
670
|
+
}
|
|
671
|
+
catch {
|
|
672
|
+
// the first record becomes the base instead
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (typeof driver.keys === 'function') {
|
|
676
|
+
t.reset = true;
|
|
677
|
+
entriesSoon(t, ENTRY_DELAY);
|
|
678
|
+
}
|
|
679
|
+
const stop = driver.start(sink, id);
|
|
680
|
+
if (typeof stop === 'function')
|
|
681
|
+
stops.set(driver, stop);
|
|
682
|
+
}
|
|
683
|
+
catch (error) {
|
|
684
|
+
untrack(id, `Could not watch this store: ${message(error)}`);
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
const list = () => {
|
|
688
|
+
const out = [];
|
|
689
|
+
for (const t of [...stores.values()]) {
|
|
690
|
+
if (t.driver.deref())
|
|
691
|
+
out.push(t.info);
|
|
692
|
+
else
|
|
693
|
+
untrack(t.info.id);
|
|
694
|
+
}
|
|
695
|
+
return out;
|
|
696
|
+
};
|
|
697
|
+
const dispose = () => {
|
|
698
|
+
if (disposed)
|
|
699
|
+
return;
|
|
700
|
+
disposed = true;
|
|
701
|
+
if (timer !== null)
|
|
702
|
+
clearTimeout(timer);
|
|
703
|
+
timer = null;
|
|
704
|
+
for (const t of stores.values())
|
|
705
|
+
stopDriver(t);
|
|
706
|
+
stores.clear();
|
|
707
|
+
};
|
|
708
|
+
return { ...sink, track, untrack, stores: list, read, entries, entry, act, dispose };
|
|
709
|
+
}
|
|
710
|
+
//# sourceMappingURL=engine.js.map
|