Skip to content

Commit a2bf6e9

Browse files
author
Keyan Zhang
committed
make sure ReactPerfDev's public API doesn't change
1 parent b12d943 commit a2bf6e9

File tree

2 files changed

+75
-99
lines changed

2 files changed

+75
-99
lines changed

src/renderers/shared/ReactPerfDev.js

Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,51 @@
1414
var ReactDebugToolDev = require('ReactDebugToolDev');
1515
var warning = require('warning');
1616

17-
var ReactPerfDev = {};
17+
var alreadyWarned = false;
18+
19+
var warnInProductionAndReturnDefault = (val) => () => {
20+
if (alreadyWarned) {
21+
return val;
22+
}
23+
alreadyWarned = true;
24+
if (typeof console !== 'undefined') {
25+
console.error(
26+
'ReactPerf is not supported in the production builds of React. ' +
27+
'To collect measurements, please use the development build of React instead.'
28+
);
29+
}
30+
return val;
31+
};
32+
33+
var ReactPerfDev = {
34+
getLastMeasurements: warnInProductionAndReturnDefault([]),
35+
getExclusive: warnInProductionAndReturnDefault([]),
36+
getInclusive: warnInProductionAndReturnDefault([]),
37+
getWasted: warnInProductionAndReturnDefault([]),
38+
getOperations: warnInProductionAndReturnDefault([]),
39+
printExclusive: warnInProductionAndReturnDefault(undefined),
40+
printInclusive: warnInProductionAndReturnDefault(undefined),
41+
printWasted: warnInProductionAndReturnDefault(undefined),
42+
printOperations: warnInProductionAndReturnDefault(undefined),
43+
start: warnInProductionAndReturnDefault(undefined),
44+
stop: warnInProductionAndReturnDefault(undefined),
45+
isRunning: warnInProductionAndReturnDefault(false),
46+
// Deprecated:
47+
printDOM: warnInProductionAndReturnDefault(undefined),
48+
getMeasurementsSummaryMap: warnInProductionAndReturnDefault([]),
49+
};
1850

1951
if (__DEV__) {
20-
var alreadyWarned = false;
21-
2252
var roundFloat = function(val, base = 2) {
2353
var n = Math.pow(10, base);
2454
return Math.floor(val * n) / n;
2555
};
2656

27-
var warnInProduction = function() {
28-
if (alreadyWarned) {
29-
return;
30-
}
31-
alreadyWarned = true;
32-
if (typeof console !== 'undefined') {
33-
console.error(
34-
'ReactPerf is not supported in the production builds of React. ' +
35-
'To collect measurements, please use the development build of React instead.'
36-
);
37-
}
38-
};
39-
4057
var getLastMeasurements = function() {
41-
if (!__DEV__) {
42-
warnInProduction();
43-
return [];
44-
}
45-
4658
return ReactDebugToolDev.getFlushHistory();
4759
};
4860

4961
var getExclusive = function(flushHistory = getLastMeasurements()) {
50-
if (!__DEV__) {
51-
warnInProduction();
52-
return [];
53-
}
54-
5562
var aggregatedStats = {};
5663
var affectedIDs = {};
5764

@@ -102,11 +109,6 @@ if (__DEV__) {
102109
};
103110

104111
var getInclusive = function(flushHistory = getLastMeasurements()) {
105-
if (!__DEV__) {
106-
warnInProduction();
107-
return [];
108-
}
109-
110112
var aggregatedStats = {};
111113
var affectedIDs = {};
112114

@@ -175,11 +177,6 @@ if (__DEV__) {
175177
};
176178

177179
var getWasted = function(flushHistory = getLastMeasurements()) {
178-
if (!__DEV__) {
179-
warnInProduction();
180-
return [];
181-
}
182-
183180
var aggregatedStats = {};
184181
var affectedIDs = {};
185182

@@ -273,11 +270,6 @@ if (__DEV__) {
273270
};
274271

275272
var getOperations = function(flushHistory = getLastMeasurements()) {
276-
if (!__DEV__) {
277-
warnInProduction();
278-
return [];
279-
}
280-
281273
var stats = [];
282274
flushHistory.forEach((flush, flushIndex) => {
283275
var {operations, treeSnapshot} = flush;
@@ -301,11 +293,6 @@ if (__DEV__) {
301293
};
302294

303295
var printExclusive = function(flushHistory) {
304-
if (!__DEV__) {
305-
warnInProduction();
306-
return;
307-
}
308-
309296
var stats = getExclusive(flushHistory);
310297
var table = stats.map(item => {
311298
var {key, instanceCount, totalDuration} = item;
@@ -327,11 +314,6 @@ if (__DEV__) {
327314
};
328315

329316
var printInclusive = function(flushHistory) {
330-
if (!__DEV__) {
331-
warnInProduction();
332-
return;
333-
}
334-
335317
var stats = getInclusive(flushHistory);
336318
var table = stats.map(item => {
337319
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
@@ -346,11 +328,6 @@ if (__DEV__) {
346328
};
347329

348330
var printWasted = function(flushHistory) {
349-
if (!__DEV__) {
350-
warnInProduction();
351-
return;
352-
}
353-
354331
var stats = getWasted(flushHistory);
355332
var table = stats.map(item => {
356333
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
@@ -365,11 +342,6 @@ if (__DEV__) {
365342
};
366343

367344
var printOperations = function(flushHistory) {
368-
if (!__DEV__) {
369-
warnInProduction();
370-
return;
371-
}
372-
373345
var stats = getOperations(flushHistory);
374346
var table = stats.map(stat => ({
375347
'Owner > Node': stat.key,
@@ -407,29 +379,14 @@ if (__DEV__) {
407379
};
408380

409381
var start = function() {
410-
if (!__DEV__) {
411-
warnInProduction();
412-
return;
413-
}
414-
415382
ReactDebugToolDev.beginProfiling();
416383
};
417384

418385
var stop = function() {
419-
if (!__DEV__) {
420-
warnInProduction();
421-
return;
422-
}
423-
424386
ReactDebugToolDev.endProfiling();
425387
};
426388

427389
var isRunning = function() {
428-
if (!__DEV__) {
429-
warnInProduction();
430-
return false;
431-
}
432-
433390
return ReactDebugToolDev.isProfiling();
434391
};
435392

src/renderers/shared/__tests__/ReactPerfDev-test.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -446,29 +446,6 @@ describe('ReactPerfDev', function() {
446446
expect(ReactPerfDev.isRunning()).toBe(false);
447447
});
448448

449-
it('should print console error only once', () => {
450-
__DEV__ = false;
451-
452-
spyOn(console, 'error');
453-
454-
expect(ReactPerfDev.getLastMeasurements()).toEqual([]);
455-
expect(ReactPerfDev.getExclusive()).toEqual([]);
456-
expect(ReactPerfDev.getInclusive()).toEqual([]);
457-
expect(ReactPerfDev.getWasted()).toEqual([]);
458-
expect(ReactPerfDev.getOperations()).toEqual([]);
459-
expect(ReactPerfDev.printExclusive()).toEqual(undefined);
460-
expect(ReactPerfDev.printInclusive()).toEqual(undefined);
461-
expect(ReactPerfDev.printWasted()).toEqual(undefined);
462-
expect(ReactPerfDev.printOperations()).toEqual(undefined);
463-
expect(ReactPerfDev.start()).toBe(undefined);
464-
expect(ReactPerfDev.stop()).toBe(undefined);
465-
expect(ReactPerfDev.isRunning()).toBe(false);
466-
467-
expect(console.error.calls.count()).toBe(1);
468-
469-
__DEV__ = true;
470-
});
471-
472449
it('should work when measurement starts during reconciliation', () => {
473450
// https://github.com/facebook/react/issues/6949#issuecomment-230371009
474451
class Measurer extends React.Component {
@@ -517,3 +494,45 @@ describe('ReactPerfDev', function() {
517494
}]);
518495
});
519496
});
497+
498+
describe('ReactPerfDev in production', () => {
499+
var ReactPerfDev;
500+
var oldProcess;
501+
502+
beforeEach(function() {
503+
__DEV__ = false;
504+
oldProcess = process;
505+
global.process = {env: {NODE_ENV: 'production'}};
506+
507+
jest.resetModuleRegistry();
508+
ReactPerfDev = require('ReactPerfDev');
509+
});
510+
511+
afterEach(function() {
512+
__DEV__ = true;
513+
global.process = oldProcess;
514+
});
515+
516+
it('should be disabled in production and print console error only once', () => {
517+
__DEV__ = false;
518+
519+
spyOn(console, 'error');
520+
521+
expect(ReactPerfDev.getLastMeasurements()).toEqual([]);
522+
expect(ReactPerfDev.getExclusive()).toEqual([]);
523+
expect(ReactPerfDev.getInclusive()).toEqual([]);
524+
expect(ReactPerfDev.getWasted()).toEqual([]);
525+
expect(ReactPerfDev.getOperations()).toEqual([]);
526+
expect(ReactPerfDev.printExclusive()).toEqual(undefined);
527+
expect(ReactPerfDev.printInclusive()).toEqual(undefined);
528+
expect(ReactPerfDev.printWasted()).toEqual(undefined);
529+
expect(ReactPerfDev.printOperations()).toEqual(undefined);
530+
expect(ReactPerfDev.start()).toBe(undefined);
531+
expect(ReactPerfDev.stop()).toBe(undefined);
532+
expect(ReactPerfDev.isRunning()).toBe(false);
533+
534+
expect(console.error.calls.count()).toBe(1);
535+
536+
__DEV__ = true;
537+
});
538+
});

0 commit comments

Comments
 (0)