Skip to content

Commit 8bc23ce

Browse files
committed
ref: extract into popActivity
1 parent c255137 commit 8bc23ce

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

packages/react/src/profiler.tsx

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,32 @@ function warnAboutTracing(name: string): void {
5959
}
6060

6161
/**
62-
* getInitActivity pushes activity based on React component mount
62+
* pushActivity creates an new react activity
6363
* @param name displayName of component that started activity
6464
*/
65-
const getInitActivity = (name: string): number | null => {
66-
if (globalTracingIntegration !== null) {
67-
// tslint:disable-next-line:no-unsafe-any
68-
return (globalTracingIntegration as any).constructor.pushActivity(name, {
69-
description: `<${name}>`,
70-
op: 'react',
71-
});
65+
const pushActivity = (name: string): number | null => {
66+
if (globalTracingIntegration === null) {
67+
return null;
7268
}
73-
return null;
69+
70+
// tslint:disable-next-line:no-unsafe-any
71+
return (globalTracingIntegration as any).constructor.pushActivity(name, {
72+
description: `<${name}>`,
73+
op: 'react',
74+
});
75+
};
76+
77+
/**
78+
* popActivity removes a React activity if it exists
79+
* @param activity id of activity that is being popped
80+
*/
81+
const popActivity = (activity: number | null): void => {
82+
if (activity === null || globalTracingIntegration === null) {
83+
return;
84+
}
85+
86+
// tslint:disable-next-line:no-unsafe-any
87+
(globalTracingIntegration as any).constructor.popActivity(activity);
7488
};
7589

7690
export type ProfilerProps = {
@@ -79,13 +93,13 @@ export type ProfilerProps = {
7993

8094
class Profiler extends React.Component<ProfilerProps> {
8195
public activity: number | null = null;
82-
public tracingIntegration: Integration | null = getTracingIntegration();
8396

8497
public constructor(props: ProfilerProps) {
8598
super(props);
8699

87-
if (this.tracingIntegration) {
88-
this.activity = getInitActivity(this.props.name);
100+
// We should check for tracing integration per Profiler instance
101+
if (getTracingIntegration()) {
102+
this.activity = pushActivity(this.props.name);
89103
} else {
90104
warnAboutTracing(this.props.name);
91105
}
@@ -103,12 +117,7 @@ class Profiler extends React.Component<ProfilerProps> {
103117
}
104118

105119
public finishProfile = () => {
106-
if (!this.activity || !this.tracingIntegration) {
107-
return;
108-
}
109-
110-
// tslint:disable-next-line:no-unsafe-any
111-
(this.tracingIntegration as any).constructor.popActivity(this.activity);
120+
popActivity(this.activity);
112121
this.activity = null;
113122
};
114123

@@ -149,7 +158,7 @@ function withProfiler<P extends object>(WrappedComponent: React.ComponentType<P>
149158
* @param name displayName of component being profiled
150159
*/
151160
function useProfiler(name: string): void {
152-
const [activity] = React.useState(() => getInitActivity(name));
161+
const [activity] = React.useState(() => pushActivity(name));
153162

154163
React.useEffect(() => {
155164
afterNextFrame(() => {

0 commit comments

Comments
 (0)