Move local plugin discovery to dispatcher/redux store

Summary: In order to have update notifications, this must live outside the UI component, but it also gives some additional benefits like better testability of previously effectful UI.

Reviewed By: jknoxville

Differential Revision: D18173166

fbshipit-source-id: 1cacb6c7893423a7920a6620dfb76e631caba101
This commit is contained in:
Pascal Hartig
2019-11-05 05:27:38 -08:00
committed by Facebook Github Bot
parent 42a77094f4
commit 432bb1b00a
6 changed files with 170 additions and 42 deletions

View File

@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {default as reducer, registerInstalledPlugins} from '../pluginManager';
test('reduce empty registerInstalledPlugins', () => {
const result = reducer(undefined, registerInstalledPlugins(new Map()));
expect(result).toEqual({installedPlugins: new Map()});
});
const EXAMPLE_PLUGIN = {
name: 'test',
version: '0.1',
description: 'my test plugin',
};
test('reduce registerInstalledPlugins, clear again', () => {
const result = reducer(
undefined,
registerInstalledPlugins(new Map([['test', EXAMPLE_PLUGIN]])),
);
expect(result).toEqual({
installedPlugins: new Map([['test', EXAMPLE_PLUGIN]]),
});
const result2 = reducer(result, registerInstalledPlugins(new Map()));
expect(result2).toEqual({installedPlugins: new Map()});
});

View File

@@ -36,6 +36,10 @@ import settings, {
Settings as SettingsState,
Action as SettingsAction,
} from './settings';
import pluginManager, {
State as PluginManagerState,
Action as PluginManagerAction,
} from './pluginManager';
import user, {State as UserState, Action as UserAction} from './user';
import JsonFileStorage from '../utils/jsonFileReduxPersistStorage';
import os from 'os';
@@ -56,6 +60,7 @@ export type Actions =
| UserAction
| SettingsAction
| SupportFormAction
| PluginManagerAction
| {type: 'INIT'};
export type State = {
@@ -67,6 +72,7 @@ export type State = {
user: UserState & PersistPartial;
settingsState: SettingsState & PersistPartial;
supportForm: SupportFormState;
pluginManager: PluginManagerState;
};
export type Store = ReduxStore<State, Actions>;
@@ -106,6 +112,7 @@ export default combineReducers<State, Actions>({
),
plugins,
supportForm,
pluginManager,
user: persistReducer(
{
key: 'user',

View File

@@ -0,0 +1,50 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {Actions} from './';
export type PluginDefinition = {
name: string;
version: string;
description: string;
};
export type PluginMap = Map<string, PluginDefinition>;
export type State = {
installedPlugins: PluginMap;
};
export type Action = {
type: 'REGISTER_INSTALLED_PLUGINS';
payload: PluginMap;
};
const INITIAL_STATE: State = {
installedPlugins: new Map(),
};
export default function reducer(
state: State = INITIAL_STATE,
action: Actions,
): State {
if (action.type === 'REGISTER_INSTALLED_PLUGINS') {
return {
...state,
installedPlugins: action.payload,
};
} else {
return state;
}
}
export const registerInstalledPlugins = (payload: PluginMap): Action => ({
type: 'REGISTER_INSTALLED_PLUGINS',
payload,
});