mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 19:31:46 +00:00
refactor(plugin): apply new plugin loader at all type of plugin
This commit is contained in:
@ -9,43 +9,20 @@ import config from './config';
|
||||
|
||||
// eslint-disable-next-line import/order
|
||||
import { preloadPlugins } from 'virtual:PreloadPlugins';
|
||||
import { PluginBaseConfig, PluginContext, PreloadPluginFactory } from './plugins/utils/builder';
|
||||
import {
|
||||
PluginBaseConfig,
|
||||
PluginBuilder,
|
||||
PreloadPluginFactory
|
||||
} from './plugins/utils/builder';
|
||||
import { loadAllPreloadPlugins, registerPreloadPlugin } from './loader/preload';
|
||||
|
||||
const createContext = <
|
||||
Key extends keyof PluginBuilderList,
|
||||
Config extends PluginBaseConfig = PluginBuilderList[Key]['config'],
|
||||
>(name: Key): PluginContext<Config> => ({
|
||||
getConfig: () => deepmerge(pluginBuilders[name].config, config.get(`plugins.${name}`) ?? {}) as unknown as Config,
|
||||
setConfig: (newConfig) => {
|
||||
config.setPartial(`plugins.${name}`, newConfig);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
const preloadedPluginList = [];
|
||||
|
||||
const pluginConfig = config.plugins.getPlugins();
|
||||
Object.entries(preloadPlugins)
|
||||
.filter(([id]) => {
|
||||
const typedId = id as keyof PluginBuilderList;
|
||||
const config = deepmerge(pluginBuilders[typedId].config, pluginConfig[typedId] ?? {});
|
||||
|
||||
return config.enabled;
|
||||
})
|
||||
.forEach(async ([id]) => {
|
||||
if (Object.hasOwn(preloadPlugins, id)) {
|
||||
const factory = (preloadPlugins as Record<string, PreloadPluginFactory<PluginBaseConfig>>)[id];
|
||||
|
||||
try {
|
||||
const context = createContext(id as keyof PluginBuilderList);
|
||||
const plugin = await factory(context);
|
||||
plugin.onLoad?.();
|
||||
preloadedPluginList.push(plugin);
|
||||
} catch (error) {
|
||||
console.error('[YTMusic]', `Cannot load preload plugin "${id}": ${String(error)}`);
|
||||
}
|
||||
}
|
||||
Object.entries(pluginBuilders).forEach(([id, builder]) => {
|
||||
const typedBuilder = builder as PluginBuilder<string, PluginBaseConfig>;
|
||||
const plugin = preloadPlugins[id] as PreloadPluginFactory<PluginBaseConfig> | undefined;
|
||||
|
||||
registerPreloadPlugin(id, typedBuilder, plugin);
|
||||
});
|
||||
loadAllPreloadPlugins();
|
||||
|
||||
contextBridge.exposeInMainWorld('mainConfig', config);
|
||||
contextBridge.exposeInMainWorld('electronIs', is);
|
||||
|
||||
Reference in New Issue
Block a user