Skip to content

Commit 1109164

Browse files
authored
Merge pull request #1954 from wix/feat/Adding_profiler
Adding a profiler and adding playgroundscreen for examples
2 parents e808253 + 1b6e5e4 commit 1109164

File tree

4 files changed

+131
-12
lines changed

4 files changed

+131
-12
lines changed

example/src/screens/menuScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const MenuScreen = (props: Props) => {
6969
{renderEntry(testIDs.menu.EXPANDABLE_CALENDAR, 'Expandable Calendar', 'ExpandableCalendarScreen')}
7070
{renderEntry(testIDs.menu.TIMELINE_CALENDAR, 'Timeline Calendar', 'TimelineCalendarScreen')}
7171
{renderEntry(testIDs.menu.WEEK_CALENDAR, 'Week Calendar', 'ExpandableCalendarScreen', {weekView: true})}
72-
{/* {renderEntry(testIDs.menu.PLAYGROUND, 'Playground', 'Playground')} */}
72+
{renderEntry(testIDs.menu.PLAYGROUND, 'Playground', 'Playground')}
7373
<View style={styles.switchContainer}>
7474
<Text>Force RTL</Text>
7575
<Switch value={forceRTL} onValueChange={toggleRTL}/>
Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
import React, {useState, useCallback} from 'react';
2-
import {Text, View, StyleSheet} from 'react-native';
3-
import {ExpandableCalendar, CalendarProvider} from 'react-native-calendars';
2+
import {View, Text, Button, StyleSheet} from 'react-native';
3+
import {Profiler, Calendar, CalendarList, ExpandableCalendar, CalendarProvider} from 'react-native-calendars';
44

5-
const INITIAL_DATE = '2019-04-01';
5+
const INITIAL_DATE = '2022-07-07';
6+
const enum elements {
7+
CALENDAR = 'Calendar',
8+
LIST = 'CalendarList',
9+
EXPANDABLE = 'Expandable',
10+
}
11+
const BLUE = '#00BBF2';
612

713
export default function PlaygroundScreen() {
814
const [selectedDate, setSelectedDate] = useState(INITIAL_DATE);
15+
const [element, setElement] = useState(elements.LIST);
16+
917

1018
const onDayPress = useCallback((day) => {
1119
setSelectedDate(day.dateString);
1220
}, [selectedDate]);
1321

14-
return (
15-
<View style={styles.container}>
16-
<Text style={styles.text}>Selected Date: {selectedDate}</Text>
22+
const renderCalendar = () => {
23+
return (
24+
<Calendar
25+
current={INITIAL_DATE}
26+
style={styles.calendar}
27+
/>
28+
);
29+
};
30+
31+
const renderCalendarList = () => {
32+
return (
33+
<CalendarList
34+
current={INITIAL_DATE}
35+
style={styles.calendar}
36+
horizontal
37+
/>
38+
);
39+
};
40+
41+
const renderExpendableCalendar = () => {
42+
return (
1743
<CalendarProvider date={INITIAL_DATE}>
1844
<ExpandableCalendar
1945
pastScrollRange={3}
@@ -23,22 +49,50 @@ export default function PlaygroundScreen() {
2349
style={styles.calendar}
2450
/>
2551
</CalendarProvider>
26-
</View>
52+
);
53+
};
54+
55+
const renderElement = () => {
56+
switch (element) {
57+
case elements.CALENDAR:
58+
return renderCalendar();
59+
case elements.LIST:
60+
return renderCalendarList();
61+
case elements.EXPANDABLE:
62+
return renderExpendableCalendar();
63+
default:
64+
return renderCalendar();
65+
}
66+
};
67+
68+
return (
69+
<>
70+
<View style={styles.buttonsContainer}>
71+
<Button color={BLUE} title='Calendar' onPress={() => setElement(elements.CALENDAR)}/>
72+
<Button color={BLUE} title='Calendar List' onPress={() => setElement(elements.LIST)}/>
73+
<Button color={BLUE} title='Expandable' onPress={() => setElement(elements.EXPANDABLE)}/>
74+
</View>
75+
<Text style={styles.text}>Selected Date: {selectedDate}</Text>
76+
<Profiler id={element}>
77+
{renderElement()}
78+
</Profiler>
79+
</>
2780
);
2881
}
2982

3083
const styles = StyleSheet.create({
31-
container : {
32-
flex : 1
84+
buttonsContainer: {
85+
padding: 5,
86+
flexDirection: 'row',
87+
justifyContent: 'space-between'
3388
},
3489
calendar: {
3590
borderWidth: 1,
3691
borderColor: '#b6c1cd'
3792
},
3893
text : {
39-
marginLeft: 10,
94+
alignSelf: 'center',
4095
padding : 20,
41-
color : '#00BBF2',
4296
fontSize: 16
4397
}
4498
});

src/Profiler.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// @ts-nocheck
2+
3+
// Taken from
4+
// https://medium.com/life-at-paperless/how-to-use-the-react-profiler-component-to-measure-performance-improvements-from-hooks-d43b7092d7a8
5+
6+
// Profiler callback
7+
// https://reactjs.org/docs/profiler.html#onrender-callback
8+
9+
import React, {Profiler as RProfiler, ProfilerProps as RProfilerProps, PropsWithChildren} from 'react';
10+
11+
export type ProfilerProps = Pick<RProfilerProps, 'id'>;
12+
13+
// The entire render time since execution of this file (likely on page load)
14+
const cumulativeDuration: {[key: string]: string} = {};
15+
16+
export default class Profiler extends React.Component<PropsWithChildren<ProfilerProps>> {
17+
onRender = (...profileData) => {
18+
logProfileData(getProfileData(profileData));
19+
};
20+
21+
render() {
22+
const {children, id} = this.props;
23+
return (
24+
<RProfiler id={id} onRender={this.onRender}>
25+
{children}
26+
</RProfiler>
27+
);
28+
}
29+
}
30+
31+
// TODO: fix typescript...
32+
export const getProfileData = ([
33+
id, // the "id" prop of the Profiler tree that has just committed
34+
phase, // either "mount" (if the tree just mounted) or "update" (if it re-rendered)
35+
actualDuration, // time spent rendering the committed update
36+
baseDuration, // estimated time to render the entire subtree without memoization
37+
startTime, // when React began rendering this update
38+
commitTime, // when React committed this update
39+
interactions // the Set of interactions belonging to this update
40+
]) => {
41+
cumulativeDuration[id] = Number(((cumulativeDuration[id] ?? 0) + actualDuration).toFixed(2));
42+
return {
43+
id,
44+
interactions,
45+
phase,
46+
actualDuration: Number(actualDuration.toFixed(2)),
47+
baseDuration: Number(baseDuration.toFixed(2)),
48+
commitTime: Number(commitTime.toFixed(2)),
49+
cumulativeDuration: cumulativeDuration[id],
50+
startTime: Number(startTime.toFixed(2))
51+
};
52+
};
53+
54+
export const logProfileData = ({id, actualDuration, cumulativeDuration, phase}) => {
55+
console.group(phase);
56+
// table did not work for me so I used log instead
57+
console.log(id, ':', actualDuration, cumulativeDuration);
58+
// console.table({
59+
// actualDuration,
60+
// baseDuration,
61+
// cumulativeDuration
62+
// });
63+
console.groupEnd();
64+
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ export type {TimelineListProps, TimelineListRenderItemInfo} from './timeline-lis
2222
export {default as CalendarUtils} from './services';
2323
export type {DateData, AgendaEntry, AgendaSchedule} from './types';
2424
export {default as LocaleConfig} from 'xdate';
25+
export {default as Profiler} from './Profiler';

0 commit comments

Comments
 (0)