From ca53f35875f9dfd1f78dbd647a44be8adb005910 Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Thu, 28 Nov 2019 03:47:32 -0800 Subject: [PATCH] Refactor export data functions and exportpersistedstate function to just expect state Summary: This diff refactors the `exportpersistedstate` and few of the functions in exportData.tsx to just expect the Redux State instead the store object. Reviewed By: mweststrate Differential Revision: D18733011 fbshipit-source-id: 56739917b49142ba4b6e79e7c16378fe60d6ac3b --- headless/index.tsx | 6 +++-- src/chrome/ShareSheetExportUrl.tsx | 2 +- src/index.tsx | 2 +- src/plugin.tsx | 3 ++- src/plugins/fresco/index.tsx | 4 +-- src/plugins/layout/index.tsx | 4 +-- src/utils/__tests__/pluginUtils.node.js | 4 +-- src/utils/exportData.tsx | 35 ++++++++++++------------- src/utils/exportMetrics.tsx | 2 +- 9 files changed, 32 insertions(+), 30 deletions(-) diff --git a/headless/index.tsx b/headless/index.tsx index 2c078201a..e35b8dc67 100644 --- a/headless/index.tsx +++ b/headless/index.tsx @@ -177,7 +177,9 @@ async function exitActions( ); outputAndExit(payload); } else { - const {serializedString, errorArray} = await exportStore(store); + const {serializedString, errorArray} = await exportStore( + store.getState(), + ); errorArray.forEach(console.error); outputAndExit(serializedString); } @@ -254,7 +256,7 @@ async function startFlipper(userArguments: UserArguments) { errorAndExit(e); }); } else { - exportStore(store) + exportStore(store.getState()) .then(({serializedString}) => { outputAndExit(serializedString); }) diff --git a/src/chrome/ShareSheetExportUrl.tsx b/src/chrome/ShareSheetExportUrl.tsx index 3200a1558..2bde79b50 100644 --- a/src/chrome/ShareSheetExportUrl.tsx +++ b/src/chrome/ShareSheetExportUrl.tsx @@ -118,7 +118,7 @@ export default class ShareSheetExportUrl extends Component { } }; const {serializedString, errorArray} = await reportPlatformFailures( - exportStore(store, this.idler, statusUpdate), + exportStore(store.getState(), this.idler, statusUpdate), `${EXPORT_FLIPPER_TRACE_EVENT}:UI_LINK`, ); const uploadMarker = `${EXPORT_FLIPPER_TRACE_EVENT}:upload`; diff --git a/src/index.tsx b/src/index.tsx index 2ce55d20d..350bd4283 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -31,7 +31,7 @@ export {selectPlugin, StaticView} from './reducers/connections'; export {writeBufferToFile, bufferToBlob} from './utils/screenshot'; export {getPluginKey, getPersistedState} from './utils/pluginUtils'; export {Idler} from './utils/Idler'; -export {Store, MiddlewareAPI} from './reducers/index'; +export {Store, MiddlewareAPI, State as ReduxState} from './reducers/index'; export {default as BaseDevice} from './devices/BaseDevice'; export {default as SidebarExtensions} from './fb-stubs/LayoutInspectorSidebarExtensions'; export { diff --git a/src/plugin.tsx b/src/plugin.tsx index 426fb0573..690d3b435 100644 --- a/src/plugin.tsx +++ b/src/plugin.tsx @@ -18,6 +18,7 @@ import BaseDevice from './devices/BaseDevice'; import {serialize, deserialize} from './utils/serialization'; import {Idler} from './utils/Idler'; import {StaticView} from './reducers/connections'; +import {State as ReduxState} from './reducers'; type Parameters = any; // This function is intended to be called from outside of the plugin. @@ -103,7 +104,7 @@ export abstract class FlipperBasePlugin< | (( callClient: (method: string, params?: any) => Promise, persistedState: StaticPersistedState | undefined, - store: MiddlewareAPI | undefined, + store: ReduxState | undefined, idler?: Idler, statusUpdate?: (msg: string) => void, ) => Promise) diff --git a/src/plugins/fresco/index.tsx b/src/plugins/fresco/index.tsx index ec61f199f..749242236 100644 --- a/src/plugins/fresco/index.tsx +++ b/src/plugins/fresco/index.tsx @@ -19,7 +19,7 @@ import { } from './api'; import {Fragment} from 'react'; import {ImagesMap} from './ImagePool'; -import {MetricType, MiddlewareAPI} from 'flipper'; +import {MetricType, ReduxState} from 'flipper'; import React from 'react'; import ImagesCacheOverview from './ImagesCacheOverview'; import { @@ -101,7 +101,7 @@ export default class FlipperImagesPlugin extends FlipperPlugin< static exportPersistedState = ( callClient: (method: string, params?: any) => Promise, persistedState: PersistedState, - store?: MiddlewareAPI, + store?: ReduxState, ): Promise => { const defaultPromise = Promise.resolve(persistedState); if (!persistedState) { diff --git a/src/plugins/layout/index.tsx b/src/plugins/layout/index.tsx index 9881f6e85..27f9c62de 100644 --- a/src/plugins/layout/index.tsx +++ b/src/plugins/layout/index.tsx @@ -11,7 +11,6 @@ import { ElementID, Element, ElementSearchResultSet, - MiddlewareAPI, PluginClient, FlexColumn, FlexRow, @@ -27,6 +26,7 @@ import { colors, SupportRequestFormManager, constants, + ReduxState, } from 'flipper'; import Inspector from './Inspector'; import ToolbarIcon from './ToolbarIcon'; @@ -97,7 +97,7 @@ export default class Layout extends FlipperPlugin { allNodes: PersistedState; }>, persistedState: PersistedState | undefined, - store: MiddlewareAPI | undefined, + store: ReduxState | undefined, ): Promise => { if (!store) { return persistedState; diff --git a/src/utils/__tests__/pluginUtils.node.js b/src/utils/__tests__/pluginUtils.node.js index 945d8a110..ddfc7b8e7 100644 --- a/src/utils/__tests__/pluginUtils.node.js +++ b/src/utils/__tests__/pluginUtils.node.js @@ -15,7 +15,7 @@ import type {State as PluginsState} from '../../reducers/plugins.tsx'; import type {State as PluginStatesState} from '../../reducers/pluginStates.tsx'; import type {PluginDefinition} from '../../dispatcher/plugins.tsx'; import {FlipperBasePlugin} from 'flipper'; -import type {MiddlewareAPI} from '../../reducers/index.tsx'; +import type {ReduxState} from '../../reducers/index.tsx'; class MockFlipperPluginWithDefaultPersistedState extends FlipperBasePlugin< *, *, @@ -32,7 +32,7 @@ class MockFlipperPluginWithExportPersistedState extends FlipperBasePlugin< static exportPersistedState = ( callClient: (string, ?Object) => Promise, persistedState: ?{msg: string}, - store: ?MiddlewareAPI, + store: ?ReduxState, ): Promise => { return Promise.resolve({msg: 'MockFlipperPluginWithExportPersistedState'}); }; diff --git a/src/utils/exportData.tsx b/src/utils/exportData.tsx index 1baecc8e8..f1de61302 100644 --- a/src/utils/exportData.tsx +++ b/src/utils/exportData.tsx @@ -11,7 +11,7 @@ import os from 'os'; import path from 'path'; import electron from 'electron'; import {getInstance as getLogger} from '../fb-stubs/Logger'; -import {Store, MiddlewareAPI} from '../reducers'; +import {Store, State as ReduxState} from '../reducers'; import {DeviceExport} from '../devices/BaseDevice'; import {State as PluginStatesState} from '../reducers/pluginStates'; import {PluginNotification} from '../reducers/notifications'; @@ -397,13 +397,13 @@ export async function fetchMetadata( clients: Client[], pluginStates: PluginStatesState, pluginsMap: Map, - store: MiddlewareAPI, + state: ReduxState, statusUpdate?: (msg: string) => void, idler?: Idler, ): Promise<{pluginStates: PluginStatesState; errorArray: Array}> { const newPluginState = {...pluginStates}; const errorArray: Array = []; - const selectedDevice = store.getState().connections.selectedDevice; + const selectedDevice = state.connections.selectedDevice; for (const client of clients) { if ( !selectedDevice || @@ -412,7 +412,7 @@ export async function fetchMetadata( ) { continue; } - const selectedPlugins = store.getState().plugins.selectedPlugins; + const selectedPlugins = state.plugins.selectedPlugins; const selectedFilteredPlugins = selectedPlugins.length > 0 ? client.plugins.filter(plugin => selectedPlugins.includes(plugin)) @@ -436,7 +436,7 @@ export async function fetchMetadata( exportState( callClient(client, plugin), newPluginState[key], - store, + state, idler, statusUpdate, ), @@ -462,18 +462,17 @@ export async function fetchMetadata( } export async function getStoreExport( - store: MiddlewareAPI, + state: ReduxState, statusUpdate?: (msg: string) => void, idler?: Idler, ): Promise<{exportData: ExportType | null; errorArray: Array}> { - const state = store.getState(); const {clients} = state.connections; const client = clients.find( client => client.id === state.connections.selectedApp, ); const {pluginStates} = state; const {plugins} = state; - const {selectedDevice} = store.getState().connections; + const {selectedDevice} = state.connections; if (!selectedDevice) { throw new Error('Please select a device before exporting data.'); } @@ -500,18 +499,18 @@ export async function getStoreExport( [client], pluginStates, pluginsMap, - store, + state, statusUpdate, idler, ); getLogger().trackTimeSince(fetchMetaDataMarker, fetchMetaDataMarker, { - plugins: store.getState().plugins.selectedPlugins, + plugins: state.plugins.selectedPlugins, }); const {errorArray} = metadata; const newPluginState = metadata.pluginStates; - const {activeNotifications} = store.getState().notifications; - const {devicePlugins, clientPlugins} = store.getState().plugins; + const {activeNotifications} = state.notifications; + const {devicePlugins, clientPlugins} = state.plugins; const exportData = await processStore( { activeNotifications, @@ -521,7 +520,7 @@ export async function getStoreExport( devicePlugins, clientPlugins, salt: uuid.v4(), - selectedPlugins: store.getState().plugins.selectedPlugins, + selectedPlugins: state.plugins.selectedPlugins, statusUpdate, }, idler, @@ -530,7 +529,7 @@ export async function getStoreExport( } export function exportStore( - store: MiddlewareAPI, + state: ReduxState, idler?: Idler, statusUpdate?: (msg: string) => void, ): Promise<{serializedString: string; errorArray: Array}> { @@ -540,12 +539,12 @@ export function exportStore( try { statusUpdate && statusUpdate('Preparing to export Flipper data...'); const {exportData, errorArray} = await getStoreExport( - store, + state, statusUpdate, idler, ); if (exportData != null) { - exportData.supportRequestDetails = store.getState().supportForm?.supportFormV2; + exportData.supportRequestDetails = state.supportForm?.supportFormV2; statusUpdate && statusUpdate('Serializing Flipper data...'); const serializedString = JSON.stringify(exportData); if (serializedString.length <= 0) { @@ -555,7 +554,7 @@ export function exportStore( EXPORT_FLIPPER_TRACE_TIME_SERIALIZATION_EVENT, EXPORT_FLIPPER_TRACE_TIME_SERIALIZATION_EVENT, { - plugins: store.getState().plugins.selectedPlugins, + plugins: state.plugins.selectedPlugins, }, ); resolve({serializedString, errorArray}); @@ -575,7 +574,7 @@ export const exportStoreToFile = ( idler?: Idler, statusUpdate?: (msg: string) => void, ): Promise<{errorArray: Array}> => { - return exportStore(store, idler, statusUpdate).then( + return exportStore(store.getState(), idler, statusUpdate).then( ({serializedString, errorArray}) => { return promisify(fs.writeFile)(exportFilePath, serializedString).then( () => { diff --git a/src/utils/exportMetrics.tsx b/src/utils/exportMetrics.tsx index 98b0a01d1..57c20adbc 100644 --- a/src/utils/exportMetrics.tsx +++ b/src/utils/exportMetrics.tsx @@ -68,7 +68,7 @@ export async function exportMetricsWithoutTrace( store.getState().connections.clients, pluginStates, pluginsMap, - store, + store.getState(), ); const newPluginStates = metadata.pluginStates; const {errorArray} = metadata;