signal-desktop/ts/components/Profiler.tsx

58 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-08-11 23:06:20 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
import React from 'react';
import * as log from '../logging/log';
2021-08-11 23:06:20 +00:00
type InternalPropsType = Readonly<{
id: string;
children: ReactNode;
onRender(
id: string,
phase: 'mount' | 'update',
actualDuration: number,
baseDuration: number,
startTime: number,
commitTime: number,
interactions: Set<unknown>
): void;
}>;
2022-11-18 00:45:19 +00:00
function Fallback({ children }: InternalPropsType): JSX.Element {
2021-08-11 23:06:20 +00:00
return <>{children}</>;
2022-11-18 00:45:19 +00:00
}
2021-08-11 23:06:20 +00:00
const BaseProfiler: React.FC<InternalPropsType> =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(React as any).unstable_Profiler || Fallback;
export type PropsType = Readonly<{
id: string;
children: ReactNode;
}>;
const onRender: InternalPropsType['onRender'] = (
id,
phase,
actual,
base,
start,
commit
) => {
log.info(
2021-08-11 23:06:20 +00:00
`Profiler.tsx(${id}): actual=${actual.toFixed(1)}ms phase=${phase} ` +
`base=${base.toFixed(1)}ms start=${start.toFixed(1)}ms ` +
`commit=${commit.toFixed(1)}ms`
);
};
2022-11-18 00:45:19 +00:00
export function Profiler({ id, children }: PropsType): JSX.Element {
2021-08-11 23:06:20 +00:00
return (
<BaseProfiler id={id} onRender={onRender}>
{children}
</BaseProfiler>
);
2022-11-18 00:45:19 +00:00
}