[Plugins] Implement proper execution
This commit is contained in:
parent
7465e42354
commit
5c4026685e
3 changed files with 81 additions and 9 deletions
|
@ -1,11 +1,19 @@
|
|||
import { Indexable, PluginManifest, Plugin } from "@types";
|
||||
import logger from "./logger";
|
||||
|
||||
type EvaledPlugin = {
|
||||
onLoad?(): void;
|
||||
onUnload(): void;
|
||||
};
|
||||
|
||||
export const plugins: Indexable<Plugin> = {};
|
||||
const loadedPlugins: Indexable<EvaledPlugin> = {};
|
||||
|
||||
export async function fetchPlugin(url: string) {
|
||||
if (!url.endsWith("/")) url += "/";
|
||||
|
||||
if (plugins[url]) throw new Error(`That plugin is already installed!`);
|
||||
const id = url.split("://")[1];
|
||||
if (typeof url !== "string" || url in plugins) throw new Error("Plugin ID invalid or taken");
|
||||
|
||||
let pluginManifest: PluginManifest;
|
||||
|
||||
|
@ -15,10 +23,72 @@ export async function fetchPlugin(url: string) {
|
|||
throw new Error(`Failed to fetch manifest for ${url}`);
|
||||
}
|
||||
|
||||
plugins[url] = {
|
||||
id: url.split("://")[1],
|
||||
let pluginJs: string;
|
||||
|
||||
try {
|
||||
pluginJs = await (await fetch(new URL("plugin.js", url), { cache: "no-store" })).text()
|
||||
} catch {
|
||||
throw new Error(`Failed to fetch JS for ${url}`)
|
||||
}
|
||||
|
||||
plugins[id] = {
|
||||
id: id,
|
||||
manifest: pluginManifest,
|
||||
enabled: true,
|
||||
js: "",
|
||||
enabled: false,
|
||||
js: pluginJs,
|
||||
};
|
||||
}
|
||||
|
||||
// Bundlers don't like eval
|
||||
const theSystemHasBeenDestroyed = eval;
|
||||
|
||||
export function evalPlugin(plugin: Plugin) {
|
||||
// TODO: Refactor to not depend on own window object
|
||||
const vendettaForPlugins = Object.assign({}, window.vendetta);
|
||||
const pluginString = `(vendetta)=>{return ${plugin.js}}\n//# sourceURL=${plugin.id}`;
|
||||
|
||||
const ret = theSystemHasBeenDestroyed(pluginString)(vendettaForPlugins);
|
||||
return typeof ret == "function" ? ret() : ret;
|
||||
}
|
||||
|
||||
export function startPlugin(id: string) {
|
||||
const plugin = plugins[id];
|
||||
if (!plugin) throw new Error("Attempted to start non-existent plugin");
|
||||
|
||||
try {
|
||||
const pluginRet: EvaledPlugin = evalPlugin(plugin);
|
||||
loadedPlugins[id] = pluginRet;
|
||||
pluginRet.onLoad?.();
|
||||
plugin.enabled = true;
|
||||
} catch(e) {
|
||||
logger.error(`Plugin ${plugin.id} errored whilst loading, and will be unloaded`, e);
|
||||
|
||||
try {
|
||||
loadedPlugins[plugin.id]?.onUnload?.();
|
||||
} catch(e2) {
|
||||
logger.error(`Plugin ${plugin.id} errored whilst unloading`, e2);
|
||||
}
|
||||
|
||||
delete loadedPlugins[id];
|
||||
plugin.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
export function stopPlugin(id: string) {
|
||||
const plugin = plugins[id];
|
||||
const pluginRet = loadedPlugins[id];
|
||||
if (!plugin) throw new Error("Attempted to stop non-existent plugin");
|
||||
if (!pluginRet) throw new Error("Attempted to stop a non-started plugin");
|
||||
|
||||
try {
|
||||
loadedPlugins[plugin.id]?.onUnload?.();
|
||||
} catch(e) {
|
||||
logger.error(`Plugin ${plugin.id} errored whilst unloading`, e);
|
||||
}
|
||||
|
||||
delete loadedPlugins[id];
|
||||
plugin.enabled = false;
|
||||
}
|
||||
|
||||
// TODO: When startAllPlugins exists, return this so cleanup in index.ts is easier
|
||||
const stopAllPlugins = () => Object.keys(loadedPlugins).forEach(stopPlugin);
|
|
@ -2,8 +2,9 @@ import { ReactNative as RN, stylesheet } from "@metro/common";
|
|||
import { Forms } from "@ui/components";
|
||||
import { Plugin } from "@types";
|
||||
import { getAssetIDByName } from "@/ui/assets";
|
||||
import { startPlugin, stopPlugin } from "@/lib/plugins";
|
||||
|
||||
const { FormRow, FormText, FormSwitch } = Forms;
|
||||
const { FormRow, FormSwitch } = Forms;
|
||||
|
||||
const styles = stylesheet.createThemedStyleSheet({
|
||||
card: {
|
||||
|
@ -35,8 +36,9 @@ export default function PluginCard({ plugin }: PluginCardProps) {
|
|||
<FormSwitch
|
||||
value={plugin.enabled}
|
||||
onValueChange={(v: boolean) => {
|
||||
alert(v);
|
||||
if (v) startPlugin(plugin.id); else stopPlugin(plugin.id);
|
||||
setEnabled(v);
|
||||
plugin.enabled = enabled;
|
||||
}}
|
||||
/>
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ export default function Plugins() {
|
|||
keyExtractor={item => item.id}
|
||||
/>
|
||||
<RN.View style={styles.disclaimer}>
|
||||
<FormText style={styles.disclaimerText}>Plugins are currently non-functional, but most of the infrastructure and UI is in place.</FormText>
|
||||
<FormText style={styles.disclaimerText}>Plugins are currently non-permanent whilst I find a storage solution.</FormText>
|
||||
</RN.View>
|
||||
</>
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue