Skip to content

Instantly share code, notes, and snippets.

@dakom
Created October 10, 2017 08:43
Show Gist options
  • Save dakom/e68503e59c7a73c3bc926b7e8ae65a07 to your computer and use it in GitHub Desktop.
Save dakom/e68503e59c7a73c3bc926b7e8ae65a07 to your computer and use it in GitHub Desktop.
class extends React.Component {
private canRender: boolean = false;
private latestData: any;
constructor(props) {
super(props);
let nJobs = 0;
let lastRenderTime: number;
props.someObservableThing.listen(data => {
nJobs++;
this.latestData = data;
if (this.canRender) {
const now = performance.now();
this.canRender = false;
this.setState({
data: this.latestData,
jobsPerRender: nJobs,
fps: (lastRenderTime === undefined) ? 0 : 1000 / (now - lastRenderTime)
});
nJobs = 0;
lastRenderTime = now;
}
});
this.state = {};
}
/* Lifecycle */
componentDidMount() {
this.canRender = true;
}
componentDidUpdate() {
this.canRender = true;
}
render() {
outputStats(this.state);
return this.state.data === undefined ? null : <View {...this.state.data} />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment