Convert multiple arguments as options for the helper functions

Summary: With the each addition of the CLI arguments, there were addition in the arguments to the helper functions for exportStore. To control it, I have moved those arguments into an object

Reviewed By: jknoxville

Differential Revision: D16378511

fbshipit-source-id: e620bc0d4863aa6029a57771aa970aebb7294ba9
This commit is contained in:
Pritesh Nandgaonkar
2019-07-22 12:50:22 -07:00
committed by Facebook Github Bot
parent aeee96c050
commit 23d4327f69
2 changed files with 188 additions and 130 deletions

View File

@@ -150,20 +150,28 @@ test('test generateNotifications helper function', () => {
});
test('test processStore function for empty state', () => {
const json = processStore([], null, {}, [], new Map(), 'salt', []);
const json = processStore({
activeNotifications: [],
device: null,
pluginStates: {},
clients: [],
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: [],
});
expect(json).resolves.toBeNull();
});
test('test processStore function for an iOS device connected', async () => {
const json = await processStore(
[],
new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS', []),
{},
[],
new Map(),
'salt',
[],
);
const json = await processStore({
activeNotifications: [],
device: new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS', []),
pluginStates: {},
clients: [],
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
// $FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {device, clients} = json;
@@ -188,15 +196,15 @@ test('test processStore function for an iOS device connected with client plugin
[],
);
const clientIdentifier = generateClientIdentifier(device, 'testapp');
const json = await processStore(
[],
const json = await processStore({
activeNotifications: [],
device,
{[clientIdentifier]: {msg: 'Test plugin'}},
[generateClientFromDevice(device, 'testapp')],
new Map(),
'salt',
[],
);
pluginStates: {[clientIdentifier]: {msg: 'Test plugin'}},
clients: [generateClientFromDevice(device, 'testapp')],
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;
@@ -236,11 +244,10 @@ test('test processStore function to have only the client for the selected device
selectedDevice,
'testapp',
);
const json = await processStore(
[],
selectedDevice,
{
const json = await processStore({
activeNotifications: [],
device: selectedDevice,
pluginStates: {
[unselectedDeviceClientIdentifier + '#testapp']: {
msg: 'Test plugin unselected device',
},
@@ -248,14 +255,14 @@ test('test processStore function to have only the client for the selected device
msg: 'Test plugin selected device',
},
},
[
clients: [
selectedDeviceClient,
generateClientFromDevice(unselectedDevice, 'testapp'),
],
new Map(),
'salt',
[],
);
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already added
@@ -294,10 +301,10 @@ test('test processStore function to have multiple clients for the selected devic
const client1 = generateClientFromDevice(selectedDevice, 'testapp1');
const client2 = generateClientFromDevice(selectedDevice, 'testapp2');
const json = await processStore(
[],
selectedDevice,
{
const json = await processStore({
activeNotifications: [],
device: selectedDevice,
pluginStates: {
[clientIdentifierApp1 + '#testapp1']: {
msg: 'Test plugin App1',
},
@@ -305,14 +312,14 @@ test('test processStore function to have multiple clients for the selected devic
msg: 'Test plugin App2',
},
},
[
clients: [
generateClientFromDevice(selectedDevice, 'testapp1'),
generateClientFromDevice(selectedDevice, 'testapp2'),
],
new Map(),
'salt',
[],
);
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already added
const {clients} = json;
@@ -343,19 +350,19 @@ test('test processStore function for device plugin state and no clients', async
'iOS',
[],
);
const json = await processStore(
[],
selectedDevice,
{
const json = await processStore({
activeNotifications: [],
device: selectedDevice,
pluginStates: {
'serial#TestDevicePlugin': {
msg: 'Test Device plugin',
},
},
[],
new Map([['TestDevicePlugin', TestDevicePlugin]]),
'salt',
[],
);
clients: [],
devicePlugins: new Map([['TestDevicePlugin', TestDevicePlugin]]),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;
@@ -376,19 +383,19 @@ test('test processStore function for unselected device plugin state and no clien
'iOS',
[],
);
const json = await processStore(
[],
selectedDevice,
{
const json = await processStore({
activeNotifications: [],
device: selectedDevice,
pluginStates: {
'unselectedDeviceIdentifier#TestDevicePlugin': {
msg: 'Test Device plugin',
},
},
[],
new Map([['TestDevicePlugin', TestDevicePlugin]]),
'salt',
[],
);
clients: [],
devicePlugins: new Map([['TestDevicePlugin', TestDevicePlugin]]),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;
@@ -418,16 +425,15 @@ test('test processStore function for notifications for selected device', async (
notification,
client: client.id,
};
const json = await processStore(
[activeNotification],
selectedDevice,
{},
[client],
new Map([['TestDevicePlugin', TestDevicePlugin]]),
'salt',
[],
);
const json = await processStore({
activeNotifications: [activeNotification],
device: selectedDevice,
pluginStates: {},
clients: [client],
devicePlugins: new Map([['TestDevicePlugin', TestDevicePlugin]]),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
@@ -477,15 +483,15 @@ test('test processStore function for notifications for unselected device', async
notification,
client: unselectedclient.id,
};
const json = await processStore(
[activeNotification],
selectedDevice,
{},
[client, unselectedclient],
new Map(),
'salt',
[],
);
const json = await processStore({
activeNotifications: [activeNotification],
device: selectedDevice,
pluginStates: {},
clients: [client, unselectedclient],
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;
@@ -514,15 +520,15 @@ test('test processStore function for selected plugins', async () => {
msg: 'Test plugin2',
},
};
const json = await processStore(
[],
selectedDevice,
pluginstates,
[client],
new Map(),
'salt',
['plugin2'],
);
const json = await processStore({
activeNotifications: [],
device: selectedDevice,
pluginStates: pluginstates,
clients: [client],
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: ['plugin2'],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;
@@ -557,15 +563,16 @@ test('test processStore function for no selected plugins', async () => {
msg: 'Test plugin2',
},
};
const json = await processStore(
[],
selectedDevice,
pluginstates,
[client],
new Map(),
'salt',
[],
);
const json = await processStore({
activeNotifications: [],
device: selectedDevice,
pluginStates: pluginstates,
clients: [client],
devicePlugins: new Map(),
salt: 'salt',
selectedPlugins: [],
});
expect(json).toBeDefined();
//$FlowFixMe Flow doesn't that its a test and the assertion for null is already done
const {pluginStates} = json.store;

View File

@@ -39,6 +39,32 @@ export type ExportType = {|
},
|};
type ProcessPluginStatesOptions = {|
clients: Array<ClientExport>,
serial: string,
allPluginStates: PluginStatesState,
devicePlugins: Map<string, Class<FlipperDevicePlugin<>>>,
selectedPlugins: Array<string>,
statusUpdate?: (msg: string) => void,
|};
type ProcessNotificationStatesOptions = {
clients: Array<ClientExport>,
serial: string,
allActiveNotifications: Array<PluginNotification>,
devicePlugins: Map<string, Class<FlipperDevicePlugin<>>>,
statusUpdate?: (msg: string) => void,
};
type AddSaltToDeviceSerialOptions = {
salt: string,
device: BaseDevice,
clients: Array<ClientExport>,
pluginStates: PluginStatesState,
pluginNotification: Array<PluginNotification>,
statusUpdate?: (msg: string) => void,
};
export function processClients(
clients: Array<ClientExport>,
serial: string,
@@ -69,13 +95,17 @@ export function pluginsClassMap(
}
export function processPluginStates(
clients: Array<ClientExport>,
serial: string,
allPluginStates: PluginStatesState,
devicePlugins: Map<string, Class<FlipperDevicePlugin<>>>,
selectedPlugins: Array<string>,
statusUpdate?: (msg: string) => void,
options: ProcessPluginStatesOptions,
): PluginStatesState {
const {
clients,
serial,
allPluginStates,
devicePlugins,
selectedPlugins,
statusUpdate,
} = options;
let pluginStates = {};
statusUpdate &&
statusUpdate('Filtering the plugin states for the filtered Clients...');
@@ -101,12 +131,15 @@ export function processPluginStates(
}
export function processNotificationStates(
clients: Array<ClientExport>,
serial: string,
allActiveNotifications: Array<PluginNotification>,
devicePlugins: Map<string, Class<FlipperDevicePlugin<>>>,
statusUpdate?: (msg: string) => void,
options: ProcessNotificationStatesOptions,
): Array<PluginNotification> {
const {
clients,
serial,
allActiveNotifications,
devicePlugins,
statusUpdate,
} = options;
statusUpdate &&
statusUpdate('Filtering the notifications for the filtered Clients...');
const activeNotifications = allActiveNotifications.filter(notif => {
@@ -122,13 +155,16 @@ export function processNotificationStates(
}
const addSaltToDeviceSerial = async (
salt: string,
device: BaseDevice,
clients: Array<ClientExport>,
pluginStates: PluginStatesState,
pluginNotification: Array<PluginNotification>,
statusUpdate?: (msg: string) => void,
options: AddSaltToDeviceSerialOptions,
): Promise<ExportType> => {
const {
salt,
device,
clients,
pluginStates,
pluginNotification,
statusUpdate,
} = options;
const {serial} = device;
const newSerial = salt + '-' + serial;
const newDevice = new ArchivedDevice(
@@ -191,7 +227,7 @@ const addSaltToDeviceSerial = async (
};
};
export const processStore = async (
type ProcessStoreOptions = {|
activeNotifications: Array<PluginNotification>,
device: ?BaseDevice,
pluginStates: PluginStatesState,
@@ -200,34 +236,49 @@ export const processStore = async (
salt: string,
selectedPlugins: Array<string>,
statusUpdate?: (msg: string) => void,
|};
export const processStore = async (
options: ProcessStoreOptions,
): Promise<?ExportType> => {
const {
activeNotifications,
device,
pluginStates,
clients,
devicePlugins,
salt,
selectedPlugins,
statusUpdate,
} = options;
if (device) {
const {serial} = device;
const processedClients = processClients(clients, serial, statusUpdate);
const processedPluginStates = processPluginStates(
processedClients,
const processedPluginStates = processPluginStates({
clients: processedClients,
serial,
pluginStates,
allPluginStates: pluginStates,
devicePlugins,
selectedPlugins,
statusUpdate,
);
const processedActiveNotifications = processNotificationStates(
processedClients,
});
const processedActiveNotifications = processNotificationStates({
clients: processedClients,
serial,
activeNotifications,
allActiveNotifications: activeNotifications,
devicePlugins,
statusUpdate,
);
});
// Adding salt to the device id, so that the device_id in the device list is unique.
const exportFlipperData = await addSaltToDeviceSerial(
const exportFlipperData = await addSaltToDeviceSerial({
salt,
device,
processedClients,
processedPluginStates,
processedActiveNotifications,
clients: processedClients,
pluginStates: processedPluginStates,
pluginNotification: processedActiveNotifications,
statusUpdate,
);
});
return exportFlipperData;
}
return null;
@@ -319,16 +370,16 @@ export async function getStoreExport(
const {activeNotifications} = store.getState().notifications;
const {devicePlugins} = store.getState().plugins;
const exportData = await processStore(
const exportData = await processStore({
activeNotifications,
selectedDevice,
newPluginState,
clients.map(client => client.toJSON()),
device: selectedDevice,
pluginStates: newPluginState,
clients: clients.map(client => client.toJSON()),
devicePlugins,
uuid.v4(),
store.getState().plugins.selectedPlugins,
salt: uuid.v4(),
selectedPlugins: store.getState().plugins.selectedPlugins,
statusUpdate,
);
});
return {exportData, errorArray};
}