diff --git a/src/index.ts b/src/index.ts index 297c620..b2376ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,7 @@ import { patchAssets, all, find, getAssetByID, getAssetByName, getAssetIDByName import initSettings from "@ui/settings"; import { fixTheme } from "@ui/fixTheme"; import { connectToDebugger, patchLogHook, versionHash } from "@lib/debug"; -import { plugins, fetchPlugin, evalPlugin, stopPlugin, removePlugin, getSettings } from "@lib/plugins"; +import { plugins, fetchPlugin, evalPlugin, stopPlugin, removePlugin, getSettings, initializePlugins } from "@lib/plugins"; import settings from "@lib/settings"; import { registerCommand } from "@lib/commands"; @@ -66,6 +66,7 @@ async function init() { patchLogHook(); patchAssets(); fixTheme(); + initializePlugins(); initSettings(); } catch (e: Error | any) { erroredOnLoad = true; diff --git a/src/lib/plugins.ts b/src/lib/plugins.ts index 6eede12..17e351e 100644 --- a/src/lib/plugins.ts +++ b/src/lib/plugins.ts @@ -1,6 +1,6 @@ import { Indexable, PluginManifest, Plugin } from "@types"; import { navigation } from "@metro/common"; -import { createStorage, wrapSync } from "@lib/storage"; +import { awaitSyncWrapper, createStorage, wrapSync } from "@lib/storage"; import logger from "@lib/logger"; import Subpage from "@ui/settings/components/Subpage"; @@ -10,16 +10,7 @@ type EvaledPlugin = { settings: JSX.Element; }; -export const plugins = wrapSync(createStorage>("VENDETTA_PLUGINS").then(async function (store) { - for (let p of Object.keys(store)) { - const plugin: Plugin = store[p]; - - if (plugin.update) await fetchPlugin(plugin.id, false); - if (plugin.enabled && plugins[p]) await startPlugin(p); - } - - return store; -})); +export const plugins = wrapSync(createStorage>("VENDETTA_PLUGINS")); const loadedPlugins: Indexable = {}; export async function fetchPlugin(id: string, enabled = true) { @@ -120,6 +111,15 @@ export function removePlugin(id: string) { delete plugins[id]; } +export async function initializePlugins() { + await awaitSyncWrapper(plugins); + + const allIds = Object.keys(plugins); + await Promise.allSettled(allIds.map((pl) => fetchPlugin(pl, false))); + for (const pl of allIds.filter((pl) => plugins[pl].enabled)) + startPlugin(pl); +} + export const getSettings = (id: string) => loadedPlugins[id]?.settings; export function showSettings(plugin: Plugin) { @@ -130,4 +130,4 @@ export function showSettings(plugin: Plugin) { name: plugin.manifest.name, children: settings, }); -} \ No newline at end of file +}