Add support for custom files in BugReporting

Reviewed By: astreet

Differential Revision: D3287186

fbshipit-source-id: ce7000ac110b78a4fb94c66229046617c292675f
This commit is contained in:
Alexander Blom 2016-05-16 07:13:20 -07:00 committed by Facebook Github Bot 4
parent 6a3fc86778
commit 610cfdc819
1 changed files with 33 additions and 10 deletions

View File

@ -13,11 +13,13 @@
const BugReportingNativeModule = require('NativeModules').BugReporting;
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
const Map = require('Map');
import type EmitterSubscription from 'EmitterSubscription';
type ExtraData = { [key: string]: string };
type SourceCallback = () => string;
type DebugData = { extras: ExtraData, files: ExtraData };
/**
* A simple class for collecting bug report data. Components can add sources that will be queried when a bug report
@ -26,8 +28,8 @@ type SourceCallback = () => string;
* returned by `addSource` when they are unmounted.
*/
class BugReporting {
static _sources: Map<string, SourceCallback> = new Map();
static _extraSources: Map<string, SourceCallback> = new Map();
static _fileSources: Map<string, SourceCallback> = new Map();
static _subscription: ?EmitterSubscription = null;
/**
@ -49,11 +51,27 @@ class BugReporting {
* 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}'`);
return this._addSource(key, callback, BugReporting._extraSources);
}
/**
* 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 addFileSource(key: string, callback: SourceCallback): {remove: () => void} {
return this._addSource(key, callback, BugReporting._fileSources);
}
static _addSource(key: string, callback: SourceCallback, source: Map<string, SourceCallback>): {remove: () => void} {
if (source.has(key)) {
console.warn(`BugReporting.add* called multiple times for same key '${key}'`);
}
BugReporting._sources.set(key, callback);
return {remove: () => { BugReporting._sources.delete(key); }};
source.set(key, callback);
return {remove: () => { source.delete(key); }};
}
/**
@ -62,16 +80,21 @@ class BugReporting {
* If available, this will call `NativeModules.BugReporting.setExtraData(extraData)`
* after collecting `extraData`.
*/
static collectExtraData(): ExtraData {
static collectExtraData(): DebugData {
const extraData: ExtraData = {};
for (const [key, callback] of BugReporting._sources) {
for (const [key, callback] of BugReporting._extraSources) {
extraData[key] = callback();
}
const fileData: ExtraData = {};
for (const [key, callback] of BugReporting._fileSources) {
fileData[key] = callback();
}
console.log('BugReporting extraData:', extraData);
BugReportingNativeModule &&
BugReportingNativeModule.setExtraData &&
BugReportingNativeModule.setExtraData(extraData);
return extraData;
BugReportingNativeModule.setExtraData(extraData, fileData);
return { extras: extraData, files: fileData };
}
}