From 7d483f04fe5b446f517ee85f18ac74e6f03372d2 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Thu, 4 Apr 2019 00:03:53 +0200 Subject: [PATCH] [react] RE: Add unstable_Profiler (#34253) * feat(react): Add unstable_Profiler * OnRenderCallback -> ProfilerOnRenderCallback OnRenderCallback is to generic * Interaction -> SchedulerInteraction It's actually part of scheduler/tracing --- types/react/index.d.ts | 30 ++++++++++++++++++++++++++++++ types/react/test/tsx.tsx | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/types/react/index.d.ts b/types/react/index.d.ts index 1ec28d9a2e..bf5acd0682 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -22,6 +22,7 @@ // Paul Sherman // Saransh Kataria // Kanitkorn Sujautra +// Sebastian Silbermann // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -43,6 +44,15 @@ type NativeTransitionEvent = TransitionEvent; type NativeUIEvent = UIEvent; type NativeWheelEvent = WheelEvent; +/** + * defined in scheduler/tracing + */ +interface SchedulerInteraction { + id: number; + name: string; + timestamp: number; +} + // tslint:disable-next-line:export-just-namespace export = React; export as namespace React; @@ -356,6 +366,26 @@ declare namespace React { const Suspense: ExoticComponent; const version: string; + /** + * {@link https://github.com/bvaughn/rfcs/blob/profiler/text/0000-profiler.md#detailed-design | API} + */ + type ProfilerOnRenderCallback = ( + id: string, + phase: "mount" | "update", + actualDuration: number, + baseDuration: number, + startTime: number, + commitTime: number, + interactions: Set, + ) => void; + interface ProfilerProps { + children?: ReactNode; + id: string; + onRender: ProfilerOnRenderCallback; + } + + const unstable_Profiler: ExoticComponent; + // // Component API // ---------------------------------------------------------------------- diff --git a/types/react/test/tsx.tsx b/types/react/test/tsx.tsx index f88db9ae7a..bd21d316ca 100644 --- a/types/react/test/tsx.tsx +++ b/types/react/test/tsx.tsx @@ -321,6 +321,41 @@ const ForwardRef3 = React.forwardRef( ; ; +const Profiler = React.unstable_Profiler; + +// 'id' is missing +; // $ExpectError +// 'onRender' is missing +; // $ExpectError +// 'number' is not assignable to 'string' +; // $ExpectError + + { + const message = `${id} ${phase} took ${actualDuration.toFixed(2)}s actual, ${baseDuration.toFixed(2)}s base`; + + const commitMessage = `commit started ${startTime.toFixed(2)} within ${commitTime}`; + + const interactionsSummary = Array.from(interactions) + .map(interaction => { + return `${interaction.id}: '${interaction.name}' started at ${interaction.timestamp.toFixed(2)}`; + }) + .join("\n"); + const interactionMessage = `there were ${interactions.size} interactions:\n${interactionsSummary}`; + }} +> +
+; + type ImgProps = React.ComponentProps<'img'>; // $ExpectType "async" | "auto" | "sync" | undefined type ImgPropsDecoding = ImgProps['decoding'];