mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 03:11:46 +00:00
feat: plugin load await
This commit is contained in:
@ -49,35 +49,33 @@ export const forceUnloadMainPlugin = async (
|
||||
const plugin = loadedPluginMap[id];
|
||||
if (!plugin) return;
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
try {
|
||||
const hasStopped = stopPlugin(id, plugin, {
|
||||
ctx: 'backend',
|
||||
context: createContext(id, win),
|
||||
});
|
||||
if (
|
||||
hasStopped ||
|
||||
(
|
||||
hasStopped === null &&
|
||||
typeof plugin.backend !== 'function' && plugin.backend
|
||||
)
|
||||
) {
|
||||
delete loadedPluginMap[id];
|
||||
console.log(LoggerPrefix, `"${id}" plugin is unloaded`);
|
||||
resolve();
|
||||
} else {
|
||||
console.log(
|
||||
LoggerPrefix,
|
||||
`Cannot unload "${id}" plugin`,
|
||||
);
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(LoggerPrefix, `Cannot unload "${id}" plugin: ${String(err)}`);
|
||||
reject(err);
|
||||
try {
|
||||
const hasStopped = await stopPlugin(id, plugin, {
|
||||
ctx: 'backend',
|
||||
context: createContext(id, win),
|
||||
});
|
||||
if (
|
||||
hasStopped ||
|
||||
(
|
||||
hasStopped === null &&
|
||||
typeof plugin.backend !== 'function' && plugin.backend
|
||||
)
|
||||
) {
|
||||
delete loadedPluginMap[id];
|
||||
console.log(LoggerPrefix, `"${id}" plugin is unloaded`);
|
||||
return;
|
||||
} else {
|
||||
console.log(
|
||||
LoggerPrefix,
|
||||
`Cannot unload "${id}" plugin`,
|
||||
);
|
||||
return Promise.reject();
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(LoggerPrefix, `Cannot unload "${id}" plugin`);
|
||||
console.trace(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
};
|
||||
|
||||
export const forceLoadMainPlugin = async (
|
||||
@ -87,34 +85,31 @@ export const forceLoadMainPlugin = async (
|
||||
const plugin = mainPlugins[id];
|
||||
if (!plugin) return;
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
try {
|
||||
const hasStarted = startPlugin(id, plugin, {
|
||||
ctx: 'backend',
|
||||
context: createContext(id, win),
|
||||
});
|
||||
if (
|
||||
hasStarted ||
|
||||
(
|
||||
hasStarted === null &&
|
||||
typeof plugin.backend !== 'function' && plugin.backend
|
||||
)
|
||||
) {
|
||||
loadedPluginMap[id] = plugin;
|
||||
resolve();
|
||||
} else {
|
||||
console.log(LoggerPrefix, `Cannot load "${id}" plugin`);
|
||||
reject();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
LoggerPrefix,
|
||||
`Cannot initialize "${id}" plugin: `,
|
||||
);
|
||||
console.trace(err);
|
||||
reject(err);
|
||||
try {
|
||||
const hasStarted = await startPlugin(id, plugin, {
|
||||
ctx: 'backend',
|
||||
context: createContext(id, win),
|
||||
});
|
||||
if (
|
||||
hasStarted ||
|
||||
(
|
||||
hasStarted === null &&
|
||||
typeof plugin.backend !== 'function' && plugin.backend
|
||||
)
|
||||
) {
|
||||
loadedPluginMap[id] = plugin;
|
||||
} else {
|
||||
console.log(LoggerPrefix, `Cannot load "${id}" plugin`);
|
||||
return Promise.reject();
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(
|
||||
LoggerPrefix,
|
||||
`Cannot initialize "${id}" plugin: `,
|
||||
);
|
||||
console.trace(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
};
|
||||
|
||||
export const loadAllMainPlugins = async (win: BrowserWindow) => {
|
||||
@ -134,9 +129,9 @@ export const loadAllMainPlugins = async (win: BrowserWindow) => {
|
||||
await Promise.allSettled(queue);
|
||||
};
|
||||
|
||||
export const unloadAllMainPlugins = (win: BrowserWindow) => {
|
||||
export const unloadAllMainPlugins = async (win: BrowserWindow) => {
|
||||
for (const id of Object.keys(loadedPluginMap)) {
|
||||
forceUnloadMainPlugin(id, win);
|
||||
await forceUnloadMainPlugin(id, win);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -20,10 +20,10 @@ const createContext = (id: string): PreloadContext<PluginConfig> => ({
|
||||
},
|
||||
});
|
||||
|
||||
export const forceUnloadPreloadPlugin = (id: string) => {
|
||||
export const forceUnloadPreloadPlugin = async (id: string) => {
|
||||
if (!loadedPluginMap[id]) return;
|
||||
|
||||
const hasStopped = stopPlugin(id, loadedPluginMap[id], {
|
||||
const hasStopped = await stopPlugin(id, loadedPluginMap[id], {
|
||||
ctx: 'preload',
|
||||
context: createContext(id),
|
||||
});
|
||||
@ -41,12 +41,12 @@ export const forceUnloadPreloadPlugin = (id: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const forceLoadPreloadPlugin = (id: string) => {
|
||||
export const forceLoadPreloadPlugin = async (id: string) => {
|
||||
try {
|
||||
const plugin = preloadPlugins[id];
|
||||
if (!plugin) return;
|
||||
|
||||
const hasStarted = startPlugin(id, plugin, {
|
||||
const hasStarted = await startPlugin(id, plugin, {
|
||||
ctx: 'preload',
|
||||
context: createContext(id),
|
||||
});
|
||||
@ -71,25 +71,25 @@ export const forceLoadPreloadPlugin = (id: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const loadAllPreloadPlugins = () => {
|
||||
export const loadAllPreloadPlugins = async () => {
|
||||
const pluginConfigs = config.plugins.getPlugins();
|
||||
|
||||
for (const [pluginId, pluginDef] of Object.entries(preloadPlugins)) {
|
||||
const config = deepmerge(pluginDef.config ?? { enable: false }, pluginConfigs[pluginId] ?? {}) ;
|
||||
const config = deepmerge(pluginDef.config ?? { enable: false }, pluginConfigs[pluginId] ?? {});
|
||||
|
||||
if (config.enabled) {
|
||||
forceLoadPreloadPlugin(pluginId);
|
||||
await forceLoadPreloadPlugin(pluginId);
|
||||
} else {
|
||||
if (loadedPluginMap[pluginId]) {
|
||||
forceUnloadPreloadPlugin(pluginId);
|
||||
await forceUnloadPreloadPlugin(pluginId);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const unloadAllPreloadPlugins = () => {
|
||||
export const unloadAllPreloadPlugins = async () => {
|
||||
for (const id of Object.keys(loadedPluginMap)) {
|
||||
forceUnloadPreloadPlugin(id);
|
||||
await forceUnloadPreloadPlugin(id);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ export const createContext = <Config extends PluginConfig>(id: string): Renderer
|
||||
},
|
||||
});
|
||||
|
||||
export const forceUnloadRendererPlugin = (id: string) => {
|
||||
export const forceUnloadRendererPlugin = async (id: string) => {
|
||||
unregisterStyleMap[id]?.forEach((unregister) => unregister());
|
||||
|
||||
delete unregisterStyleMap[id];
|
||||
@ -40,7 +40,7 @@ export const forceUnloadRendererPlugin = (id: string) => {
|
||||
const plugin = rendererPlugins[id];
|
||||
if (!plugin) return;
|
||||
|
||||
const hasStopped = stopPlugin(id, plugin, { ctx: 'renderer', context: createContext(id) });
|
||||
const hasStopped = await stopPlugin(id, plugin, { ctx: 'renderer', context: createContext(id) });
|
||||
if (plugin?.stylesheets) {
|
||||
document.querySelector(`style#plugin-${id}`)?.remove();
|
||||
}
|
||||
@ -57,11 +57,11 @@ export const forceUnloadRendererPlugin = (id: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const forceLoadRendererPlugin = (id: string) => {
|
||||
export const forceLoadRendererPlugin = async (id: string) => {
|
||||
const plugin = rendererPlugins[id];
|
||||
if (!plugin) return;
|
||||
|
||||
const hasEvaled = startPlugin(id, plugin, {
|
||||
const hasEvaled = await startPlugin(id, plugin, {
|
||||
ctx: 'renderer',
|
||||
context: createContext(id),
|
||||
});
|
||||
@ -93,25 +93,25 @@ export const forceLoadRendererPlugin = (id: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const loadAllRendererPlugins = () => {
|
||||
export const loadAllRendererPlugins = async () => {
|
||||
const pluginConfigs = window.mainConfig.plugins.getPlugins();
|
||||
|
||||
for (const [pluginId, pluginDef] of Object.entries(rendererPlugins)) {
|
||||
const config = deepmerge(pluginDef.config, pluginConfigs[pluginId] ?? {}) ;
|
||||
|
||||
if (config.enabled) {
|
||||
forceLoadRendererPlugin(pluginId);
|
||||
await forceLoadRendererPlugin(pluginId);
|
||||
} else {
|
||||
if (loadedPluginMap[pluginId]) {
|
||||
forceUnloadRendererPlugin(pluginId);
|
||||
await forceUnloadRendererPlugin(pluginId);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const unloadAllRendererPlugins = () => {
|
||||
export const unloadAllRendererPlugins = async () => {
|
||||
for (const id of Object.keys(loadedPluginMap)) {
|
||||
forceUnloadRendererPlugin(id);
|
||||
await forceUnloadRendererPlugin(id);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user