refactor(polyfills): restruct

This commit is contained in:
amsyarasyiq 2024-05-23 20:27:16 +08:00
parent 4181e15e89
commit 4c0d84f15d
5 changed files with 23 additions and 16 deletions

View file

@ -1,4 +1,4 @@
//! Starting from 202.4, Promise.allSettled may be undefined due to conflicting then/promise versions, so we use our own.
//! Around 202.4, Promise.allSettled was undefined due to conflicting then/promise versions(?), so we use our own.
const allSettledFulfill = <T>(value: T) => ({ status: "fulfilled", value });
const allSettledReject = <T>(reason: T) => ({ status: "rejected", reason });
const mapAllSettled = <T>(item: T) => Promise.resolve(item).then(allSettledFulfill, allSettledReject);

View file

@ -3,11 +3,13 @@ import * as commands from "@lib/api/commands";
import { getVendettaLoaderIdentity, isPyonLoader } from "@lib/api/native/loader";
import patcher from "@lib/api/patcher";
import * as storage from "@lib/api/storage";
import { createStorage } from "@lib/api/storage";
import * as debug from "@lib/debug";
import * as plugins from "@lib/managers/plugins";
import * as themes from "@lib/managers/themes";
import { loaderConfig, settings } from "@lib/settings";
import * as utils from "@lib/utils";
import { logModule } from "@lib/utils/logger";
import * as metro from "@metro";
import * as common from "@metro/common";
import { Forms } from "@metro/common/components";
@ -21,6 +23,19 @@ import { createElement, useEffect } from "react";
import { View } from "react-native";
import SparkMD5 from "spark-md5";
export async function createVdPluginObject(plugin: plugins.BunnyPlugin) {
return {
...window.vendetta,
plugin: {
id: plugin.id,
manifest: plugin.manifest,
// Wrapping this with wrapSync is NOT an option.
storage: await createStorage<Record<string, any>>(storage.createMMKVBackend(plugin.id)),
},
logger: new logModule(`Bunny » ${plugin.manifest.name}`),
};
}
export const initVendettaObject = (): any => {
const api = window.vendetta = {
patcher: {

View file

@ -1,8 +1,8 @@
import initFixes from "@core/fixes";
import { initFetchI18nStrings } from "@core/i18n";
import { initCorePlugins } from "@core/plugins";
import { initVendettaObject } from "@core/polyfills/vendettaObject";
import initSettings from "@core/ui/settings";
import { initVendettaObject } from "@core/vendettaObject";
import { patchCommands } from "@lib/api/commands";
import { injectFluxInterceptor } from "@lib/api/flux";
import { removeFile, writeFile } from "@lib/api/native/fs";

View file

@ -1,9 +1,10 @@
import { allSettled } from "@core/polyfills";
import { allSettled } from "@core/polyfills/allSettled";
import { createVdPluginObject } from "@core/polyfills/vendettaObject";
import { awaitSyncWrapper, createMMKVBackend, createStorage, purgeStorage, wrapSync } from "@lib/api/storage";
import { settings } from "@lib/settings";
import { safeFetch } from "@lib/utils";
import { BUNNY_PROXY_PREFIX, PROXY_PREFIX } from "@lib/utils/constants";
import { logger, logModule } from "@lib/utils/logger";
import { logger } from "@lib/utils/logger";
import { Author } from "@lib/utils/types";
type EvaledPlugin = {
@ -87,19 +88,10 @@ export async function installPlugin(id: string, enabled = true) {
* @internal
*/
export async function evalPlugin(plugin: BunnyPlugin) {
const vendettaForPlugins = {
...window.vendetta,
plugin: {
id: plugin.id,
manifest: plugin.manifest,
// Wrapping this with wrapSync is NOT an option.
storage: await createStorage<Record<string, any>>(createMMKVBackend(plugin.id)),
},
logger: new logModule(`Bunny » ${plugin.manifest.name}`),
};
const vdObject = await createVdPluginObject(plugin);
const pluginString = `vendetta=>{return ${plugin.js}}\n//# sourceURL=${plugin.id}?hash=${plugin.manifest.hash}`;
const raw = (0, eval)(pluginString)(vendettaForPlugins);
const raw = (0, eval)(pluginString)(vdObject);
const ret = typeof raw === "function" ? raw() : raw;
return ret?.default ?? ret ?? {};
}

View file

@ -2,7 +2,7 @@
* Theming system in Bunny is currently a prototype, expect an unreadable theme implementation below
*/
import { allSettled } from "@core/polyfills";
import { allSettled } from "@core/polyfills/allSettled";
import { getStoredTheme, getThemeFilePath } from "@lib/api/native/loader";
import { ThemeManager } from "@lib/api/native/modules";
import { after, before, instead } from "@lib/api/patcher";