Skip to content

Commit 8a2187b

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Remove TimerMixin from TimerExample
Summary: $title Reviewed By: yungsters Differential Revision: D10180650 fbshipit-source-id: e757f2ae9a2de582c4b5260e557a66b5ad0c7f57
1 parent fdfe422 commit 8a2187b

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

RNTester/js/TimerExample.js

+39-15
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ var React = require('react');
1414
var createReactClass = require('create-react-class');
1515
var ReactNative = require('react-native');
1616
var {AlertIOS, Platform, ToastAndroid, Text, View} = ReactNative;
17-
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
18-
* found when Flow v0.54 was deployed. To see the error delete this comment and
19-
* run Flow. */
20-
var TimerMixin = require('react-timer-mixin');
2117
var RNTesterButton = require('./RNTesterButton');
2218
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
2319
* found when Flow v0.54 was deployed. To see the error delete this comment and
@@ -123,13 +119,15 @@ class RequestIdleCallbackTester extends React.Component<{}, $FlowFixMeState> {
123119

124120
var TimerTester = createReactClass({
125121
displayName: 'TimerTester',
126-
mixins: [TimerMixin],
127122

128123
_ii: 0,
129124
_iters: 0,
130125
_start: 0,
126+
_timerId: (null: ?TimeoutID),
127+
_rafId: (null: ?AnimationFrameID),
128+
_intervalId: (null: ?IntervalID),
129+
_immediateId: (null: ?Object),
131130
_timerFn: (null: ?() => any),
132-
_handle: (null: any),
133131

134132
render: function() {
135133
var args = 'fn' + (this.props.dt !== undefined ? ', ' + this.props.dt : '');
@@ -140,6 +138,24 @@ var TimerTester = createReactClass({
140138
);
141139
},
142140

141+
componentWillUnmount() {
142+
if (this._timerId != null) {
143+
clearTimeout(this._timerId);
144+
}
145+
146+
if (this._rafId != null) {
147+
cancelAnimationFrame(this._rafId);
148+
}
149+
150+
if (this._immediateId != null) {
151+
clearImmediate(this._immediateId);
152+
}
153+
154+
if (this._intervalId != null) {
155+
clearInterval(this._intervalId);
156+
}
157+
},
158+
143159
_run: function() {
144160
if (!this._start) {
145161
var d = new Date();
@@ -152,19 +168,25 @@ var TimerTester = createReactClass({
152168
} else if (this.props.dt > 20) {
153169
this._iters = 10;
154170
}
155-
this._timerFn = () => this.setTimeout(this._run, this.props.dt);
171+
this._timerFn = () => {
172+
this._timerId = setTimeout(this._run, this.props.dt);
173+
};
156174
} else if (this.props.type === 'requestAnimationFrame') {
157-
this._timerFn = () => this.requestAnimationFrame(this._run);
175+
this._timerFn = () => {
176+
this._rafId = requestAnimationFrame(this._run);
177+
};
158178
} else if (this.props.type === 'setImmediate') {
159179
this._iters = 5000;
160-
this._timerFn = () => this.setImmediate(this._run);
180+
this._timerFn = () => {
181+
this._immediateId = setImmediate(this._run);
182+
};
161183
} else if (this.props.type === 'setInterval') {
162184
this._iters = 30; // Only used for forceUpdate periodicidy
163185
this._timerFn = null;
164-
this._handle = this.setInterval(this._run, this.props.dt);
186+
this._intervalId = setInterval(this._run, this.props.dt);
165187
}
166188
}
167-
if (this._ii >= this._iters && !this._handle) {
189+
if (this._ii >= this._iters && this._intervalId == null) {
168190
var d = new Date();
169191
var e = d.getTime() - this._start;
170192
var msg =
@@ -195,14 +217,16 @@ var TimerTester = createReactClass({
195217
if (this._ii % (this._iters / 5) === 0) {
196218
this.forceUpdate();
197219
}
198-
this._timerFn && this._timerFn();
220+
if (this._timerFn) {
221+
this._timerId = this._timerFn();
222+
}
199223
},
200224

201225
clear: function() {
202-
this.clearInterval(this._handle); // invalid handles are ignored
203-
if (this._handle) {
226+
if (this._intervalId != null) {
227+
clearInterval(this._intervalId);
204228
// Configure things so we can do a final run to update UI and reset state.
205-
this._handle = null;
229+
this._intervalId = null;
206230
this._iters = this._ii;
207231
this._run();
208232
}

0 commit comments

Comments
 (0)