文件
mynode/packages/lib/hooks/plugins/usePlugin.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

44 行
1.3 KiB
TypeScript

import PluginService from '../../services/plugins/PluginService';
import Logger from '@joplin/utils/Logger';
import shim from '../../shim';
const logger = Logger.create('usePlugin');
const usePlugin = (pluginId: string) => {
const React = shim.react();
const [pluginReloadCounter, setPluginReloadCounter] = React.useState(0);
const plugin = React.useMemo(() => {
if (!PluginService.instance().pluginIds.includes(pluginId)) {
return null;
}
if (pluginReloadCounter > 0) {
logger.debug('Reloading plugin', pluginId, 'because the set of loaded plugins changed.');
}
return PluginService.instance().pluginById(pluginId);
// The dependency on pluginReloadCounter is important -- it ensures that the plugin
// matches the one loaded in the PluginService.
}, [pluginId, pluginReloadCounter]);
const reloadCounterRef = React.useRef(0);
reloadCounterRef.current = pluginReloadCounter;
// The plugin may need to be re-fetched from the PluginService. When a plugin is reloaded,
// its Plugin object is replaced with a new one.
React.useEffect(() => {
const { remove } = PluginService.instance().addLoadedPluginsChangeListener(() => {
setPluginReloadCounter(reloadCounterRef.current + 1);
});
return () => {
remove();
};
}, []);
return plugin;
};
export default usePlugin;