Skip to content

Commit c41e86c

Browse files
petehuntzpao
authored andcommitted
Make ReactDefaultPerf work server-side
We were reading from window which was throwing when ReactDefaultPerf was injected.
1 parent 4d8f044 commit c41e86c

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

src/test/ReactDefaultPerf.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
"use strict";
2121

22+
var performanceNow = require('performanceNow');
23+
2224
var ReactDefaultPerf = {};
2325

2426
if (__DEV__) {
@@ -186,9 +188,9 @@ if (__DEV__) {
186188
var fnArgs = _getFnArguments(func);
187189

188190
return function() {
189-
var timeBeforeFn = now();
191+
var timeBeforeFn = performanceNow();
190192
var fnReturn = func.apply(this, arguments);
191-
var timeAfterFn = now();
193+
var timeAfterFn = performanceNow();
192194

193195
/**
194196
* Hold onto arguments in a readable way: args[1] -> args.component.
@@ -224,7 +226,7 @@ if (__DEV__) {
224226
var callback = _getCallback(objName, fnName);
225227
callback && callback(this, args, fnReturn, log, info);
226228

227-
log.timing.timeToLog = now() - timeAfterFn;
229+
log.timing.timeToLog = performanceNow() - timeAfterFn;
228230

229231
return fnReturn;
230232
};
@@ -400,17 +402,6 @@ if (__DEV__) {
400402
var _microTime = function(time) {
401403
return Math.round(time * 1000) / 1000;
402404
};
403-
404-
/**
405-
* Shim window.performance.now
406-
* We can't assign window.performance.now and then call it, so need to bind.
407-
* TODO: Support Firefox < 15 for now
408-
*/
409-
var performance = window && (window.performance || window.webkitPeformance);
410-
if (!performance || !performance.now) {
411-
performance = Date;
412-
}
413-
var now = performance.now.bind(performance);
414405
}
415406

416407
module.exports = ReactDefaultPerf;

src/test/performanceNow.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2013 Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* @providesModule performanceNow
17+
* @typechecks static-only
18+
*/
19+
20+
"use strict";
21+
22+
var ExecutionEnvironment = require('ExecutionEnvironment');
23+
24+
/**
25+
* Detect if we can use window.performance.now() and gracefully
26+
* fallback to Date.now() if it doesn't exist.
27+
* We need to support Firefox < 15 for now due to Facebook's webdriver
28+
* infrastructure.
29+
*/
30+
var performance = null;
31+
32+
if (ExecutionEnvironment.canUseDOM) {
33+
performance = window.performance || window.webkitPerformance;
34+
}
35+
36+
if (!performance || !performance.now) {
37+
performance = Date;
38+
}
39+
40+
var performanceNow = performance.now.bind(performance);
41+
42+
module.exports = performanceNow;

0 commit comments

Comments
 (0)