From 885718b05720d84f59e689d41119efc7ca635f2f Mon Sep 17 00:00:00 2001 From: Rosie Val Date: Fri, 7 Aug 2026 00:58:10 -0400 Subject: [PATCH] ServerDrawer: patch jsx runtimes synchronously on load --- .../src/lib/createElementIntercept.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/plugins/server-drawer/src/lib/createElementIntercept.ts b/plugins/server-drawer/src/lib/createElementIntercept.ts index 9a6ee8b..160bcbf 100644 --- a/plugins/server-drawer/src/lib/createElementIntercept.ts +++ b/plugins/server-drawer/src/lib/createElementIntercept.ts @@ -1,5 +1,5 @@ import { React } from "@vendetta/metro/common"; -import { rawFindAllAsync } from "./rawFind"; +import { rawFindAll, rawFindAllAsync } from "./rawFind"; interface Intercept { replacement: React.ComponentType; @@ -315,6 +315,29 @@ export function patchCreateElement(cleanups: (() => void)[]) { const patchedRuntimes = new WeakSet(); let scanning = false; + function patchRuntime(runtime: any) { + if (patchedRuntimes.has(runtime)) return; + patchedRuntimes.add(runtime); + + for (const key of ["jsx", "jsxs", "jsxDEV"] as const) { + const orig = runtime[key]; + if (typeof orig !== "function") continue; + + runtime[key] = makeWrapper(orig, undefined); + restoreJsx.push(() => { runtime[key] = orig; }); + } + } + + // Patch any jsx runtimes already loaded before Discord's first render pass starts. + // This is synchronous because the race we're fixing is specifically the first QuestDock render: + // an async scan completes too late and the element is created through an unpatched runtime. + const initialRuntimes = rawFindAll( + (m: any) => typeof m?.jsx === "function" && typeof m?.jsxs === "function" && "Fragment" in m, + ); + for (const runtime of initialRuntimes) { + patchRuntime(runtime); + } + const scan = () => { if (scanning) return; scanning = true; @@ -323,16 +346,7 @@ export function patchCreateElement(cleanups: (() => void)[]) { (m: any) => typeof m?.jsx === "function" && typeof m?.jsxs === "function" && "Fragment" in m, ).then((runtimes) => { for (const runtime of runtimes) { - if (patchedRuntimes.has(runtime)) continue; - patchedRuntimes.add(runtime); - - for (const key of ["jsx", "jsxs", "jsxDEV"] as const) { - const orig = runtime[key]; - if (typeof orig !== "function") continue; - - runtime[key] = makeWrapper(orig, undefined); - restoreJsx.push(() => { runtime[key] = orig; }); - } + patchRuntime(runtime); } }).catch((e: any) => { console.error("[ServerDrawer] rawFindAllAsync error", e);