cleanup and open source JS BugReporting module
Summary:This is just the JS part of a bug reporting module that can be used with a pure JS flow or a native reporting flow. It's handy for grabbing data from a bunch of random components, like the ids of the rows that are visible in a list view at the time the bug is reported, or a description of the current navigation stack. It's initialized in `AppRegistry` so it's always ready to go and bug reports will always include the universal `runApplication` info, but won't actually do anything by default unless `collectExtraData` is called. Note: some apps may call `runApplication` multiple times if they have multiple react native root views. This will just overwrite the source so it will just report the most recently mounted one. Reviewed By: foghina Differential Revision: D3200203 fb-gh-sync-id: 8ed45fc9b289e8d9c50d3c85926213bd245d3ecc fbshipit-source-id: 8ed45fc9b289e8d9c50d3c85926213bd245d3ecc
This commit is contained in:
parent
eb21b25e2d
commit
ad15b74aae
|
@ -12,6 +12,7 @@
|
|||
'use strict';
|
||||
|
||||
var BatchedBridge = require('BatchedBridge');
|
||||
var BugReporting = require('BugReporting');
|
||||
var ReactNative = require('ReactNative');
|
||||
|
||||
var invariant = require('fbjs/lib/invariant');
|
||||
|
@ -79,13 +80,15 @@ var AppRegistry = {
|
|||
},
|
||||
|
||||
runApplication: function(appKey: string, appParameters: any): void {
|
||||
console.log(
|
||||
const msg =
|
||||
'Running application "' + appKey + '" with appParams: ' +
|
||||
JSON.stringify(appParameters) + '. ' +
|
||||
'__DEV__ === ' + String(__DEV__) +
|
||||
', development-level warning are ' + (__DEV__ ? 'ON' : 'OFF') +
|
||||
', performance optimizations are ' + (__DEV__ ? 'OFF' : 'ON')
|
||||
);
|
||||
', performance optimizations are ' + (__DEV__ ? 'OFF' : 'ON');
|
||||
console.log(msg);
|
||||
BugReporting.init();
|
||||
BugReporting.addSource('AppRegistry.runApplication', () => msg);
|
||||
invariant(
|
||||
runnables[appKey] && runnables[appKey].run,
|
||||
'Application ' + appKey + ' has not been registered. This ' +
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
* Copyright (c) 2013-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule BugReporting
|
||||
* @flow
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const BatchedBridge = require('BatchedBridge');
|
||||
const BugReportingNativeModule = require('NativeModules').BugReporting;
|
||||
|
||||
type ExtraData = { [key: string]: string };
|
||||
type SourceCallback = () => string;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* that are currently visible on screen. Components should also remember to call `remove()` on the object that is
|
||||
* returned by `addSource` when they are unmounted.
|
||||
*/
|
||||
class BugReporting {
|
||||
|
||||
static _sources: Map<string, SourceCallback> = new Map();
|
||||
|
||||
/**
|
||||
* `init` is called in `AppRegistry.runApplication`, so you shouldn't have to worry about it.
|
||||
*/
|
||||
static init() {
|
||||
BatchedBridge.registerCallableModule( // idempotent
|
||||
'BugReporting',
|
||||
BugReporting,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a string key to a simple callback that should return a string payload to be attached
|
||||
* to a bug report. Source callbacks are called when `collectExtraData` is called.
|
||||
*
|
||||
* Returns an object to remove the source when the component unmounts.
|
||||
*
|
||||
* Conflicts trample with a warning.
|
||||
*/
|
||||
static addSource(key: string, callback: SourceCallback): {remove: () => void} {
|
||||
if (BugReporting._sources.has(key)) {
|
||||
console.warn(`BugReporting.addSource called multiple times for same key '${key}'`);
|
||||
}
|
||||
BugReporting._sources.set(key, callback);
|
||||
return {remove: () => { BugReporting._sources.delete(key); }};
|
||||
}
|
||||
|
||||
/**
|
||||
* This can be called from a native bug reporting flow, or from JS code.
|
||||
*
|
||||
* If available, this will call `NativeModules.BugReporting.setExtraData(extraData)`
|
||||
* after collecting `extraData`.
|
||||
*/
|
||||
static collectExtraData(): ExtraData {
|
||||
const extraData: ExtraData = {};
|
||||
for (const [key, callback] of BugReporting._sources) {
|
||||
extraData[key] = callback();
|
||||
}
|
||||
console.log('BugReporting extraData:', extraData);
|
||||
BugReportingNativeModule &&
|
||||
BugReportingNativeModule.setExtraData &&
|
||||
BugReportingNativeModule.setExtraData(extraData);
|
||||
return extraData;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BugReporting;
|
Loading…
Reference in New Issue