You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-12-20 23:30:05 +02:00
Web: Add support for auto-reloading dev plugins on change (#11545)
This commit is contained in:
@@ -15,6 +15,7 @@ import { AppState } from '../../utils/types';
|
||||
import usePrevious from '@joplin/lib/hooks/usePrevious';
|
||||
import PlatformImplementation from '../../services/plugins/PlatformImplementation';
|
||||
import AccessibleView from '../accessibility/AccessibleView';
|
||||
import useOnDevPluginsUpdated from './utils/useOnDevPluginsUpdated';
|
||||
|
||||
const logger = Logger.create('PluginRunnerWebView');
|
||||
|
||||
@@ -29,20 +30,33 @@ const usePlugins = (
|
||||
pluginRunner: PluginRunner,
|
||||
webviewLoaded: boolean,
|
||||
pluginSettings: PluginSettings,
|
||||
pluginSupportEnabled: boolean,
|
||||
devPluginPath: string,
|
||||
) => {
|
||||
const store = useStore<AppState>();
|
||||
const lastPluginRunner = usePrevious(pluginRunner);
|
||||
const [reloadCounter, setReloadCounter] = useState(0);
|
||||
|
||||
// Only set reloadAll to true here -- this ensures that all plugins are reloaded,
|
||||
// even if loadPlugins is cancelled and re-run.
|
||||
const reloadAllRef = useRef(false);
|
||||
reloadAllRef.current ||= pluginRunner !== lastPluginRunner;
|
||||
|
||||
useOnDevPluginsUpdated(async (pluginId: string) => {
|
||||
logger.info(`Dev plugin ${pluginId} updated. Reloading...`);
|
||||
await PluginService.instance().unloadPlugin(pluginId);
|
||||
setReloadCounter(counter => counter + 1);
|
||||
}, devPluginPath, pluginSupportEnabled);
|
||||
|
||||
useAsyncEffect(async (event) => {
|
||||
if (!webviewLoaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (reloadCounter > 0) {
|
||||
logger.debug('Reloading with counter set to', reloadCounter);
|
||||
}
|
||||
|
||||
await loadPlugins({
|
||||
pluginRunner,
|
||||
pluginSettings,
|
||||
@@ -56,7 +70,7 @@ const usePlugins = (
|
||||
if (!event.cancelled) {
|
||||
reloadAllRef.current = false;
|
||||
}
|
||||
}, [pluginRunner, store, webviewLoaded, pluginSettings]);
|
||||
}, [pluginRunner, store, webviewLoaded, pluginSettings, reloadCounter]);
|
||||
};
|
||||
|
||||
const useUnloadPluginsOnGlobalDisable = (
|
||||
@@ -79,6 +93,7 @@ interface Props {
|
||||
serializedPluginSettings: SerializedPluginSettings;
|
||||
pluginSupportEnabled: boolean;
|
||||
pluginStates: PluginStates;
|
||||
devPluginPath: string;
|
||||
pluginHtmlContents: PluginHtmlContents;
|
||||
themeId: number;
|
||||
}
|
||||
@@ -98,7 +113,7 @@ const PluginRunnerWebViewComponent: React.FC<Props> = props => {
|
||||
}, [webviewReloadCounter]);
|
||||
|
||||
const pluginSettings = usePluginSettings(props.serializedPluginSettings);
|
||||
usePlugins(pluginRunner, webviewLoaded, pluginSettings);
|
||||
usePlugins(pluginRunner, webviewLoaded, pluginSettings, props.pluginSupportEnabled, props.devPluginPath);
|
||||
useUnloadPluginsOnGlobalDisable(props.pluginStates, props.pluginSupportEnabled);
|
||||
|
||||
const onLoadStart = useCallback(() => {
|
||||
@@ -183,6 +198,7 @@ export default connect((state: AppState) => {
|
||||
const result: Props = {
|
||||
serializedPluginSettings: state.settings['plugins.states'],
|
||||
pluginSupportEnabled: state.settings['plugins.pluginSupportEnabled'],
|
||||
devPluginPath: state.settings['plugins.devPluginPaths'],
|
||||
pluginStates: state.pluginService.plugins,
|
||||
pluginHtmlContents: state.pluginService.pluginHtmlContents,
|
||||
themeId: state.settings.theme,
|
||||
|
||||
Reference in New Issue
Block a user