From f1e94fe6347d744c0feed20af3ab0e7c82df5eda Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Sun, 3 Aug 2025 14:17:47 +0700 Subject: [PATCH] fix(lib/modules/finders/get): undefined unsub function on immediately found lookup --- lib/modules/src/finders/get.ts | 8 ++++++-- lib/utils/src/callback.ts | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/modules/src/finders/get.ts b/lib/modules/src/finders/get.ts index bb04cb0..74960a7 100644 --- a/lib/modules/src/finders/get.ts +++ b/lib/modules/src/finders/get.ts @@ -1,4 +1,4 @@ -import { noop } from '@revenge-mod/utils/callback' +import { asap, noop } from '@revenge-mod/utils/callback' import { lookupModule, lookupModuleByImportedPath, @@ -88,7 +88,11 @@ export function getModule( if (max === 1) { const [exports, id] = lookupModule(filter, options!) if (id !== undefined) { - callback(exports, id) + // Run callback at the end of the event loop + // This ensures that the noop is returned first + asap(() => { + callback(exports, id) + }) return noop } } else diff --git a/lib/utils/src/callback.ts b/lib/utils/src/callback.ts index 2c5e60b..7975c8d 100644 --- a/lib/utils/src/callback.ts +++ b/lib/utils/src/callback.ts @@ -12,4 +12,17 @@ export function debounce any>( }) } +const now = (cb: () => any) => { + cb() +} + +/** + * A function that runs the callback as soon as possible. + * @param cb The callback to run. + */ +export const asap = (cb: (...args: any[]) => any) => { + // @ts-expect-error + ;(HermesInternal.enqueueJob ?? setImmediate ?? now)(cb) +} + export const noop = () => {}