Skip to content

Commit 239f2aa

Browse files
authored
minor optimizations (#227)
1 parent 91d587e commit 239f2aa

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

packages/scan/src/core/monitor/index.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
getTimings,
66
isCompositeFiber,
77
} from 'bippy';
8-
import { useEffect } from 'react';
8+
import { type FC, useEffect } from 'react';
99
import {
1010
type MonitoringOptions,
1111
ReactScanInternals,
@@ -43,23 +43,29 @@ export type MonitoringWithoutRouteProps = Omit<
4343
'route' | 'path'
4444
>;
4545

46-
export const Monitoring = ({
46+
const DEFAULT_URL = 'https://monitoring.react-scan.com/api/v1/ingest';
47+
48+
function noopCatch() {
49+
return null;
50+
}
51+
52+
export const Monitoring: FC<MonitoringProps> = ({
4753
url,
4854
apiKey,
4955
params,
5056
path = null, // path passed down would be reactive
5157
route = null,
5258
commit = null,
5359
branch = null,
54-
}: MonitoringProps) => {
60+
}) => {
5561
if (!apiKey)
5662
throw new Error('Please provide a valid API key for React Scan monitoring');
57-
url ??= 'https://monitoring.react-scan.com/api/v1/ingest';
63+
url ??= DEFAULT_URL;
5864

5965
Store.monitor.value ??= {
6066
pendingRequests: 0,
6167
interactions: [],
62-
session: getSession({ commit, branch }).catch(() => null),
68+
session: getSession({ commit, branch }).catch(noopCatch),
6369
url,
6470
apiKey,
6571
route,
@@ -90,7 +96,7 @@ export const scanMonitoring = (options: MonitoringOptions) => {
9096

9197
let flushInterval: ReturnType<typeof setInterval>;
9298

93-
export const startMonitoring = () => {
99+
export const startMonitoring = (): void => {
94100
if (!Store.monitor.value) {
95101
if (process.env.NODE_ENV !== 'production') {
96102
throw new Error(
@@ -148,7 +154,7 @@ export const startMonitoring = () => {
148154
const aggregateComponentRenderToInteraction = (
149155
fiber: Fiber,
150156
renders: Array<Render>,
151-
) => {
157+
): void => {
152158
const monitor = Store.monitor.value;
153159
if (!monitor || !monitor.interactions || monitor.interactions.length === 0)
154160
return;

packages/scan/src/core/monitor/performance.ts

+14-20
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ const minifiedPatterns = [
6565
];
6666

6767
const isMinified = (name: string): boolean => {
68-
if (!name || typeof name !== 'string') {
69-
return true;
70-
}
71-
7268
for (let i = 0; i < minifiedPatterns.length; i++) {
7369
if (minifiedPatterns[i].test(name)) return true;
7470
}
@@ -149,8 +145,7 @@ const getFirstNamedAncestorCompositeFiber = (element: Element) => {
149145
while (!parentCompositeFiber && curr.parentElement) {
150146
curr = curr.parentElement;
151147

152-
const { parentCompositeFiber: fiber } =
153-
getCompositeComponentFromElement(curr);
148+
const fiber = getCompositeComponentFromElement(curr).parentCompositeFiber;
154149

155150
if (!fiber) {
156151
continue;
@@ -162,22 +157,18 @@ const getFirstNamedAncestorCompositeFiber = (element: Element) => {
162157
return parentCompositeFiber;
163158
};
164159

165-
let unsubscribeTrackVisibilityChange: (() => void) | undefined;
166160
// fixme: compress me if this stays here for bad interaction time checks
167161
let lastVisibilityHiddenAt: number | 'never-hidden' = 'never-hidden';
168162

163+
const onVisibilityChange = () => {
164+
if (document.hidden) {
165+
lastVisibilityHiddenAt = Date.now();
166+
}
167+
};
168+
169169
const trackVisibilityChange = () => {
170-
unsubscribeTrackVisibilityChange?.();
171-
const onVisibilityChange = () => {
172-
if (document.hidden) {
173-
lastVisibilityHiddenAt = Date.now();
174-
}
175-
};
170+
document.removeEventListener('visibilitychange', onVisibilityChange);
176171
document.addEventListener('visibilitychange', onVisibilityChange);
177-
178-
unsubscribeTrackVisibilityChange = () => {
179-
document.removeEventListener('visibilitychange', onVisibilityChange);
180-
};
181172
};
182173

183174
// todo: update monitoring api to expose filters for component names
@@ -226,13 +217,16 @@ export function initPerformanceMonitoring(options?: Partial<PathFilters>) {
226217
};
227218
}
228219

220+
const POINTER_EVENTS = new Set(['pointerdown', 'pointerup', 'click']);
221+
const KEYBOARD_EVENTS = new Set(['keydown', 'keyup']);
222+
229223
const getInteractionType = (
230224
eventName: string,
231225
): 'pointer' | 'keyboard' | null => {
232-
if (['pointerdown', 'pointerup', 'click'].includes(eventName)) {
226+
if (POINTER_EVENTS.has(eventName)) {
233227
return 'pointer';
234228
}
235-
if (['keydown', 'keyup'].includes(eventName)) {
229+
if (KEYBOARD_EVENTS.has(eventName)) {
236230
return 'keyboard';
237231
}
238232
return null;
@@ -323,5 +317,5 @@ const setupPerformanceListener = (
323317
/* Should collect error logs*/
324318
}
325319

326-
return () => po.disconnect();
320+
return po.disconnect.bind(po);
327321
};

packages/scan/src/core/monitor/utils.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ interface ExtendedNavigator extends Navigator {
1212
deviceMemory?: number;
1313
}
1414

15+
const MOBILE_PATTERN =
16+
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
17+
18+
const TABLET_PATTERN = /iPad|Tablet/i;
19+
1520
const getDeviceType = () => {
1621
const userAgent = navigator.userAgent;
1722

18-
if (
19-
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
20-
userAgent,
21-
)
22-
) {
23+
if (MOBILE_PATTERN.test(userAgent)) {
2324
return Device.MOBILE;
2425
}
25-
if (/iPad|Tablet/i.test(userAgent)) {
26+
if (TABLET_PATTERN.test(userAgent)) {
2627
return Device.TABLET;
2728
}
2829
return Device.DESKTOP;
@@ -32,9 +33,7 @@ const getDeviceType = () => {
3233
* Measure layout time
3334
*/
3435
export const doubleRAF = (callback: (...args: unknown[]) => void) => {
35-
return requestAnimationFrame(() => {
36-
requestAnimationFrame(callback);
37-
});
36+
return requestAnimationFrame(requestAnimationFrame.bind(window, callback));
3837
};
3938

4039
export const generateId = () => {

0 commit comments

Comments
 (0)