mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 18:41:47 +00:00
fix: fix setPartial
This commit is contained in:
@ -1,17 +1,20 @@
|
||||
import Store from 'electron-store';
|
||||
|
||||
import { deepmerge as createDeepmerge } from '@fastify/deepmerge';
|
||||
|
||||
import defaultConfig from './defaults';
|
||||
import plugins from './plugins';
|
||||
import store from './store';
|
||||
|
||||
import { restart } from '../providers/app-controls';
|
||||
|
||||
const deepmerge = createDeepmerge();
|
||||
|
||||
const set = (key: string, value: unknown) => {
|
||||
store.set(key, value);
|
||||
};
|
||||
const setPartial = (value: object) => {
|
||||
// deepmerge(store.get, value);
|
||||
const setPartial = (key: string, value: object) => {
|
||||
deepmerge(store.get(key), value);
|
||||
store.set(value);
|
||||
};
|
||||
|
||||
|
||||
26
src/index.ts
26
src/index.ts
@ -101,11 +101,7 @@ ipcMain.handle('get-main-plugin-names', () => Object.keys(mainPlugins));
|
||||
|
||||
const initHook = (win: BrowserWindow) => {
|
||||
ipcMain.handle('get-config', (_, id: keyof PluginBuilderList) => config.get(`plugins.${id}` as never) ?? pluginBuilders[id].config);
|
||||
ipcMain.handle('set-config', (_, name: string, obj: object) => config.setPartial({
|
||||
plugins: {
|
||||
[name]: obj,
|
||||
}
|
||||
}));
|
||||
ipcMain.handle('set-config', (_, name: string, obj: object) => config.setPartial(`plugins.${name}`, obj));
|
||||
|
||||
config.watch((newValue) => {
|
||||
const value = newValue as Record<string, unknown>;
|
||||
@ -144,30 +140,18 @@ async function loadPlugins(win: BrowserWindow) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
const createContext = <
|
||||
Key extends keyof PluginBuilderList,
|
||||
Config extends PluginBaseConfig = PluginBuilderList[Key]['config'],
|
||||
>(name: Key): MainPluginContext<Config> => ({
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
getConfig: () => config.get(`plugins.${name}`) as unknown as Config,
|
||||
setConfig: (newConfig) => {
|
||||
config.setPartial({
|
||||
plugins: {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
[name]: {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
...config.get(`plugins.${name}`),
|
||||
...newConfig,
|
||||
},
|
||||
},
|
||||
});
|
||||
config.setPartial(`plugins.${name}`, newConfig);
|
||||
|
||||
return Promise.resolve();
|
||||
},
|
||||
|
||||
|
||||
send: (event: string, ...args: unknown[]) => {
|
||||
win.webContents.send(event, ...args);
|
||||
},
|
||||
@ -175,7 +159,7 @@ async function loadPlugins(win: BrowserWindow) {
|
||||
ipcMain.handle(event, async (_, ...args) => listener(...args as never));
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
|
||||
for (const [pluginId, options] of config.plugins.getEnabled()) {
|
||||
const builder = pluginBuilders[pluginId as keyof PluginBuilderList];
|
||||
|
||||
23
src/menu.ts
23
src/menu.ts
@ -13,7 +13,7 @@ import { pluginBuilders } from 'virtual:PluginBuilders';
|
||||
/* eslint-enable import/order */
|
||||
|
||||
import { getAvailablePluginNames } from './plugins/utils/main';
|
||||
import { MenuPluginContext, MenuPluginFactory, PluginBaseConfig, PluginContext } from './plugins/utils/builder';
|
||||
import { MenuPluginContext, MenuPluginFactory, PluginBaseConfig } from './plugins/utils/builder';
|
||||
|
||||
export type MenuTemplate = Electron.MenuItemConstructorOptions[];
|
||||
|
||||
@ -48,22 +48,13 @@ export const refreshMenu = (win: BrowserWindow) => {
|
||||
|
||||
export const mainMenuTemplate = async (win: BrowserWindow): Promise<MenuTemplate> => {
|
||||
const innerRefreshMenu = () => refreshMenu(win);
|
||||
const createContext = <Config extends PluginBaseConfig>(name: string): MenuPluginContext<Config> => ({
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
const createContext = <
|
||||
Key extends keyof PluginBuilderList,
|
||||
Config extends PluginBaseConfig = PluginBuilderList[Key]['config'],
|
||||
>(name: Key): MenuPluginContext<Config> => ({
|
||||
getConfig: () => config.get(`plugins.${name}`) as unknown as Config,
|
||||
setConfig: (newConfig) => {
|
||||
config.setPartial({
|
||||
plugins: {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
[name]: {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
...config.get(`plugins.${name}`),
|
||||
...newConfig,
|
||||
},
|
||||
},
|
||||
});
|
||||
config.setPartial(`plugins.${name}`, newConfig);
|
||||
|
||||
return Promise.resolve();
|
||||
},
|
||||
@ -83,7 +74,7 @@ export const mainMenuTemplate = async (win: BrowserWindow): Promise<MenuTemplate
|
||||
}
|
||||
|
||||
const factory = menuList[id] as MenuPluginFactory<PluginBaseConfig>;
|
||||
const template = await factory(createContext(id));
|
||||
const template = await factory(createContext(id as never));
|
||||
|
||||
return {
|
||||
label: pluginLabel,
|
||||
|
||||
@ -23,8 +23,8 @@ type IF<T> = (args: T) => T;
|
||||
type Promisable<T> = T | Promise<T>;
|
||||
|
||||
export type PluginContext<Config extends PluginBaseConfig = PluginBaseConfig> = {
|
||||
getConfig: () => Promise<Config>;
|
||||
setConfig: (config: DeepPartial<Config>) => Promise<void>;
|
||||
getConfig: () => Promisable<Config>;
|
||||
setConfig: (config: DeepPartial<Config>) => Promisable<void>;
|
||||
};
|
||||
|
||||
export type MainPluginContext<Config extends PluginBaseConfig = PluginBaseConfig> = PluginContext<Config> & {
|
||||
|
||||
@ -144,10 +144,11 @@ const createContext = <
|
||||
|
||||
const rendererPluginResult = await Promise.allSettled(
|
||||
enabledPluginNameAndOptions.map(async ([id]) => {
|
||||
// HACK: eslint has a bug detects the type of rendererPlugins as "any"
|
||||
const builder = (rendererPlugins as Record<string, RendererPluginFactory<PluginBaseConfig>>)[id];
|
||||
|
||||
const context = createContext(id as never);
|
||||
return [id, await builder(context as never)] as const;
|
||||
const context = createContext(id as keyof PluginBuilderList);
|
||||
return [id, await builder(context)] as const;
|
||||
}),
|
||||
);
|
||||
|
||||
@ -172,12 +173,12 @@ const createContext = <
|
||||
console.trace(error);
|
||||
}
|
||||
});
|
||||
|
||||
window.ipcRenderer.on('config-changed', (_event, id: string, newConfig) => {
|
||||
|
||||
window.ipcRenderer.on('config-changed', (_event, id: string, newConfig: PluginBaseConfig) => {
|
||||
const plugin = rendererPluginList.find(([pluginId]) => pluginId === id);
|
||||
|
||||
if (plugin) {
|
||||
plugin[1].onConfigChange?.(newConfig as never);
|
||||
plugin[1].onConfigChange?.(newConfig);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user