@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,765 @@
|
|
|
1
|
+
import { register, unregister } from './registry.js';
|
|
2
|
+
/**
|
|
3
|
+
* Finds context providers (tag 10) under `root`, a FiberRoot or a fiber. `seen` maps each provider fiber, and its
|
|
4
|
+
* alternate, to the last value reported, and is updated as providers are reported.
|
|
5
|
+
*
|
|
6
|
+
* Self-contained, like the engine: the Metro bridge runs it from its source inside apps without the SDK. Iterative
|
|
7
|
+
* with a stack of its own rather than `return` pointers, which may lead to the other copy of a parent.
|
|
8
|
+
*/
|
|
9
|
+
export function walkProviders(root, seen, options = {}) {
|
|
10
|
+
const PROVIDER = 10;
|
|
11
|
+
const changed = options.changed === true;
|
|
12
|
+
const visit = typeof options.visit === 'function' ? options.visit : undefined;
|
|
13
|
+
const clock = typeof options.now === 'function'
|
|
14
|
+
? options.now
|
|
15
|
+
: typeof performance === 'object' && performance !== null && typeof performance.now === 'function'
|
|
16
|
+
? () => performance.now()
|
|
17
|
+
: () => Date.now();
|
|
18
|
+
const budget = typeof options.budget === 'number' && options.budget >= 0 ? options.budget : Infinity;
|
|
19
|
+
const deadline = budget === Infinity ? Infinity : clock() + budget;
|
|
20
|
+
const limit = typeof options.limit === 'number' && options.limit > 0 ? options.limit : Infinity;
|
|
21
|
+
const from = options.from;
|
|
22
|
+
const top = from ? from.top : 'current' in root ? root.current : root;
|
|
23
|
+
const stack = from ? from.stack.slice() : [];
|
|
24
|
+
const providers = [];
|
|
25
|
+
let visited = 0;
|
|
26
|
+
let removed = false;
|
|
27
|
+
let node = from ? from.node : top;
|
|
28
|
+
while (node != null) {
|
|
29
|
+
visited++;
|
|
30
|
+
if (node.tag === PROVIDER) {
|
|
31
|
+
const type = node.type;
|
|
32
|
+
// React 19 renders the context itself; React 18 renders `context.Provider`, which points back to it.
|
|
33
|
+
const context = type != null && typeof type === 'object' && type._context != null ? type._context : type;
|
|
34
|
+
const props = node.memoizedProps;
|
|
35
|
+
const value = props != null && typeof props === 'object' ? props.value : undefined;
|
|
36
|
+
const alternate = node.alternate;
|
|
37
|
+
const own = seen.has(node);
|
|
38
|
+
if (!own && (alternate == null || !seen.has(alternate))) {
|
|
39
|
+
providers.push({ fiber: node, context, value, first: true });
|
|
40
|
+
seen.set(node, value);
|
|
41
|
+
if (alternate != null)
|
|
42
|
+
seen.set(alternate, value);
|
|
43
|
+
}
|
|
44
|
+
else if (changed) {
|
|
45
|
+
const last = own ? seen.get(node) : seen.get(alternate);
|
|
46
|
+
if (!(last === value || (last !== last && value !== value))) {
|
|
47
|
+
providers.push({ fiber: node, context, value, first: false });
|
|
48
|
+
seen.set(node, value);
|
|
49
|
+
if (alternate != null)
|
|
50
|
+
seen.set(alternate, value);
|
|
51
|
+
}
|
|
52
|
+
else if (!own) {
|
|
53
|
+
seen.set(node, value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (changed && !removed && node.deletions != null && node.deletions.length > 0)
|
|
58
|
+
removed = true;
|
|
59
|
+
if (visit)
|
|
60
|
+
visit(node);
|
|
61
|
+
let child = node.child;
|
|
62
|
+
// A subtree that bailed out keeps its child fiber, so nothing under it changed in this commit.
|
|
63
|
+
if (changed && child != null && node.alternate != null && node.alternate.child === child)
|
|
64
|
+
child = null;
|
|
65
|
+
if (child != null) {
|
|
66
|
+
stack.push(node);
|
|
67
|
+
node = child;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
let next = null;
|
|
71
|
+
while (node != null && node !== top) {
|
|
72
|
+
if (node.sibling != null) {
|
|
73
|
+
next = node.sibling;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
node = stack.length > 0 ? stack.pop() : null;
|
|
77
|
+
}
|
|
78
|
+
node = next;
|
|
79
|
+
}
|
|
80
|
+
if (node != null &&
|
|
81
|
+
top != null &&
|
|
82
|
+
(providers.length >= limit || (deadline !== Infinity && (visited & 127) === 0 && clock() >= deadline))) {
|
|
83
|
+
return { providers, visited, removed, next: { top, node, stack } };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { providers, visited, removed, next: null };
|
|
87
|
+
}
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// The global hook
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
const HOOK = '__REACT_DEVTOOLS_GLOBAL_HOOK__';
|
|
92
|
+
const HOST_ROOT = 3;
|
|
93
|
+
/**
|
|
94
|
+
* Has `onCommit` called after each commit React reports to the React DevTools global hook. An existing hook keeps
|
|
95
|
+
* its object, which React holds on to: only its `onCommitFiberRoot` property is wrapped, the original called
|
|
96
|
+
* first. Without a hook, installs a minimal one, which only works when this runs before react-dom loads.
|
|
97
|
+
* Returns the undo, which leaves a wrapper someone else wrapped since in place, passing calls on.
|
|
98
|
+
*/
|
|
99
|
+
export function installReactHook(onCommit) {
|
|
100
|
+
const none = { hook: undefined, undo: () => { } };
|
|
101
|
+
if (typeof window === 'undefined')
|
|
102
|
+
return none;
|
|
103
|
+
try {
|
|
104
|
+
// Where React looks the hook up; the same object as `window` in browsers and React Native.
|
|
105
|
+
const scope = globalThis;
|
|
106
|
+
let hook = scope[HOOK];
|
|
107
|
+
if (hook === undefined) {
|
|
108
|
+
hook = minimalHook();
|
|
109
|
+
Object.defineProperty(scope, HOOK, { value: hook, configurable: true, writable: true, enumerable: false });
|
|
110
|
+
if (scope[HOOK] !== hook)
|
|
111
|
+
return none;
|
|
112
|
+
}
|
|
113
|
+
// `isDisabled` opts out of DevTools; React then never calls the hook, so there is nothing to wrap.
|
|
114
|
+
if (hook === null || typeof hook !== 'object' || hook.isDisabled)
|
|
115
|
+
return none;
|
|
116
|
+
const target = hook;
|
|
117
|
+
const original = target.onCommitFiberRoot;
|
|
118
|
+
let active = true;
|
|
119
|
+
const wrapper = function (...args) {
|
|
120
|
+
try {
|
|
121
|
+
if (typeof original === 'function')
|
|
122
|
+
return original.apply(this, args);
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
if (active) {
|
|
126
|
+
try {
|
|
127
|
+
onCommit(args[1]);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
// never throw into React's commit
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
target.onCommitFiberRoot = wrapper;
|
|
136
|
+
if (target.onCommitFiberRoot !== wrapper)
|
|
137
|
+
return none;
|
|
138
|
+
return {
|
|
139
|
+
hook: target,
|
|
140
|
+
undo: () => {
|
|
141
|
+
active = false;
|
|
142
|
+
try {
|
|
143
|
+
if (target.onCommitFiberRoot === wrapper)
|
|
144
|
+
target.onCommitFiberRoot = original;
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// a hook frozen since keeps the wrapper, which now only passes calls on
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return none;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* What React needs of the hook, plus what React Refresh and DevTools read when they load later: `renderers`
|
|
158
|
+
* filled by `inject`, and `getFiberRoots`. No `checkDCE`, so React still suggests installing DevTools.
|
|
159
|
+
*/
|
|
160
|
+
function minimalHook() {
|
|
161
|
+
const renderers = new Map();
|
|
162
|
+
const roots = new Map();
|
|
163
|
+
let nextId = 0;
|
|
164
|
+
const getFiberRoots = (id) => {
|
|
165
|
+
let mounted = roots.get(id);
|
|
166
|
+
if (!mounted)
|
|
167
|
+
roots.set(id, (mounted = new Set()));
|
|
168
|
+
return mounted;
|
|
169
|
+
};
|
|
170
|
+
return {
|
|
171
|
+
renderers,
|
|
172
|
+
supportsFiber: true,
|
|
173
|
+
inject: (renderer) => {
|
|
174
|
+
const id = ++nextId;
|
|
175
|
+
renderers.set(id, renderer);
|
|
176
|
+
return id;
|
|
177
|
+
},
|
|
178
|
+
getFiberRoots,
|
|
179
|
+
// Keeps mounted roots the way React DevTools does, so a client started later finds them.
|
|
180
|
+
onCommitFiberRoot: (id, root) => {
|
|
181
|
+
const state = root?.current?.memoizedState;
|
|
182
|
+
if (state == null || state.element == null)
|
|
183
|
+
getFiberRoots(id).delete(root);
|
|
184
|
+
else
|
|
185
|
+
getFiberRoots(id).add(root);
|
|
186
|
+
},
|
|
187
|
+
onCommitFiberUnmount: () => { },
|
|
188
|
+
onPostCommitFiberRoot: () => { },
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// Stores
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
/** Per discovery step, so a large tree never blocks a frame: walking time, then providers to set up. */
|
|
195
|
+
const CHUNK_MS = 5;
|
|
196
|
+
const CHUNK_PROVIDERS = 250;
|
|
197
|
+
/** Unmounted providers are looked for at most this often, after a commit that removed something. */
|
|
198
|
+
const SWEEP_MS = 2_000;
|
|
199
|
+
/** Instance counts are sent once they settle, since a list can mount many providers one commit after another. */
|
|
200
|
+
const ANNOUNCE_MS = 300;
|
|
201
|
+
/** Context stores never take most of the registry's 100 places from the app's other stores. */
|
|
202
|
+
const MAX_APP_CONTEXTS = 40;
|
|
203
|
+
const MAX_LIBRARY_CONTEXTS = 20;
|
|
204
|
+
/** Up to this many providers under one name, the store's state holds each one's value; past it, the latest one. */
|
|
205
|
+
const MAX_KEYED = 20;
|
|
206
|
+
/**
|
|
207
|
+
* Shows the React Context providers the app mounts as stores, one per context and name. The state is the provider's
|
|
208
|
+
* value, or with several providers each one's value by key. Read only, and only from development builds of React.
|
|
209
|
+
*/
|
|
210
|
+
export function trackContexts(ctx) {
|
|
211
|
+
const seen = new WeakMap();
|
|
212
|
+
const instances = new WeakMap();
|
|
213
|
+
const groups = new Map();
|
|
214
|
+
const labels = new Map();
|
|
215
|
+
const byId = new Map();
|
|
216
|
+
const roots = new WeakMap();
|
|
217
|
+
const numbers = new WeakMap();
|
|
218
|
+
const counts = { app: 0, library: 0 };
|
|
219
|
+
const announcing = new Set();
|
|
220
|
+
const fresh = [];
|
|
221
|
+
/** Stores whose state may have changed, recorded once the walk under way ends. */
|
|
222
|
+
const dirty = new Set();
|
|
223
|
+
/** Roots whose first walk is still going; stores found meanwhile wait for it, to be announced once. */
|
|
224
|
+
let discovering = 0;
|
|
225
|
+
let numbered = 0;
|
|
226
|
+
let stopped = false;
|
|
227
|
+
let lastSweep = 0;
|
|
228
|
+
let sweepTimer = null;
|
|
229
|
+
let announceTimer = null;
|
|
230
|
+
const all = () => [...groups.values()].flatMap((byName) => [...byName.values()]);
|
|
231
|
+
const later = (run, ms) => setTimeout(() => {
|
|
232
|
+
if (stopped)
|
|
233
|
+
return;
|
|
234
|
+
try {
|
|
235
|
+
run();
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
fail(error);
|
|
239
|
+
}
|
|
240
|
+
}, ms);
|
|
241
|
+
const announce = (group, extra) => {
|
|
242
|
+
if (group.sink === null || group.storeId === undefined)
|
|
243
|
+
return;
|
|
244
|
+
ctx.emit('state.store', { store: { ...group.driver.info, ...extra, id: group.storeId } });
|
|
245
|
+
};
|
|
246
|
+
/** Whether the count changed. */
|
|
247
|
+
const count = (group) => {
|
|
248
|
+
const size = group.instances.size;
|
|
249
|
+
const info = group.driver.info;
|
|
250
|
+
if ((info.instances ?? 1) === size)
|
|
251
|
+
return false;
|
|
252
|
+
if (size > 1)
|
|
253
|
+
info.instances = size;
|
|
254
|
+
else
|
|
255
|
+
delete info.instances;
|
|
256
|
+
return true;
|
|
257
|
+
};
|
|
258
|
+
const recount = (group) => {
|
|
259
|
+
if (!count(group) || group.fresh)
|
|
260
|
+
return;
|
|
261
|
+
announcing.add(group);
|
|
262
|
+
announceTimer ??= later(() => {
|
|
263
|
+
announceTimer = null;
|
|
264
|
+
for (const pending of announcing)
|
|
265
|
+
if (pending.live)
|
|
266
|
+
announce(pending);
|
|
267
|
+
announcing.clear();
|
|
268
|
+
}, ANNOUNCE_MS);
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* One provider's value, or each one's by key: one value standing for several would make each diff compare one
|
|
272
|
+
* provider with another. Very many, such as React Native's one per `<Text>` that holds elements, keep the latest value.
|
|
273
|
+
*/
|
|
274
|
+
const stateOf = (group) => {
|
|
275
|
+
const size = group.instances.size;
|
|
276
|
+
if (size === 1)
|
|
277
|
+
return group.instances.values().next().value.value;
|
|
278
|
+
if (size > MAX_KEYED)
|
|
279
|
+
return group.latest;
|
|
280
|
+
const state = {};
|
|
281
|
+
for (const instance of group.instances)
|
|
282
|
+
state[instance.key] = instance.value;
|
|
283
|
+
return state;
|
|
284
|
+
};
|
|
285
|
+
/** Records the stores that changed, once each: a list that renders again changes all its providers in one commit. */
|
|
286
|
+
const flush = () => {
|
|
287
|
+
for (const group of dirty) {
|
|
288
|
+
if (!group.live || group.fresh)
|
|
289
|
+
continue;
|
|
290
|
+
const next = stateOf(group);
|
|
291
|
+
if (next === group.state)
|
|
292
|
+
continue;
|
|
293
|
+
group.state = next;
|
|
294
|
+
if (group.sink !== null && group.storeId !== undefined)
|
|
295
|
+
group.sink.record(group.storeId, next, undefined, 'set');
|
|
296
|
+
}
|
|
297
|
+
dirty.clear();
|
|
298
|
+
};
|
|
299
|
+
const enroll = (group) => {
|
|
300
|
+
const kind = group.vendor === undefined ? 'app' : 'library';
|
|
301
|
+
if (counts[kind] >= (kind === 'app' ? MAX_APP_CONTEXTS : MAX_LIBRARY_CONTEXTS))
|
|
302
|
+
return;
|
|
303
|
+
group.id = register(group.driver);
|
|
304
|
+
if (group.id === undefined)
|
|
305
|
+
return;
|
|
306
|
+
counts[kind]++;
|
|
307
|
+
byId.set(group.id, group);
|
|
308
|
+
};
|
|
309
|
+
const settle = () => {
|
|
310
|
+
flush();
|
|
311
|
+
if (discovering > 0)
|
|
312
|
+
return;
|
|
313
|
+
for (const group of fresh.splice(0)) {
|
|
314
|
+
group.fresh = false;
|
|
315
|
+
if (!group.live)
|
|
316
|
+
continue;
|
|
317
|
+
count(group);
|
|
318
|
+
group.state = stateOf(group);
|
|
319
|
+
enroll(group);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
const dispose = (group) => {
|
|
323
|
+
group.live = false;
|
|
324
|
+
const byName = groups.get(group.context);
|
|
325
|
+
byName?.delete(group.name);
|
|
326
|
+
if (byName?.size === 0)
|
|
327
|
+
groups.delete(group.context);
|
|
328
|
+
if (labels.get(group.label) === group)
|
|
329
|
+
labels.delete(group.label);
|
|
330
|
+
announcing.delete(group);
|
|
331
|
+
dirty.delete(group);
|
|
332
|
+
if (group.id === undefined)
|
|
333
|
+
return;
|
|
334
|
+
counts[group.vendor === undefined ? 'app' : 'library']--;
|
|
335
|
+
byId.delete(group.id);
|
|
336
|
+
unregister(group.id);
|
|
337
|
+
};
|
|
338
|
+
const prune = (group) => {
|
|
339
|
+
let lost = false;
|
|
340
|
+
for (const instance of [...group.instances]) {
|
|
341
|
+
if (mounted(instance.fiber))
|
|
342
|
+
continue;
|
|
343
|
+
group.instances.delete(instance);
|
|
344
|
+
lost = true;
|
|
345
|
+
}
|
|
346
|
+
if (group.instances.size === 0)
|
|
347
|
+
dispose(group);
|
|
348
|
+
else if (lost) {
|
|
349
|
+
recount(group);
|
|
350
|
+
dirty.add(group);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
const sweep = () => {
|
|
354
|
+
sweepTimer = null;
|
|
355
|
+
lastSweep = Date.now();
|
|
356
|
+
for (const group of all())
|
|
357
|
+
prune(group);
|
|
358
|
+
flush();
|
|
359
|
+
};
|
|
360
|
+
const sweepSoon = () => {
|
|
361
|
+
if (sweepTimer !== null)
|
|
362
|
+
return;
|
|
363
|
+
sweepTimer = later(sweep, Math.max(0, lastSweep + SWEEP_MS - Date.now()));
|
|
364
|
+
};
|
|
365
|
+
/** One label per context, so the registry never takes two contexts for hot-reloaded copies of one store. */
|
|
366
|
+
const labelFor = (name, context) => {
|
|
367
|
+
for (let n = 1;; n++) {
|
|
368
|
+
const label = n === 1 ? name : `${name} #${n}`;
|
|
369
|
+
const holder = labels.get(label);
|
|
370
|
+
if (holder?.live)
|
|
371
|
+
prune(holder);
|
|
372
|
+
if (!holder?.live || holder.context === context)
|
|
373
|
+
return label;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
const nameFor = (fiber, context) => {
|
|
377
|
+
const name = contextName(context) ?? ownerName(fiber);
|
|
378
|
+
if (name !== undefined)
|
|
379
|
+
return name;
|
|
380
|
+
let n = numbers.get(context);
|
|
381
|
+
if (n === undefined)
|
|
382
|
+
numbers.set(context, (n = ++numbered));
|
|
383
|
+
return `Context #${n}`;
|
|
384
|
+
};
|
|
385
|
+
const keyFor = (fiber, group) => {
|
|
386
|
+
let base;
|
|
387
|
+
// A list keys the provider itself or the component that renders it.
|
|
388
|
+
for (let node = fiber, hops = 0; node != null && hops < 6 && base === undefined; node = node.return, hops++) {
|
|
389
|
+
if (node.key != null)
|
|
390
|
+
base = String(node.key);
|
|
391
|
+
}
|
|
392
|
+
const n = (group.keys.get(base ?? '#') ?? 0) + 1;
|
|
393
|
+
group.keys.set(base ?? '#', n);
|
|
394
|
+
if (base === undefined)
|
|
395
|
+
return `#${n}`;
|
|
396
|
+
return n === 1 ? base : `${base} #${n}`;
|
|
397
|
+
};
|
|
398
|
+
const add = (fiber, context, value) => {
|
|
399
|
+
const name = nameFor(fiber, context);
|
|
400
|
+
let byName = groups.get(context);
|
|
401
|
+
let group = byName?.get(name);
|
|
402
|
+
if (!group) {
|
|
403
|
+
const vendor = vendorOf(fiber, context, value);
|
|
404
|
+
const label = labelFor(name, context);
|
|
405
|
+
const created = {
|
|
406
|
+
context,
|
|
407
|
+
name,
|
|
408
|
+
label,
|
|
409
|
+
vendor,
|
|
410
|
+
instances: new Set(),
|
|
411
|
+
keys: new Map(),
|
|
412
|
+
latest: value,
|
|
413
|
+
state: undefined,
|
|
414
|
+
id: undefined,
|
|
415
|
+
sink: null,
|
|
416
|
+
storeId: undefined,
|
|
417
|
+
live: true,
|
|
418
|
+
fresh: true,
|
|
419
|
+
driver: {
|
|
420
|
+
info: { library: 'context', label, via: 'fiber', actions: false, ...(vendor !== undefined && { vendor }) },
|
|
421
|
+
getState: () => created.state,
|
|
422
|
+
start(sink, id) {
|
|
423
|
+
created.sink = sink;
|
|
424
|
+
created.storeId = id;
|
|
425
|
+
return () => {
|
|
426
|
+
if (created.sink === sink)
|
|
427
|
+
created.sink = null;
|
|
428
|
+
};
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
};
|
|
432
|
+
group = created;
|
|
433
|
+
if (!byName)
|
|
434
|
+
groups.set(context, (byName = new Map()));
|
|
435
|
+
byName.set(name, group);
|
|
436
|
+
labels.set(label, group);
|
|
437
|
+
fresh.push(group);
|
|
438
|
+
}
|
|
439
|
+
const instance = { group, fiber, value, key: keyFor(fiber, group) };
|
|
440
|
+
instances.set(fiber, instance);
|
|
441
|
+
if (fiber.alternate)
|
|
442
|
+
instances.set(fiber.alternate, instance);
|
|
443
|
+
group.instances.add(instance);
|
|
444
|
+
dirty.add(group);
|
|
445
|
+
recount(group);
|
|
446
|
+
};
|
|
447
|
+
const handle = (hit) => {
|
|
448
|
+
const fiber = hit.fiber;
|
|
449
|
+
let instance = instances.get(fiber);
|
|
450
|
+
if (!instance && fiber.alternate) {
|
|
451
|
+
instance = instances.get(fiber.alternate);
|
|
452
|
+
if (instance)
|
|
453
|
+
instances.set(fiber, instance);
|
|
454
|
+
}
|
|
455
|
+
if (instance?.group.live) {
|
|
456
|
+
instance.fiber = fiber;
|
|
457
|
+
const { value } = hit;
|
|
458
|
+
if (instance.value === value || (instance.value !== instance.value && value !== value))
|
|
459
|
+
return;
|
|
460
|
+
instance.value = value;
|
|
461
|
+
instance.group.latest = value;
|
|
462
|
+
dirty.add(instance.group);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (hit.context !== null && typeof hit.context === 'object')
|
|
466
|
+
add(fiber, hit.context, hit.value);
|
|
467
|
+
};
|
|
468
|
+
const step = (root, state) => {
|
|
469
|
+
const result = walkProviders(root, seen, { budget: CHUNK_MS, limit: CHUNK_PROVIDERS, from: state.cursor });
|
|
470
|
+
for (const hit of result.providers)
|
|
471
|
+
handle(hit);
|
|
472
|
+
state.cursor = result.next;
|
|
473
|
+
if (!state.cursor)
|
|
474
|
+
discovering--;
|
|
475
|
+
settle();
|
|
476
|
+
if (state.cursor) {
|
|
477
|
+
later(() => step(root, state), 0);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
// Commits during the walk can unmount parts of the tree it had yet to reach.
|
|
481
|
+
if (state.commits > 0)
|
|
482
|
+
sweepSoon();
|
|
483
|
+
};
|
|
484
|
+
const discover = (root) => {
|
|
485
|
+
const current = root?.current;
|
|
486
|
+
// `_debugOwner` exists only in development builds.
|
|
487
|
+
const ignored = current == null || current.tag !== HOST_ROOT || !('_debugOwner' in current);
|
|
488
|
+
const state = { cursor: null, commits: 0, ignored };
|
|
489
|
+
roots.set(root, state);
|
|
490
|
+
if (ignored)
|
|
491
|
+
return;
|
|
492
|
+
discovering++;
|
|
493
|
+
step(root, state);
|
|
494
|
+
};
|
|
495
|
+
const onCommit = (root) => {
|
|
496
|
+
if (stopped || root === null || typeof root !== 'object')
|
|
497
|
+
return;
|
|
498
|
+
try {
|
|
499
|
+
const state = roots.get(root);
|
|
500
|
+
if (!state)
|
|
501
|
+
return discover(root);
|
|
502
|
+
if (state.ignored)
|
|
503
|
+
return;
|
|
504
|
+
if (state.cursor)
|
|
505
|
+
state.commits++;
|
|
506
|
+
const result = walkProviders(root, seen, { changed: true });
|
|
507
|
+
for (const hit of result.providers)
|
|
508
|
+
handle(hit);
|
|
509
|
+
settle();
|
|
510
|
+
if (result.removed)
|
|
511
|
+
sweepSoon();
|
|
512
|
+
}
|
|
513
|
+
catch (error) {
|
|
514
|
+
fail(error);
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
const halt = () => {
|
|
518
|
+
stopped = true;
|
|
519
|
+
installed.undo();
|
|
520
|
+
if (sweepTimer !== null)
|
|
521
|
+
clearTimeout(sweepTimer);
|
|
522
|
+
if (announceTimer !== null)
|
|
523
|
+
clearTimeout(announceTimer);
|
|
524
|
+
};
|
|
525
|
+
// React internals changed shape, or a bug: stop for good rather than fail on every commit. The stores stay, with a note.
|
|
526
|
+
const fail = (error) => {
|
|
527
|
+
if (stopped)
|
|
528
|
+
return;
|
|
529
|
+
const note = `Stopped watching React Context: ${error instanceof Error ? error.message : String(error)}`;
|
|
530
|
+
for (const group of all())
|
|
531
|
+
announce(group, { note });
|
|
532
|
+
ctx.console.warn(`[capybara] ${note}`);
|
|
533
|
+
halt();
|
|
534
|
+
};
|
|
535
|
+
const installed = installReactHook(onCommit);
|
|
536
|
+
// A hook from React DevTools (always there in React Native) knows the trees mounted already.
|
|
537
|
+
const hook = installed.hook;
|
|
538
|
+
if (hook && hook.renderers instanceof Map && hook.renderers.size > 0 && typeof hook.getFiberRoots === 'function') {
|
|
539
|
+
later(() => {
|
|
540
|
+
for (const id of [...hook.renderers.keys()]) {
|
|
541
|
+
for (const root of [...hook.getFiberRoots(id)]) {
|
|
542
|
+
if (root !== null && typeof root === 'object' && !roots.has(root))
|
|
543
|
+
discover(root);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}, 0);
|
|
547
|
+
}
|
|
548
|
+
return {
|
|
549
|
+
info(store) {
|
|
550
|
+
const group = byId.get(store.id);
|
|
551
|
+
return group?.live ? { ...group.driver.info, id: store.id } : store;
|
|
552
|
+
},
|
|
553
|
+
stop() {
|
|
554
|
+
if (!stopped)
|
|
555
|
+
halt();
|
|
556
|
+
for (const group of all())
|
|
557
|
+
dispose(group);
|
|
558
|
+
},
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
/** Whether the fiber, or its other copy, still hangs from a root: React cuts removed subtrees loose. */
|
|
562
|
+
function mounted(fiber) {
|
|
563
|
+
const reaches = (start) => {
|
|
564
|
+
let node = start;
|
|
565
|
+
for (let hops = 0; node != null && hops < 100_000; hops++) {
|
|
566
|
+
if (node.tag === HOST_ROOT)
|
|
567
|
+
return true;
|
|
568
|
+
node = node.return;
|
|
569
|
+
}
|
|
570
|
+
return false;
|
|
571
|
+
};
|
|
572
|
+
return reaches(fiber) || reaches(fiber.alternate);
|
|
573
|
+
}
|
|
574
|
+
// ---------------------------------------------------------------------------
|
|
575
|
+
// Names
|
|
576
|
+
// ---------------------------------------------------------------------------
|
|
577
|
+
const COMPONENT_TAGS = new Set([0, 1, 11, 14, 15]);
|
|
578
|
+
function contextName(context) {
|
|
579
|
+
try {
|
|
580
|
+
const name = context.displayName;
|
|
581
|
+
return typeof name === 'string' && name !== '' ? name : undefined;
|
|
582
|
+
}
|
|
583
|
+
catch {
|
|
584
|
+
return undefined;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* The component that rendered the provider: its owner, which development builds keep, else the closest one above.
|
|
589
|
+
* `owned` keeps to the owner, for an element created outside any render (e.g. passed to `root.render()`) has none.
|
|
590
|
+
*/
|
|
591
|
+
function ownerName(fiber, owned = false) {
|
|
592
|
+
try {
|
|
593
|
+
for (let owner = fiber._debugOwner, hops = 0; owner != null && hops < 20; hops++) {
|
|
594
|
+
// A Server Component owner is plain info with a name.
|
|
595
|
+
if (!('tag' in owner))
|
|
596
|
+
return typeof owner.name === 'string' && owner.name !== '' ? owner.name : undefined;
|
|
597
|
+
const name = componentName(owner);
|
|
598
|
+
if (name !== undefined)
|
|
599
|
+
return name;
|
|
600
|
+
owner = owner._debugOwner;
|
|
601
|
+
}
|
|
602
|
+
if (owned)
|
|
603
|
+
return undefined;
|
|
604
|
+
for (let node = fiber.return, hops = 0; node != null && hops < 50; node = node.return, hops++) {
|
|
605
|
+
if (!COMPONENT_TAGS.has(node.tag))
|
|
606
|
+
continue;
|
|
607
|
+
const name = componentName(node);
|
|
608
|
+
if (name !== undefined)
|
|
609
|
+
return name;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
catch {
|
|
613
|
+
// a getter that threw: numbered instead
|
|
614
|
+
}
|
|
615
|
+
return undefined;
|
|
616
|
+
}
|
|
617
|
+
function componentName(fiber) {
|
|
618
|
+
if (!COMPONENT_TAGS.has(fiber.tag))
|
|
619
|
+
return undefined;
|
|
620
|
+
// memo() and forwardRef() wrap the function, sometimes one inside the other.
|
|
621
|
+
let type = fiber.elementType ?? fiber.type;
|
|
622
|
+
for (let depth = 0; type != null && depth < 3; depth++) {
|
|
623
|
+
const name = type.displayName ?? (typeof type === 'function' ? type.name : undefined);
|
|
624
|
+
// Bundlers rename clashing functions with a suffix, e.g. Vite's pre-bundling turns SWRConfig into SWRConfig$1.
|
|
625
|
+
if (typeof name === 'string' && name !== '')
|
|
626
|
+
return name.replace(/\$\d+$/, '') || name;
|
|
627
|
+
if (typeof type !== 'object')
|
|
628
|
+
return undefined;
|
|
629
|
+
type = type.render ?? type.type;
|
|
630
|
+
}
|
|
631
|
+
return undefined;
|
|
632
|
+
}
|
|
633
|
+
// ---------------------------------------------------------------------------
|
|
634
|
+
// Library contexts
|
|
635
|
+
// ---------------------------------------------------------------------------
|
|
636
|
+
/** Contexts that libraries name, by display name. */
|
|
637
|
+
const LIBRARY_CONTEXTS = new Map(Object.entries({
|
|
638
|
+
ReactRedux: 'react-redux',
|
|
639
|
+
TextAncestorContext: 'React Native',
|
|
640
|
+
RootTagContext: 'React Native',
|
|
641
|
+
ScrollViewContext: 'React Native',
|
|
642
|
+
VirtualizedListContext: 'React Native',
|
|
643
|
+
ImageAnalyticsTagContext: 'React Native',
|
|
644
|
+
SafeAreaInsetsContext: 'react-native-safe-area-context',
|
|
645
|
+
SafeAreaFrameContext: 'react-native-safe-area-context',
|
|
646
|
+
LinkingContext: 'React Navigation',
|
|
647
|
+
LocaleDirContext: 'React Navigation',
|
|
648
|
+
UnhandledLinkingContext: 'React Navigation',
|
|
649
|
+
NavigatorContext: 'Expo Router',
|
|
650
|
+
RouteNode: 'Expo Router',
|
|
651
|
+
}));
|
|
652
|
+
/** Library components that render providers, by the names their code gives them. Names an app might use are left out. */
|
|
653
|
+
const LIBRARY_COMPONENTS = new Map(Object.entries({
|
|
654
|
+
QueryClientProvider: 'TanStack Query',
|
|
655
|
+
QueryErrorResetBoundary: 'TanStack Query',
|
|
656
|
+
PersistQueryClientProvider: 'TanStack Query',
|
|
657
|
+
HydrationBoundary: 'TanStack Query',
|
|
658
|
+
SWRConfig: 'SWR',
|
|
659
|
+
BaseNavigationContainer: 'React Navigation',
|
|
660
|
+
NavigationContainerInner: 'React Navigation',
|
|
661
|
+
NavigationIndependentTree: 'React Navigation',
|
|
662
|
+
NavigationProvider: 'React Navigation',
|
|
663
|
+
NavigationStateListenerProvider: 'React Navigation',
|
|
664
|
+
PreventRemoveProvider: 'React Navigation',
|
|
665
|
+
EnsureSingleNavigator: 'React Navigation',
|
|
666
|
+
SceneView: 'React Navigation',
|
|
667
|
+
ServerContainer: 'React Navigation',
|
|
668
|
+
FrameSizeProvider: 'React Navigation',
|
|
669
|
+
AnimatedHeaderHeightProvider: 'React Navigation',
|
|
670
|
+
BottomTabView: 'React Navigation',
|
|
671
|
+
CardContainerInner: 'React Navigation',
|
|
672
|
+
DrawerViewBase: 'React Navigation',
|
|
673
|
+
NativeStackView: 'React Navigation',
|
|
674
|
+
MaterialTopTabView: 'React Navigation',
|
|
675
|
+
ExpoRoot: 'Expo Router',
|
|
676
|
+
ContextNavigator: 'Expo Router',
|
|
677
|
+
InnerRoot: 'Expo Router',
|
|
678
|
+
RootModalProvider: 'Expo Router',
|
|
679
|
+
LinkPreviewContextProvider: 'Expo Router',
|
|
680
|
+
NativeTabsNavigator: 'Expo Router',
|
|
681
|
+
SlotNavigator: 'Expo Router',
|
|
682
|
+
SplitViewNavigator: 'Expo Router',
|
|
683
|
+
ExperimentalStackNavigator: 'Expo Router',
|
|
684
|
+
ZoomTransitionSourceContextProvider: 'Expo Router',
|
|
685
|
+
ZoomTransitionTargetContextProvider: 'Expo Router',
|
|
686
|
+
SafeAreaProvider: 'react-native-safe-area-context',
|
|
687
|
+
ScreenStack: 'react-native-screens',
|
|
688
|
+
ScreenStackItem: 'react-native-screens',
|
|
689
|
+
InnerScreen: 'react-native-screens',
|
|
690
|
+
GestureDetectorProvider: 'react-native-screens',
|
|
691
|
+
ReanimatedScreenProvider: 'react-native-screens',
|
|
692
|
+
ReanimatedNativeStackScreen: 'react-native-screens',
|
|
693
|
+
GestureHandlerRootView: 'react-native-gesture-handler',
|
|
694
|
+
InterceptingGestureDetector: 'react-native-gesture-handler',
|
|
695
|
+
ScrollViewResponderProvider: 'react-native-gesture-handler',
|
|
696
|
+
SkipEntering: 'react-native-reanimated',
|
|
697
|
+
AppContainer: 'React Native',
|
|
698
|
+
}));
|
|
699
|
+
/**
|
|
700
|
+
* The library a context comes from, or undefined for the app's own. Runs once per store. Only the owner counts, not
|
|
701
|
+
* a component above: an app's provider passed to `root.render()` inside a library's has no owner.
|
|
702
|
+
*/
|
|
703
|
+
function vendorOf(fiber, context, value) {
|
|
704
|
+
try {
|
|
705
|
+
const redux = globalThis[Symbol.for('react-redux-context')];
|
|
706
|
+
if (redux instanceof Map)
|
|
707
|
+
for (const known of redux.values())
|
|
708
|
+
if (known === context)
|
|
709
|
+
return 'react-redux';
|
|
710
|
+
if (value !== null && typeof value === 'object') {
|
|
711
|
+
const v = value;
|
|
712
|
+
if (typeof v.getQueryCache === 'function' && typeof v.getMutationCache === 'function')
|
|
713
|
+
return 'TanStack Query';
|
|
714
|
+
const cache = v.cache;
|
|
715
|
+
if (typeof v.mutate === 'function' && cache != null && typeof cache.get === 'function' && typeof cache.set === 'function') {
|
|
716
|
+
return 'SWR';
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
// Bundlers number names they had to rename, e.g. `SWRConfig$1`.
|
|
720
|
+
const plain = (text) => text?.replace(/(?:\$|_)\d+$/, '');
|
|
721
|
+
const byName = LIBRARY_CONTEXTS.get(plain(contextName(context)) ?? '') ?? LIBRARY_COMPONENTS.get(plain(ownerName(fiber, true)) ?? '');
|
|
722
|
+
return byName ?? sourcePackage(fiber);
|
|
723
|
+
}
|
|
724
|
+
catch {
|
|
725
|
+
return undefined;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* The package whose code created the provider element, from where React says it was created. Web only: React
|
|
730
|
+
* Native's stacks name the bundle rather than files.
|
|
731
|
+
*/
|
|
732
|
+
function sourcePackage(fiber) {
|
|
733
|
+
let path;
|
|
734
|
+
const source = fiber._debugSource;
|
|
735
|
+
if (typeof source?.fileName === 'string') {
|
|
736
|
+
path = source.fileName;
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
// React Native's frames name the bundle, never a file: not worth formatting the stack for.
|
|
740
|
+
if (typeof navigator === 'object' && navigator?.product === 'ReactNative')
|
|
741
|
+
return undefined;
|
|
742
|
+
const stack = fiber._debugStack?.stack;
|
|
743
|
+
if (typeof stack !== 'string' || stack.includes('UnknownOwner'))
|
|
744
|
+
return undefined;
|
|
745
|
+
// The first frame is React's jsxDEV or createElement; the next is the code that created the element.
|
|
746
|
+
const frame = stack.split('\n').filter((line) => /:\d+:\d+\)?\s*$/.test(line))[1];
|
|
747
|
+
path = frame && /((?:[a-z][\w+.-]*:\/\/|\/)[^\s()]+?)(?::\d+){1,2}\)?\s*$/i.exec(frame)?.[1]?.replace(/[?#].*$/, '');
|
|
748
|
+
}
|
|
749
|
+
if (path === undefined)
|
|
750
|
+
return undefined;
|
|
751
|
+
// Turbopack names chunks after the path they hold, with underscores for slashes.
|
|
752
|
+
if (/\/node_modules_[^/]*$/.test(path))
|
|
753
|
+
return 'a dependency';
|
|
754
|
+
const at = path.lastIndexOf('node_modules/');
|
|
755
|
+
if (at < 0)
|
|
756
|
+
return undefined;
|
|
757
|
+
const rest = path.slice(at + 'node_modules/'.length);
|
|
758
|
+
// Vite serves dependencies as `.vite/deps/<name>.js`, with `@scope_name` for scoped ones.
|
|
759
|
+
const vite = /^\.vite\/deps\/([^/]+?)\.js$/.exec(rest)?.[1];
|
|
760
|
+
if (vite !== undefined)
|
|
761
|
+
return vite.startsWith('chunk-') ? 'a dependency' : vite.replace(/^(@[^_]+)_/, '$1/');
|
|
762
|
+
const parts = rest.split('/');
|
|
763
|
+
return (parts[0]?.startsWith('@') ? `${parts[0]}/${parts[1]}` : parts[0]) || 'a dependency';
|
|
764
|
+
}
|
|
765
|
+
//# sourceMappingURL=context.js.map
|