From 9c59f56aac226f1050e353769c09738849e021cf Mon Sep 17 00:00:00 2001 From: Su-Yong Date: Sun, 12 Nov 2023 00:50:09 +0900 Subject: [PATCH] refactor(plugin): apply new plugin loader at all type of plugin --- src/loader/menu.ts | 72 +++++++++++++++++++++++++ src/loader/preload.ts | 90 ++++++++++++++++++++++++++++++++ src/menu.ts | 67 ++++++++++++------------ src/plugins/lumiastream/index.ts | 2 +- src/preload.ts | 47 +++++------------ 5 files changed, 208 insertions(+), 70 deletions(-) create mode 100644 src/loader/menu.ts create mode 100644 src/loader/preload.ts diff --git a/src/loader/menu.ts b/src/loader/menu.ts new file mode 100644 index 00000000..901cfc85 --- /dev/null +++ b/src/loader/menu.ts @@ -0,0 +1,72 @@ +import { deepmerge } from 'deepmerge-ts'; + +import { MenuPluginContext, MenuPluginFactory, PluginBaseConfig, PluginBuilder } from '../plugins/utils/builder'; +import config from '../config'; +import { setApplicationMenu } from '../menu'; + +import type { BrowserWindow, MenuItemConstructorOptions } from 'electron'; + +const allPluginFactoryList: Record> = {}; +const allPluginBuilders: Record> = {}; +const menuTemplateMap: Record = {}; + +const createContext = < + Key extends keyof PluginBuilderList, + Config extends PluginBaseConfig = PluginBuilderList[Key]['config'], +>(id: Key, win: BrowserWindow): MenuPluginContext => ({ + getConfig: () => deepmerge(allPluginBuilders[id].config, config.get(`plugins.${id}`) ?? {}) as Config, + setConfig: (newConfig) => { + config.setPartial(`plugins.${id}`, newConfig); + }, + window: win, + refresh: async () => { + await setApplicationMenu(win); + + if (config.plugins.isEnabled('in-app-menu')) { + win.webContents.send('refresh-in-app-menu'); + } + }, +}); + +export const forceLoadMenuPlugin = async (id: keyof PluginBuilderList, win: BrowserWindow) => { + try { + const factory = allPluginFactoryList[id]; + if (!factory) return; + + const context = createContext(id, win); + menuTemplateMap[id] = await factory(context); + + console.log('[YTMusic]', `"${id}" plugin is loaded`); + } catch (err) { + console.log('[YTMusic]', `Cannot initialize "${id}" plugin: ${String(err)}`); + } +}; + +export const loadAllMenuPlugins = async (win: BrowserWindow) => { + const pluginConfigs = config.plugins.getPlugins(); + + 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] ?? {}); + + if (config.enabled) { + await forceLoadMenuPlugin(pluginId as keyof PluginBuilderList, win); + } + } +}; + +export const getMenuTemplate = (id: Key): MenuItemConstructorOptions[] | undefined => { + return menuTemplateMap[id]; +}; +export const getAllMenuTemplate = () => { + return menuTemplateMap; +}; +export const registerMenuPlugin = ( + id: string, + builder: PluginBuilder, + factory?: MenuPluginFactory, +) => { + if (factory) allPluginFactoryList[id] = factory; + allPluginBuilders[id] = builder; +}; diff --git a/src/loader/preload.ts b/src/loader/preload.ts new file mode 100644 index 00000000..7d55942c --- /dev/null +++ b/src/loader/preload.ts @@ -0,0 +1,90 @@ +import { deepmerge } from 'deepmerge-ts'; + +import { + PluginBaseConfig, + PluginBuilder, + PreloadPlugin, + PluginContext, + PreloadPluginFactory +} from '../plugins/utils/builder'; +import config from '../config'; + +const allPluginFactoryList: Record> = {}; +const allPluginBuilders: Record> = {}; +const unregisterStyleMap: Record void)[]> = {}; +const loadedPluginMap: Record> = {}; + +const createContext = < + Key extends keyof PluginBuilderList, + Config extends PluginBaseConfig = PluginBuilderList[Key]['config'], +>(id: Key): PluginContext => ({ + getConfig: () => deepmerge(allPluginBuilders[id].config, config.get(`plugins.${id}`) ?? {}) as Config, + setConfig: (newConfig) => { + config.setPartial(`plugins.${id}`, newConfig); + }, +}); + +const forceUnloadPreloadPlugin = (id: keyof PluginBuilderList) => { + unregisterStyleMap[id]?.forEach((unregister) => unregister()); + delete unregisterStyleMap[id]; + + loadedPluginMap[id]?.onUnload?.(); + delete loadedPluginMap[id]; + + console.log('[YTMusic]', `"${id}" plugin is unloaded`); +}; + +export const forceLoadPreloadPlugin = async (id: keyof PluginBuilderList) => { + try { + const factory = allPluginFactoryList[id]; + if (!factory) return; + + const context = createContext(id); + const plugin = await factory(context); + loadedPluginMap[id] = plugin; + plugin.onLoad?.(); + + console.log('[YTMusic]', `"${id}" plugin is loaded`); + } catch (err) { + console.log('[YTMusic]', `Cannot initialize "${id}" plugin: ${String(err)}`); + } +}; + +export const loadAllPreloadPlugins = async () => { + const pluginConfigs = config.plugins.getPlugins(); + + 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] ?? {}); + + if (config.enabled) { + await forceLoadPreloadPlugin(pluginId as keyof PluginBuilderList); + } else { + if (loadedPluginMap[pluginId as keyof PluginBuilderList]) { + forceUnloadPreloadPlugin(pluginId as keyof PluginBuilderList); + } + } + } +}; + +export const unloadAllPreloadPlugins = () => { + for (const id of Object.keys(loadedPluginMap)) { + forceUnloadPreloadPlugin(id as keyof PluginBuilderList); + } +}; + +export const getLoadedPreloadPlugin = (id: Key): PreloadPlugin | undefined => { + return loadedPluginMap[id]; +}; +export const getAllLoadedPreloadPlugins = () => { + return loadedPluginMap; +}; +export const registerPreloadPlugin = ( + id: string, + builder: PluginBuilder, + factory?: PreloadPluginFactory, +) => { + if (factory) allPluginFactoryList[id] = factory; + allPluginBuilders[id] = builder; +}; diff --git a/src/menu.ts b/src/menu.ts index d270d9d9..d8ddb75e 100644 --- a/src/menu.ts +++ b/src/menu.ts @@ -1,7 +1,6 @@ import is from 'electron-is'; import { app, BrowserWindow, clipboard, dialog, Menu } from 'electron'; import prompt from 'custom-electron-prompt'; -import { deepmerge } from 'deepmerge-ts'; import { restart } from './providers/app-controls'; import config from './config'; @@ -14,15 +13,18 @@ import { pluginBuilders } from 'virtual:PluginBuilders'; /* eslint-enable import/order */ import { getAvailablePluginNames } from './plugins/utils/main'; -import { MenuPluginContext, MenuPluginFactory, PluginBaseConfig } from './plugins/utils/builder'; +import { + MenuPluginFactory, + PluginBaseConfig, + PluginBuilder +} from './plugins/utils/builder'; +import { getAllMenuTemplate, loadAllMenuPlugins, registerMenuPlugin } from './loader/menu'; export type MenuTemplate = Electron.MenuItemConstructorOptions[]; // True only if in-app-menu was loaded on launch const inAppMenuActive = config.plugins.isEnabled('in-app-menu'); -const betaPlugins = ['crossfade', 'lumiastream']; - const pluginEnabledMenu = (plugin: string, label = '', hasSubmenu = false, refreshMenu: (() => void ) | undefined = undefined): Electron.MenuItemConstructorOptions => ({ label: label || plugin, type: 'checkbox', @@ -47,49 +49,46 @@ export const refreshMenu = (win: BrowserWindow) => { } }; +Object.entries(pluginBuilders).forEach(([id, builder]) => { + const typedBuilder = builder as PluginBuilder; + const plugin = menuList[id] as MenuPluginFactory | undefined; + + registerMenuPlugin(id, typedBuilder, plugin); +}); + export const mainMenuTemplate = async (win: BrowserWindow): Promise => { const innerRefreshMenu = () => refreshMenu(win); - const createContext = < - Key extends keyof PluginBuilderList, - Config extends PluginBaseConfig = PluginBuilderList[Key]['config'], - >(name: Key): MenuPluginContext => ({ - getConfig: () => deepmerge(pluginBuilders[name].config, config.get(`plugins.${name}`) ?? {}) as unknown as Config, - setConfig: (newConfig) => { - config.setPartial(`plugins.${name}`, newConfig); - }, - window: win, - refresh: () => refreshMenu(win), - }); - const availablePlugins = getAvailablePluginNames(); - const menuResult = await Promise.allSettled( - availablePlugins.map(async (id) => { - let pluginLabel = pluginBuilders[id as keyof PluginBuilderList]?.name ?? id; - if (betaPlugins.includes(pluginLabel)) { - pluginLabel += ' [beta]'; - } + await loadAllMenuPlugins(win); - if (!config.plugins.isEnabled(id)) { - return pluginEnabledMenu(id, pluginLabel, true, innerRefreshMenu); - } + const menuResult = Object.entries(getAllMenuTemplate()).map(([id, template]) => { + const pluginLabel = (pluginBuilders[id as keyof PluginBuilderList])?.name ?? id; - const factory = menuList[id] as MenuPluginFactory; - const template = await factory(createContext(id as never)); + if (!config.plugins.isEnabled(id)) { + return [ + id, + pluginEnabledMenu(id, pluginLabel, true, innerRefreshMenu), + ] as const; + } - return { + return [ + id, + { label: pluginLabel, submenu: [ pluginEnabledMenu(id, 'Enabled', true, innerRefreshMenu), { type: 'separator' }, ...template, ], - } satisfies Electron.MenuItemConstructorOptions; - }), - ); + } satisfies Electron.MenuItemConstructorOptions + ] as const; + }); + + const availablePlugins = getAvailablePluginNames(); + const pluginMenus = availablePlugins.map((id) => { + const predefinedTemplate = menuResult.find((it) => it[0] === id); + if (predefinedTemplate) return predefinedTemplate[1]; - const pluginMenus = menuResult.map((it, index) => { - if (it.status === 'fulfilled') return it.value; - const id = availablePlugins[index]; const pluginLabel = pluginBuilders[id as keyof PluginBuilderList]?.name ?? id; return pluginEnabledMenu(id, pluginLabel, true, innerRefreshMenu); diff --git a/src/plugins/lumiastream/index.ts b/src/plugins/lumiastream/index.ts index b3af3375..e8a2db55 100644 --- a/src/plugins/lumiastream/index.ts +++ b/src/plugins/lumiastream/index.ts @@ -1,7 +1,7 @@ import { createPluginBuilder } from '../utils/builder'; const builder = createPluginBuilder('lumiastream', { - name: 'Lumia Stream', + name: 'Lumia Stream [beta]', restartNeeded: true, config: { enabled: false, diff --git a/src/preload.ts b/src/preload.ts index 5f6ac5d0..ea8f138a 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -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 => ({ - 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>)[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; + const plugin = preloadPlugins[id] as PreloadPluginFactory | undefined; + + registerPreloadPlugin(id, typedBuilder, plugin); }); +loadAllPreloadPlugins(); contextBridge.exposeInMainWorld('mainConfig', config); contextBridge.exposeInMainWorld('electronIs', is);