From 4c0d84f15d760904bab5946f6c750f2009d755ef Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Thu, 23 May 2024 20:27:16 +0800 Subject: [PATCH] refactor(polyfills): restruct --- .../{polyfills.ts => polyfills/allSettled.ts} | 2 +- src/core/{ => polyfills}/vendettaObject.ts | 15 +++++++++++++++ src/index.ts | 2 +- src/lib/managers/plugins.ts | 18 +++++------------- src/lib/managers/themes.ts | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) rename src/core/{polyfills.ts => polyfills/allSettled.ts} (77%) rename src/core/{ => polyfills}/vendettaObject.ts (95%) diff --git a/src/core/polyfills.ts b/src/core/polyfills/allSettled.ts similarity index 77% rename from src/core/polyfills.ts rename to src/core/polyfills/allSettled.ts index 5a6996d..82f1f7c 100644 --- a/src/core/polyfills.ts +++ b/src/core/polyfills/allSettled.ts @@ -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 = (value: T) => ({ status: "fulfilled", value }); const allSettledReject = (reason: T) => ({ status: "rejected", reason }); const mapAllSettled = (item: T) => Promise.resolve(item).then(allSettledFulfill, allSettledReject); diff --git a/src/core/vendettaObject.ts b/src/core/polyfills/vendettaObject.ts similarity index 95% rename from src/core/vendettaObject.ts rename to src/core/polyfills/vendettaObject.ts index 1049a66..6011b96 100644 --- a/src/core/vendettaObject.ts +++ b/src/core/polyfills/vendettaObject.ts @@ -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>(storage.createMMKVBackend(plugin.id)), + }, + logger: new logModule(`Bunny » ${plugin.manifest.name}`), + }; +} + export const initVendettaObject = (): any => { const api = window.vendetta = { patcher: { diff --git a/src/index.ts b/src/index.ts index 2ac9b94..a415f69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/lib/managers/plugins.ts b/src/lib/managers/plugins.ts index 9fb85d0..95aeaeb 100644 --- a/src/lib/managers/plugins.ts +++ b/src/lib/managers/plugins.ts @@ -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>(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 ?? {}; } diff --git a/src/lib/managers/themes.ts b/src/lib/managers/themes.ts index 78573ab..9e05c02 100644 --- a/src/lib/managers/themes.ts +++ b/src/lib/managers/themes.ts @@ -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";