Skip to content

Commit

Permalink
Add support for custom files in BugReporting
Browse files Browse the repository at this point in the history
Reviewed By: astreet

Differential Revision: D3287186

fbshipit-source-id: ce7000ac110b78a4fb94c66229046617c292675f
  • Loading branch information
lexs authored and Facebook Github Bot 4 committed May 16, 2016
1 parent 6a3fc86 commit 610cfdc
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions Libraries/BugReporting/BugReporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

/**
Expand All @@ -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); }};
}

/**
Expand All @@ -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 };
}
}

Expand Down

0 comments on commit 610cfdc

Please sign in to comment.