Refactor BugReporing to avoid multiple registers

Reviewed By: sahrens

Differential Revision: D3316146

fbshipit-source-id: 80df4ea73150ba6920a03fe336b3ddb207ba535a
This commit is contained in:
Alexander Blom 2016-05-18 14:23:40 -07:00 committed by Facebook Github Bot 7
parent 7db7f78dc7
commit dc6a44713d
2 changed files with 7 additions and 6 deletions

View File

@ -89,9 +89,7 @@ var AppRegistry = {
', development-level warning are ' + (__DEV__ ? 'ON' : 'OFF') +
', performance optimizations are ' + (__DEV__ ? 'OFF' : 'ON');
infoLog(msg);
BugReporting.init();
BugReporting.addSource('AppRegistry.runApplication' + runCount++, () => msg);
BugReporting.addFileSource('react_hierarchy.txt', () => require('dumpReactTree')());
invariant(
runnables[appKey] && runnables[appKey].run,
'Application ' + appKey + ' has not been registered. This ' +

View File

@ -22,6 +22,10 @@ type ExtraData = { [key: string]: string };
type SourceCallback = () => string;
type DebugData = { extras: ExtraData, files: ExtraData };
function defaultExtras() {
BugReporting.addFileSource('react_hierarchy.txt', () => require('dumpReactTree')());
}
/**
* A simple class for collecting bug report data. Components can add sources that will be queried when a bug report
* is created via `collectExtraData`. For example, a list component might add a source that provides the list of rows
@ -33,13 +37,11 @@ class BugReporting {
static _fileSources: Map<string, SourceCallback> = new Map();
static _subscription: ?EmitterSubscription = null;
/**
* `init` is called in `AppRegistry.runApplication`, so you shouldn't have to worry about it.
*/
static init() {
static _maybeInit() {
if (!BugReporting._subscription) {
BugReporting._subscription = RCTDeviceEventEmitter
.addListener('collectBugExtraData', BugReporting.collectExtraData, null);
defaultExtras();
}
}
@ -68,6 +70,7 @@ class BugReporting {
}
static _addSource(key: string, callback: SourceCallback, source: Map<string, SourceCallback>): {remove: () => void} {
BugReporting._maybeInit();
if (source.has(key)) {
console.warn(`BugReporting.add* called multiple times for same key '${key}'`);
}