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
This commit is contained in:
Pritesh Nandgaonkar
2019-11-28 03:47:32 -08:00
committed by Facebook Github Bot
parent bf85da17ab
commit ca53f35875
9 changed files with 32 additions and 30 deletions

View File

@@ -177,7 +177,9 @@ async function exitActions(
); );
outputAndExit(payload); outputAndExit(payload);
} else { } else {
const {serializedString, errorArray} = await exportStore(store); const {serializedString, errorArray} = await exportStore(
store.getState(),
);
errorArray.forEach(console.error); errorArray.forEach(console.error);
outputAndExit(serializedString); outputAndExit(serializedString);
} }
@@ -254,7 +256,7 @@ async function startFlipper(userArguments: UserArguments) {
errorAndExit(e); errorAndExit(e);
}); });
} else { } else {
exportStore(store) exportStore(store.getState())
.then(({serializedString}) => { .then(({serializedString}) => {
outputAndExit(serializedString); outputAndExit(serializedString);
}) })

View File

@@ -118,7 +118,7 @@ export default class ShareSheetExportUrl extends Component<Props, State> {
} }
}; };
const {serializedString, errorArray} = await reportPlatformFailures( const {serializedString, errorArray} = await reportPlatformFailures(
exportStore(store, this.idler, statusUpdate), exportStore(store.getState(), this.idler, statusUpdate),
`${EXPORT_FLIPPER_TRACE_EVENT}:UI_LINK`, `${EXPORT_FLIPPER_TRACE_EVENT}:UI_LINK`,
); );
const uploadMarker = `${EXPORT_FLIPPER_TRACE_EVENT}:upload`; const uploadMarker = `${EXPORT_FLIPPER_TRACE_EVENT}:upload`;

View File

@@ -31,7 +31,7 @@ export {selectPlugin, StaticView} from './reducers/connections';
export {writeBufferToFile, bufferToBlob} from './utils/screenshot'; export {writeBufferToFile, bufferToBlob} from './utils/screenshot';
export {getPluginKey, getPersistedState} from './utils/pluginUtils'; export {getPluginKey, getPersistedState} from './utils/pluginUtils';
export {Idler} from './utils/Idler'; 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 BaseDevice} from './devices/BaseDevice';
export {default as SidebarExtensions} from './fb-stubs/LayoutInspectorSidebarExtensions'; export {default as SidebarExtensions} from './fb-stubs/LayoutInspectorSidebarExtensions';
export { export {

View File

@@ -18,6 +18,7 @@ import BaseDevice from './devices/BaseDevice';
import {serialize, deserialize} from './utils/serialization'; import {serialize, deserialize} from './utils/serialization';
import {Idler} from './utils/Idler'; import {Idler} from './utils/Idler';
import {StaticView} from './reducers/connections'; import {StaticView} from './reducers/connections';
import {State as ReduxState} from './reducers';
type Parameters = any; type Parameters = any;
// This function is intended to be called from outside of the plugin. // 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<any>, callClient: (method: string, params?: any) => Promise<any>,
persistedState: StaticPersistedState | undefined, persistedState: StaticPersistedState | undefined,
store: MiddlewareAPI | undefined, store: ReduxState | undefined,
idler?: Idler, idler?: Idler,
statusUpdate?: (msg: string) => void, statusUpdate?: (msg: string) => void,
) => Promise<StaticPersistedState | undefined>) ) => Promise<StaticPersistedState | undefined>)

View File

@@ -19,7 +19,7 @@ import {
} from './api'; } from './api';
import {Fragment} from 'react'; import {Fragment} from 'react';
import {ImagesMap} from './ImagePool'; import {ImagesMap} from './ImagePool';
import {MetricType, MiddlewareAPI} from 'flipper'; import {MetricType, ReduxState} from 'flipper';
import React from 'react'; import React from 'react';
import ImagesCacheOverview from './ImagesCacheOverview'; import ImagesCacheOverview from './ImagesCacheOverview';
import { import {
@@ -101,7 +101,7 @@ export default class FlipperImagesPlugin extends FlipperPlugin<
static exportPersistedState = ( static exportPersistedState = (
callClient: (method: string, params?: any) => Promise<any>, callClient: (method: string, params?: any) => Promise<any>,
persistedState: PersistedState, persistedState: PersistedState,
store?: MiddlewareAPI, store?: ReduxState,
): Promise<PersistedState> => { ): Promise<PersistedState> => {
const defaultPromise = Promise.resolve(persistedState); const defaultPromise = Promise.resolve(persistedState);
if (!persistedState) { if (!persistedState) {

View File

@@ -11,7 +11,6 @@ import {
ElementID, ElementID,
Element, Element,
ElementSearchResultSet, ElementSearchResultSet,
MiddlewareAPI,
PluginClient, PluginClient,
FlexColumn, FlexColumn,
FlexRow, FlexRow,
@@ -27,6 +26,7 @@ import {
colors, colors,
SupportRequestFormManager, SupportRequestFormManager,
constants, constants,
ReduxState,
} from 'flipper'; } from 'flipper';
import Inspector from './Inspector'; import Inspector from './Inspector';
import ToolbarIcon from './ToolbarIcon'; import ToolbarIcon from './ToolbarIcon';
@@ -97,7 +97,7 @@ export default class Layout extends FlipperPlugin<State, any, PersistedState> {
allNodes: PersistedState; allNodes: PersistedState;
}>, }>,
persistedState: PersistedState | undefined, persistedState: PersistedState | undefined,
store: MiddlewareAPI | undefined, store: ReduxState | undefined,
): Promise<PersistedState | undefined> => { ): Promise<PersistedState | undefined> => {
if (!store) { if (!store) {
return persistedState; return persistedState;

View File

@@ -15,7 +15,7 @@ import type {State as PluginsState} from '../../reducers/plugins.tsx';
import type {State as PluginStatesState} from '../../reducers/pluginStates.tsx'; import type {State as PluginStatesState} from '../../reducers/pluginStates.tsx';
import type {PluginDefinition} from '../../dispatcher/plugins.tsx'; import type {PluginDefinition} from '../../dispatcher/plugins.tsx';
import {FlipperBasePlugin} from 'flipper'; import {FlipperBasePlugin} from 'flipper';
import type {MiddlewareAPI} from '../../reducers/index.tsx'; import type {ReduxState} from '../../reducers/index.tsx';
class MockFlipperPluginWithDefaultPersistedState extends FlipperBasePlugin< class MockFlipperPluginWithDefaultPersistedState extends FlipperBasePlugin<
*, *,
*, *,
@@ -32,7 +32,7 @@ class MockFlipperPluginWithExportPersistedState extends FlipperBasePlugin<
static exportPersistedState = ( static exportPersistedState = (
callClient: (string, ?Object) => Promise<Object>, callClient: (string, ?Object) => Promise<Object>,
persistedState: ?{msg: string}, persistedState: ?{msg: string},
store: ?MiddlewareAPI, store: ?ReduxState,
): Promise<?{msg: string}> => { ): Promise<?{msg: string}> => {
return Promise.resolve({msg: 'MockFlipperPluginWithExportPersistedState'}); return Promise.resolve({msg: 'MockFlipperPluginWithExportPersistedState'});
}; };

View File

@@ -11,7 +11,7 @@ import os from 'os';
import path from 'path'; import path from 'path';
import electron from 'electron'; import electron from 'electron';
import {getInstance as getLogger} from '../fb-stubs/Logger'; 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 {DeviceExport} from '../devices/BaseDevice';
import {State as PluginStatesState} from '../reducers/pluginStates'; import {State as PluginStatesState} from '../reducers/pluginStates';
import {PluginNotification} from '../reducers/notifications'; import {PluginNotification} from '../reducers/notifications';
@@ -397,13 +397,13 @@ export async function fetchMetadata(
clients: Client[], clients: Client[],
pluginStates: PluginStatesState, pluginStates: PluginStatesState,
pluginsMap: Map<string, typeof FlipperDevicePlugin | typeof FlipperPlugin>, pluginsMap: Map<string, typeof FlipperDevicePlugin | typeof FlipperPlugin>,
store: MiddlewareAPI, state: ReduxState,
statusUpdate?: (msg: string) => void, statusUpdate?: (msg: string) => void,
idler?: Idler, idler?: Idler,
): Promise<{pluginStates: PluginStatesState; errorArray: Array<Error>}> { ): Promise<{pluginStates: PluginStatesState; errorArray: Array<Error>}> {
const newPluginState = {...pluginStates}; const newPluginState = {...pluginStates};
const errorArray: Array<Error> = []; const errorArray: Array<Error> = [];
const selectedDevice = store.getState().connections.selectedDevice; const selectedDevice = state.connections.selectedDevice;
for (const client of clients) { for (const client of clients) {
if ( if (
!selectedDevice || !selectedDevice ||
@@ -412,7 +412,7 @@ export async function fetchMetadata(
) { ) {
continue; continue;
} }
const selectedPlugins = store.getState().plugins.selectedPlugins; const selectedPlugins = state.plugins.selectedPlugins;
const selectedFilteredPlugins = const selectedFilteredPlugins =
selectedPlugins.length > 0 selectedPlugins.length > 0
? client.plugins.filter(plugin => selectedPlugins.includes(plugin)) ? client.plugins.filter(plugin => selectedPlugins.includes(plugin))
@@ -436,7 +436,7 @@ export async function fetchMetadata(
exportState( exportState(
callClient(client, plugin), callClient(client, plugin),
newPluginState[key], newPluginState[key],
store, state,
idler, idler,
statusUpdate, statusUpdate,
), ),
@@ -462,18 +462,17 @@ export async function fetchMetadata(
} }
export async function getStoreExport( export async function getStoreExport(
store: MiddlewareAPI, state: ReduxState,
statusUpdate?: (msg: string) => void, statusUpdate?: (msg: string) => void,
idler?: Idler, idler?: Idler,
): Promise<{exportData: ExportType | null; errorArray: Array<Error>}> { ): Promise<{exportData: ExportType | null; errorArray: Array<Error>}> {
const state = store.getState();
const {clients} = state.connections; const {clients} = state.connections;
const client = clients.find( const client = clients.find(
client => client.id === state.connections.selectedApp, client => client.id === state.connections.selectedApp,
); );
const {pluginStates} = state; const {pluginStates} = state;
const {plugins} = state; const {plugins} = state;
const {selectedDevice} = store.getState().connections; const {selectedDevice} = state.connections;
if (!selectedDevice) { if (!selectedDevice) {
throw new Error('Please select a device before exporting data.'); throw new Error('Please select a device before exporting data.');
} }
@@ -500,18 +499,18 @@ export async function getStoreExport(
[client], [client],
pluginStates, pluginStates,
pluginsMap, pluginsMap,
store, state,
statusUpdate, statusUpdate,
idler, idler,
); );
getLogger().trackTimeSince(fetchMetaDataMarker, fetchMetaDataMarker, { getLogger().trackTimeSince(fetchMetaDataMarker, fetchMetaDataMarker, {
plugins: store.getState().plugins.selectedPlugins, plugins: state.plugins.selectedPlugins,
}); });
const {errorArray} = metadata; const {errorArray} = metadata;
const newPluginState = metadata.pluginStates; const newPluginState = metadata.pluginStates;
const {activeNotifications} = store.getState().notifications; const {activeNotifications} = state.notifications;
const {devicePlugins, clientPlugins} = store.getState().plugins; const {devicePlugins, clientPlugins} = state.plugins;
const exportData = await processStore( const exportData = await processStore(
{ {
activeNotifications, activeNotifications,
@@ -521,7 +520,7 @@ export async function getStoreExport(
devicePlugins, devicePlugins,
clientPlugins, clientPlugins,
salt: uuid.v4(), salt: uuid.v4(),
selectedPlugins: store.getState().plugins.selectedPlugins, selectedPlugins: state.plugins.selectedPlugins,
statusUpdate, statusUpdate,
}, },
idler, idler,
@@ -530,7 +529,7 @@ export async function getStoreExport(
} }
export function exportStore( export function exportStore(
store: MiddlewareAPI, state: ReduxState,
idler?: Idler, idler?: Idler,
statusUpdate?: (msg: string) => void, statusUpdate?: (msg: string) => void,
): Promise<{serializedString: string; errorArray: Array<Error>}> { ): Promise<{serializedString: string; errorArray: Array<Error>}> {
@@ -540,12 +539,12 @@ export function exportStore(
try { try {
statusUpdate && statusUpdate('Preparing to export Flipper data...'); statusUpdate && statusUpdate('Preparing to export Flipper data...');
const {exportData, errorArray} = await getStoreExport( const {exportData, errorArray} = await getStoreExport(
store, state,
statusUpdate, statusUpdate,
idler, idler,
); );
if (exportData != null) { if (exportData != null) {
exportData.supportRequestDetails = store.getState().supportForm?.supportFormV2; exportData.supportRequestDetails = state.supportForm?.supportFormV2;
statusUpdate && statusUpdate('Serializing Flipper data...'); statusUpdate && statusUpdate('Serializing Flipper data...');
const serializedString = JSON.stringify(exportData); const serializedString = JSON.stringify(exportData);
if (serializedString.length <= 0) { if (serializedString.length <= 0) {
@@ -555,7 +554,7 @@ export function exportStore(
EXPORT_FLIPPER_TRACE_TIME_SERIALIZATION_EVENT, EXPORT_FLIPPER_TRACE_TIME_SERIALIZATION_EVENT,
EXPORT_FLIPPER_TRACE_TIME_SERIALIZATION_EVENT, EXPORT_FLIPPER_TRACE_TIME_SERIALIZATION_EVENT,
{ {
plugins: store.getState().plugins.selectedPlugins, plugins: state.plugins.selectedPlugins,
}, },
); );
resolve({serializedString, errorArray}); resolve({serializedString, errorArray});
@@ -575,7 +574,7 @@ export const exportStoreToFile = (
idler?: Idler, idler?: Idler,
statusUpdate?: (msg: string) => void, statusUpdate?: (msg: string) => void,
): Promise<{errorArray: Array<Error>}> => { ): Promise<{errorArray: Array<Error>}> => {
return exportStore(store, idler, statusUpdate).then( return exportStore(store.getState(), idler, statusUpdate).then(
({serializedString, errorArray}) => { ({serializedString, errorArray}) => {
return promisify(fs.writeFile)(exportFilePath, serializedString).then( return promisify(fs.writeFile)(exportFilePath, serializedString).then(
() => { () => {

View File

@@ -68,7 +68,7 @@ export async function exportMetricsWithoutTrace(
store.getState().connections.clients, store.getState().connections.clients,
pluginStates, pluginStates,
pluginsMap, pluginsMap,
store, store.getState(),
); );
const newPluginStates = metadata.pluginStates; const newPluginStates = metadata.pluginStates;
const {errorArray} = metadata; const {errorArray} = metadata;