-
Notifications
You must be signed in to change notification settings - Fork 47.2k
/
ReactFiberDevToolsHook.js
81 lines (73 loc) · 2.13 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
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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 {Fiber} from './ReactFiber';
import type {FiberRoot} from './ReactFiberRoot';
import warning from 'fbjs/lib/warning';
declare var __REACT_DEVTOOLS_GLOBAL_HOOK__: Object | void;
let onCommitFiberRoot = null;
let onCommitFiberUnmount = null;
let hasLoggedError = false;
function catchErrors(fn) {
return function(arg) {
try {
return fn(arg);
} catch (err) {
if (__DEV__ && !hasLoggedError) {
hasLoggedError = true;
warning(false, 'React DevTools encountered an error: %s', err);
}
}
};
}
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.supportsFiber) {
if (__DEV__) {
warning(
false,
'The installed version of React DevTools is too old and will not work ' +
'with the current version of React. Please update React DevTools. ' +
'https://fb.me/react-devtools',
);
}
// DevTools exists, even though it doesn't support Fiber.
return true;
}
try {
const rendererID = hook.inject(internals);
// We have successfully injected, so now it is safe to set up hooks.
onCommitFiberRoot = catchErrors(root =>
hook.onCommitFiberRoot(rendererID, root),
);
onCommitFiberUnmount = catchErrors(fiber =>
hook.onCommitFiberUnmount(rendererID, fiber),
);
} catch (err) {
// Catch all errors because it is unsafe to throw during initialization.
if (__DEV__) {
warning(false, 'React DevTools encountered an error: %s.', err);
}
}
// DevTools exists
return true;
}
export function onCommitRoot(root: FiberRoot) {
if (typeof onCommitFiberRoot === 'function') {
onCommitFiberRoot(root);
}
}
export function onCommitUnmount(fiber: Fiber) {
if (typeof onCommitFiberUnmount === 'function') {
onCommitFiberUnmount(fiber);
}
}