Command processing (1/n)
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*: implemented basic plugin action queue processing. Reviewed By: mweststrate Differential Revision: D26164945 fbshipit-source-id: 5d8ad9b4d7b1300e92883d24a71da9ca1f85b183
This commit is contained in:
committed by
Facebook GitHub Bot
parent
f10f963ff1
commit
8efdde08c4
@@ -27,7 +27,7 @@ import path from 'path';
|
||||
import tmp from 'tmp';
|
||||
import {promisify} from 'util';
|
||||
import {reportPlatformFailures, reportUsage} from '../utils/metrics';
|
||||
import {activatePlugin, pluginInstalled} from '../reducers/pluginManager';
|
||||
import {loadPlugin, pluginInstalled} from '../reducers/pluginManager';
|
||||
import {showErrorNotification} from '../utils/notifications';
|
||||
|
||||
// Adapter which forces node.js implementation for axios instead of browser implementation
|
||||
@@ -130,7 +130,7 @@ async function handlePluginDownload(
|
||||
}
|
||||
if (pluginIsDisabledForAllConnectedClients(store.getState(), plugin)) {
|
||||
dispatch(
|
||||
activatePlugin({
|
||||
loadPlugin({
|
||||
plugin: installedPlugin,
|
||||
enable: startedByUser,
|
||||
notifyIfFailed: startedByUser,
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
import {Store} from '../reducers/index';
|
||||
import {Logger} from '../fb-interfaces/Logger';
|
||||
import {
|
||||
pluginActivationHandled,
|
||||
LoadPluginActionPayload,
|
||||
pluginCommandsProcessed,
|
||||
registerInstalledPlugins,
|
||||
} from '../reducers/pluginManager';
|
||||
import {
|
||||
@@ -22,7 +23,6 @@ import {sideEffect} from '../utils/sideEffect';
|
||||
import {requirePlugin} from './plugins';
|
||||
import {registerPluginUpdate} from '../reducers/connections';
|
||||
import {showErrorNotification} from '../utils/notifications';
|
||||
import {reportUsage} from '../utils/metrics';
|
||||
|
||||
const maxInstalledPluginVersionsToKeep = 2;
|
||||
|
||||
@@ -44,40 +44,42 @@ export default (store: Store, _logger: Logger) => {
|
||||
sideEffect(
|
||||
store,
|
||||
{name: 'handlePluginActivation', throttleMs: 1000, fireImmediately: true},
|
||||
(state) => state.pluginManager.pluginActivationQueue,
|
||||
(state) => state.pluginManager.pluginCommandsQueue,
|
||||
(queue, store) => {
|
||||
for (const request of queue) {
|
||||
try {
|
||||
reportUsage(
|
||||
'plugin:activate',
|
||||
{
|
||||
version: request.plugin.version,
|
||||
enable: request.enable ? '1' : '0',
|
||||
notifyIfFailed: request.notifyIfFailed ? '1' : '0',
|
||||
},
|
||||
request.plugin.id,
|
||||
);
|
||||
const plugin = requirePlugin(request.plugin);
|
||||
const enablePlugin = request.enable;
|
||||
store.dispatch(
|
||||
registerPluginUpdate({
|
||||
plugin,
|
||||
enablePlugin,
|
||||
}),
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Failed to activate plugin ${request.plugin.title} v${request.plugin.version}`,
|
||||
err,
|
||||
);
|
||||
if (request.notifyIfFailed) {
|
||||
showErrorNotification(
|
||||
`Failed to load plugin "${request.plugin.title}" v${request.plugin.version}`,
|
||||
);
|
||||
}
|
||||
for (const command of queue) {
|
||||
switch (command.type) {
|
||||
case 'LOAD_PLUGIN':
|
||||
loadPlugin(store, command.payload);
|
||||
break;
|
||||
default:
|
||||
console.error('Unexpected plugin command', command);
|
||||
break;
|
||||
}
|
||||
}
|
||||
store.dispatch(pluginActivationHandled(queue.length));
|
||||
store.dispatch(pluginCommandsProcessed(queue.length));
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
function loadPlugin(store: Store, payload: LoadPluginActionPayload) {
|
||||
try {
|
||||
const plugin = requirePlugin(payload.plugin);
|
||||
const enablePlugin = payload.enable;
|
||||
store.dispatch(
|
||||
registerPluginUpdate({
|
||||
plugin,
|
||||
enablePlugin,
|
||||
}),
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Failed to activate plugin ${payload.plugin.title} v${payload.plugin.version}`,
|
||||
err,
|
||||
);
|
||||
if (payload.notifyIfFailed) {
|
||||
showErrorNotification(
|
||||
`Failed to load plugin "${payload.plugin.title}" v${payload.plugin.version}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user