From 90103d98537525b87c612f4516f4e2bce9157885 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Tue, 28 Nov 2023 05:23:35 +0900 Subject: [PATCH] fix: lifecycle check edge case: - There may be plugins that don't have a start or stop function --- src/loader/main.ts | 36 ++++++++++++++++++++++++------------ src/loader/preload.ts | 25 ++++++++++++++++++++----- src/loader/renderer.ts | 29 +++++++++++++++++++++++------ src/utils/index.ts | 14 ++++++++------ 4 files changed, 75 insertions(+), 29 deletions(-) diff --git a/src/loader/main.ts b/src/loader/main.ts index 18780a5c..33b02964 100644 --- a/src/loader/main.ts +++ b/src/loader/main.ts @@ -55,18 +55,24 @@ export const forceUnloadMainPlugin = async ( ctx: 'backend', 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( '[YTMusic]', - `Cannot unload "${id}" plugin: no stop function`, + `Cannot unload "${id}" plugin`, ); reject(); return; } - - delete loadedPluginMap[id]; - console.log('[YTMusic]', `"${id}" plugin is unloaded`); - resolve(); } catch (err) { console.log('[YTMusic]', `Cannot unload "${id}" plugin: ${String(err)}`); reject(err); @@ -87,14 +93,20 @@ export const forceLoadMainPlugin = async ( ctx: 'backend', context: createContext(id, win), }); - if (!hasStarted) { - console.log('[YTMusic]', `Cannot load "${id}" plugin`); - reject(); - return; + if ( + hasStarted || + ( + hasStarted === null && + typeof plugin.backend !== 'function' && plugin.backend + ) + ) { + loadedPluginMap[id] = plugin; + resolve(); } - loadedPluginMap[id] = plugin; - resolve(); + console.log('[YTMusic]', `Cannot load "${id}" plugin`); + reject(); + return; } catch (err) { console.error( '[YTMusic]', diff --git a/src/loader/preload.ts b/src/loader/preload.ts index 68297d0e..8c8924dc 100644 --- a/src/loader/preload.ts +++ b/src/loader/preload.ts @@ -21,11 +21,18 @@ export const forceUnloadPreloadPlugin = (id: string) => { ctx: 'preload', context: createContext(id), }); - if (!hasStopped) { - console.log('[YTMusic]', `Cannot stop "${id}" plugin`); - return; + if ( + hasStopped || + ( + 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) => { @@ -38,7 +45,15 @@ export const forceLoadPreloadPlugin = (id: string) => { 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`); } catch (err) { diff --git a/src/loader/renderer.ts b/src/loader/renderer.ts index 637a2ced..0a1d723e 100644 --- a/src/loader/renderer.ts +++ b/src/loader/renderer.ts @@ -40,11 +40,21 @@ export const forceUnloadRendererPlugin = (id: string) => { const plugin = rendererPlugins[id]; if (!plugin) return; - stopPlugin(id, plugin, { ctx: 'renderer', context: createContext(id) }); - if (plugin?.stylesheets) + const hasStopped = stopPlugin(id, plugin, { ctx: 'renderer', context: createContext(id) }); + if (plugin?.stylesheets) { 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) => { @@ -56,7 +66,14 @@ export const forceLoadRendererPlugin = (id: string) => { context: createContext(id), }); - if (hasEvaled || plugin?.stylesheets) { + if ( + hasEvaled || + plugin?.stylesheets || + ( + hasEvaled === null && + typeof plugin?.renderer !== 'function' && plugin?.renderer + ) + ) { loadedPluginMap[id] = plugin; if (plugin?.stylesheets) { @@ -70,7 +87,7 @@ export const forceLoadRendererPlugin = (id: string) => { document.adoptedStyleSheets = [...document.adoptedStyleSheets, ...styleSheetList]; } - if (!hasEvaled) console.log('[YTMusic]', `"${id}" plugin is loaded`); + console.log('[YTMusic]', `"${id}" plugin is loaded`); } else { console.log('[YTMusic]', `Cannot initialize "${id}" plugin`); } diff --git a/src/utils/index.ts b/src/utils/index.ts index c73ba316..2e4fccc8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -38,7 +38,7 @@ export const startPlugin = (id: string, def: Plugin ? def[options.ctx] as PluginLifecycleSimple : (def[options.ctx] as PluginLifecycleExtra)?.start; - if (!lifecycle) return false; + if (!lifecycle) return null; try { const defContext = def[options.ctx]; @@ -57,17 +57,18 @@ export const startPlugin = (id: string, def: Plugin return true; } 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; } }; export const stopPlugin = (id: string, def: PluginDef, options: Options) => { - if (!def[options.ctx]) return false; + if (!def || !def[options.ctx]) return false; if (typeof def[options.ctx] === 'function') return false; const stop = def[options.ctx] as PluginLifecycleSimple; - if (!stop) return false; + if (!stop) return null; try { const start = performance.now(); @@ -77,9 +78,10 @@ export const stopPlugin = (id: string, def: PluginD return true; } catch (err) { - console.log( - `[YTM] Failed to execute ${id}::${options.ctx}: ${String(err)}`, + console.error( + `[YTM] Failed to execute ${id}::${options.ctx}`, ); + console.trace(err); return false; } };