mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 02:51:46 +00:00
refactor(plugin): new plugin system poc
This commit is contained in:
61
src/plugins/utils/builder.ts
Normal file
61
src/plugins/utils/builder.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import type {
|
||||
BrowserWindow,
|
||||
MenuItemConstructorOptions,
|
||||
} from 'electron';
|
||||
|
||||
export type PluginBaseConfig = {
|
||||
enabled: boolean;
|
||||
};
|
||||
export type BasePlugin<Config extends PluginBaseConfig> = {
|
||||
onLoad?: () => void;
|
||||
onConfigChange?: (newConfig: Config) => void;
|
||||
}
|
||||
export type RendererPlugin<Config extends PluginBaseConfig> = BasePlugin<Config>;
|
||||
export type MainPlugin<Config extends PluginBaseConfig> = Omit<BasePlugin<Config>, 'onLoad'> & {
|
||||
onLoad?: (window: BrowserWindow) => void;
|
||||
};
|
||||
export type PreloadPlugin<Config extends PluginBaseConfig> = BasePlugin<Config>;
|
||||
|
||||
type DeepPartial<T> = {
|
||||
[P in keyof T]?: DeepPartial<T[P]>;
|
||||
};
|
||||
export type PluginContext<Config extends PluginBaseConfig> = {
|
||||
getConfig: () => Config;
|
||||
setConfig: (config: DeepPartial<Config>) => void;
|
||||
|
||||
send: (event: string, ...args: unknown[]) => void;
|
||||
on: (event: string, listener: (...args: unknown[]) => void) => void;
|
||||
};
|
||||
|
||||
type IF<T> = (args: T) => T;
|
||||
export type PluginBuilder<ID extends string, Config extends PluginBaseConfig> = {
|
||||
createRenderer: IF<(context: PluginContext<Config>) => RendererPlugin<Config>>;
|
||||
createMain: IF<(context: PluginContext<Config>) => MainPlugin<Config>>;
|
||||
createPreload: IF<(context: PluginContext<Config>) => PreloadPlugin<Config>>;
|
||||
createMenu: IF<(context: PluginContext<Config>) => MenuItemConstructorOptions[]>;
|
||||
|
||||
id: ID;
|
||||
config: Config;
|
||||
name?: string;
|
||||
styles?: string[];
|
||||
};
|
||||
export type PluginBuilderOptions<Config extends PluginBaseConfig = PluginBaseConfig> = {
|
||||
name?: string;
|
||||
|
||||
config: Config;
|
||||
styles?: string[];
|
||||
}
|
||||
export const createPluginBuilder = <ID extends string, Config extends PluginBaseConfig>(
|
||||
id: ID,
|
||||
options: PluginBuilderOptions<Config>,
|
||||
): PluginBuilder<ID, Config> => ({
|
||||
createRenderer: (plugin) => plugin,
|
||||
createMain: (plugin) => plugin,
|
||||
createPreload: (plugin) => plugin,
|
||||
createMenu: (plugin) => plugin,
|
||||
|
||||
id,
|
||||
name: options.name,
|
||||
config: options.config,
|
||||
styles: options.styles,
|
||||
});
|
||||
@ -26,4 +26,9 @@ export interface MenuPlugin<ConfigType extends Config> extends Plugin<ConfigType
|
||||
onEnable: (config: ConfigType) => void;
|
||||
}
|
||||
|
||||
export const defineConfig = <ConfigType extends Config>(config: ConfigType) => config;
|
||||
const defaultPluginConfig: Record<string, unknown> = {};
|
||||
export const definePluginConfig = <T>(id: string, defaultValue: T): T => {
|
||||
defaultPluginConfig[id] = defaultValue;
|
||||
|
||||
return defaultValue;
|
||||
};
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
import type { Config, RendererPlugin } from '../common';
|
||||
|
||||
export const defineRendererPlugin = <ConfigType extends Config>(plugin: RendererPlugin<ConfigType>) => plugin;
|
||||
1
src/plugins/utils/types.ts
Normal file
1
src/plugins/utils/types.ts
Normal file
@ -0,0 +1 @@
|
||||
export type PluginConfig<T extends keyof PluginBuilderList> = PluginBuilderList[T]['config'];
|
||||
Reference in New Issue
Block a user