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
This commit is contained in:
PalmDevs 2025-07-13 15:52:14 +07:00
parent 709a944d49
commit 6a38136762
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
10 changed files with 59 additions and 37 deletions

View file

@ -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<T>(
const store = _stores[name]
if (store) {
callback(store as DiscordModules.Flux.Store<T>)
return noopFalse
return noop
}
return waitForModules(byStoreName<T>(name), callback)

View file

@ -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<string, SettingsItem>) {
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

View file

@ -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<T> = (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<T>(
const [exports, id] = lookupModuleByImportedPath(path)
if (id !== undefined) {
callback(exports, id)
return noopFalse
return noop
}
const unsub = waitForModuleByImportedPath(path, (exports, id) => {

View file

@ -25,7 +25,7 @@ export interface BaseWaitForModulesOptions<All extends boolean = boolean> {
all?: All
}
export type WaitForModulesUnsubscribeFunction = () => boolean
export type WaitForModulesUnsubscribeFunction = () => void
export type WaitForModulesCallback<T> = (exports: T, id: Metro.ModuleID) => any
@ -82,7 +82,7 @@ export function waitForModules(
filter: Filter,
callback: WaitForModulesCallback<any>,
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<T = any>(
path: string,
callback: WaitForModulesCallback<T>,
options?: BaseWaitForModulesOptions,
) {
): WaitForModulesUnsubscribeFunction {
const unsub = onModuleFinishedImporting(
options?.all
? (id, cmpPath) => {

View file

@ -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)
}
}

View file

@ -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

View file

@ -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<RunApplicationCallback>()
export const sAfterRunApplication = new Set<RunApplicationCallback>()

View file

@ -21,3 +21,5 @@ export namespace ReactNative {
getAssetByID(assetId: number): AssetsRegistry.PackagerAsset
}
}
export type RunApplicationCallback = () => any

View file

@ -12,5 +12,4 @@ export function debounce<F extends (...args: any[]) => any>(
})
}
export const noopFalse = () => false
export const noop = () => {}

View file

@ -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 }>