mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 19:31:46 +00:00
make log prefix consistent
This commit is contained in:
@ -6,7 +6,9 @@ import type {
|
|||||||
|
|
||||||
import type {
|
import type {
|
||||||
PluginDef,
|
PluginDef,
|
||||||
PluginConfig, PluginLifecycleExtra, PluginLifecycleSimple,
|
PluginConfig,
|
||||||
|
PluginLifecycleExtra,
|
||||||
|
PluginLifecycleSimple,
|
||||||
} from '@/types/plugins';
|
} from '@/types/plugins';
|
||||||
|
|
||||||
export const createPlugin = <
|
export const createPlugin = <
|
||||||
@ -32,38 +34,58 @@ type Options<Config extends PluginConfig> =
|
|||||||
| { ctx: 'preload'; context: PreloadContext<Config> }
|
| { ctx: 'preload'; context: PreloadContext<Config> }
|
||||||
| { ctx: 'renderer'; context: RendererContext<Config> };
|
| { ctx: 'renderer'; context: RendererContext<Config> };
|
||||||
|
|
||||||
export const startPlugin = <Config extends PluginConfig>(id: string, def: PluginDef<unknown, unknown, unknown, Config>, options: Options<Config>) => {
|
export const startPlugin = <Config extends PluginConfig>(
|
||||||
|
id: string,
|
||||||
|
def: PluginDef<unknown, unknown, unknown, Config>,
|
||||||
|
options: Options<Config>,
|
||||||
|
) => {
|
||||||
const lifecycle =
|
const lifecycle =
|
||||||
typeof def[options.ctx] === 'function'
|
typeof def[options.ctx] === 'function'
|
||||||
? def[options.ctx] as PluginLifecycleSimple<Config, unknown>
|
? (def[options.ctx] as PluginLifecycleSimple<Config, unknown>)
|
||||||
: (def[options.ctx] as PluginLifecycleExtra<Config, typeof options.context, unknown>)?.start;
|
: (
|
||||||
|
def[options.ctx] as PluginLifecycleExtra<
|
||||||
|
Config,
|
||||||
|
typeof options.context,
|
||||||
|
unknown
|
||||||
|
>
|
||||||
|
)?.start;
|
||||||
|
|
||||||
if (!lifecycle) return null;
|
if (!lifecycle) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const defContext = def[options.ctx];
|
const defContext = def[options.ctx];
|
||||||
if (defContext && typeof defContext !== 'function') {
|
if (defContext && typeof defContext !== 'function') {
|
||||||
Object.entries(defContext)
|
Object.entries(defContext).forEach(([key, value]) => {
|
||||||
.forEach(([key, value]) => {
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
if (typeof value === 'function')
|
||||||
if (typeof value === 'function') defContext[key as keyof typeof defContext] = value.bind(defContext);
|
defContext[key as keyof typeof defContext] = value.bind(defContext);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
lifecycle.bind(def[options.ctx])(options.context as Config & typeof options.context);
|
lifecycle.bind(def[options.ctx])(
|
||||||
|
options.context as Config & typeof options.context,
|
||||||
|
);
|
||||||
|
|
||||||
console.log(`[YTM] Executed ${id}::${options.ctx} in ${performance.now() - start} ms`);
|
console.log(
|
||||||
|
`[YTMusic] Executed ${id}::${options.ctx} in ${
|
||||||
|
performance.now() - start
|
||||||
|
} ms`,
|
||||||
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`[YTM] Failed to start ${id}::${options.ctx}`);
|
console.error(`[YTMusic] Failed to start ${id}::${options.ctx}`);
|
||||||
console.trace(err);
|
console.trace(err);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const stopPlugin = <Config extends PluginConfig>(id: string, def: PluginDef<unknown, unknown, unknown, Config>, options: Options<Config>) => {
|
export const stopPlugin = <Config extends PluginConfig>(
|
||||||
|
id: string,
|
||||||
|
def: PluginDef<unknown, unknown, unknown, Config>,
|
||||||
|
options: Options<Config>,
|
||||||
|
) => {
|
||||||
if (!def || !def[options.ctx]) return false;
|
if (!def || !def[options.ctx]) return false;
|
||||||
if (typeof def[options.ctx] === 'function') return false;
|
if (typeof def[options.ctx] === 'function') return false;
|
||||||
|
|
||||||
@ -72,15 +94,19 @@ export const stopPlugin = <Config extends PluginConfig>(id: string, def: PluginD
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
stop.bind(def[options.ctx])(options.context as Config & typeof options.context);
|
stop.bind(def[options.ctx])(
|
||||||
|
options.context as Config & typeof options.context,
|
||||||
|
);
|
||||||
|
|
||||||
console.log(`[YTM] Executed ${id}::${options.ctx} in ${performance.now() - start} ms`);
|
console.log(
|
||||||
|
`[YTMusic] Executed ${id}::${options.ctx} in ${
|
||||||
|
performance.now() - start
|
||||||
|
} ms`,
|
||||||
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
console.error(`[YTMusic] Failed to execute ${id}::${options.ctx}`);
|
||||||
`[YTM] Failed to execute ${id}::${options.ctx}`,
|
|
||||||
);
|
|
||||||
console.trace(err);
|
console.trace(err);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user