@@ -3,6 +3,9 @@ import { Integration, IntegrationClass } from '@sentry/types';
33import { logger } from '@sentry/utils' ;
44import * as hoistNonReactStatic from 'hoist-non-react-statics' ;
55import * as React from 'react' ;
6+ // tslint:disable: no-implicit-dependencies
7+ // @ts -ignore
8+ import * as kap from 'scheduler' ;
69
710export const UNKNOWN_COMPONENT = 'unknown' ;
811
@@ -39,6 +42,35 @@ function afterNextFrame(callback: Function): void {
3942 timeout = window . setTimeout ( done , 100 ) ;
4043}
4144
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+
4274const getInitActivity = ( name : string ) : number | null => {
4375 const tracingIntegration = getCurrentHub ( ) . getIntegration ( TRACING_GETTER ) ;
4476
@@ -62,20 +94,45 @@ export type ProfilerProps = {
6294
6395class Profiler extends React . Component < ProfilerProps > {
6496 public activity : number | null ;
97+ public hasProfilingMode : boolean = false ;
98+
6599 public constructor ( props : ProfilerProps ) {
66100 super ( props ) ;
67101
102+ // TODO: Extract this out into global state
103+ this . hasProfilingMode = isProfilingModeOn ( ) ;
104+
68105 this . activity = getInitActivity ( this . props . name ) ;
69106 }
70107
71108 public componentDidMount ( ) : void {
72- afterNextFrame ( this . finishProfile ) ;
109+ if ( ! this . hasProfilingMode ) {
110+ afterNextFrame ( this . finishProfile ) ;
111+ }
73112 }
74113
75114 public componentWillUnmount ( ) : void {
76- afterNextFrame ( this . finishProfile ) ;
115+ if ( ! this . hasProfilingMode ) {
116+ afterNextFrame ( this . finishProfile ) ;
117+ }
77118 }
78119
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+
79136 public finishProfile = ( ) => {
80137 if ( ! this . activity ) {
81138 return ;
@@ -90,6 +147,15 @@ class Profiler extends React.Component<ProfilerProps> {
90147 } ;
91148
92149 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+
93159 return this . props . children ;
94160 }
95161}
0 commit comments