forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserPerfRunnerContext.react.js
More file actions
195 lines (170 loc) · 5.56 KB
/
BrowserPerfRunnerContext.react.js
File metadata and controls
195 lines (170 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var BenchmarkQueue = React.createClass({
propTypes: {
debug: React.PropTypes.bool,
onChange: React.PropTypes.func.isRequired,
initialQueue: React.PropTypes.array.isRequired,
maxTime: React.PropTypes.number,
onCompleteEach: React.PropTypes.func,
onError: React.PropTypes.func
},
getDefaultProps: function(){
return {
maxTime: 5
};
},
getInitialState: function(){
return {
queue: this.props.initialQueue.slice()
};
},
setItemState: function(state){
state.test = this.state.queue[0].test;
state.react = this.state.queue[0].react;
this.props.onChange(state);
},
handleContextReady: function(window){
var benchmark = window.Benchmark(window.exports);
benchmark.options.maxTime = this.props.maxTime; //DEBUG
var itemState = {
testRunnerURL: window.location.href,
name: window.exports.name,
platform: window.Benchmark.platform.description,
reactVersion: window.React.version,
isMinified: (function(){
var code = window.React.render.toString();
return code.indexOf(',') - code.indexOf('(') <= 2;
}())
};
this.setItemState(itemState);
var self = this;
benchmark.on('start error cycle complete', function(){
var stats = JSON.parse(JSON.stringify(benchmark.stats));
itemState.stats = stats;
itemState.isRunning = benchmark.running;
itemState.error = benchmark.error;
self.setItemState(itemState);
});
if (this.props.onError) {
benchmark.on('error', this.props.onError);
}
benchmark.on('complete', function(){
var queue = self.state.queue.slice();
var queueItem = queue.shift();
if (self.props.onCompleteEach) {
self.props.onCompleteEach(queueItem);
}
self.setState({ queue:queue });
});
benchmark.run({async:true});
},
shouldComponentUpdate: function(nextProps, nextState){
return nextState.queue.length < this.state.queue.length;
},
render: function(){
if (!(this.state.queue && this.state.queue.length > 0)){
return React.DOM.div({style:{display:'none'}});
}
return BrowserPerfRunnerContext({
debug: this.props.debug,
test: this.state.queue[0].test,
react: this.state.queue[0].react,
onReady: this.handleContextReady
});
}
});
var BrowserPerfRunnerContext = React.createClass({
propTypes: {
debug: React.PropTypes.bool,
test: function(object, key){
React.PropTypes.string.isRequired(object, key);
if (/\.jsx?$/i.test(object[key])) return;
throw Error('Expected `' + key + '` to be a test file name with extension `.js` or `.jsx`');
},
react: function(object, key){
React.PropTypes.string.isRequired(object, key);
if (/^(?:builds\/.+|edge|previous|(?:\d+\.){2}\d+)$/.test(object[key])) return;
throw Error('Expected `' + key + '` prop to be a valid react version string, build string or "edge" or "previous"');
},
onReady: React.PropTypes.func.isRequired
},
getInitialState: function(){
return {
testRunnerURL:'about:blank'
};
},
// _handleFrameError: function(error){
// console.error('BrowserPerfRunnerContext', error);
// },
//
// _handleFrameLoad: function(event){
// console.log('BrowserPerfRunnerContext', event);
// },
//
_handleMessage: function(event){
if (location.href.indexOf(event.origin) !== 0)
return console.debug('BrowserPerfRunnerContext#_handleMessage ignored message from ' + event.origin);
if (event.source.location.href.indexOf(this.state.testRunnerURL) === -1)
return console.debug('BrowserPerfRunnerContext#_handleMessage ignored message from ' + event.source.location.href);
if (event.data !== 'Ready!')
return console.debug('BrowserPerfRunnerContext#_handleMessage ignored message ' + JSON.stringify(event.data));
this.props.onReady(event.source);
},
_getTestRunnerURL: function(props){
return 'runner.html' +
'?' +
'debug=' + (props.debug ? 1 : 0) +
'&' +
'react=' + encodeURIComponent(props.react) +
'&' +
'test=' + encodeURIComponent(props.test)
},
_renderState: function(props){
return {
testRunnerURL: this._getTestRunnerURL(props)
};
},
componentDidMount: function(){
var node = this.refs.iframe.getDOMNode();
// node.onload = this._handleFrameLoad;
// node.onerror = this._handleFrameError;
if (window.addEventListener) {
window.addEventListener('message', this._handleMessage, false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', this._handleMessage);
} else {
throw Error('cannot attach onmessage listener');
}
this.setState(this._renderState(this.props));
},
componentWillUnmount: function(){
if (window.removeEventListener) {
window.removeEventListener('message', this._handleMessage);
} else if (window.detachEvent) {
window.detachEvent('onmessage', this._handleMessage);
} else {
throw Error('cannot detach onmessage listener');
}
this.refs.iframe.getDOMNode().src = '';
},
componentWillReceiveProps: function(nextProps){
this.setState(this._renderState(nextProps));
},
shouldComponentUpdate: function(nextProps, nextState){
return nextState.testRunnerURL != this.state.testRunnerURL;
},
render: function(){
return (
React.DOM.iframe({
ref: 'iframe',
name: "BrowserPerfRunnerContextFrame",
style: this.style,
src: this.state.testRunnerURL
})
);
},
style: {
position: 'absolute',
right: '100%',
bottom: '100%'
}
});