diff --git a/src/reducers/__tests__/connections.node.tsx b/src/reducers/__tests__/connections.node.tsx index 6a6649891..2159ad1fe 100644 --- a/src/reducers/__tests__/connections.node.tsx +++ b/src/reducers/__tests__/connections.node.tsx @@ -10,6 +10,8 @@ import reducer from '../connections'; import {State} from '../connections'; import BaseDevice from '../../devices/BaseDevice'; +import MacDevice from '../../devices/MacDevice'; +import {FlipperDevicePlugin} from '../../plugin'; test('REGISTER_DEVICE doesnt remove error', () => { const initialState: State = reducer(undefined, { @@ -32,6 +34,27 @@ test('REGISTER_DEVICE doesnt remove error', () => { ]); }); +test('triggering REGISTER_DEVICE before REGISTER_PLUGINS still registers device plugins', () => { + class TestDevicePlugin extends FlipperDevicePlugin { + static id = 'test'; + static supportsDevice() { + return true; + } + } + + const stateWithDevice = reducer(undefined, { + type: 'REGISTER_DEVICE', + payload: new MacDevice(), + }); + + const endState = reducer(stateWithDevice, { + type: 'REGISTER_PLUGINS', + payload: [TestDevicePlugin], + }); + + expect(endState.devices[0].devicePlugins).toEqual(['test']); +}); + test('errors are collected on a by name basis', () => { const initialState: State = reducer(undefined, { type: 'SERVER_ERROR', diff --git a/src/reducers/connections.tsx b/src/reducers/connections.tsx index 9cabd1136..3cc446dc1 100644 --- a/src/reducers/connections.tsx +++ b/src/reducers/connections.tsx @@ -27,6 +27,8 @@ import SupportRequestFormV2 from '../fb-stubs/SupportRequestFormV2'; import SupportRequestDetails from '../fb-stubs/SupportRequestDetails'; import {getPluginKey} from '../utils/pluginUtils'; import {deconstructClientId} from '../utils/clientUtils'; +import {FlipperDevicePlugin} from '../plugin'; +import {RegisterPluginAction} from './plugins'; export type StaticView = | null @@ -141,7 +143,8 @@ export type Action = | { type: 'SELECT_CLIENT'; payload: string; - }; + } + | RegisterPluginAction; const DEFAULT_PLUGIN = 'DeviceLogs'; const DEFAULT_DEVICE_BLACKLIST = [MacDevice]; @@ -367,6 +370,26 @@ const reducer = (state: State = INITAL_STATE, action: Actions): State => { }; } + case 'REGISTER_PLUGINS': { + // plugins are registered after creating the base devices, so update them + const plugins = action.payload; + plugins.forEach(plugin => { + if (plugin.prototype instanceof FlipperDevicePlugin) { + // smell: devices are mutable + state.devices.forEach(device => { + // @ts-ignore + if (plugin.supportsDevice(device)) { + device.devicePlugins = [ + ...(device.devicePlugins || []), + plugin.id, + ]; + } + }); + } + }); + return state; + } + default: return state; } diff --git a/src/reducers/plugins.tsx b/src/reducers/plugins.tsx index 1ab925339..5304a3f8c 100644 --- a/src/reducers/plugins.tsx +++ b/src/reducers/plugins.tsx @@ -21,13 +21,15 @@ export type State = { selectedPlugins: Array; }; -type P = typeof FlipperPlugin | typeof FlipperDevicePlugin; +type PluginClass = typeof FlipperPlugin | typeof FlipperDevicePlugin; + +export type RegisterPluginAction = { + type: 'REGISTER_PLUGINS'; + payload: Array; +}; export type Action = - | { - type: 'REGISTER_PLUGINS'; - payload: Array

; - } + | RegisterPluginAction | { type: 'GATEKEEPED_PLUGINS'; payload: Array; @@ -61,7 +63,7 @@ export default function reducer( if (action.type === 'REGISTER_PLUGINS') { return produce(state, draft => { const {devicePlugins, clientPlugins} = draft; - action.payload.forEach((p: P) => { + action.payload.forEach((p: PluginClass) => { if (devicePlugins.has(p.id) || clientPlugins.has(p.id)) { return; } @@ -105,7 +107,7 @@ export const selectedPlugins = (payload: Array): Action => ({ payload, }); -export const registerPlugins = (payload: Array

): Action => ({ +export const registerPlugins = (payload: Array): Action => ({ type: 'REGISTER_PLUGINS', payload, });