diff options
author | V <vendicated@riseup.net> | 2023-04-22 03:18:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-22 01:18:19 +0000 |
commit | 63fc354d483b86857bbee7f540c66ba614fc0f1f (patch) | |
tree | 6ebe100e5ff63254adfdb0c44cb473dcc339cf35 /src/plugins/index.ts | |
parent | c6f0c84935db37e2a18578a081404d84292fe36a (diff) | |
download | Vencord-63fc354d483b86857bbee7f540c66ba614fc0f1f.tar.gz Vencord-63fc354d483b86857bbee7f540c66ba614fc0f1f.tar.bz2 Vencord-63fc354d483b86857bbee7f540c66ba614fc0f1f.zip |
feat: auto-managed flux subscriptions via plugin.flux (#959)
Diffstat (limited to 'src/plugins/index.ts')
-rw-r--r-- | src/plugins/index.ts | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/src/plugins/index.ts b/src/plugins/index.ts index d0d16c2..ab69ca7 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -20,6 +20,8 @@ import { registerCommand, unregisterCommand } from "@api/Commands"; import { Settings } from "@api/settings"; import Logger from "@utils/Logger"; import { Patch, Plugin } from "@utils/types"; +import { FluxDispatcher } from "@webpack/common"; +import { FluxEvents } from "@webpack/types"; import Plugins from "~plugins"; @@ -111,56 +113,64 @@ export function startDependenciesRecursive(p: Plugin) { } export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) { + const { name, commands, flux } = p; + if (p.start) { - logger.info("Starting plugin", p.name); + logger.info("Starting plugin", name); if (p.started) { - logger.warn(`${p.name} already started`); + logger.warn(`${name} already started`); return false; } try { p.start(); p.started = true; } catch (e) { - logger.error(`Failed to start ${p.name}\n`, e); + logger.error(`Failed to start ${name}\n`, e); return false; } } - if (p.commands?.length) { - logger.info("Registering commands of plugin", p.name); - for (const cmd of p.commands) { + if (commands?.length) { + logger.info("Registering commands of plugin", name); + for (const cmd of commands) { try { - registerCommand(cmd, p.name); + registerCommand(cmd, name); } catch (e) { logger.error(`Failed to register command ${cmd.name}\n`, e); return false; } } + } + if (flux) { + for (const event in flux) { + FluxDispatcher.subscribe(event as FluxEvents, flux[event]); + } } return true; }, p => `startPlugin ${p.name}`); export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plugin) { + const { name, commands, flux } = p; if (p.stop) { - logger.info("Stopping plugin", p.name); + logger.info("Stopping plugin", name); if (!p.started) { - logger.warn(`${p.name} already stopped`); + logger.warn(`${name} already stopped`); return false; } try { p.stop(); p.started = false; } catch (e) { - logger.error(`Failed to stop ${p.name}\n`, e); + logger.error(`Failed to stop ${name}\n`, e); return false; } } - if (p.commands?.length) { - logger.info("Unregistering commands of plugin", p.name); - for (const cmd of p.commands) { + if (commands?.length) { + logger.info("Unregistering commands of plugin", name); + for (const cmd of commands) { try { unregisterCommand(cmd.name); } catch (e) { @@ -170,5 +180,11 @@ export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plu } } + if (flux) { + for (const event in flux) { + FluxDispatcher.unsubscribe(event as FluxEvents, flux[event]); + } + } + return true; }, p => `stopPlugin ${p.name}`); |