mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 11:01:45 +00:00
change plugin system
This commit is contained in:
64
src/utils/index.ts
Normal file
64
src/utils/index.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import {
|
||||
BackendContext,
|
||||
PreloadContext,
|
||||
RendererContext,
|
||||
} from '@/types/contexts';
|
||||
import { PluginDef, PluginConfig } from '@/types/plugins';
|
||||
|
||||
export const createPlugin = (
|
||||
def: Omit<PluginDef, 'config'> & {
|
||||
config?: Omit<PluginConfig, 'enabled'>;
|
||||
},
|
||||
): PluginDef => def as PluginDef;
|
||||
|
||||
type Options =
|
||||
| { ctx: 'backend'; context: BackendContext }
|
||||
| { ctx: 'preload'; context: PreloadContext }
|
||||
| { ctx: 'renderer'; context: RendererContext };
|
||||
|
||||
export const startPlugin = (id: string, def: PluginDef, options: Options) => {
|
||||
const lifecycle: (ctx: (typeof options)['context']) => void =
|
||||
typeof def[options.ctx] === 'function'
|
||||
? def[options.ctx]
|
||||
: // @ts-expect-error TS is dum dum
|
||||
def[options.ctx]?.start;
|
||||
|
||||
if (!lifecycle) return false;
|
||||
|
||||
try {
|
||||
const start = performance.now();
|
||||
lifecycle(options.context);
|
||||
|
||||
// prettier-ignore
|
||||
console.log(`[YTM] Executed ${id}::${options.ctx} in ${performance.now() - start} ms`);
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log(`[YTM] Failed to start ${id}::${options.ctx}: ${String(err)}`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const stopPlugin = (id: string, def: PluginDef, options: Options) => {
|
||||
if (!def[options.ctx]) return false;
|
||||
if (typeof def[options.ctx] === 'function') return false;
|
||||
|
||||
// @ts-expect-error TS is dum dum
|
||||
const stop: (ctx: typeof options.context) => void = def[options.ctx]?.stop;
|
||||
if (!stop) return false;
|
||||
|
||||
try {
|
||||
const start = performance.now();
|
||||
stop(options.context);
|
||||
|
||||
// prettier-ignore
|
||||
console.log(`[YTM] Executed ${id}::${options.ctx} in ${performance.now() - start} ms`);
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log(
|
||||
`[YTM] Failed to execute ${id}::${options.ctx}: ${String(err)}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user