Skip to content

Commit 52e259c

Browse files
Brian Vaughnfacebook-github-bot
authored andcommitted
[PR] Add React.Profiler (React v16.9+)
Summary: * Profiler RFC: https://github.com/reactjs/rfcs/blob/master/text/0051-profiler.md * Profiler release documentation: reactjs/react.dev#2176 cc jbrown215 Pull Request resolved: #7979 Reviewed By: bvaughn Differential Revision: D16562521 Pulled By: jbrown215 fbshipit-source-id: 7afee058cfe159a30807d2c7f60966942dbac504
1 parent 66b3ab1 commit 52e259c

File tree

11 files changed

+702
-566
lines changed

11 files changed

+702
-566
lines changed

lib/react.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,28 @@ declare module react {
373373
inputs: ?$ReadOnlyArray<mixed>,
374374
): void;
375375

376+
declare export type Interaction = {
377+
name: string,
378+
timestamp: number,
379+
...
380+
};
381+
382+
declare type ProfilerOnRenderFnType = (
383+
id: string,
384+
phase: "mount" | "update",
385+
actualDuration: number,
386+
baseDuration: number,
387+
startTime: number,
388+
commitTime: number,
389+
interactions: Set<Interaction>,
390+
) => void;
391+
392+
declare export var Profiler: React$AbstractComponent<{|
393+
children?: React$Node,
394+
id: string,
395+
onRender: ProfilerOnRenderFnType,
396+
|}, void>;
397+
376398
declare export default {|
377399
+DOM: typeof DOM,
378400
+PropTypes: typeof PropTypes,
@@ -394,6 +416,7 @@ declare module react {
394416
+Children: typeof Children,
395417
+ConcurrentMode: typeof ConcurrentMode,
396418
+StrictMode: typeof StrictMode,
419+
+Profiler: typeof Profiler,
397420
+Suspense: typeof Suspense,
398421
+useContext: typeof useContext,
399422
+useState: typeof useState,

tests/getters_and_setters/getters_and_setters.exp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ References:
465465
react.js:17:13
466466
17| (<Example a="bad" />); // error: number ~> string
467467
^^^^^ [1]
468-
<BUILTINS>/react.js:447:36
469-
447| number: React$PropType$Primitive<number>,
468+
<BUILTINS>/react.js:470:36
469+
470| number: React$PropType$Primitive<number>,
470470
^^^^^^ [2]
471471

472472

@@ -482,8 +482,8 @@ References:
482482
react.js:18:20
483483
18| (<Example a={0} c={0} />); // error: number ~> string
484484
^ [1]
485-
<BUILTINS>/react.js:449:36
486-
449| string: React$PropType$Primitive<string>,
485+
<BUILTINS>/react.js:472:36
486+
472| string: React$PropType$Primitive<string>,
487487
^^^^^^ [2]
488488

489489

tests/new_react/new_react.exp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ Cannot assign `this.props.x` to `_` because number [1] is incompatible with stri
194194
^^^^^^^^^^^^
195195

196196
References:
197-
<BUILTINS>/react.js:447:36
198-
447| number: React$PropType$Primitive<number>,
197+
<BUILTINS>/react.js:470:36
198+
470| number: React$PropType$Primitive<number>,
199199
^^^^^^ [1]
200200
classes.js:57:12
201201
57| var _: string = this.props.x;
@@ -397,8 +397,8 @@ Cannot assign `this.props.z` to `qux` because:
397397
^^^^^^^^^^^^
398398

399399
References:
400-
<BUILTINS>/react.js:447:36
401-
447| number: React$PropType$Primitive<number>,
400+
<BUILTINS>/react.js:470:36
401+
470| number: React$PropType$Primitive<number>,
402402
^^^^^^ [1]
403403
new_react.js:19:18
404404
19| var qux: string = this.props.z;
@@ -414,8 +414,8 @@ Cannot assign `this.props.x` to `w` because string [1] is incompatible with numb
414414
^^^^^^^^^^^^
415415

416416
References:
417-
<BUILTINS>/react.js:449:36
418-
449| string: React$PropType$Primitive<string>,
417+
<BUILTINS>/react.js:472:36
418+
472| string: React$PropType$Primitive<string>,
419419
^^^^^^ [1]
420420
new_react.js:20:15
421421
20| var w:number = this.props.x;
@@ -449,8 +449,8 @@ References:
449449
new_react.js:29:23
450450
29| var element = <C x = {0}/>;
451451
^ [1]
452-
<BUILTINS>/react.js:449:36
453-
449| string: React$PropType$Primitive<string>,
452+
<BUILTINS>/react.js:472:36
453+
472| string: React$PropType$Primitive<string>,
454454
^^^^^^ [2]
455455

456456

@@ -579,8 +579,8 @@ Cannot assign `this.props.x` to `a` because:
579579
^^^^^^^^^^^^
580580

581581
References:
582-
<BUILTINS>/react.js:449:36
583-
449| string: React$PropType$Primitive<string>,
582+
<BUILTINS>/react.js:472:36
583+
472| string: React$PropType$Primitive<string>,
584584
^^^^^^ [1]
585585
props.js:14:16
586586
14| var a: number = this.props.x; // error
@@ -616,8 +616,8 @@ Cannot assign `this.props.z` to `c` because:
616616
^^^^^^^^^^^^
617617

618618
References:
619-
<BUILTINS>/react.js:447:36
620-
447| number: React$PropType$Primitive<number>,
619+
<BUILTINS>/react.js:470:36
620+
470| number: React$PropType$Primitive<number>,
621621
^^^^^^ [1]
622622
props.js:16:16
623623
16| var c: string = this.props.z; // error
@@ -638,14 +638,14 @@ References:
638638
props.js:20:29
639639
20| var element = <TestProps x={false} y={false} z={false} />; // 3 errors
640640
^^^^^ [1]
641-
<BUILTINS>/react.js:449:36
642-
449| string: React$PropType$Primitive<string>,
641+
<BUILTINS>/react.js:472:36
642+
472| string: React$PropType$Primitive<string>,
643643
^^^^^^ [2]
644644
props.js:20:49
645645
20| var element = <TestProps x={false} y={false} z={false} />; // 3 errors
646646
^^^^^ [3]
647-
<BUILTINS>/react.js:447:36
648-
447| number: React$PropType$Primitive<number>,
647+
<BUILTINS>/react.js:470:36
648+
470| number: React$PropType$Primitive<number>,
649649
^^^^^^ [4]
650650

651651

@@ -702,8 +702,8 @@ References:
702702
props2.js:9:41
703703
9| getInitialState: function(): { bar: number } {
704704
^^^^^^ [1]
705-
<BUILTINS>/react.js:449:36
706-
449| string: React$PropType$Primitive<string>,
705+
<BUILTINS>/react.js:472:36
706+
472| string: React$PropType$Primitive<string>,
707707
^^^^^^ [2]
708708
props2.js:15:42
709709
15| return <C {...this.state} foo = {0} />;
@@ -734,13 +734,13 @@ Cannot get `React.PropTypes.string.inRequired` because property `inRequired` is
734734
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
735735

736736
References:
737-
<BUILTINS>/react.js:422:39
737+
<BUILTINS>/react.js:445:39
738738
v
739-
422| type ReactPropsChainableTypeChecker = {
740-
423| (props: any, propName: string, componentName: string, href?: string): ?Error,
741-
424| isRequired: ReactPropsCheckType,
742-
425| ...
743-
426| };
739+
445| type ReactPropsChainableTypeChecker = {
740+
446| (props: any, propName: string, componentName: string, href?: string): ?Error,
741+
447| isRequired: ReactPropsCheckType,
742+
448| ...
743+
449| };
744744
^ [1]
745745

746746

tests/react/profiler.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @flow
2+
3+
import React from 'react';
4+
5+
{
6+
const {Profiler} = React;
7+
8+
function onRender(
9+
id: string,
10+
phase: "mount" | "update",
11+
actualDuration: number,
12+
baseDuration: number,
13+
startTime: number,
14+
commitTime: number,
15+
interactions: Set,
16+
) {
17+
// Dummy callback
18+
}
19+
20+
<Profiler id="fake" onRender={onRender} />
21+
<Profiler id="fake" onRender={onRender}>
22+
<div />
23+
</Profiler>
24+
}
25+
26+
{
27+
const {Profiler} = React;
28+
29+
<Profiler /> // Error: no "id" or "onRender" prop
30+
<Profiler id="fake" /> // Error: no "onRender" prop
31+
<Profiler onRender={onRender} /> // Error: no "id" prop
32+
<Profiler id="fake" onRender={null} /> // Error: invalid "onRender" prop
33+
34+
function badOnRender(foo: number) {}
35+
36+
<Profiler id="fake" onRender={badOnRender} /> // Error: invalid "onRender" prop
37+
}

0 commit comments

Comments
 (0)