@@ -59,18 +59,32 @@ function warnAboutTracing(name: string): void {
59
59
}
60
60
61
61
/**
62
- * getInitActivity pushes activity based on React component mount
62
+ * pushActivity creates an new react activity
63
63
* @param name displayName of component that started activity
64
64
*/
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 ;
72
68
}
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 ) ;
74
88
} ;
75
89
76
90
export type ProfilerProps = {
@@ -79,13 +93,13 @@ export type ProfilerProps = {
79
93
80
94
class Profiler extends React . Component < ProfilerProps > {
81
95
public activity : number | null = null ;
82
- public tracingIntegration : Integration | null = getTracingIntegration ( ) ;
83
96
84
97
public constructor ( props : ProfilerProps ) {
85
98
super ( props ) ;
86
99
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 ) ;
89
103
} else {
90
104
warnAboutTracing ( this . props . name ) ;
91
105
}
@@ -103,12 +117,7 @@ class Profiler extends React.Component<ProfilerProps> {
103
117
}
104
118
105
119
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 ) ;
112
121
this . activity = null ;
113
122
} ;
114
123
@@ -149,7 +158,7 @@ function withProfiler<P extends object>(WrappedComponent: React.ComponentType<P>
149
158
* @param name displayName of component being profiled
150
159
*/
151
160
function useProfiler ( name : string ) : void {
152
- const [ activity ] = React . useState ( ( ) => getInitActivity ( name ) ) ;
161
+ const [ activity ] = React . useState ( ( ) => pushActivity ( name ) ) ;
153
162
154
163
React . useEffect ( ( ) => {
155
164
afterNextFrame ( ( ) => {
0 commit comments