Skip to content

Commit dc0d9ba

Browse files
committed
ref: add disabled and extra activities:
1 parent 8bc23ce commit dc0d9ba

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

packages/react/src/profiler.tsx

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub } from '@sentry/browser';
2-
import { Integration, IntegrationClass } from '@sentry/types';
2+
import { Integration, IntegrationClass, Span } from '@sentry/types';
33
import { logger } from '@sentry/utils';
44
import * as hoistNonReactStatic from 'hoist-non-react-statics';
55
import * as React from 'react';
@@ -88,20 +88,41 @@ const popActivity = (activity: number | null): void => {
8888
};
8989

9090
export type ProfilerProps = {
91+
// The name of the component being profiled.
9192
name: string;
93+
// If the Profiler is disabled. False by default.
94+
disabled?: boolean;
9295
};
9396

97+
/**
98+
* The Profiler component leverages Sentry's Tracing integration to generate
99+
* spans based on component lifecycles.
100+
*/
94101
class Profiler extends React.Component<ProfilerProps> {
95102
public activity: number | null = null;
103+
// The activity representing when a component was mounted onto a page.
104+
public mountInfo: {
105+
activity: number | null;
106+
span: Span | null;
107+
} = {
108+
activity: null,
109+
span: null,
110+
};
111+
// The activity representing how long a component was on the page.
112+
public visibleActivity: number | null = null;
96113

97114
public constructor(props: ProfilerProps) {
98115
super(props);
116+
const { name, disabled = false } = this.props;
117+
118+
if (disabled) {
119+
return;
120+
}
99121

100-
// We should check for tracing integration per Profiler instance
101122
if (getTracingIntegration()) {
102-
this.activity = pushActivity(this.props.name);
123+
this.activity = pushActivity(name);
103124
} else {
104-
warnAboutTracing(this.props.name);
125+
warnAboutTracing(name);
105126
}
106127
}
107128

@@ -117,8 +138,10 @@ class Profiler extends React.Component<ProfilerProps> {
117138
}
118139

119140
public finishProfile = () => {
120-
popActivity(this.activity);
121-
this.activity = null;
141+
afterNextFrame(() => {
142+
popActivity(this.activity);
143+
this.activity = null;
144+
});
122145
};
123146

124147
public render(): React.ReactNode {
@@ -131,13 +154,17 @@ class Profiler extends React.Component<ProfilerProps> {
131154
* component in a {@link Profiler} component.
132155
*
133156
* @param WrappedComponent component that is wrapped by Profiler
134-
* @param name displayName of component being profiled
157+
* @param options the {@link ProfilerProps} you can pass into the Profiler
135158
*/
136-
function withProfiler<P extends object>(WrappedComponent: React.ComponentType<P>, name?: string): React.FC<P> {
137-
const componentDisplayName = name || WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
159+
function withProfiler<P extends object>(
160+
WrappedComponent: React.ComponentType<P>,
161+
options?: Partial<ProfilerProps>,
162+
): React.FC<P> {
163+
const componentDisplayName =
164+
(options && options.name) || WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
138165

139166
const Wrapped: React.FC<P> = (props: P) => (
140-
<Profiler name={componentDisplayName}>
167+
<Profiler name={componentDisplayName} disabled={options && options.disabled}>
141168
<WrappedComponent {...props} />
142169
</Profiler>
143170
);

0 commit comments

Comments
 (0)