-
Notifications
You must be signed in to change notification settings - Fork 47.5k
/
Copy pathReactFiberDevToolsHook.js
537 lines (494 loc) · 15.1 KB
/
ReactFiberDevToolsHook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Lane, Lanes} from './ReactFiberLane';
import type {Fiber, FiberRoot} from './ReactInternalTypes';
import type {ReactNodeList, Wakeable} from 'shared/ReactTypes';
import type {EventPriority} from './ReactEventPriorities';
// import type {DevToolsProfilingHooks} from 'react-devtools-shared/src/backend/types';
// TODO: This import doesn't work because the DevTools depend on the DOM version of React
// and to properly type check against DOM React we can't also type check again non-DOM
// React which this hook might be in.
type DevToolsProfilingHooks = any;
import {getLabelForLane, TotalLanes} from 'react-reconciler/src/ReactFiberLane';
import {DidCapture} from './ReactFiberFlags';
import {
consoleManagedByDevToolsDuringStrictMode,
enableProfilerTimer,
enableSchedulingProfiler,
} from 'shared/ReactFeatureFlags';
import {
DiscreteEventPriority,
ContinuousEventPriority,
DefaultEventPriority,
IdleEventPriority,
} from './ReactEventPriorities';
import {
ImmediatePriority as ImmediateSchedulerPriority,
UserBlockingPriority as UserBlockingSchedulerPriority,
NormalPriority as NormalSchedulerPriority,
IdlePriority as IdleSchedulerPriority,
log,
unstable_setDisableYieldValue,
} from './Scheduler';
import {setSuppressWarning} from 'shared/consoleWithStackDev';
import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';
declare const __REACT_DEVTOOLS_GLOBAL_HOOK__: Object | void;
let rendererID = null;
let injectedHook = null;
let injectedProfilingHooks: DevToolsProfilingHooks | null = null;
let hasLoggedError = false;
export const isDevToolsPresent =
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined';
export function injectInternals(internals: Object): boolean {
if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') {
// No DevTools
return false;
}
const hook = __REACT_DEVTOOLS_GLOBAL_HOOK__;
if (hook.isDisabled) {
// This isn't a real property on the hook, but it can be set to opt out
// of DevTools integration and associated warnings and logs.
// https://github.com/facebook/react/issues/3877
return true;
}
if (!hook.supportsFiber) {
if (__DEV__) {
console.error(
'The installed version of React DevTools is too old and will not work ' +
'with the current version of React. Please update React DevTools. ' +
'https://react.dev/link/react-devtools',
);
}
// DevTools exists, even though it doesn't support Fiber.
return true;
}
try {
if (enableSchedulingProfiler) {
// Conditionally inject these hooks only if Timeline profiler is supported by this build.
// This gives DevTools a way to feature detect that isn't tied to version number
// (since profiling and timeline are controlled by different feature flags).
internals = {
...internals,
getLaneLabelMap,
injectProfilingHooks,
};
}
rendererID = hook.inject(internals);
// We have successfully injected, so now it is safe to set up hooks.
injectedHook = hook;
} catch (err) {
// Catch all errors because it is unsafe to throw during initialization.
if (__DEV__) {
console.error('React instrumentation encountered an error: %s.', err);
}
}
if (hook.checkDCE) {
// This is the real DevTools.
return true;
} else {
// This is likely a hook installed by Fast Refresh runtime.
return false;
}
}
export function onScheduleRoot(root: FiberRoot, children: ReactNodeList) {
if (__DEV__) {
if (
injectedHook &&
typeof injectedHook.onScheduleFiberRoot === 'function'
) {
try {
injectedHook.onScheduleFiberRoot(rendererID, root, children);
} catch (err) {
if (__DEV__ && !hasLoggedError) {
hasLoggedError = true;
console.error('React instrumentation encountered an error: %s', err);
}
}
}
}
}
export function onCommitRoot(root: FiberRoot, eventPriority: EventPriority) {
if (injectedHook && typeof injectedHook.onCommitFiberRoot === 'function') {
try {
const didError = (root.current.flags & DidCapture) === DidCapture;
if (enableProfilerTimer) {
let schedulerPriority;
switch (eventPriority) {
case DiscreteEventPriority:
schedulerPriority = ImmediateSchedulerPriority;
break;
case ContinuousEventPriority:
schedulerPriority = UserBlockingSchedulerPriority;
break;
case DefaultEventPriority:
schedulerPriority = NormalSchedulerPriority;
break;
case IdleEventPriority:
schedulerPriority = IdleSchedulerPriority;
break;
default:
schedulerPriority = NormalSchedulerPriority;
break;
}
injectedHook.onCommitFiberRoot(
rendererID,
root,
schedulerPriority,
didError,
);
} else {
injectedHook.onCommitFiberRoot(rendererID, root, undefined, didError);
}
} catch (err) {
if (__DEV__) {
if (!hasLoggedError) {
hasLoggedError = true;
console.error('React instrumentation encountered an error: %s', err);
}
}
}
}
}
export function onPostCommitRoot(root: FiberRoot) {
if (
injectedHook &&
typeof injectedHook.onPostCommitFiberRoot === 'function'
) {
try {
injectedHook.onPostCommitFiberRoot(rendererID, root);
} catch (err) {
if (__DEV__) {
if (!hasLoggedError) {
hasLoggedError = true;
console.error('React instrumentation encountered an error: %s', err);
}
}
}
}
}
export function onCommitUnmount(fiber: Fiber) {
if (injectedHook && typeof injectedHook.onCommitFiberUnmount === 'function') {
try {
injectedHook.onCommitFiberUnmount(rendererID, fiber);
} catch (err) {
if (__DEV__) {
if (!hasLoggedError) {
hasLoggedError = true;
console.error('React instrumentation encountered an error: %s', err);
}
}
}
}
}
export function setIsStrictModeForDevtools(newIsStrictMode: boolean) {
if (consoleManagedByDevToolsDuringStrictMode) {
if (typeof log === 'function') {
// We're in a test because Scheduler.log only exists
// in SchedulerMock. To reduce the noise in strict mode tests,
// suppress warnings and disable scheduler yielding during the double render
unstable_setDisableYieldValue(newIsStrictMode);
setSuppressWarning(newIsStrictMode);
}
if (injectedHook && typeof injectedHook.setStrictMode === 'function') {
try {
injectedHook.setStrictMode(rendererID, newIsStrictMode);
} catch (err) {
if (__DEV__) {
if (!hasLoggedError) {
hasLoggedError = true;
console.error(
'React instrumentation encountered an error: %s',
err,
);
}
}
}
}
} else {
if (newIsStrictMode) {
disableLogs();
} else {
reenableLogs();
}
}
}
// Profiler API hooks
function injectProfilingHooks(profilingHooks: DevToolsProfilingHooks): void {
injectedProfilingHooks = profilingHooks;
}
function getLaneLabelMap(): Map<Lane, string> | null {
if (enableSchedulingProfiler) {
const map: Map<Lane, string> = new Map();
let lane = 1;
for (let index = 0; index < TotalLanes; index++) {
const label = ((getLabelForLane(lane): any): string);
map.set(lane, label);
lane *= 2;
}
return map;
} else {
return null;
}
}
export function markCommitStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markCommitStarted === 'function'
) {
injectedProfilingHooks.markCommitStarted(lanes);
}
}
}
export function markCommitStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markCommitStopped === 'function'
) {
injectedProfilingHooks.markCommitStopped();
}
}
}
export function markComponentRenderStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentRenderStarted === 'function'
) {
injectedProfilingHooks.markComponentRenderStarted(fiber);
}
}
}
export function markComponentRenderStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentRenderStopped === 'function'
) {
injectedProfilingHooks.markComponentRenderStopped();
}
}
}
export function markComponentPassiveEffectMountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectMountStarted ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectMountStarted(fiber);
}
}
}
export function markComponentPassiveEffectMountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectMountStopped ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectMountStopped();
}
}
}
export function markComponentPassiveEffectUnmountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectUnmountStarted ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectUnmountStarted(fiber);
}
}
}
export function markComponentPassiveEffectUnmountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectUnmountStopped ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectUnmountStopped();
}
}
}
export function markComponentLayoutEffectMountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectMountStarted ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectMountStarted(fiber);
}
}
}
export function markComponentLayoutEffectMountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectMountStopped ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectMountStopped();
}
}
}
export function markComponentLayoutEffectUnmountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectUnmountStarted ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectUnmountStarted(fiber);
}
}
}
export function markComponentLayoutEffectUnmountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectUnmountStopped ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectUnmountStopped();
}
}
}
export function markComponentErrored(
fiber: Fiber,
thrownValue: mixed,
lanes: Lanes,
): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentErrored === 'function'
) {
injectedProfilingHooks.markComponentErrored(fiber, thrownValue, lanes);
}
}
}
export function markComponentSuspended(
fiber: Fiber,
wakeable: Wakeable,
lanes: Lanes,
): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentSuspended === 'function'
) {
injectedProfilingHooks.markComponentSuspended(fiber, wakeable, lanes);
}
}
}
export function markLayoutEffectsStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markLayoutEffectsStarted === 'function'
) {
injectedProfilingHooks.markLayoutEffectsStarted(lanes);
}
}
}
export function markLayoutEffectsStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markLayoutEffectsStopped === 'function'
) {
injectedProfilingHooks.markLayoutEffectsStopped();
}
}
}
export function markPassiveEffectsStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markPassiveEffectsStarted === 'function'
) {
injectedProfilingHooks.markPassiveEffectsStarted(lanes);
}
}
}
export function markPassiveEffectsStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markPassiveEffectsStopped === 'function'
) {
injectedProfilingHooks.markPassiveEffectsStopped();
}
}
}
export function markRenderStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderStarted === 'function'
) {
injectedProfilingHooks.markRenderStarted(lanes);
}
}
}
export function markRenderYielded(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderYielded === 'function'
) {
injectedProfilingHooks.markRenderYielded();
}
}
}
export function markRenderStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderStopped === 'function'
) {
injectedProfilingHooks.markRenderStopped();
}
}
}
export function markRenderScheduled(lane: Lane): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderScheduled === 'function'
) {
injectedProfilingHooks.markRenderScheduled(lane);
}
}
}
export function markForceUpdateScheduled(fiber: Fiber, lane: Lane): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markForceUpdateScheduled === 'function'
) {
injectedProfilingHooks.markForceUpdateScheduled(fiber, lane);
}
}
}
export function markStateUpdateScheduled(fiber: Fiber, lane: Lane): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markStateUpdateScheduled === 'function'
) {
injectedProfilingHooks.markStateUpdateScheduled(fiber, lane);
}
}
}