Update plugin store after installation

Summary: Address the small regression introduced by D18173166. When closing the plugin manager after installing/removing, the store wasn't updated in between.

Reviewed By: jknoxville

Differential Revision: D18270821

fbshipit-source-id: 4ff54bc7607d06fa423cf8e673f216ae0a5d19da
This commit is contained in:
Pascal Hartig
2019-11-05 05:27:38 -08:00
committed by Facebook Github Bot
parent 432bb1b00a
commit d2dfb924fd
2 changed files with 35 additions and 11 deletions

View File

@@ -33,10 +33,15 @@ import fs from 'fs-extra';
import {PluginManager as PM} from 'live-plugin-manager'; import {PluginManager as PM} from 'live-plugin-manager';
import {reportPlatformFailures, reportUsage} from '../utils/metrics'; import {reportPlatformFailures, reportUsage} from '../utils/metrics';
import restartFlipper from '../utils/restartFlipper'; import restartFlipper from '../utils/restartFlipper';
import {PluginMap, PluginDefinition} from '../reducers/pluginManager'; import {
import {PLUGIN_DIR} from '../dispatcher/pluginManager'; PluginMap,
import {State as AppState} from '../reducers'; PluginDefinition,
registerInstalledPlugins,
} from '../reducers/pluginManager';
import {PLUGIN_DIR, readInstalledPlugins} from '../dispatcher/pluginManager';
import {State as AppState, Store} from '../reducers';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {Dispatch, Action} from 'redux';
const ALGOLIA_APPLICATION_ID = 'OFCNCOG2CU'; const ALGOLIA_APPLICATION_ID = 'OFCNCOG2CU';
const ALGOLIA_API_KEY = 'f54e21fa3a2a0160595bb058179bfb1e'; const ALGOLIA_API_KEY = 'f54e21fa3a2a0160595bb058179bfb1e';
@@ -94,19 +99,22 @@ type PropsFromState = {
installedPlugins: PluginMap; installedPlugins: PluginMap;
}; };
type DispatchFromProps = {
refreshInstalledPlugins: () => void;
};
type OwnProps = { type OwnProps = {
searchIndexFactory: () => algoliasearch.Index; searchIndexFactory: () => algoliasearch.Index;
autoHeight: boolean; autoHeight: boolean;
}; };
type Props = OwnProps & PropsFromState; type Props = OwnProps & PropsFromState & DispatchFromProps;
const defaultProps: Props = { const defaultProps: OwnProps = {
searchIndexFactory: () => { searchIndexFactory: () => {
const client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY); const client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY);
return client.initIndex('npm-search'); return client.initIndex('npm-search');
}, },
installedPlugins: new Map(),
autoHeight: false, autoHeight: false,
}; };
@@ -120,6 +128,7 @@ const PluginInstaller = function props(props: Props) {
props.searchIndexFactory, props.searchIndexFactory,
// TODO(T56693735): Refactor this to directly take props. // TODO(T56693735): Refactor this to directly take props.
async () => props.installedPlugins, async () => props.installedPlugins,
props.refreshInstalledPlugins,
); );
const restartApp = useCallback(() => { const restartApp = useCallback(() => {
restartFlipper(); restartFlipper();
@@ -286,6 +295,7 @@ function useNPMSearch(
setQuery: (query: string) => void, setQuery: (query: string) => void,
searchClientFactory: () => algoliasearch.Index, searchClientFactory: () => algoliasearch.Index,
getInstalledPlugins: () => Promise<Map<string, PluginDefinition>>, getInstalledPlugins: () => Promise<Map<string, PluginDefinition>>,
refreshInstalledPlugins: () => void,
): TableRows_immutable { ): TableRows_immutable {
const index = useMemo(searchClientFactory, []); const index = useMemo(searchClientFactory, []);
const [installedPlugins, setInstalledPlugins] = useState( const [installedPlugins, setInstalledPlugins] = useState(
@@ -304,6 +314,7 @@ function useNPMSearch(
}, []); }, []);
const onInstall = useCallback(async () => { const onInstall = useCallback(async () => {
refreshInstalledPlugins();
getAndSetInstalledPlugins(); getAndSetInstalledPlugins();
setRestartRequired(true); setRestartRequired(true);
}, []); }, []);
@@ -366,9 +377,15 @@ function useNPMSearch(
return List(results.map(createRow)); return List(results.map(createRow));
} }
export default connect<PropsFromState, {}, OwnProps, AppState>( export default connect<PropsFromState, DispatchFromProps, OwnProps, AppState>(
({pluginManager: {installedPlugins}}) => ({ ({pluginManager: {installedPlugins}}) => ({
installedPlugins, installedPlugins,
}), }),
{}, (dispatch: Dispatch<Action<any>>) => ({
refreshInstalledPlugins: () => {
readInstalledPlugins().then(plugins =>
dispatch(registerInstalledPlugins(plugins)),
);
},
}),
)(PluginInstaller); )(PluginInstaller);

View File

@@ -20,7 +20,7 @@ import {
export const PLUGIN_DIR = path.join(homedir(), '.flipper', 'thirdparty'); export const PLUGIN_DIR = path.join(homedir(), '.flipper', 'thirdparty');
async function getInstalledPlugins(): Promise<PluginMap> { export async function readInstalledPlugins(): Promise<PluginMap> {
const pluginDirExists = await fs.pathExists(PLUGIN_DIR); const pluginDirExists = await fs.pathExists(PLUGIN_DIR);
if (!pluginDirExists) { if (!pluginDirExists) {
@@ -50,8 +50,15 @@ async function getInstalledPlugins(): Promise<PluginMap> {
return new Map(plugins.filter(Boolean)); return new Map(plugins.filter(Boolean));
} }
export default (store: Store, _logger: Logger) => { function refreshInstalledPlugins(store: Store) {
getInstalledPlugins().then(plugins => readInstalledPlugins().then(plugins =>
store.dispatch(registerInstalledPlugins(plugins)), store.dispatch(registerInstalledPlugins(plugins)),
); );
}
export default (store: Store, _logger: Logger) => {
// This needn't happen immediately and is (light) I/O work.
window.requestIdleCallback(() => {
refreshInstalledPlugins(store);
});
}; };