1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00

Reload just the changed plugin

This commit is contained in:
Henry Heino 2024-12-23 14:10:06 -08:00
parent f69d495e75
commit a3b555e1a1
2 changed files with 20 additions and 10 deletions

View File

@ -42,9 +42,9 @@ const usePlugins = (
const reloadAllRef = useRef(false); const reloadAllRef = useRef(false);
reloadAllRef.current ||= pluginRunner !== lastPluginRunner; reloadAllRef.current ||= pluginRunner !== lastPluginRunner;
useOnDevPluginsUpdated(() => { useOnDevPluginsUpdated(async (pluginId: string) => {
logger.info('Dev plugin updated. Reloading...'); logger.info(`Dev plugin ${pluginId} updated. Reloading...`);
reloadAllRef.current = true; await PluginService.instance().unloadPlugin(pluginId);
setReloadCounter(counter => counter + 1); setReloadCounter(counter => counter + 1);
}, devPluginPath, pluginSupportEnabled); }, devPluginPath, pluginSupportEnabled);

View File

@ -1,10 +1,10 @@
import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect'; import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect';
import shim from '@joplin/lib/shim'; import shim from '@joplin/lib/shim';
import time from '@joplin/lib/time'; import time from '@joplin/lib/time';
import { join } from 'path'; import { basename, join } from 'path';
import { useRef } from 'react'; import { useRef } from 'react';
type OnDevPluginChange = ()=> void; type OnDevPluginChange = (id: string)=> void;
const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginPath: string, pluginSupportEnabled: boolean) => { const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginPath: string, pluginSupportEnabled: boolean) => {
const onDevPluginChangeRef = useRef(onDevPluginChange); const onDevPluginChangeRef = useRef(onDevPluginChange);
@ -16,10 +16,12 @@ const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginP
const itemToLastModTime = new Map<string, number>(); const itemToLastModTime = new Map<string, number>();
while (!event.cancelled) { // publishPath should point to the publish/ subfolder of a plugin's development
const publishFolder = join(devPluginPath, 'publish'); // directory.
const dirStats = await shim.fsDriver().readDirStats(publishFolder); const checkPluginChange = async (pluginPublishPath: string) => {
const dirStats = await shim.fsDriver().readDirStats(pluginPublishPath);
let hasChange = false; let hasChange = false;
let changedPluginId = '';
for (const item of dirStats) { for (const item of dirStats) {
if (item.path.endsWith('.jpl')) { if (item.path.endsWith('.jpl')) {
const lastModTime = itemToLastModTime.get(item.path); const lastModTime = itemToLastModTime.get(item.path);
@ -27,6 +29,8 @@ const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginP
if (lastModTime === undefined || lastModTime < modTime) { if (lastModTime === undefined || lastModTime < modTime) {
itemToLastModTime.set(item.path, modTime); itemToLastModTime.set(item.path, modTime);
hasChange = true; hasChange = true;
changedPluginId = basename(item.path, '.jpl');
break;
} }
} }
} }
@ -38,11 +42,17 @@ const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginP
// will always be true, even with no plugin reload. // will always be true, even with no plugin reload.
isFirstUpdateRef.current = false; isFirstUpdateRef.current = false;
} else { } else {
onDevPluginChangeRef.current(); onDevPluginChangeRef.current(changedPluginId);
} }
} }
};
await time.sleep(5); while (!event.cancelled) {
const publishFolder = join(devPluginPath, 'publish');
await checkPluginChange(publishFolder);
const pollingIntervalSeconds = 5;
await time.sleep(pollingIntervalSeconds);
} }
}, [devPluginPath, pluginSupportEnabled]); }, [devPluginPath, pluginSupportEnabled]);
}; };