Skip to content

Commit c255137

Browse files
committed
ref: use global tracing integration
1 parent e997d54 commit c255137

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

packages/react/src/profiler.tsx

+33-17
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,37 @@ function afterNextFrame(callback: Function): void {
3939
timeout = window.setTimeout(done, 100);
4040
}
4141

42+
let globalTracingIntegration: Integration | null = null;
43+
const getTracingIntegration = () => {
44+
if (globalTracingIntegration) {
45+
return globalTracingIntegration;
46+
}
47+
48+
globalTracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
49+
return globalTracingIntegration;
50+
};
51+
52+
/** JSDOC */
53+
function warnAboutTracing(name: string): void {
54+
if (globalTracingIntegration === null) {
55+
logger.warn(
56+
`Unable to profile component ${name} due to invalid Tracing Integration. Please make sure to setup the Tracing integration.`,
57+
);
58+
}
59+
}
60+
4261
/**
4362
* getInitActivity pushes activity based on React component mount
4463
* @param name displayName of component that started activity
4564
*/
4665
const getInitActivity = (name: string): number | null => {
47-
const tracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
48-
49-
if (tracingIntegration !== null) {
66+
if (globalTracingIntegration !== null) {
5067
// tslint:disable-next-line:no-unsafe-any
51-
return (tracingIntegration as any).constructor.pushActivity(name, {
68+
return (globalTracingIntegration as any).constructor.pushActivity(name, {
5269
description: `<${name}>`,
5370
op: 'react',
5471
});
5572
}
56-
57-
logger.warn(
58-
`Unable to profile component ${name} due to invalid Tracing Integration. Please make sure to setup the Tracing integration.`,
59-
);
6073
return null;
6174
};
6275

@@ -65,11 +78,17 @@ export type ProfilerProps = {
6578
};
6679

6780
class Profiler extends React.Component<ProfilerProps> {
68-
public activity: number | null;
81+
public activity: number | null = null;
82+
public tracingIntegration: Integration | null = getTracingIntegration();
83+
6984
public constructor(props: ProfilerProps) {
7085
super(props);
7186

72-
this.activity = getInitActivity(this.props.name);
87+
if (this.tracingIntegration) {
88+
this.activity = getInitActivity(this.props.name);
89+
} else {
90+
warnAboutTracing(this.props.name);
91+
}
7392
}
7493

7594
// If a component mounted, we can finish the mount activity.
@@ -84,16 +103,13 @@ class Profiler extends React.Component<ProfilerProps> {
84103
}
85104

86105
public finishProfile = () => {
87-
if (!this.activity) {
106+
if (!this.activity || !this.tracingIntegration) {
88107
return;
89108
}
90109

91-
const tracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
92-
if (tracingIntegration !== null) {
93-
// tslint:disable-next-line:no-unsafe-any
94-
(tracingIntegration as any).constructor.popActivity(this.activity);
95-
this.activity = null;
96-
}
110+
// tslint:disable-next-line:no-unsafe-any
111+
(this.tracingIntegration as any).constructor.popActivity(this.activity);
112+
this.activity = null;
97113
};
98114

99115
public render(): React.ReactNode {

0 commit comments

Comments
 (0)