跳到主要内容

React Fiber Tools Hook

实现了 React dev tools 使用的 Hook 。

一、作用

二、开发工具是否存在

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.
// 这不是 hook 上的真实属性,但可以设置以选择退出
// DevTools 集成以及相关的警告和日志。
// 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.
// DevTools 存在,即使它不支持 Fiber。
return true;
}
try {
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: %o.', err);
}
}
if (hook.checkDCE) {
// This is the real DevTools.
// 这是正式的开发者工具。
return true;
} else {
// This is likely a hook installed by Fast Refresh runtime.
// 这很可能是由 Fast Refresh 运行时安装的钩子。
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: %o', 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: %o', 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: %o', 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: %o', err);
}
}
}
}
}

八、为开发工具设置严格模式

export function setIsStrictModeForDevtools(newIsStrictMode: boolean) {
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
//
// 我们正在进行测试,因为 Scheduler.log 仅存在于 SchedulerMock 中。
// 为了减少严格模式测试中的噪音,
// 在双重渲染期间抑制警告并禁用调度器的让渡
unstable_setDisableYieldValue(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: %o', err);
}
}
}
}
}

九、注入性能分析钩子

export function injectProfilingHooks(
profilingHooks: DevToolsProfilingHooks,
): void {
injectedProfilingHooks = profilingHooks;
}

十、标记状态

1.标记提交已开始

export function markCommitStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markCommitStarted === 'function'
) {
injectedProfilingHooks.markCommitStarted(lanes);
}
}
}

2.标记提交停止

export function markCommitStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markCommitStopped === 'function'
) {
injectedProfilingHooks.markCommitStopped();
}
}
}

3.标记组件渲染已开始

export function markComponentRenderStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentRenderStarted === 'function'
) {
injectedProfilingHooks.markComponentRenderStarted(fiber);
}
}
}

4.标记组件渲染已停止

export function markComponentRenderStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentRenderStopped === 'function'
) {
injectedProfilingHooks.markComponentRenderStopped();
}
}
}

5.标记组件被动效果挂载开始

export function markComponentPassiveEffectMountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectMountStarted ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectMountStarted(fiber);
}
}
}

6.标记组件被动效果挂载已停止

export function markComponentPassiveEffectMountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectMountStopped ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectMountStopped();
}
}
}

6.标记组件被动效果卸载开始

export function markComponentPassiveEffectUnmountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectUnmountStarted ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectUnmountStarted(fiber);
}
}
}

7.标记组件被动效果卸载已停止

export function markComponentPassiveEffectUnmountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentPassiveEffectUnmountStopped ===
'function'
) {
injectedProfilingHooks.markComponentPassiveEffectUnmountStopped();
}
}
}

8.标记组件布局副作用挂载已停止

export function markComponentLayoutEffectMountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectMountStarted ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectMountStarted(fiber);
}
}
}

9.标记组件布局副作用挂载已停止

export function markComponentLayoutEffectMountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectMountStopped ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectMountStopped();
}
}
}

10. 标记组件布局副作用卸载已开始

export function markComponentLayoutEffectUnmountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectUnmountStarted ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectUnmountStarted(fiber);
}
}
}

11. 标记组件布局副作用卸载已停止

export function markComponentLayoutEffectUnmountStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentLayoutEffectUnmountStopped ===
'function'
) {
injectedProfilingHooks.markComponentLayoutEffectUnmountStopped();
}
}
}

12. 标记组件出错

export function markComponentErrored(
fiber: Fiber,
thrownValue: mixed,
lanes: Lanes,
): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentErrored === 'function'
) {
injectedProfilingHooks.markComponentErrored(fiber, thrownValue, lanes);
}
}
}

13. 标记组件已挂起

export function markComponentSuspended(
fiber: Fiber,
wakeable: Wakeable,
lanes: Lanes,
): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markComponentSuspended === 'function'
) {
injectedProfilingHooks.markComponentSuspended(fiber, wakeable, lanes);
}
}
}

14. 标记布局效果已开始

export function markLayoutEffectsStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markLayoutEffectsStarted === 'function'
) {
injectedProfilingHooks.markLayoutEffectsStarted(lanes);
}
}
}

15. 标记布局效果已停止

export function markLayoutEffectsStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markLayoutEffectsStopped === 'function'
) {
injectedProfilingHooks.markLayoutEffectsStopped();
}
}
}

16. 标记被动效果已开始

export function markPassiveEffectsStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markPassiveEffectsStarted === 'function'
) {
injectedProfilingHooks.markPassiveEffectsStarted(lanes);
}
}
}

17. 标记被动效果已停止

export function markPassiveEffectsStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markPassiveEffectsStopped === 'function'
) {
injectedProfilingHooks.markPassiveEffectsStopped();
}
}
}

18. 标记渲染已开始

export function markRenderStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderStarted === 'function'
) {
injectedProfilingHooks.markRenderStarted(lanes);
}
}
}

19. 标记渲染已产出

export function markRenderYielded(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderYielded === 'function'
) {
injectedProfilingHooks.markRenderYielded();
}
}
}

20. 标记渲染已停止

export function markRenderStopped(): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderStopped === 'function'
) {
injectedProfilingHooks.markRenderStopped();
}
}
}

21. 标记渲染已调度

export function markRenderScheduled(lane: Lane): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markRenderScheduled === 'function'
) {
injectedProfilingHooks.markRenderScheduled(lane);
}
}
}

22. 标记强制更新已安排

export function markForceUpdateScheduled(fiber: Fiber, lane: Lane): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markForceUpdateScheduled === 'function'
) {
injectedProfilingHooks.markForceUpdateScheduled(fiber, lane);
}
}
}

23. 标记状态更新已安排

export function markStateUpdateScheduled(fiber: Fiber, lane: Lane): void {
if (enableSchedulingProfiler) {
if (
injectedProfilingHooks !== null &&
typeof injectedProfilingHooks.markStateUpdateScheduled === 'function'
) {
injectedProfilingHooks.markStateUpdateScheduled(fiber, lane);
}
}
}

十一、 变量

declare const __REACT_DEVTOOLS_GLOBAL_HOOK__: Object | void;

let rendererID = null;
let injectedHook = null;
let injectedProfilingHooks: DevToolsProfilingHooks | null = null;
let hasLoggedError = false;