From d2dfb924fd61f2895a5e227115179fc17666390b Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Tue, 5 Nov 2019 05:27:38 -0800 Subject: [PATCH] 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 --- src/chrome/PluginInstaller.tsx | 33 ++++++++++++++++++++++++-------- src/dispatcher/pluginManager.tsx | 13 ++++++++++--- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/chrome/PluginInstaller.tsx b/src/chrome/PluginInstaller.tsx index bf1cbbd42..da89506fe 100644 --- a/src/chrome/PluginInstaller.tsx +++ b/src/chrome/PluginInstaller.tsx @@ -33,10 +33,15 @@ import fs from 'fs-extra'; import {PluginManager as PM} from 'live-plugin-manager'; import {reportPlatformFailures, reportUsage} from '../utils/metrics'; import restartFlipper from '../utils/restartFlipper'; -import {PluginMap, PluginDefinition} from '../reducers/pluginManager'; -import {PLUGIN_DIR} from '../dispatcher/pluginManager'; -import {State as AppState} from '../reducers'; +import { + PluginMap, + 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 {Dispatch, Action} from 'redux'; const ALGOLIA_APPLICATION_ID = 'OFCNCOG2CU'; const ALGOLIA_API_KEY = 'f54e21fa3a2a0160595bb058179bfb1e'; @@ -94,19 +99,22 @@ type PropsFromState = { installedPlugins: PluginMap; }; +type DispatchFromProps = { + refreshInstalledPlugins: () => void; +}; + type OwnProps = { searchIndexFactory: () => algoliasearch.Index; autoHeight: boolean; }; -type Props = OwnProps & PropsFromState; +type Props = OwnProps & PropsFromState & DispatchFromProps; -const defaultProps: Props = { +const defaultProps: OwnProps = { searchIndexFactory: () => { const client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY); return client.initIndex('npm-search'); }, - installedPlugins: new Map(), autoHeight: false, }; @@ -120,6 +128,7 @@ const PluginInstaller = function props(props: Props) { props.searchIndexFactory, // TODO(T56693735): Refactor this to directly take props. async () => props.installedPlugins, + props.refreshInstalledPlugins, ); const restartApp = useCallback(() => { restartFlipper(); @@ -286,6 +295,7 @@ function useNPMSearch( setQuery: (query: string) => void, searchClientFactory: () => algoliasearch.Index, getInstalledPlugins: () => Promise>, + refreshInstalledPlugins: () => void, ): TableRows_immutable { const index = useMemo(searchClientFactory, []); const [installedPlugins, setInstalledPlugins] = useState( @@ -304,6 +314,7 @@ function useNPMSearch( }, []); const onInstall = useCallback(async () => { + refreshInstalledPlugins(); getAndSetInstalledPlugins(); setRestartRequired(true); }, []); @@ -366,9 +377,15 @@ function useNPMSearch( return List(results.map(createRow)); } -export default connect( +export default connect( ({pluginManager: {installedPlugins}}) => ({ installedPlugins, }), - {}, + (dispatch: Dispatch>) => ({ + refreshInstalledPlugins: () => { + readInstalledPlugins().then(plugins => + dispatch(registerInstalledPlugins(plugins)), + ); + }, + }), )(PluginInstaller); diff --git a/src/dispatcher/pluginManager.tsx b/src/dispatcher/pluginManager.tsx index 0a22c9718..e3265b594 100644 --- a/src/dispatcher/pluginManager.tsx +++ b/src/dispatcher/pluginManager.tsx @@ -20,7 +20,7 @@ import { export const PLUGIN_DIR = path.join(homedir(), '.flipper', 'thirdparty'); -async function getInstalledPlugins(): Promise { +export async function readInstalledPlugins(): Promise { const pluginDirExists = await fs.pathExists(PLUGIN_DIR); if (!pluginDirExists) { @@ -50,8 +50,15 @@ async function getInstalledPlugins(): Promise { return new Map(plugins.filter(Boolean)); } -export default (store: Store, _logger: Logger) => { - getInstalledPlugins().then(plugins => +function refreshInstalledPlugins(store: Store) { + readInstalledPlugins().then(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); + }); };