From 6a38136762b26a97a32be6d512e815b4942d7931 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Sun, 13 Jul 2025 15:52:14 +0700 Subject: [PATCH] fix(lib)!: breaking changes - Remove `noopFalse` from `@revenge-mod/utils/callback` - Do not return result from unpatch/undo/unsub functions - `onSettingsModulesLoaded` from `@revenge-mod/discord/modules/settings` now calls the callback immediately if settings modules are already loaded instead of throwing --- lib/discord/src/common/flux.ts | 4 +-- lib/discord/src/modules/settings/index.ts | 29 ++++++++++++++------ lib/modules/src/finders/get.ts | 10 +++---- lib/modules/src/finders/wait.ts | 6 ++-- lib/modules/src/metro/subscriptions/index.ts | 20 ++++++++++---- lib/react/src/native/index.ts | 11 +++++--- lib/react/src/native/patches.ts | 2 +- lib/react/src/types.ts | 2 ++ lib/utils/src/callback.ts | 1 - src/plugins/init/developer-settings/index.ts | 11 +++----- 10 files changed, 59 insertions(+), 37 deletions(-) diff --git a/lib/discord/src/common/flux.ts b/lib/discord/src/common/flux.ts index 4f7e935..2827094 100644 --- a/lib/discord/src/common/flux.ts +++ b/lib/discord/src/common/flux.ts @@ -11,7 +11,7 @@ import { } from '@revenge-mod/modules/finders/lookup' import { waitForModules } from '@revenge-mod/modules/finders/wait' import { getModuleDependencies } from '@revenge-mod/modules/metro/utils' -import { noopFalse } from '@revenge-mod/utils/callback' +import { noop } from '@revenge-mod/utils/callback' import { cached, cacheFilterResultForId } from '../../../modules/src/caches' import { FilterResultFlags } from '../../../modules/src/finders/_internal' import type { @@ -73,7 +73,7 @@ export function getStore( const store = _stores[name] if (store) { callback(store as DiscordModules.Flux.Store) - return noopFalse + return noop } return waitForModules(byStoreName(name), callback) diff --git a/lib/discord/src/modules/settings/index.ts b/lib/discord/src/modules/settings/index.ts index 975b58c..1130962 100644 --- a/lib/discord/src/modules/settings/index.ts +++ b/lib/discord/src/modules/settings/index.ts @@ -1,4 +1,5 @@ import { ReactNavigationNative } from '@revenge-mod/externals/react-navigation' +import { noop } from '@revenge-mod/utils/callback' import { findInTree } from '@revenge-mod/utils/tree' import { Constants } from '../../common' import { RootNavigationRef } from '../main_tabs_v2' @@ -22,17 +23,23 @@ export function isSettingsModulesLoaded() { * Subscribes to when settings modules are loaded. * Plugins should ideally register their settings in the given callback to ensure fast startup time. * + * If settings modules are already loaded, the callback will be called immediately. + * * @param subcription The subscription function to call when the settings modules are loaded. * @returns A function to unsubscribe from the event. - * @throws Throws an error if the settings modules are already loaded. Check with {@link isSettingsModulesLoaded} first. */ export function onSettingsModulesLoaded( subcription: SettingsModulesLoadedSubscription, ) { - if (sData[2]) throw new Error('Settings modules already loaded') + if (isSettingsModulesLoaded()) { + subcription() + return noop + } sSubscriptions.add(subcription) - return () => sSubscriptions.delete(subcription) + return () => { + sSubscriptions.delete(subcription) + } } /** @@ -44,7 +51,9 @@ export function onSettingsModulesLoaded( */ export function registerSettingsSection(key: string, section: SettingsSection) { sData[0][key] = section - return () => delete sData[0][key] + return () => { + delete sData[0][key] + } } /** @@ -56,7 +65,9 @@ export function registerSettingsSection(key: string, section: SettingsSection) { */ export function registerSettingsItem(key: string, item: SettingsItem) { sData[1][key] = item - return () => delete sData[1][key] + return () => { + delete sData[1][key] + } } /** @@ -68,9 +79,7 @@ export function registerSettingsItem(key: string, item: SettingsItem) { export function registerSettingsItems(record: Record) { Object.assign(sData[1], record) return () => { - let ret = true - for (const key in record) ret &&= delete sData[1][key] - return ret + for (const key in record) delete sData[1][key] } } @@ -86,7 +95,9 @@ export function addSettingsItemToSection(key: string, item: string) { if (!section) throw new Error(`Section "${key}" does not exist`) const newLength = section.settings.push(item) - return () => delete section.settings[newLength - 1] + return () => { + delete section.settings[newLength - 1] + } } const { CommonActions, StackActions } = ReactNavigationNative diff --git a/lib/modules/src/finders/get.ts b/lib/modules/src/finders/get.ts index 9158fac..bb04cb0 100644 --- a/lib/modules/src/finders/get.ts +++ b/lib/modules/src/finders/get.ts @@ -1,4 +1,4 @@ -import { noopFalse } from '@revenge-mod/utils/callback' +import { noop } from '@revenge-mod/utils/callback' import { lookupModule, lookupModuleByImportedPath, @@ -35,7 +35,7 @@ export type GetModuleResult< export type GetModuleCallback = (exports: T, id: Metro.ModuleID) => any -export type GetModuleUnsubscribeFunction = () => boolean +export type GetModuleUnsubscribeFunction = () => void /** * Get modules matching the filter. @@ -89,13 +89,13 @@ export function getModule( const [exports, id] = lookupModule(filter, options!) if (id !== undefined) { callback(exports, id) - return noopFalse + return noop } } else for (const [exports, id] of lookupModules(filter, options!)) { callback(exports, id) max-- - if (!max) return noopFalse + if (!max) return noop } const unsub = waitForModules( @@ -133,7 +133,7 @@ export function getModuleByImportedPath( const [exports, id] = lookupModuleByImportedPath(path) if (id !== undefined) { callback(exports, id) - return noopFalse + return noop } const unsub = waitForModuleByImportedPath(path, (exports, id) => { diff --git a/lib/modules/src/finders/wait.ts b/lib/modules/src/finders/wait.ts index 3560160..d4c03af 100644 --- a/lib/modules/src/finders/wait.ts +++ b/lib/modules/src/finders/wait.ts @@ -25,7 +25,7 @@ export interface BaseWaitForModulesOptions { all?: All } -export type WaitForModulesUnsubscribeFunction = () => boolean +export type WaitForModulesUnsubscribeFunction = () => void export type WaitForModulesCallback = (exports: T, id: Metro.ModuleID) => any @@ -82,7 +82,7 @@ export function waitForModules( filter: Filter, callback: WaitForModulesCallback, options?: WaitForModulesOptions, -) { +): WaitForModulesUnsubscribeFunction { if (__BUILD_FLAG_DEBUG_MODULE_WAITS__) nativeLoggingHook( `\u001b[94mWaiting for module matching: \u001b[93m${filter.key}\u001b[0m`, @@ -150,7 +150,7 @@ export function waitForModuleByImportedPath( path: string, callback: WaitForModulesCallback, options?: BaseWaitForModulesOptions, -) { +): WaitForModulesUnsubscribeFunction { const unsub = onModuleFinishedImporting( options?.all ? (id, cmpPath) => { diff --git a/lib/modules/src/metro/subscriptions/index.ts b/lib/modules/src/metro/subscriptions/index.ts index 6bafc8e..fdad8ca 100644 --- a/lib/modules/src/metro/subscriptions/index.ts +++ b/lib/modules/src/metro/subscriptions/index.ts @@ -30,7 +30,9 @@ export type ModuleFinishedImportingCallback = ( */ export function onAnyModuleInitialized(callback: ModuleInitializedCallback) { sInitializeAny.add(callback) - return () => sInitializeAny.delete(callback) + return () => { + sInitializeAny.delete(callback) + } } /** @@ -56,7 +58,9 @@ export function onModuleInitialized( } set.add(callback) - return () => set.delete(callback) + return () => { + set.delete(callback) + } } /** @@ -71,7 +75,9 @@ export function onModuleFinishedImporting( callback: ModuleFinishedImportingCallback, ) { sImportedPath.add(callback) - return () => sImportedPath.delete(callback) + return () => { + sImportedPath.delete(callback) + } } /** @@ -88,7 +94,9 @@ export function onAnyModuleFirstRequired( callback: ModuleFirstRequiredCallback, ) { sRequireAny.add(callback) - return () => sRequireAny.delete(callback) + return () => { + sRequireAny.delete(callback) + } } /** @@ -113,5 +121,7 @@ export function onModuleFirstRequired( } set.add(callback) - return () => set.delete(callback) + return () => { + set.delete(callback) + } } diff --git a/lib/react/src/native/index.ts b/lib/react/src/native/index.ts index af2ea6c..cec7ced 100644 --- a/lib/react/src/native/index.ts +++ b/lib/react/src/native/index.ts @@ -1,4 +1,5 @@ import { sAfterRunApplication, sBeforeRunApplication } from './patches' +import type { RunApplicationCallback } from '@revenge-mod/react/types' /** * Registers a callback to be run when a call to {@link AppRegistry.runApplication} is made. @@ -8,7 +9,9 @@ import { sAfterRunApplication, sBeforeRunApplication } from './patches' */ export function onRunApplication(callback: RunApplicationCallback) { sBeforeRunApplication.add(callback) - return () => sBeforeRunApplication.delete(callback) + return () => { + sBeforeRunApplication.delete(callback) + } } /** @@ -19,7 +22,7 @@ export function onRunApplication(callback: RunApplicationCallback) { */ export function onRunApplicationFinished(callback: RunApplicationCallback) { sAfterRunApplication.add(callback) - return () => sAfterRunApplication.delete(callback) + return () => { + sAfterRunApplication.delete(callback) + } } - -export type RunApplicationCallback = () => any diff --git a/lib/react/src/native/patches.ts b/lib/react/src/native/patches.ts index 95049bd..a3a1926 100644 --- a/lib/react/src/native/patches.ts +++ b/lib/react/src/native/patches.ts @@ -2,7 +2,7 @@ import { byProps } from '@revenge-mod/modules/finders/filters' import { waitForModules } from '@revenge-mod/modules/finders/wait' import { after, before } from '@revenge-mod/patcher' import { ReactNative } from '..' -import type { RunApplicationCallback } from '.' +import type { RunApplicationCallback } from '../types' export const sBeforeRunApplication = new Set() export const sAfterRunApplication = new Set() diff --git a/lib/react/src/types.ts b/lib/react/src/types.ts index c6286ce..f0425d5 100644 --- a/lib/react/src/types.ts +++ b/lib/react/src/types.ts @@ -21,3 +21,5 @@ export namespace ReactNative { getAssetByID(assetId: number): AssetsRegistry.PackagerAsset } } + +export type RunApplicationCallback = () => any diff --git a/lib/utils/src/callback.ts b/lib/utils/src/callback.ts index 179f3ee..2c5e60b 100644 --- a/lib/utils/src/callback.ts +++ b/lib/utils/src/callback.ts @@ -12,5 +12,4 @@ export function debounce any>( }) } -export const noopFalse = () => false export const noop = () => {} diff --git a/src/plugins/init/developer-settings/index.ts b/src/plugins/init/developer-settings/index.ts index c1abf8b..087080b 100644 --- a/src/plugins/init/developer-settings/index.ts +++ b/src/plugins/init/developer-settings/index.ts @@ -1,5 +1,4 @@ import { - isSettingsModulesLoaded, onSettingsModulesLoaded, refreshSettingsOverviewScreen, } from '@revenge-mod/discord/modules/settings' @@ -37,11 +36,9 @@ registerPlugin<{ storage: Storage }>( start(api_) { api = api_ - if (isSettingsModulesLoaded()) require('./utils').register() - else - onSettingsModulesLoaded(() => { - require('./utils').register() - }) + onSettingsModulesLoaded(() => { + require('./utils').register() + }) if (api_.plugin.flags & PluginFlags.EnabledLate) refreshSettingsOverviewScreen() @@ -59,5 +56,5 @@ registerPlugin<{ storage: Storage }>( InternalPluginFlags.Internal, ) -// Expose to EvaluateJavaScriptSetting +// Expose to EvalJSSetting export let api: PluginApi<{ storage: Storage }>