bippy 0.6.0 → 0.6.1-dev.03b829a
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/LICENSE +1 -1
- package/README.md +126 -136
- package/dist/core.cjs +1 -1
- package/dist/core.d.cts +45 -60
- package/dist/core.d.ts +45 -60
- package/dist/core.js +1 -1
- package/dist/core2.cjs +9 -0
- package/dist/core2.d.cts +11 -3
- package/dist/core2.d.ts +11 -3
- package/dist/core2.js +9 -0
- package/dist/errors.d.cts +447 -0
- package/dist/errors.d.ts +447 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +11 -3
- package/dist/index.d.ts +11 -3
- package/dist/index.iife.js +1 -1
- package/dist/index.js +1 -1
- package/dist/install-hook-only.cjs +1 -1
- package/dist/install-hook-only.d.cts +9 -1
- package/dist/install-hook-only.d.ts +9 -1
- package/dist/install-hook-only.iife.js +1 -1
- package/dist/install-hook-only.js +1 -1
- package/dist/rdt-hook.cjs +1 -1
- package/dist/rdt-hook.js +1 -1
- package/dist/source.cjs +14 -4
- package/dist/source.d.cts +27 -9
- package/dist/source.d.ts +27 -9
- package/dist/source.js +14 -4
- package/package.json +32 -22
- package/src/core.ts +368 -314
- package/src/errors.ts +34 -0
- package/src/install-hook-only.ts +2 -2
- package/src/rdt-hook.ts +150 -153
- package/src/react-internals/generated/react-work-tags.ts +318 -0
- package/src/react-internals/index.ts +67 -0
- package/src/react-internals/semver.ts +78 -0
- package/src/react-internals/types.ts +269 -0
- package/src/source/constants.ts +1 -1
- package/src/source/error-stack.ts +11 -0
- package/src/source/get-display-name-from-source.ts +1 -1
- package/src/source/get-source.ts +4 -3
- package/src/source/index.ts +9 -1
- package/src/source/inspect-hooks.ts +221 -169
- package/src/source/owner-stack.ts +88 -114
- package/src/source/parse-debug-stack.ts +4 -4
- package/src/source/parse-hook-names.ts +1 -1
- package/src/source/parse-stack.ts +1 -1
- package/src/source/symbolication.ts +488 -88
- package/dist/get-source.cjs +0 -19
- package/dist/get-source.js +0 -19
- package/dist/react-refresh.cjs +0 -9
- package/dist/react-refresh.d.cts +0 -66
- package/dist/react-refresh.d.ts +0 -66
- package/dist/react-refresh.js +0 -9
- package/dist/unsubscribe.d.cts +0 -298
- package/dist/unsubscribe.d.ts +0 -298
- package/src/react-refresh/constants.ts +0 -9
- package/src/react-refresh/detect-hmr-transport.ts +0 -33
- package/src/react-refresh/index.ts +0 -173
- package/src/react-refresh/metro-hmr-transport.ts +0 -188
- package/src/react-refresh/next-webpack-hmr-transport.ts +0 -72
- package/src/react-refresh/normalize-hmr-file-path.ts +0 -24
- package/src/react-refresh/types.ts +0 -7
- package/src/react-refresh/vite-hmr-transport.ts +0 -116
- package/src/types.ts +0 -438
- package/src/unsubscribe.ts +0 -17
package/src/errors.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export class BippyError extends Error {
|
|
2
|
+
constructor(message: string, cause?: unknown) {
|
|
3
|
+
super(message, { cause });
|
|
4
|
+
this.name = "BippyError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class BippyHookInspectionError extends BippyError {
|
|
9
|
+
constructor(message: string, cause?: unknown) {
|
|
10
|
+
super(message, cause);
|
|
11
|
+
this.name = "BippyHookInspectionError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class BippyUnsupportedHookError extends BippyHookInspectionError {
|
|
16
|
+
constructor(message: string, cause?: unknown) {
|
|
17
|
+
super(message, cause);
|
|
18
|
+
this.name = "BippyUnsupportedHookError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class BippyHookRenderError extends BippyHookInspectionError {
|
|
23
|
+
constructor(message: string, cause?: unknown) {
|
|
24
|
+
super(message, cause);
|
|
25
|
+
this.name = "BippyHookRenderError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class BippySourceMapError extends BippyError {
|
|
30
|
+
constructor(message: string, cause?: unknown) {
|
|
31
|
+
super(message, cause);
|
|
32
|
+
this.name = "BippySourceMapError";
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/install-hook-only.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getRDTHook } from "./rdt-hook.js";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
getRDTHook();
|
package/src/rdt-hook.ts
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
//
|
|
2
|
-
// this file is super important to load the __REACT_DEVTOOLS_GLOBAL_HOOK__ object
|
|
3
|
-
// without this, we can't stub the React DevTools global hook, we don't have a way to instrument the application
|
|
4
|
-
// make sure you import this file first before anything else (particularly React)
|
|
1
|
+
// This module must load before React so renderers can inject into the hook.
|
|
5
2
|
|
|
6
|
-
import type { ReactDevToolsGlobalHook, ReactRenderer } from "./
|
|
7
|
-
|
|
3
|
+
import type { ReactDevToolsGlobalHook, ReactRenderer } from "./react-internals/index.js";
|
|
4
|
+
|
|
5
|
+
export interface Unsubscribe extends Disposable {
|
|
6
|
+
(): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface ActiveListener {
|
|
10
|
+
(): unknown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface RendererInjectListener {
|
|
14
|
+
(renderer: ReactRenderer): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface RDTHookReplaceListener {
|
|
18
|
+
(rdtHook: ReactDevToolsGlobalHook): void;
|
|
19
|
+
}
|
|
8
20
|
|
|
9
21
|
export const version = process.env.VERSION;
|
|
10
22
|
export const BIPPY_INSTRUMENTATION_STRING = `bippy-${version}`;
|
|
11
23
|
|
|
12
24
|
const objectDefineProperty = Object.defineProperty;
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
14
|
-
const objectHasOwnProperty = Object.prototype.hasOwnProperty;
|
|
15
25
|
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
const noOp = (): void => {};
|
|
27
|
+
|
|
28
|
+
export const createUnsubscribe = (unsubscribe: () => void): Unsubscribe =>
|
|
29
|
+
Object.assign(unsubscribe, { [Symbol.dispose]: unsubscribe });
|
|
19
30
|
|
|
20
31
|
const checkDCE = (functionToCheck: unknown): void => {
|
|
21
32
|
try {
|
|
@@ -26,7 +37,7 @@ const checkDCE = (functionToCheck: unknown): void => {
|
|
|
26
37
|
"React is running in production mode, but dead code " +
|
|
27
38
|
"elimination has not been applied. Read how to correctly " +
|
|
28
39
|
"configure React for production: " +
|
|
29
|
-
"https://
|
|
40
|
+
"https://react.dev/link/perf-use-production-build",
|
|
30
41
|
);
|
|
31
42
|
});
|
|
32
43
|
}
|
|
@@ -39,62 +50,75 @@ export const isRealReactDevtools = (
|
|
|
39
50
|
return Boolean(rdtHook && "getFiberRoots" in rdtHook);
|
|
40
51
|
};
|
|
41
52
|
|
|
42
|
-
|
|
43
|
-
let injectFnStr: string | undefined = undefined;
|
|
53
|
+
export const _onActiveListeners = new Set<ActiveListener>();
|
|
44
54
|
|
|
45
|
-
export const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
55
|
+
export const _renderers = new Set<ReactRenderer>();
|
|
56
|
+
|
|
57
|
+
const rendererInjectListeners = new Set<RendererInjectListener>();
|
|
58
|
+
const rdtHookReplaceListeners = new Set<RDTHookReplaceListener>();
|
|
59
|
+
// HACK: Hook replacement can leave an old inject wrapper in the call chain, so notifications must be deduplicated.
|
|
60
|
+
const notifiedRenderers = new WeakSet<ReactRenderer>();
|
|
61
|
+
const notifyingInjectByHook = new WeakMap<
|
|
62
|
+
ReactDevToolsGlobalHook,
|
|
63
|
+
ReactDevToolsGlobalHook["inject"]
|
|
64
|
+
>();
|
|
65
|
+
|
|
66
|
+
const notifyActiveListeners = (): void => {
|
|
67
|
+
for (const listener of _onActiveListeners) {
|
|
68
|
+
listener();
|
|
51
69
|
}
|
|
52
|
-
// https://github.com/facebook/react/blob/8f8b336734d7c807f5aa11b0f31540e63302d789/packages/react-refresh/src/ReactFreshRuntime.js#L459
|
|
53
|
-
return Boolean(injectFnStr?.includes("(injected)"));
|
|
54
70
|
};
|
|
55
71
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
72
|
+
const notifyRendererInjectListeners = (renderer: ReactRenderer): void => {
|
|
73
|
+
for (const listener of rendererInjectListeners) {
|
|
74
|
+
listener(renderer);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
59
77
|
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
const notifyRDTHookReplaceListeners = (rdtHook: ReactDevToolsGlobalHook): void => {
|
|
79
|
+
for (const listener of rdtHookReplaceListeners) {
|
|
80
|
+
listener(rdtHook);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
65
83
|
|
|
66
|
-
const
|
|
67
|
-
if (rdtHook.inject ===
|
|
68
|
-
const
|
|
84
|
+
const setRendererInjectDispatcher = (rdtHook: ReactDevToolsGlobalHook): void => {
|
|
85
|
+
if (rdtHook.inject === notifyingInjectByHook.get(rdtHook)) return;
|
|
86
|
+
const previousInject = rdtHook.inject;
|
|
69
87
|
const nextInject = (renderer: ReactRenderer) => {
|
|
70
|
-
const rendererId =
|
|
88
|
+
const rendererId = previousInject.call(rdtHook, renderer);
|
|
71
89
|
if (!notifiedRenderers.has(renderer)) {
|
|
72
90
|
notifiedRenderers.add(renderer);
|
|
73
|
-
|
|
74
|
-
listener(renderer);
|
|
75
|
-
}
|
|
91
|
+
notifyRendererInjectListeners(renderer);
|
|
76
92
|
}
|
|
77
93
|
return rendererId;
|
|
78
94
|
};
|
|
79
95
|
rdtHook.inject = nextInject;
|
|
80
|
-
|
|
96
|
+
notifyingInjectByHook.set(rdtHook, nextInject);
|
|
81
97
|
};
|
|
82
98
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
* single shared inject wrapper lets multiple consumers (override methods,
|
|
86
|
-
* react-refresh, user code) observe renderers without stacking patches
|
|
87
|
-
* whose restore order matters. Returns an unsubscribe function.
|
|
88
|
-
*/
|
|
89
|
-
export const onRendererInject = (listener: (renderer: ReactRenderer) => void): Unsubscribe => {
|
|
90
|
-
ensureInjectNotifiesListeners(getRDTHook());
|
|
99
|
+
export const onRendererInject = (listener: RendererInjectListener): Unsubscribe => {
|
|
100
|
+
setRendererInjectDispatcher(getRDTHook());
|
|
91
101
|
rendererInjectListeners.add(listener);
|
|
92
|
-
return
|
|
102
|
+
return createUnsubscribe(() => {
|
|
93
103
|
rendererInjectListeners.delete(listener);
|
|
94
104
|
});
|
|
95
105
|
};
|
|
96
106
|
|
|
97
|
-
export const
|
|
107
|
+
export const onRDTHookReplace = (listener: RDTHookReplaceListener): Unsubscribe => {
|
|
108
|
+
rdtHookReplaceListeners.add(listener);
|
|
109
|
+
return createUnsubscribe(() => {
|
|
110
|
+
rdtHookReplaceListeners.delete(listener);
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const getRendererMap = (rdtHook: ReactDevToolsGlobalHook): Map<number, ReactRenderer> => {
|
|
115
|
+
if (!(rdtHook.renderers instanceof Map)) {
|
|
116
|
+
rdtHook.renderers = new Map();
|
|
117
|
+
}
|
|
118
|
+
return rdtHook.renderers;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const installRDTHook = (onActive?: ActiveListener): ReactDevToolsGlobalHook => {
|
|
98
122
|
if (onActive) {
|
|
99
123
|
_onActiveListeners.add(onActive);
|
|
100
124
|
}
|
|
@@ -105,20 +129,20 @@ export const installRDTHook = (onActive?: () => unknown): ReactDevToolsGlobalHoo
|
|
|
105
129
|
_instrumentationSource: BIPPY_INSTRUMENTATION_STRING,
|
|
106
130
|
checkDCE,
|
|
107
131
|
hasUnsupportedRendererAttached: false,
|
|
108
|
-
inject(renderer) {
|
|
132
|
+
inject: (renderer) => {
|
|
109
133
|
const nextRendererId = ++rendererIdCounter;
|
|
110
134
|
renderers.set(nextRendererId, renderer);
|
|
111
135
|
_renderers.add(renderer);
|
|
112
136
|
if (!rdtHook._instrumentationIsActive) {
|
|
113
137
|
rdtHook._instrumentationIsActive = true;
|
|
114
|
-
|
|
138
|
+
notifyActiveListeners();
|
|
115
139
|
}
|
|
116
140
|
return nextRendererId;
|
|
117
141
|
},
|
|
118
|
-
on:
|
|
119
|
-
onCommitFiberRoot:
|
|
120
|
-
onCommitFiberUnmount:
|
|
121
|
-
onPostCommitFiberRoot:
|
|
142
|
+
on: noOp,
|
|
143
|
+
onCommitFiberRoot: noOp,
|
|
144
|
+
onCommitFiberUnmount: noOp,
|
|
145
|
+
onPostCommitFiberRoot: noOp,
|
|
122
146
|
renderers,
|
|
123
147
|
supportsFiber: true,
|
|
124
148
|
supportsFlight: true,
|
|
@@ -134,137 +158,110 @@ export const installRDTHook = (onActive?: () => unknown): ReactDevToolsGlobalHoo
|
|
|
134
158
|
if (newHook && typeof newHook === "object") {
|
|
135
159
|
const ourRenderers = rdtHook.renderers;
|
|
136
160
|
rdtHook = newHook;
|
|
161
|
+
const nextRenderers = getRendererMap(rdtHook);
|
|
137
162
|
if (ourRenderers.size > 0) {
|
|
138
|
-
ourRenderers.forEach((renderer,
|
|
163
|
+
ourRenderers.forEach((renderer, rendererId) => {
|
|
139
164
|
_renderers.add(renderer);
|
|
140
|
-
|
|
141
|
-
newHook.renderers.set(id, renderer);
|
|
165
|
+
nextRenderers.set(rendererId, renderer);
|
|
142
166
|
});
|
|
167
|
+
}
|
|
168
|
+
if (ourRenderers.size > 0 || rdtHookReplaceListeners.size > 0) {
|
|
143
169
|
patchRDTHook(onActive);
|
|
144
170
|
}
|
|
171
|
+
notifyRDTHookReplaceListeners(rdtHook);
|
|
145
172
|
}
|
|
146
173
|
},
|
|
147
174
|
});
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
175
|
+
if (typeof window !== "undefined") {
|
|
176
|
+
// HACK: The DevTools extension uses hasOwnProperty to decide whether it may replace an earlier hook.
|
|
177
|
+
const originalWindowHasOwnProperty = window.hasOwnProperty.bind(window);
|
|
178
|
+
const originalHasOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(
|
|
179
|
+
window,
|
|
180
|
+
"hasOwnProperty",
|
|
181
|
+
);
|
|
182
|
+
let didRunHasOwnPropertyHack = false;
|
|
183
|
+
const restoreWindowHasOwnProperty = (): void => {
|
|
184
|
+
if (originalHasOwnPropertyDescriptor) {
|
|
185
|
+
objectDefineProperty(window, "hasOwnProperty", originalHasOwnPropertyDescriptor);
|
|
186
|
+
} else {
|
|
187
|
+
Reflect.deleteProperty(window, "hasOwnProperty");
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
objectDefineProperty(window, "hasOwnProperty", {
|
|
191
|
+
configurable: true,
|
|
192
|
+
value: (...propertyKeys: [PropertyKey]) => {
|
|
193
|
+
if (!didRunHasOwnPropertyHack && propertyKeys[0] === "__REACT_DEVTOOLS_GLOBAL_HOOK__") {
|
|
194
|
+
didRunHasOwnPropertyHack = true;
|
|
195
|
+
restoreWindowHasOwnProperty();
|
|
158
196
|
globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__ = undefined;
|
|
159
|
-
|
|
160
|
-
hasRanHack = true;
|
|
161
|
-
return -0;
|
|
197
|
+
return false;
|
|
162
198
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
199
|
+
return originalWindowHasOwnProperty(...propertyKeys);
|
|
200
|
+
},
|
|
201
|
+
writable: true,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
168
204
|
} catch {
|
|
169
205
|
patchRDTHook(onActive);
|
|
170
206
|
}
|
|
171
207
|
return rdtHook;
|
|
172
208
|
};
|
|
173
209
|
|
|
174
|
-
export const patchRDTHook = (onActive?:
|
|
210
|
+
export const patchRDTHook = (onActive?: ActiveListener): void => {
|
|
175
211
|
if (onActive) {
|
|
176
212
|
_onActiveListeners.add(onActive);
|
|
177
213
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (rdtHook.renderers.size) {
|
|
194
|
-
rdtHook._instrumentationIsActive = true;
|
|
195
|
-
_onActiveListeners.forEach((listener) => listener());
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
const prevInject = rdtHook.inject;
|
|
199
|
-
const isRefresh = isReactRefresh(rdtHook);
|
|
200
|
-
if (isRefresh && !isReactDevtools) {
|
|
201
|
-
isReactRefreshOverride = true;
|
|
202
|
-
// but since the underlying implementation doens't care,
|
|
203
|
-
// it's ok: https://github.com/facebook/react/blob/18eaf51bd51fed8dfed661d64c306759101d0bfd/packages/react-refresh/src/ReactFreshRuntime.js#L430
|
|
204
|
-
const injectedRendererId = rdtHook.inject({
|
|
205
|
-
scheduleRefresh() {},
|
|
206
|
-
} as unknown as ReactRenderer);
|
|
207
|
-
if (injectedRendererId) {
|
|
208
|
-
rdtHook._instrumentationIsActive = true;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
rdtHook.inject = (renderer) => {
|
|
212
|
-
const rendererId = prevInject(renderer);
|
|
213
|
-
_renderers.add(renderer);
|
|
214
|
-
if (isRefresh) {
|
|
215
|
-
// react refresh doesn't inject this properly
|
|
216
|
-
// https://github.com/facebook/react/blob/18eaf51bd51fed8dfed661d64c306759101d0bfd/packages/react-refresh/src/ReactFreshRuntime.js#L430
|
|
217
|
-
rdtHook.renderers.set(rendererId, renderer);
|
|
218
|
-
}
|
|
219
|
-
rdtHook._instrumentationIsActive = true;
|
|
220
|
-
_onActiveListeners.forEach((listener) => listener());
|
|
221
|
-
return rendererId;
|
|
222
|
-
};
|
|
214
|
+
let didNotifyActiveListeners = false;
|
|
215
|
+
const rdtHook = globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
|
216
|
+
if (!rdtHook) return;
|
|
217
|
+
const renderers = getRendererMap(rdtHook);
|
|
218
|
+
if (!rdtHook._instrumentationSource) {
|
|
219
|
+
rdtHook.checkDCE = checkDCE;
|
|
220
|
+
rdtHook.supportsFiber = true;
|
|
221
|
+
rdtHook.supportsFlight = true;
|
|
222
|
+
rdtHook.hasUnsupportedRendererAttached = false;
|
|
223
|
+
rdtHook._instrumentationSource = BIPPY_INSTRUMENTATION_STRING;
|
|
224
|
+
rdtHook._instrumentationIsActive = false;
|
|
225
|
+
// HACK: DevTools detection must happen after setting the source to avoid recursive patching.
|
|
226
|
+
const isReactDevtools = isRealReactDevtools(rdtHook);
|
|
227
|
+
if (!isReactDevtools) {
|
|
228
|
+
rdtHook.on = noOp;
|
|
223
229
|
}
|
|
224
|
-
if (
|
|
225
|
-
|
|
226
|
-
rdtHook._instrumentationIsActive
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
) {
|
|
230
|
-
onActive?.();
|
|
230
|
+
if (renderers.size) {
|
|
231
|
+
renderers.forEach((renderer) => _renderers.add(renderer));
|
|
232
|
+
rdtHook._instrumentationIsActive = true;
|
|
233
|
+
notifyActiveListeners();
|
|
234
|
+
didNotifyActiveListeners = true;
|
|
231
235
|
}
|
|
232
|
-
|
|
236
|
+
const previousInject = rdtHook.inject;
|
|
237
|
+
rdtHook.inject = (renderer) => {
|
|
238
|
+
const rendererId = previousInject.call(rdtHook, renderer);
|
|
239
|
+
_renderers.add(renderer);
|
|
240
|
+
renderers.set(rendererId, renderer);
|
|
241
|
+
if (!rdtHook._instrumentationIsActive) {
|
|
242
|
+
rdtHook._instrumentationIsActive = true;
|
|
243
|
+
notifyActiveListeners();
|
|
244
|
+
}
|
|
245
|
+
return rendererId;
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if (!didNotifyActiveListeners && (renderers.size || rdtHook._instrumentationIsActive)) {
|
|
249
|
+
onActive?.();
|
|
250
|
+
}
|
|
233
251
|
};
|
|
234
252
|
|
|
235
253
|
export const hasRDTHook = (): boolean => {
|
|
236
|
-
return
|
|
254
|
+
return Object.hasOwn(globalThis, "__REACT_DEVTOOLS_GLOBAL_HOOK__");
|
|
237
255
|
};
|
|
238
256
|
|
|
239
257
|
/**
|
|
240
258
|
* Returns the current React DevTools global hook.
|
|
241
259
|
*/
|
|
242
|
-
export const getRDTHook = (onActive?:
|
|
260
|
+
export const getRDTHook = (onActive?: ActiveListener): ReactDevToolsGlobalHook => {
|
|
243
261
|
if (!hasRDTHook()) {
|
|
244
262
|
return installRDTHook(onActive);
|
|
245
263
|
}
|
|
246
264
|
|
|
247
265
|
patchRDTHook(onActive);
|
|
248
|
-
|
|
249
|
-
return globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__ as ReactDevToolsGlobalHook;
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
export const isClientEnvironment = (): boolean => {
|
|
253
|
-
return Boolean(
|
|
254
|
-
typeof window !== "undefined" &&
|
|
255
|
-
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
256
|
-
(window.document?.createElement || window.navigator?.product === "ReactNative"),
|
|
257
|
-
);
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Usually used purely for side effect
|
|
262
|
-
*/
|
|
263
|
-
export const safelyInstallRDTHook = () => {
|
|
264
|
-
try {
|
|
265
|
-
// __REACT_DEVTOOLS_GLOBAL_HOOK__ must exist before React is ever executed
|
|
266
|
-
if (isClientEnvironment()) {
|
|
267
|
-
getRDTHook();
|
|
268
|
-
}
|
|
269
|
-
} catch {}
|
|
266
|
+
return globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__ ?? installRDTHook(onActive);
|
|
270
267
|
};
|