From 610cfdc8198133bc89e6977193c2d7b217b03e61 Mon Sep 17 00:00:00 2001 From: Alexander Blom Date: Mon, 16 May 2016 07:13:20 -0700 Subject: [PATCH] Add support for custom files in BugReporting Reviewed By: astreet Differential Revision: D3287186 fbshipit-source-id: ce7000ac110b78a4fb94c66229046617c292675f --- Libraries/BugReporting/BugReporting.js | 43 ++++++++++++++++++++------ 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/Libraries/BugReporting/BugReporting.js b/Libraries/BugReporting/BugReporting.js index 7ac4a121b..ceeb0df58 100644 --- a/Libraries/BugReporting/BugReporting.js +++ b/Libraries/BugReporting/BugReporting.js @@ -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 = new Map(); + static _extraSources: Map = new Map(); + static _fileSources: Map = 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): {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 }; } }