Skip to content

Commit

Permalink
Reload just the changed plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
personalizedrefrigerator committed Dec 23, 2024
1 parent f69d495 commit a3b555e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const usePlugins = (
const reloadAllRef = useRef(false);
reloadAllRef.current ||= pluginRunner !== lastPluginRunner;

useOnDevPluginsUpdated(() => {
logger.info('Dev plugin updated. Reloading...');
reloadAllRef.current = true;
useOnDevPluginsUpdated(async (pluginId: string) => {
logger.info(`Dev plugin ${pluginId} updated. Reloading...`);
await PluginService.instance().unloadPlugin(pluginId);
setReloadCounter(counter => counter + 1);
}, devPluginPath, pluginSupportEnabled);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect';
import shim from '@joplin/lib/shim';
import time from '@joplin/lib/time';
import { join } from 'path';
import { basename, join } from 'path';
import { useRef } from 'react';

type OnDevPluginChange = ()=> void;
type OnDevPluginChange = (id: string)=> void;

const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginPath: string, pluginSupportEnabled: boolean) => {
const onDevPluginChangeRef = useRef(onDevPluginChange);
Expand All @@ -16,17 +16,21 @@ const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginP

const itemToLastModTime = new Map<string, number>();

while (!event.cancelled) {
const publishFolder = join(devPluginPath, 'publish');
const dirStats = await shim.fsDriver().readDirStats(publishFolder);
// publishPath should point to the publish/ subfolder of a plugin's development
// directory.
const checkPluginChange = async (pluginPublishPath: string) => {
const dirStats = await shim.fsDriver().readDirStats(pluginPublishPath);
let hasChange = false;
let changedPluginId = '';
for (const item of dirStats) {
if (item.path.endsWith('.jpl')) {
const lastModTime = itemToLastModTime.get(item.path);
const modTime = item.mtime.getTime();
if (lastModTime === undefined || lastModTime < modTime) {
itemToLastModTime.set(item.path, modTime);
hasChange = true;
changedPluginId = basename(item.path, '.jpl');
break;
}
}
}
Expand All @@ -38,11 +42,17 @@ const useOnDevPluginsUpdated = (onDevPluginChange: OnDevPluginChange, devPluginP
// will always be true, even with no plugin reload.
isFirstUpdateRef.current = false;
} else {
onDevPluginChangeRef.current();
onDevPluginChangeRef.current(changedPluginId);
}
}
};

while (!event.cancelled) {
const publishFolder = join(devPluginPath, 'publish');
await checkPluginChange(publishFolder);

await time.sleep(5);
const pollingIntervalSeconds = 5;
await time.sleep(pollingIntervalSeconds);
}
}, [devPluginPath, pluginSupportEnabled]);
};
Expand Down

0 comments on commit a3b555e

Please sign in to comment.