mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 03:11:46 +00:00
change plugin system
This commit is contained in:
@ -1,27 +1,24 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
import { BrowserWindow, ipcMain } from 'electron';
|
||||
|
||||
import { deepmerge } from 'deepmerge-ts';
|
||||
import { mainPlugins } from 'virtual:plugins';
|
||||
|
||||
import config from '../config';
|
||||
import { injectCSS } from '../plugins/utils/main';
|
||||
import {
|
||||
MainPlugin,
|
||||
MainPluginContext,
|
||||
MainPluginFactory,
|
||||
PluginBaseConfig,
|
||||
PluginBuilder
|
||||
} from '../plugins/utils/builder';
|
||||
import { PluginDef } from '@/types/plugins';
|
||||
import { BackendContext } from '@/types/contexts';
|
||||
import config from '@/config';
|
||||
import { startPlugin, stopPlugin } from '@/utils';
|
||||
|
||||
const allPluginFactoryList: Record<string, MainPluginFactory<PluginBaseConfig>> = {};
|
||||
const allPluginBuilders: Record<string, PluginBuilder<string, PluginBaseConfig>> = {};
|
||||
const unregisterStyleMap: Record<string, (() => void)[]> = {};
|
||||
const loadedPluginMap: Record<string, MainPlugin<PluginBaseConfig>> = {};
|
||||
const loadedPluginMap: Record<string, PluginDef> = {};
|
||||
|
||||
const createContext = <
|
||||
Key extends keyof PluginBuilderList,
|
||||
Config extends PluginBaseConfig = PluginBuilderList[Key]['config'],
|
||||
>(id: Key, win: BrowserWindow): MainPluginContext<Config> => ({
|
||||
getConfig: () => deepmerge(allPluginBuilders[id].config, config.get(`plugins.${id}`) ?? {}) as Config,
|
||||
const createContext = (id: string, win: BrowserWindow): BackendContext => ({
|
||||
getConfig: () =>
|
||||
// @ts-expect-error ts dum dum
|
||||
deepmerge(
|
||||
mainPlugins[id].config,
|
||||
config.get(`plugins.${id}`) ?? { enabled: false },
|
||||
),
|
||||
setConfig: (newConfig) => {
|
||||
config.setPartial(`plugins.${id}`, newConfig);
|
||||
},
|
||||
@ -29,81 +26,95 @@ const createContext = <
|
||||
send: (event: string, ...args: unknown[]) => {
|
||||
win.webContents.send(event, ...args);
|
||||
},
|
||||
// @ts-expect-error ts dum dum
|
||||
handle: (event: string, listener) => {
|
||||
ipcMain.handle(event, async (_, ...args) => listener(...args as never));
|
||||
// @ts-expect-error ts dum dum
|
||||
ipcMain.handle(event, (_, ...args) => listener(...(args as never)));
|
||||
},
|
||||
// @ts-expect-error ts dum dum
|
||||
on: (event: string, listener) => {
|
||||
ipcMain.on(event, async (_, ...args) => listener(...args as never));
|
||||
// @ts-expect-error ts dum dum
|
||||
ipcMain.on(event, (_, ...args) => listener(...(args as never)));
|
||||
},
|
||||
});
|
||||
|
||||
export const forceUnloadMainPlugin = (id: keyof PluginBuilderList, win: BrowserWindow) => {
|
||||
unregisterStyleMap[id]?.forEach((unregister) => unregister());
|
||||
delete unregisterStyleMap[id];
|
||||
export const forceUnloadMainPlugin = async (
|
||||
id: string,
|
||||
win: BrowserWindow,
|
||||
): Promise<void> => {
|
||||
const plugin = loadedPluginMap[id]!;
|
||||
if (!plugin) return;
|
||||
|
||||
loadedPluginMap[id]?.onUnload?.(win);
|
||||
delete loadedPluginMap[id];
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
try {
|
||||
const hasStopped = stopPlugin(id, plugin, {
|
||||
ctx: 'backend',
|
||||
context: createContext(id, win),
|
||||
});
|
||||
if (!hasStopped) {
|
||||
console.log(
|
||||
'[YTMusic]',
|
||||
`Cannot unload "${id}" plugin: no stop function`,
|
||||
);
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
||||
delete loadedPluginMap[id];
|
||||
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
||||
resolve();
|
||||
} catch (err) {
|
||||
console.log('[YTMusic]', `Cannot unload "${id}" plugin: ${String(err)}`);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const forceLoadMainPlugin = async (id: keyof PluginBuilderList, win: BrowserWindow) => {
|
||||
const builder = allPluginBuilders[id];
|
||||
export const forceLoadMainPlugin = async (
|
||||
id: string,
|
||||
win: BrowserWindow,
|
||||
): Promise<void> => {
|
||||
const plugin = mainPlugins[id];
|
||||
if (!plugin.backend) return;
|
||||
|
||||
Promise.allSettled(
|
||||
builder.styles?.map(async (style) => {
|
||||
const unregister = await injectCSS(win.webContents, style);
|
||||
console.log('[YTMusic]', `Injected CSS for "${id}" plugin`);
|
||||
|
||||
return unregister;
|
||||
}) ?? [],
|
||||
).then((result) => {
|
||||
unregisterStyleMap[id] = result
|
||||
.map((it) => it.status === 'fulfilled' && it.value)
|
||||
.filter(Boolean);
|
||||
|
||||
let isInjectSuccess = true;
|
||||
result.forEach((it) => {
|
||||
if (it.status === 'rejected') {
|
||||
isInjectSuccess = false;
|
||||
|
||||
console.log('[YTMusic]', `Cannot inject "${id}" plugin style: ${String(it.reason)}`);
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
try {
|
||||
const hasStarted = startPlugin(id, plugin, {
|
||||
ctx: 'backend',
|
||||
context: createContext(id, win),
|
||||
});
|
||||
if (!hasStarted) {
|
||||
console.log('[YTMusic]', `Cannot load "${id}" plugin`);
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (isInjectSuccess) console.log('[YTMusic]', `"${id}" plugin data is loaded`);
|
||||
|
||||
loadedPluginMap[id] = plugin;
|
||||
resolve();
|
||||
} catch (err) {
|
||||
console.log(
|
||||
'[YTMusic]',
|
||||
`Cannot initialize "${id}" plugin: ${String(err)}`,
|
||||
);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const factory = allPluginFactoryList[id];
|
||||
if (!factory) return;
|
||||
|
||||
const context = createContext(id, win);
|
||||
const plugin = await factory(context);
|
||||
loadedPluginMap[id] = plugin;
|
||||
plugin.onLoad?.(win);
|
||||
|
||||
console.log('[YTMusic]', `"${id}" plugin is loaded`);
|
||||
} catch (err) {
|
||||
console.log('[YTMusic]', `Cannot initialize "${id}" plugin: ${String(err)}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const loadAllMainPlugins = async (win: BrowserWindow) => {
|
||||
const pluginConfigs = config.plugins.getPlugins();
|
||||
const queue: Promise<void>[] = [];
|
||||
|
||||
for (const [pluginId, builder] of Object.entries(allPluginBuilders)) {
|
||||
const typedBuilder = builder as PluginBuilderList[keyof PluginBuilderList];
|
||||
|
||||
const config = deepmerge(typedBuilder.config, pluginConfigs[pluginId as keyof PluginBuilderList] ?? {});
|
||||
|
||||
for (const [plugin, pluginDef] of Object.entries(mainPlugins)) {
|
||||
const config = deepmerge(pluginDef.config, pluginConfigs[plugin] ?? {});
|
||||
if (config.enabled) {
|
||||
await forceLoadMainPlugin(pluginId as keyof PluginBuilderList, win);
|
||||
} else {
|
||||
if (loadedPluginMap[pluginId as keyof PluginBuilderList]) {
|
||||
forceUnloadMainPlugin(pluginId as keyof PluginBuilderList, win);
|
||||
}
|
||||
queue.push(forceLoadMainPlugin(plugin, win));
|
||||
} else if (loadedPluginMap[plugin]) {
|
||||
queue.push(forceUnloadMainPlugin(plugin, win));
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(queue);
|
||||
};
|
||||
|
||||
export const unloadAllMainPlugins = (win: BrowserWindow) => {
|
||||
@ -112,17 +123,10 @@ export const unloadAllMainPlugins = (win: BrowserWindow) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getLoadedMainPlugin = <Key extends keyof PluginBuilderList>(id: Key): MainPlugin<PluginBuilderList[Key]['config']> | undefined => {
|
||||
export const getLoadedMainPlugin = (id: string): PluginDef | undefined => {
|
||||
return loadedPluginMap[id];
|
||||
};
|
||||
|
||||
export const getAllLoadedMainPlugins = () => {
|
||||
return loadedPluginMap;
|
||||
};
|
||||
export const registerMainPlugin = (
|
||||
id: string,
|
||||
builder: PluginBuilder<string, PluginBaseConfig>,
|
||||
factory?: MainPluginFactory<PluginBaseConfig>,
|
||||
) => {
|
||||
if (factory) allPluginFactoryList[id] = factory;
|
||||
allPluginBuilders[id] = builder;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user