Skip to content

Commit 61346d3

Browse files
ronal2dofacebook-github-bot
authored andcommitted
remove createReactClass and TImerMixin from TimerTest.js (#21748)
Summary: Related to #21485 (comment) Remove createReactClass and TimerMixin from IntegrationTests/TimersTest.js Pull Request resolved: #21748 Reviewed By: TheSavior Differential Revision: D10366418 Pulled By: RSNara fbshipit-source-id: f7e9a1b62a17cd23374997f99dbfe239172b614f
1 parent 09d35de commit 61346d3

File tree

1 file changed

+128
-38
lines changed

1 file changed

+128
-38
lines changed

IntegrationTests/TimersTest.js

+128-38
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,136 @@
1111
'use strict';
1212

1313
const React = require('react');
14-
const createReactClass = require('create-react-class');
1514
const ReactNative = require('react-native');
16-
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
17-
* found when Flow v0.54 was deployed. To see the error delete this comment and
18-
* run Flow. */
19-
const TimerMixin = require('react-timer-mixin');
20-
2115
const {StyleSheet, Text, View} = ReactNative;
2216
const {TestModule} = ReactNative.NativeModules;
2317

24-
const TimersTest = createReactClass({
25-
displayName: 'TimersTest',
26-
mixins: [TimerMixin],
18+
type Props = $ReadOnly<{||}>;
2719

28-
_nextTest: () => {},
29-
_interval: -1,
20+
type State = {|
21+
count: number,
22+
done: boolean,
23+
|};
3024

31-
getInitialState() {
32-
return {
33-
count: 0,
34-
done: false,
35-
};
36-
},
25+
type ImmediateID = Object;
26+
27+
class TimersTest extends React.Component<Props, State> {
28+
_nextTest = () => {};
29+
_interval: ?IntervalID = null;
30+
31+
_timeoutIDs: Set<TimeoutID> = new Set();
32+
_intervalIDs: Set<IntervalID> = new Set();
33+
_immediateIDs: Set<ImmediateID> = new Set();
34+
_animationFrameIDs: Set<AnimationFrameID> = new Set();
35+
36+
state = {
37+
count: 0,
38+
done: false,
39+
};
40+
41+
setTimeout(fn: () => void, time: number): TimeoutID {
42+
const id = setTimeout(() => {
43+
this._timeoutIDs.delete(id);
44+
fn();
45+
}, time);
46+
47+
this._timeoutIDs.add(id);
48+
49+
return id;
50+
}
51+
52+
clearTimeout(id: TimeoutID) {
53+
this._timeoutIDs.delete(id);
54+
clearTimeout(id);
55+
}
56+
57+
setInterval(fn: () => void, time: number): IntervalID {
58+
const id = setInterval(() => {
59+
fn();
60+
}, time);
61+
62+
this._intervalIDs.add(id);
63+
64+
return id;
65+
}
66+
67+
clearInterval(id: IntervalID) {
68+
this._intervalIDs.delete(id);
69+
clearInterval(id);
70+
}
71+
72+
setImmediate(fn: () => void): ImmediateID {
73+
const id = setImmediate(() => {
74+
this._immediateIDs.delete(id);
75+
fn();
76+
});
77+
78+
this._immediateIDs.add(id);
79+
80+
return id;
81+
}
82+
83+
requestAnimationFrame(fn: () => void): AnimationFrameID {
84+
const id = requestAnimationFrame(() => {
85+
this._animationFrameIDs.delete(id);
86+
fn();
87+
});
88+
89+
this._animationFrameIDs.add(id);
90+
91+
return id;
92+
}
93+
94+
cancelAnimationFrame(id: AnimationFrameID): void {
95+
this._animationFrameIDs.delete(id);
96+
cancelAnimationFrame(id);
97+
}
3798

3899
componentDidMount() {
39100
this.setTimeout(this.testSetTimeout0, 1000);
40-
},
101+
}
41102

42103
testSetTimeout0() {
43104
this.setTimeout(this.testSetTimeout1, 0);
44-
},
105+
}
45106

46107
testSetTimeout1() {
47108
this.setTimeout(this.testSetTimeout50, 1);
48-
},
109+
}
49110

50111
testSetTimeout50() {
51112
this.setTimeout(this.testRequestAnimationFrame, 50);
52-
},
113+
}
53114

54115
testRequestAnimationFrame() {
55116
this.requestAnimationFrame(this.testSetInterval0);
56-
},
117+
}
57118

58119
testSetInterval0() {
59120
this._nextTest = this.testSetInterval20;
60121
this._interval = this.setInterval(this._incrementInterval, 0);
61-
},
122+
}
62123

63124
testSetInterval20() {
64125
this._nextTest = this.testSetImmediate;
65126
this._interval = this.setInterval(this._incrementInterval, 20);
66-
},
127+
}
67128

68129
testSetImmediate() {
69130
this.setImmediate(this.testClearTimeout0);
70-
},
131+
}
71132

72133
testClearTimeout0() {
73134
const timeout = this.setTimeout(() => this._fail('testClearTimeout0'), 0);
74135
this.clearTimeout(timeout);
75136
this.testClearTimeout30();
76-
},
137+
}
77138

78139
testClearTimeout30() {
79140
const timeout = this.setTimeout(() => this._fail('testClearTimeout30'), 30);
80141
this.clearTimeout(timeout);
81142
this.setTimeout(this.testClearMulti, 50);
82-
},
143+
}
83144

84145
testClearMulti() {
85146
const fails = [];
@@ -96,7 +157,7 @@ const TimersTest = createReactClass({
96157
this.setTimeout(() => this.clearTimeout(delayClear), 20);
97158

98159
this.setTimeout(this.testOrdering, 50);
99-
},
160+
}
100161

101162
testOrdering() {
102163
// Clear timers are set first because it's more likely to uncover bugs.
@@ -131,42 +192,73 @@ const TimersTest = createReactClass({
131192
25,
132193
);
133194
this.setTimeout(this.done, 50);
134-
},
195+
}
135196

136197
done() {
137198
this.setState({done: true}, () => {
138199
TestModule.markTestCompleted();
139200
});
140-
},
201+
}
202+
203+
componentWillUnmount() {
204+
for (const timeoutID of this._timeoutIDs) {
205+
clearTimeout(timeoutID);
206+
}
207+
208+
for (const intervalID of this._intervalIDs) {
209+
clearInterval(intervalID);
210+
}
211+
212+
for (const requestAnimationFrameID of this._animationFrameIDs) {
213+
cancelAnimationFrame(requestAnimationFrameID);
214+
}
215+
216+
for (const immediateID of this._immediateIDs) {
217+
clearImmediate(immediateID);
218+
}
219+
220+
this._timeoutIDs = new Set();
221+
this._intervalIDs = new Set();
222+
this._animationFrameIDs = new Set();
223+
this._immediateIDs = new Set();
224+
225+
if (this._interval != null) {
226+
clearInterval(this._interval);
227+
this._interval = null;
228+
}
229+
}
141230

142231
render() {
143232
return (
144233
<View style={styles.container}>
145234
<Text>
146-
{this.constructor.displayName + ': \n'}
235+
{this.constructor.name + ': \n'}
147236
Intervals: {this.state.count + '\n'}
148237
{this.state.done ? 'Done' : 'Testing...'}
149238
</Text>
150239
</View>
151240
);
152-
},
241+
}
153242

154243
_incrementInterval() {
155244
if (this.state.count > 3) {
156245
throw new Error('interval incremented past end.');
157246
}
158247
if (this.state.count === 3) {
159-
this.clearInterval(this._interval);
248+
if (this._interval != null) {
249+
this.clearInterval(this._interval);
250+
this._interval = null;
251+
}
160252
this.setState({count: 0}, this._nextTest);
161253
return;
162254
}
163255
this.setState({count: this.state.count + 1});
164-
},
256+
}
165257

166258
_fail(caller: string): void {
167259
throw new Error('_fail called by ' + caller);
168-
},
169-
});
260+
}
261+
}
170262

171263
const styles = StyleSheet.create({
172264
container: {
@@ -175,6 +267,4 @@ const styles = StyleSheet.create({
175267
},
176268
});
177269

178-
TimersTest.displayName = 'TimersTest';
179-
180270
module.exports = TimersTest;

0 commit comments

Comments
 (0)