mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-16 12:42:06 +00:00
fix: lifecycle check
edge case: - There may be plugins that don't have a start or stop function
This commit is contained in:
@ -55,18 +55,24 @@ export const forceUnloadMainPlugin = async (
|
|||||||
ctx: 'backend',
|
ctx: 'backend',
|
||||||
context: createContext(id, win),
|
context: createContext(id, win),
|
||||||
});
|
});
|
||||||
if (!hasStopped) {
|
if (
|
||||||
|
hasStopped ||
|
||||||
|
(
|
||||||
|
hasStopped === null &&
|
||||||
|
typeof plugin.backend !== 'function' && plugin.backend
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
delete loadedPluginMap[id];
|
||||||
|
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
'[YTMusic]',
|
'[YTMusic]',
|
||||||
`Cannot unload "${id}" plugin: no stop function`,
|
`Cannot unload "${id}" plugin`,
|
||||||
);
|
);
|
||||||
reject();
|
reject();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete loadedPluginMap[id];
|
|
||||||
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
|
||||||
resolve();
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('[YTMusic]', `Cannot unload "${id}" plugin: ${String(err)}`);
|
console.log('[YTMusic]', `Cannot unload "${id}" plugin: ${String(err)}`);
|
||||||
reject(err);
|
reject(err);
|
||||||
@ -87,14 +93,20 @@ export const forceLoadMainPlugin = async (
|
|||||||
ctx: 'backend',
|
ctx: 'backend',
|
||||||
context: createContext(id, win),
|
context: createContext(id, win),
|
||||||
});
|
});
|
||||||
if (!hasStarted) {
|
if (
|
||||||
console.log('[YTMusic]', `Cannot load "${id}" plugin`);
|
hasStarted ||
|
||||||
reject();
|
(
|
||||||
return;
|
hasStarted === null &&
|
||||||
|
typeof plugin.backend !== 'function' && plugin.backend
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
loadedPluginMap[id] = plugin;
|
||||||
|
resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
loadedPluginMap[id] = plugin;
|
console.log('[YTMusic]', `Cannot load "${id}" plugin`);
|
||||||
resolve();
|
reject();
|
||||||
|
return;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
console.error(
|
||||||
'[YTMusic]',
|
'[YTMusic]',
|
||||||
|
|||||||
@ -21,11 +21,18 @@ export const forceUnloadPreloadPlugin = (id: string) => {
|
|||||||
ctx: 'preload',
|
ctx: 'preload',
|
||||||
context: createContext(id),
|
context: createContext(id),
|
||||||
});
|
});
|
||||||
if (!hasStopped) {
|
if (
|
||||||
console.log('[YTMusic]', `Cannot stop "${id}" plugin`);
|
hasStopped ||
|
||||||
return;
|
(
|
||||||
|
hasStopped === null &&
|
||||||
|
typeof loadedPluginMap[id].preload !== 'function' && loadedPluginMap[id].preload
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
||||||
|
delete loadedPluginMap[id];
|
||||||
|
} else {
|
||||||
|
console.error('[YTMusic]', `Cannot stop "${id}" plugin`);
|
||||||
}
|
}
|
||||||
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const forceLoadPreloadPlugin = (id: string) => {
|
export const forceLoadPreloadPlugin = (id: string) => {
|
||||||
@ -38,7 +45,15 @@ export const forceLoadPreloadPlugin = (id: string) => {
|
|||||||
context: createContext(id),
|
context: createContext(id),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hasStarted) loadedPluginMap[id] = plugin;
|
if (
|
||||||
|
hasStarted ||
|
||||||
|
(
|
||||||
|
hasStarted === null &&
|
||||||
|
typeof plugin.preload !== 'function' && plugin.preload
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
loadedPluginMap[id] = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
console.log('[YTMusic]', `"${id}" plugin is loaded`);
|
console.log('[YTMusic]', `"${id}" plugin is loaded`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@ -40,11 +40,21 @@ export const forceUnloadRendererPlugin = (id: string) => {
|
|||||||
const plugin = rendererPlugins[id];
|
const plugin = rendererPlugins[id];
|
||||||
if (!plugin) return;
|
if (!plugin) return;
|
||||||
|
|
||||||
stopPlugin(id, plugin, { ctx: 'renderer', context: createContext(id) });
|
const hasStopped = stopPlugin(id, plugin, { ctx: 'renderer', context: createContext(id) });
|
||||||
if (plugin?.stylesheets)
|
if (plugin?.stylesheets) {
|
||||||
document.querySelector(`style#plugin-${id}`)?.remove();
|
document.querySelector(`style#plugin-${id}`)?.remove();
|
||||||
|
}
|
||||||
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
if (
|
||||||
|
hasStopped ||
|
||||||
|
(
|
||||||
|
hasStopped === null &&
|
||||||
|
typeof plugin?.renderer !== 'function' && plugin?.renderer
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
||||||
|
} else {
|
||||||
|
console.error('[YTMusic]', `Cannot stop "${id}" plugin`);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const forceLoadRendererPlugin = (id: string) => {
|
export const forceLoadRendererPlugin = (id: string) => {
|
||||||
@ -56,7 +66,14 @@ export const forceLoadRendererPlugin = (id: string) => {
|
|||||||
context: createContext(id),
|
context: createContext(id),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hasEvaled || plugin?.stylesheets) {
|
if (
|
||||||
|
hasEvaled ||
|
||||||
|
plugin?.stylesheets ||
|
||||||
|
(
|
||||||
|
hasEvaled === null &&
|
||||||
|
typeof plugin?.renderer !== 'function' && plugin?.renderer
|
||||||
|
)
|
||||||
|
) {
|
||||||
loadedPluginMap[id] = plugin;
|
loadedPluginMap[id] = plugin;
|
||||||
|
|
||||||
if (plugin?.stylesheets) {
|
if (plugin?.stylesheets) {
|
||||||
@ -70,7 +87,7 @@ export const forceLoadRendererPlugin = (id: string) => {
|
|||||||
document.adoptedStyleSheets = [...document.adoptedStyleSheets, ...styleSheetList];
|
document.adoptedStyleSheets = [...document.adoptedStyleSheets, ...styleSheetList];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasEvaled) console.log('[YTMusic]', `"${id}" plugin is loaded`);
|
console.log('[YTMusic]', `"${id}" plugin is loaded`);
|
||||||
} else {
|
} else {
|
||||||
console.log('[YTMusic]', `Cannot initialize "${id}" plugin`);
|
console.log('[YTMusic]', `Cannot initialize "${id}" plugin`);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,7 +38,7 @@ export const startPlugin = <Config extends PluginConfig>(id: string, def: Plugin
|
|||||||
? 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 false;
|
if (!lifecycle) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const defContext = def[options.ctx];
|
const defContext = def[options.ctx];
|
||||||
@ -57,17 +57,18 @@ export const startPlugin = <Config extends PluginConfig>(id: string, def: Plugin
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(`[YTM] Failed to start ${id}::${options.ctx}: ${String(err)}`);
|
console.error(`[YTM] Failed to start ${id}::${options.ctx}`);
|
||||||
|
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[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;
|
||||||
|
|
||||||
const stop = def[options.ctx] as PluginLifecycleSimple<Config, unknown>;
|
const stop = def[options.ctx] as PluginLifecycleSimple<Config, unknown>;
|
||||||
if (!stop) return false;
|
if (!stop) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
@ -77,9 +78,10 @@ export const stopPlugin = <Config extends PluginConfig>(id: string, def: PluginD
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(
|
console.error(
|
||||||
`[YTM] Failed to execute ${id}::${options.ctx}: ${String(err)}`,
|
`[YTM] Failed to execute ${id}::${options.ctx}`,
|
||||||
);
|
);
|
||||||
|
console.trace(err);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user