Command processing (5/n): Star plugin

Summary:
*Stack summary*: this stack refactors plugin management actions to perform them in a dispatcher rather than in the root reducer (store.tsx) as all of these actions has side effects. To do that, we store requested plugin management actions (install/update/uninstall, star/unstar) in a queue which is then handled by pluginManager dispatcher. This dispatcher then dispatches all required state updates.

*Diff summary*: refactored "star plugin" operation to perform it in pluginManager dispatcher.

Reviewed By: mweststrate

Differential Revision: D26305576

fbshipit-source-id: 90516af4e9ba8504720ddfa587f691f53e71b702
This commit is contained in:
Anton Nikolaev
2021-02-16 10:46:11 -08:00
committed by Facebook GitHub Bot
parent 0b803f810e
commit 19f2fccc79
15 changed files with 156 additions and 131 deletions

View File

@@ -11,13 +11,6 @@ import {createStore} from 'redux';
import reducers, {Actions, State as StoreState, Store} from './reducers/index';
import {stateSanitizer} from './utils/reduxDevToolsConfig';
import isProduction from './utils/isProduction';
import produce from 'immer';
import {
defaultEnabledBackgroundPlugins,
getPluginKey,
} from './utils/pluginUtils';
import Client from './Client';
import {PluginDefinition} from './plugin';
import {_SandyPluginDefinition} from 'flipper-plugin';
export const store: Store = createStore<StoreState, Actions, any, any>(
rootReducer,
@@ -34,41 +27,6 @@ export function rootReducer(
state: StoreState | undefined,
action: Actions,
): StoreState {
if (action.type === 'STAR_PLUGIN' && state) {
const {plugin, selectedApp} = action.payload;
const selectedPlugin = plugin.id;
const clients = state.connections.clients.filter(
(client) => client.query.app === selectedApp,
);
return produce(state, (draft) => {
if (!draft.connections.userStarredPlugins[selectedApp]) {
draft.connections.userStarredPlugins[selectedApp] = [];
}
const plugins = draft.connections.userStarredPlugins[selectedApp];
const idx = plugins.indexOf(selectedPlugin);
if (idx === -1) {
plugins.push(selectedPlugin);
// enabling a plugin on one device enables it on all...
clients.forEach((client) => {
startPlugin(client, plugin);
});
} else {
plugins.splice(idx, 1);
// enabling a plugin on one device disables it on all...
clients.forEach((client) => {
stopPlugin(client, plugin.id);
const pluginKey = getPluginKey(
client.id,
{serial: client.query.device_id},
plugin.id,
);
delete draft.pluginMessageQueue[pluginKey];
});
}
});
}
// otherwise
return reducers(state, action);
}
@@ -77,36 +35,3 @@ if (!isProduction()) {
// @ts-ignore
window.flipperStore = store;
}
function stopPlugin(
client: Client,
pluginId: string,
forceInitBackgroundPlugin: boolean = false,
): boolean {
if (
(forceInitBackgroundPlugin ||
!defaultEnabledBackgroundPlugins.includes(pluginId)) &&
client?.isBackgroundPlugin(pluginId)
) {
client.deinitPlugin(pluginId);
}
// stop sandy plugins
client.stopPluginIfNeeded(pluginId);
return true;
}
function startPlugin(
client: Client,
plugin: PluginDefinition,
forceInitBackgroundPlugin: boolean = false,
) {
client.startPluginIfNeeded(plugin, true);
// background plugin? connect it needed
if (
(forceInitBackgroundPlugin ||
!defaultEnabledBackgroundPlugins.includes(plugin.id)) &&
client?.isBackgroundPlugin(plugin.id)
) {
client.initPlugin(plugin.id);
}
}