2015-03-18 22:57:49 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-03-23 22:07:33 +00:00
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule TimerExample
|
2015-03-18 22:57:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
2017-07-07 21:24:25 +00:00
|
|
|
var createReactClass = require('create-react-class');
|
2016-04-09 03:36:40 +00:00
|
|
|
var ReactNative = require('react-native');
|
2015-03-18 22:57:49 +00:00
|
|
|
var {
|
|
|
|
AlertIOS,
|
2015-09-11 08:56:51 +00:00
|
|
|
Platform,
|
|
|
|
ToastAndroid,
|
2016-07-14 01:43:27 +00:00
|
|
|
Text,
|
2015-03-18 22:57:49 +00:00
|
|
|
View,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2015-03-25 02:34:12 +00:00
|
|
|
var TimerMixin = require('react-timer-mixin');
|
2017-05-06 03:50:47 +00:00
|
|
|
var RNTesterButton = require('./RNTesterButton');
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2016-07-14 01:43:27 +00:00
|
|
|
var performanceNow = require('fbjs/lib/performanceNow');
|
|
|
|
|
|
|
|
function burnCPU(milliseconds) {
|
|
|
|
const start = performanceNow();
|
|
|
|
while (performanceNow() < (start + milliseconds)) {}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class RequestIdleCallbackTester extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
message: '-',
|
|
|
|
};
|
2016-07-14 01:43:27 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_idleTimer: any = null;
|
|
|
|
_iters = 0;
|
2016-07-14 01:43:27 +00:00
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
cancelIdleCallback(this._idleTimer);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-07-14 01:43:27 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterButton onPress={this._run.bind(this, false)}>
|
2016-07-14 01:43:27 +00:00
|
|
|
Run requestIdleCallback
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterButton>
|
2016-07-14 01:43:27 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterButton onPress={this._run.bind(this, true)}>
|
2016-07-14 01:43:27 +00:00
|
|
|
Burn CPU inside of requestIdleCallback
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterButton>
|
2016-07-14 01:43:27 +00:00
|
|
|
|
2017-06-01 17:47:18 +00:00
|
|
|
<RNTesterButton onPress={this._runWithTimeout.bind(this)}>
|
|
|
|
Run requestIdleCallback with timeout option
|
|
|
|
</RNTesterButton>
|
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterButton onPress={this._runBackground}>
|
2016-07-14 01:43:27 +00:00
|
|
|
Run background task
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterButton>
|
2016-07-14 01:43:27 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterButton onPress={this._stopBackground}>
|
2016-07-14 01:43:27 +00:00
|
|
|
Stop background task
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterButton>
|
2016-07-14 01:43:27 +00:00
|
|
|
|
|
|
|
<Text>{this.state.message}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-07-14 01:43:27 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_run = (shouldBurnCPU) => {
|
2016-07-14 01:43:27 +00:00
|
|
|
cancelIdleCallback(this._idleTimer);
|
|
|
|
this._idleTimer = requestIdleCallback((deadline) => {
|
|
|
|
let message = '';
|
|
|
|
|
|
|
|
if (shouldBurnCPU) {
|
|
|
|
burnCPU(10);
|
|
|
|
message = 'Burned CPU for 10ms,';
|
|
|
|
}
|
|
|
|
this.setState({message: `${message} ${deadline.timeRemaining()}ms remaining in frame`});
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-07-14 01:43:27 +00:00
|
|
|
|
2017-06-01 17:47:18 +00:00
|
|
|
_runWithTimeout = () => {
|
|
|
|
cancelIdleCallback(this._idleTimer);
|
|
|
|
this._idleTimer = requestIdleCallback((deadline) => {
|
|
|
|
this.setState({
|
|
|
|
message: `${deadline.timeRemaining()}ms remaining in frame, it did timeout: ${deadline.didTimeout ? 'yes' : 'no'}`
|
|
|
|
});
|
|
|
|
}, { timeout: 100 });
|
|
|
|
burnCPU(100);
|
|
|
|
};
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_runBackground = () => {
|
2016-07-14 01:43:27 +00:00
|
|
|
cancelIdleCallback(this._idleTimer);
|
|
|
|
const handler = (deadline) => {
|
|
|
|
while (deadline.timeRemaining() > 5) {
|
|
|
|
burnCPU(5);
|
|
|
|
this.setState({message: `Burned CPU for 5ms ${this._iters++} times, ${deadline.timeRemaining()}ms remaining in frame`});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._idleTimer = requestIdleCallback(handler);
|
|
|
|
};
|
|
|
|
this._idleTimer = requestIdleCallback(handler);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-07-14 01:43:27 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_stopBackground = () => {
|
2016-07-14 01:43:27 +00:00
|
|
|
this._iters = 0;
|
|
|
|
cancelIdleCallback(this._idleTimer);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
}
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2017-07-07 21:24:25 +00:00
|
|
|
var TimerTester = createReactClass({
|
|
|
|
displayName: 'TimerTester',
|
2015-03-18 22:57:49 +00:00
|
|
|
mixins: [TimerMixin],
|
|
|
|
|
2015-03-23 22:07:33 +00:00
|
|
|
_ii: 0,
|
|
|
|
_iters: 0,
|
|
|
|
_start: 0,
|
|
|
|
_timerFn: (null : ?(() => any)),
|
|
|
|
_handle: (null : any),
|
|
|
|
|
2015-03-18 22:57:49 +00:00
|
|
|
render: function() {
|
|
|
|
var args = 'fn' + (this.props.dt !== undefined ? ', ' + this.props.dt : '');
|
|
|
|
return (
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterButton onPress={this._run}>
|
2015-03-18 22:57:49 +00:00
|
|
|
Measure: {this.props.type}({args}) - {this._ii || 0}
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterButton>
|
2015-03-18 22:57:49 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_run: function() {
|
|
|
|
if (!this._start) {
|
|
|
|
var d = new Date();
|
|
|
|
this._start = d.getTime();
|
|
|
|
this._iters = 100;
|
|
|
|
this._ii = 0;
|
|
|
|
if (this.props.type === 'setTimeout') {
|
|
|
|
if (this.props.dt < 1) {
|
|
|
|
this._iters = 5000;
|
|
|
|
} else if (this.props.dt > 20) {
|
|
|
|
this._iters = 10;
|
|
|
|
}
|
|
|
|
this._timerFn = () => this.setTimeout(this._run, this.props.dt);
|
|
|
|
} else if (this.props.type === 'requestAnimationFrame') {
|
|
|
|
this._timerFn = () => this.requestAnimationFrame(this._run);
|
|
|
|
} else if (this.props.type === 'setImmediate') {
|
|
|
|
this._iters = 5000;
|
|
|
|
this._timerFn = () => this.setImmediate(this._run);
|
|
|
|
} else if (this.props.type === 'setInterval') {
|
|
|
|
this._iters = 30; // Only used for forceUpdate periodicidy
|
|
|
|
this._timerFn = null;
|
|
|
|
this._handle = this.setInterval(this._run, this.props.dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this._ii >= this._iters && !this._handle) {
|
|
|
|
var d = new Date();
|
|
|
|
var e = (d.getTime() - this._start);
|
|
|
|
var msg = 'Finished ' + this._ii + ' ' + this.props.type + ' calls.\n' +
|
|
|
|
'Elapsed time: ' + e + ' ms\n' + (e / this._ii) + ' ms / iter';
|
|
|
|
console.log(msg);
|
2015-09-11 08:56:51 +00:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
AlertIOS.alert(msg);
|
|
|
|
} else if (Platform.OS === 'android') {
|
|
|
|
ToastAndroid.show(msg, ToastAndroid.SHORT);
|
|
|
|
}
|
2015-03-23 22:07:33 +00:00
|
|
|
this._start = 0;
|
2015-03-18 22:57:49 +00:00
|
|
|
this.forceUpdate(() => { this._ii = 0; });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._ii++;
|
|
|
|
// Only re-render occasionally so we don't slow down timers.
|
|
|
|
if (this._ii % (this._iters / 5) === 0) {
|
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
this._timerFn && this._timerFn();
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
|
|
|
this.clearInterval(this._handle); // invalid handles are ignored
|
|
|
|
if (this._handle) {
|
|
|
|
// Configure things so we can do a final run to update UI and reset state.
|
|
|
|
this._handle = null;
|
|
|
|
this._iters = this._ii;
|
|
|
|
this._run();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.framework = 'React';
|
|
|
|
exports.title = 'Timers, TimerMixin';
|
|
|
|
exports.description = 'The TimerMixin provides timer functions for executing ' +
|
|
|
|
'code in the future that are safely cleaned up when the component unmounts.';
|
|
|
|
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'this.setTimeout(fn, t)',
|
|
|
|
description: 'Execute function fn t milliseconds in the future. If ' +
|
|
|
|
't === 0, it will be enqueued immediately in the next event loop. ' +
|
|
|
|
'Larger values will fire on the closest frame.',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TimerTester type="setTimeout" dt={0} />
|
|
|
|
<TimerTester type="setTimeout" dt={1} />
|
|
|
|
<TimerTester type="setTimeout" dt={100} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'this.requestAnimationFrame(fn)',
|
|
|
|
description: 'Execute function fn on the next frame.',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TimerTester type="requestAnimationFrame" />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
2016-07-14 01:43:27 +00:00
|
|
|
{
|
|
|
|
title: 'this.requestIdleCallback(fn)',
|
|
|
|
description: 'Execute function fn on the next JS frame that has idle time',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<RequestIdleCallbackTester />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
2015-03-18 22:57:49 +00:00
|
|
|
{
|
|
|
|
title: 'this.setImmediate(fn)',
|
|
|
|
description: 'Execute function fn at the end of the current JS event loop.',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TimerTester type="setImmediate" />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'this.setInterval(fn, t)',
|
|
|
|
description: 'Execute function fn every t milliseconds until cancelled ' +
|
|
|
|
'or component is unmounted.',
|
2016-10-16 11:11:59 +00:00
|
|
|
render: function(): React.Element<any> {
|
2017-08-18 01:36:54 +00:00
|
|
|
class IntervalExample extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
showTimer: true,
|
|
|
|
};
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2015-03-18 22:57:49 +00:00
|
|
|
return (
|
|
|
|
<View>
|
2015-12-12 05:21:02 +00:00
|
|
|
{this.state.showTimer && this._renderTimer()}
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterButton onPress={this._toggleTimer}>
|
2015-12-12 05:21:02 +00:00
|
|
|
{this.state.showTimer ? 'Unmount timer' : 'Mount new timer'}
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterButton>
|
2015-12-12 05:21:02 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-12 05:21:02 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_renderTimer = () => {
|
2015-12-12 05:21:02 +00:00
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TimerTester ref="interval" dt={25} type="setInterval" />
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterButton onPress={() => this.refs.interval.clear() }>
|
2015-12-12 05:21:02 +00:00
|
|
|
Clear interval
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterButton>
|
2015-03-18 22:57:49 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_toggleTimer = () => {
|
2015-03-18 22:57:49 +00:00
|
|
|
this.setState({showTimer: !this.state.showTimer});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-18 22:57:49 +00:00
|
|
|
return <IntervalExample />;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|