@@ -3,6 +3,9 @@ import { Integration, IntegrationClass } from '@sentry/types';
3
3
import { logger } from '@sentry/utils' ;
4
4
import * as hoistNonReactStatic from 'hoist-non-react-statics' ;
5
5
import * as React from 'react' ;
6
+ // tslint:disable: no-implicit-dependencies
7
+ // @ts -ignore
8
+ import * as kap from 'scheduler' ;
6
9
7
10
export const UNKNOWN_COMPONENT = 'unknown' ;
8
11
@@ -39,6 +42,35 @@ function afterNextFrame(callback: Function): void {
39
42
timeout = window . setTimeout ( done , 100 ) ;
40
43
}
41
44
45
+ // This is only active in development mode and in profiling mode
46
+ // Learn how to do that here: https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977
47
+ function isProfilingModeOn ( ) : boolean {
48
+ // function Hello() {
49
+ // return /*#__PURE__*/ React.createElement('div', null);
50
+ // }
51
+
52
+ // @ts -ignore
53
+ console . log ( kap ) ;
54
+ // const lol = React.createElement(React.Profiler, { id: 'sdf', onRender: () => {} });
55
+
56
+ // @ts -ignore
57
+ // console.log(lol);
58
+ // function Kappa() {
59
+ // return /*#__PURE__*/ React.createElement(Hello, null);
60
+ // }
61
+
62
+ // I wish React exposed this better
63
+ // tslint:disable-next-line: no-unsafe-any
64
+ // console.log(Kappa());
65
+ // tslint:disable-next-line: no-unsafe-any
66
+ // if (fake._owner && fake._owner.actualDuration) {
67
+ // console.log('YES ITS ON');
68
+ // return true;
69
+ // }
70
+
71
+ return false ;
72
+ }
73
+
42
74
const getInitActivity = ( name : string ) : number | null => {
43
75
const tracingIntegration = getCurrentHub ( ) . getIntegration ( TRACING_GETTER ) ;
44
76
@@ -62,20 +94,45 @@ export type ProfilerProps = {
62
94
63
95
class Profiler extends React . Component < ProfilerProps > {
64
96
public activity : number | null ;
97
+ public hasProfilingMode : boolean = false ;
98
+
65
99
public constructor ( props : ProfilerProps ) {
66
100
super ( props ) ;
67
101
102
+ // TODO: Extract this out into global state
103
+ this . hasProfilingMode = isProfilingModeOn ( ) ;
104
+
68
105
this . activity = getInitActivity ( this . props . name ) ;
69
106
}
70
107
71
108
public componentDidMount ( ) : void {
72
- afterNextFrame ( this . finishProfile ) ;
109
+ if ( ! this . hasProfilingMode ) {
110
+ afterNextFrame ( this . finishProfile ) ;
111
+ }
73
112
}
74
113
75
114
public componentWillUnmount ( ) : void {
76
- afterNextFrame ( this . finishProfile ) ;
115
+ if ( ! this . hasProfilingMode ) {
116
+ afterNextFrame ( this . finishProfile ) ;
117
+ }
77
118
}
78
119
120
+ // TODO: Figure out how to use these values.
121
+ // We should be generating spans from these!
122
+ // > React calls this function any time a component within the profiled tree “commits” an update
123
+ // See: https://reactjs.org/docs/profiler.html#onrender-callback
124
+ // id: string,
125
+ // phase: 'mount' | 'update',
126
+ // actualDuration: number,
127
+ // baseDuration: number,
128
+ // startTime: number,
129
+ // commitTime: number,
130
+ public handleProfilerRender = ( ..._args : any [ ] ) => {
131
+ console . log ( 'SDJFLSJDF' ) ;
132
+ console . table ( _args ) ;
133
+ afterNextFrame ( this . finishProfile ) ;
134
+ } ;
135
+
79
136
public finishProfile = ( ) => {
80
137
if ( ! this . activity ) {
81
138
return ;
@@ -90,6 +147,15 @@ class Profiler extends React.Component<ProfilerProps> {
90
147
} ;
91
148
92
149
public render ( ) : React . ReactNode {
150
+ const { name } = this . props ;
151
+ if ( this . hasProfilingMode ) {
152
+ return (
153
+ < React . Profiler id = { name } onRender = { this . handleProfilerRender } >
154
+ { this . props . children }
155
+ </ React . Profiler >
156
+ ) ;
157
+ }
158
+
93
159
return this . props . children ;
94
160
}
95
161
}
0 commit comments