feat(lib/discord/modules/settings): switch to objects for storage

This commit is contained in:
PalmDevs 2025-07-18 23:41:18 +07:00
parent feaf1cd194
commit 86e02202b6
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
3 changed files with 28 additions and 24 deletions

View file

@ -4,16 +4,15 @@ import type {
SettingsSection,
} from '.'
export const sData = [
// Sections to splice in the SettingsOverviewScreen
{},
// SETTING_RENDERER_CONFIG settings
{},
false,
] as [
sections: Record<string, SettingsSection>,
config: Record<string, SettingsItem>,
loaded: boolean,
]
// Sections to splice in the SettingsOverviewScreen
export const sSections: Record<string, SettingsSection> = {}
// SETTING_RENDERER_CONFIG settings
export const sConfig: Record<string, SettingsItem> = {}
export const sData = {
loaded: false,
} as {
loaded: boolean
}
export const sSubscriptions = new Set<SettingsModulesLoadedSubscription>()

View file

@ -3,7 +3,7 @@ import { noop } from '@revenge-mod/utils/callback'
import { findInTree } from '@revenge-mod/utils/tree'
import { Constants } from '../../common'
import { RootNavigationRef } from '../main_tabs_v2'
import { sData, sSubscriptions } from './_internal'
import { sConfig, sData, sSections, sSubscriptions } from './_internal'
import type { NavigationState, PartialState } from '@react-navigation/core'
import type { DiscordModules } from '../../types'
@ -16,7 +16,7 @@ export type SettingsModulesLoadedSubscription = () => void
* Checks if the settings modules are loaded.
*/
export function isSettingsModulesLoaded() {
return sData[2]
return sData.loaded
}
/**
@ -50,9 +50,9 @@ export function onSettingsModulesLoaded(
* @returns A function to unregister the settings section.
*/
export function registerSettingsSection(key: string, section: SettingsSection) {
sData[0][key] = section
sSections[key] = section
return () => {
delete sData[0][key]
delete sSections[key]
}
}
@ -64,9 +64,9 @@ export function registerSettingsSection(key: string, section: SettingsSection) {
* @returns A function to unregister the settings item.
*/
export function registerSettingsItem(key: string, item: SettingsItem) {
sData[1][key] = item
sConfig[key] = item
return () => {
delete sData[1][key]
delete sConfig[key]
}
}
@ -77,9 +77,9 @@ export function registerSettingsItem(key: string, item: SettingsItem) {
* @returns A function to unregister the settings items.
*/
export function registerSettingsItems(record: Record<string, SettingsItem>) {
Object.assign(sData[1], record)
Object.assign(sConfig, record)
return () => {
for (const key in record) delete sData[1][key]
for (const key in record) delete sConfig[key]
}
}
@ -91,7 +91,7 @@ export function registerSettingsItems(record: Record<string, SettingsItem>) {
* @returns A function to remove the settings item from the section.
*/
export function addSettingsItemToSection(key: string, item: string) {
const section = sData[0][key]
const section = sSections[key]
if (!section) throw new Error(`Section "${key}" does not exist`)
const newLength = section.settings.push(item)

View file

@ -1,4 +1,9 @@
import { sData, sSubscriptions } from '@revenge-mod/discord/_/modules/settings'
import {
sConfig,
sData,
sSections,
sSubscriptions,
} from '@revenge-mod/discord/_/modules/settings'
import { onSettingsModulesLoaded } from '@revenge-mod/discord/modules/settings'
import { byName, byProps } from '@revenge-mod/modules/finders/filters'
import { waitForModules } from '@revenge-mod/modules/finders/wait'
@ -45,7 +50,7 @@ registerPlugin(
// We don't ever need to call this again
sSubscriptions.clear()
sData[2] = true
sData.loaded = true
let ORIGINAL_RENDERER_CONFIG =
SettingRendererConfig.SETTING_RENDERER_CONFIG
@ -57,7 +62,7 @@ registerPlugin(
get: () =>
({
...ORIGINAL_RENDERER_CONFIG,
...sData[1],
...sConfig,
}) as Record<string, SettingsItem>,
set: v => (ORIGINAL_RENDERER_CONFIG = v),
},
@ -70,7 +75,7 @@ registerPlugin(
exports => {
unsubSOS()
const customSections = sData[0]
const customSections = sSections
after(exports as { default: FC }, 'default', tree => {
const {