Command processing (4/n): Load 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 "load plugin" operation to perform it in pluginManager dispatcher.

Reviewed By: mweststrate

Differential Revision: D26166654

fbshipit-source-id: e1fe48fa2cfc5533ad4f801ca56f00fc2ca3f4c4
This commit is contained in:
Anton Nikolaev
2021-02-16 10:46:11 -08:00
committed by Facebook GitHub Bot
parent 01f02b2cab
commit 0b803f810e
5 changed files with 248 additions and 177 deletions

View File

@@ -116,19 +116,23 @@ export type Action =
plugin: PluginDefinition;
};
}
| {
type: 'PLUGIN_STARRED';
payload: {
plugin: PluginDefinition;
};
}
| {
type: 'PLUGIN_UNSTARRED';
payload: {
plugin: PluginDefinition;
};
}
| {
type: 'SELECT_CLIENT';
payload: string | null;
}
| RegisterPluginAction
| {
// Implemented by rootReducer in `store.tsx`
type: 'UPDATE_PLUGIN';
payload: {
plugin: PluginDefinition;
enablePlugin: boolean;
};
};
| RegisterPluginAction;
const DEFAULT_PLUGIN = 'DeviceLogs';
const DEFAULT_DEVICE_BLACKLIST = [MacDevice, MetroDevice];
@@ -367,6 +371,44 @@ export default (state: State = INITAL_STATE, action: Actions): State => {
});
return state;
}
case 'PLUGIN_STARRED': {
const {plugin} = action.payload;
const selectedPlugin = plugin.id;
const selectedApp = state.selectedApp
? deconstructClientId(state.selectedApp).app
: undefined;
if (!selectedApp) {
return state;
}
return produce(state, (draft) => {
if (!draft.userStarredPlugins[selectedApp]) {
draft.userStarredPlugins[selectedApp] = [];
}
const plugins = draft.userStarredPlugins[selectedApp];
const idx = plugins.indexOf(selectedPlugin);
if (idx === -1) {
plugins.push(selectedPlugin);
}
});
}
case 'PLUGIN_UNSTARRED': {
const {plugin} = action.payload;
const selectedPlugin = plugin.id;
const selectedApp = state.selectedApp;
if (!selectedApp) {
return state;
}
return produce(state, (draft) => {
if (!draft.userStarredPlugins[selectedApp]) {
draft.userStarredPlugins[selectedApp] = [];
}
const plugins = draft.userStarredPlugins[selectedApp];
const idx = plugins.indexOf(selectedPlugin);
if (idx !== -1) {
plugins.splice(idx, 1);
}
});
}
default:
return state;
}
@@ -420,12 +462,18 @@ export const selectClient = (clientId: string | null): Action => ({
payload: clientId,
});
export const registerPluginUpdate = (payload: {
plugin: PluginDefinition;
enablePlugin: boolean;
}): Action => ({
type: 'UPDATE_PLUGIN',
payload,
export const pluginStarred = (plugin: PluginDefinition): Action => ({
type: 'PLUGIN_STARRED',
payload: {
plugin,
},
});
export const pluginUnstarred = (plugin: PluginDefinition): Action => ({
type: 'PLUGIN_UNSTARRED',
payload: {
plugin,
},
});
export function getAvailableClients(