Skip to content

Commit 995da2d

Browse files
committed
delete some code
1 parent 33337c2 commit 995da2d

File tree

1 file changed

+37
-56
lines changed

1 file changed

+37
-56
lines changed

packages/react/src/profiler.tsx

+37-56
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,6 @@ const TRACING_GETTER = ({
1010
id: 'Tracing',
1111
} as any) as IntegrationClass<Integration>;
1212

13-
/**
14-
*
15-
* Based on implementation from Preact:
16-
* https:github.com/preactjs/preact/blob/9a422017fec6dab287c77c3aef63c7b2fef0c7e1/hooks/src/index.js#L301-L313
17-
*
18-
* Schedule a callback to be invoked after the browser has a chance to paint a new frame.
19-
* Do this by combining requestAnimationFrame (rAF) + setTimeout to invoke a callback after
20-
* the next browser frame.
21-
*
22-
* Also, schedule a timeout in parallel to the the rAF to ensure the callback is invoked
23-
* even if RAF doesn't fire (for example if the browser tab is not visible)
24-
*
25-
* This is what we use to tell if a component activity has finished
26-
*
27-
*/
28-
function afterNextFrame(callback: Function): void {
29-
let timeout: number | undefined;
30-
let raf: number;
31-
32-
const done = () => {
33-
window.clearTimeout(timeout);
34-
window.cancelAnimationFrame(raf);
35-
window.setTimeout(callback);
36-
};
37-
38-
raf = window.requestAnimationFrame(done);
39-
timeout = window.setTimeout(done, 100);
40-
}
41-
4213
let globalTracingIntegration: Integration | null = null;
4314
const getTracingIntegration = () => {
4415
if (globalTracingIntegration) {
@@ -119,6 +90,8 @@ export type ProfilerProps = {
11990
disabled?: boolean;
12091
// If component updates should be displayed as spans. False by default.
12192
generateUpdateSpans?: boolean;
93+
// If time component is on page should be displayed as spans. True by default.
94+
generateRenderSpans?: boolean;
12295
// props from child component
12396
updateProps: { [key: string]: any };
12497
};
@@ -131,14 +104,14 @@ class Profiler extends React.Component<ProfilerProps> {
131104
// The activity representing how long it takes to mount a component.
132105
public mountActivity: number | null = null;
133106
// The span of the mount activity
134-
public span: Span | undefined = undefined;
135-
// The activity representing how long a component was on the page.
136-
public renderActivity: number | null = null;
107+
public mountSpan: Span | undefined = undefined;
108+
// The span of the render
137109
public renderSpan: Span | undefined = undefined;
138110

139111
public static defaultProps: Partial<ProfilerProps> = {
140112
disabled: false,
141-
generateUpdateSpans: true,
113+
generateRenderSpans: true,
114+
generateUpdateSpans: false,
142115
};
143116

144117
public constructor(props: ProfilerProps) {
@@ -158,28 +131,26 @@ class Profiler extends React.Component<ProfilerProps> {
158131

159132
// If a component mounted, we can finish the mount activity.
160133
public componentDidMount(): void {
161-
// afterNextFrame(() => {
162-
this.span = getActivitySpan(this.mountActivity);
134+
this.mountSpan = getActivitySpan(this.mountActivity);
163135
popActivity(this.mountActivity);
164136
this.mountActivity = null;
165137

166138
// If we were able to obtain the spanId of the mount activity, we should set the
167139
// next activity as a child to the component mount activity.
168-
if (this.span) {
169-
this.renderSpan = this.span.startChild({
140+
if (this.mountSpan) {
141+
this.renderSpan = this.mountSpan.startChild({
170142
description: `<${this.props.name}>`,
171143
op: `react.render`,
172144
});
173145
}
174-
// });
175146
}
176147

177148
public componentDidUpdate(prevProps: ProfilerProps): void {
178-
if (prevProps.generateUpdateSpans && this.span && prevProps !== this.props) {
149+
if (prevProps.generateUpdateSpans && this.mountSpan && prevProps.updateProps !== this.props.updateProps) {
179150
const changedProps = Object.keys(prevProps).filter(k => prevProps.updateProps[k] !== this.props.updateProps[k]);
180151
if (changedProps.length > 0) {
181152
const now = timestampWithMs();
182-
const updateSpan = this.span.startChild({
153+
const updateSpan = this.mountSpan.startChild({
183154
description: `<${prevProps.name}>`,
184155
endTimestamp: now,
185156
op: `react.update`,
@@ -193,13 +164,9 @@ class Profiler extends React.Component<ProfilerProps> {
193164

194165
// If a component doesn't mount, the render activity will be end when the
195166
public componentWillUnmount(): void {
196-
afterNextFrame(() => {
197-
if (this.renderSpan) {
198-
this.renderSpan.finish();
199-
}
200-
// popActivity(this.renderActivity);
201-
// this.renderActivity = null;
202-
});
167+
if (this.renderSpan) {
168+
this.renderSpan.finish();
169+
}
203170
}
204171

205172
public render(): React.ReactNode {
@@ -243,17 +210,31 @@ function withProfiler<P extends object>(
243210
* @param name displayName of component being profiled
244211
*/
245212
function useProfiler(name: string): void {
246-
const [activity] = React.useState(() => pushActivity(name, 'mount'));
213+
const [mountActivity] = React.useState(() => {
214+
if (getTracingIntegration()) {
215+
return pushActivity(name, 'mount');
216+
}
217+
218+
warnAboutTracing(name);
219+
return null;
220+
});
247221

248222
React.useEffect(() => {
249-
afterNextFrame(() => {
250-
popActivity(activity);
251-
const renderActivity = pushActivity(name, 'render');
252-
253-
return () => {
254-
popActivity(renderActivity);
255-
};
256-
});
223+
const mountSpan = getActivitySpan(mountActivity);
224+
popActivity(mountActivity);
225+
226+
const renderSpan = mountSpan
227+
? mountSpan.startChild({
228+
description: `<${name}>`,
229+
op: `react.render`,
230+
})
231+
: undefined;
232+
233+
return () => {
234+
if (renderSpan) {
235+
renderSpan.finish();
236+
}
237+
};
257238
}, []);
258239
}
259240

0 commit comments

Comments
 (0)