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