fix: lifecycle check

edge case:
 - There may be plugins that don't have a start or stop function
This commit is contained in:
JellyBrick
2023-11-28 05:23:35 +09:00
parent bf27c73f1d
commit 90103d9853
4 changed files with 75 additions and 29 deletions

View File

@ -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]',

View File

@ -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) {

View File

@ -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`);
}

View File

@ -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 PluginLifecycleExtra<Config, typeof options.context, unknown>)?.start;
if (!lifecycle) return false;
if (!lifecycle) return null;
try {
const defContext = def[options.ctx];
@ -57,17 +57,18 @@ export const startPlugin = <Config extends PluginConfig>(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 = <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;
const stop = def[options.ctx] as PluginLifecycleSimple<Config, unknown>;
if (!stop) return false;
if (!stop) return null;
try {
const start = performance.now();
@ -77,9 +78,10 @@ export const stopPlugin = <Config extends PluginConfig>(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;
}
};