Skip to content

Commit 834ac3e

Browse files
authored
[CodeHealth] Simplify code as follow-up to #4354 (#4400)
1 parent 22c70d5 commit 834ac3e

File tree

4 files changed

+52
-31
lines changed

4 files changed

+52
-31
lines changed

src/scripts/ui/settings.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
6565
/**
6666
* @deprecated Use `settingStore.getDefaultValue` instead.
6767
*/
68-
getSettingDefaultValue<K extends keyof Settings>(id: K): Settings[K] {
68+
getSettingDefaultValue<K extends keyof Settings>(
69+
id: K
70+
): Settings[K] | undefined {
6971
return useSettingStore().getDefaultValue(id)
7072
}
7173

src/stores/settingStore.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { api } from '@/scripts/api'
77
import { app } from '@/scripts/app'
88
import type { SettingParams } from '@/types/settingTypes'
99
import type { TreeNode } from '@/types/treeExplorerTypes'
10-
import { compareVersions } from '@/utils/formatUtil'
10+
import { compareVersions, isSemVer } from '@/utils/formatUtil'
1111

1212
export const getSettingInfo = (setting: SettingParams) => {
1313
const parts = setting.category || setting.id.split('.')
@@ -21,16 +21,24 @@ export interface SettingTreeNode extends TreeNode {
2121
data?: SettingParams
2222
}
2323

24-
function tryMigrateDeprecatedValue(setting: SettingParams, value: any) {
24+
function tryMigrateDeprecatedValue(
25+
setting: SettingParams | undefined,
26+
value: unknown
27+
) {
2528
return setting?.migrateDeprecatedValue?.(value) ?? value
2629
}
2730

28-
function onChange(setting: SettingParams, newValue: any, oldValue: any) {
31+
function onChange(
32+
setting: SettingParams | undefined,
33+
newValue: unknown,
34+
oldValue: unknown
35+
) {
2936
if (setting?.onChange) {
3037
setting.onChange(newValue, oldValue)
3138
}
3239
// Backward compatibility with old settings dialog.
3340
// Some extensions still listens event emitted by the old settings dialog.
41+
// @ts-expect-error 'setting' is possibly 'undefined'.ts(18048)
3442
app.ui.settings.dispatchChange(setting.id, newValue, oldValue)
3543
}
3644

@@ -77,46 +85,64 @@ export const useSettingStore = defineStore('setting', () => {
7785
return _.cloneDeep(settingValues.value[key] ?? getDefaultValue(key))
7886
}
7987

88+
/**
89+
* Gets the setting params, asserting the type that is intentionally left off
90+
* of {@link settingsById}.
91+
* @param key The key of the setting to get.
92+
* @returns The setting.
93+
*/
94+
function getSettingById<K extends keyof Settings>(
95+
key: K
96+
): SettingParams<Settings[K]> | undefined {
97+
return settingsById.value[key] as SettingParams<Settings[K]> | undefined
98+
}
99+
80100
/**
81101
* Get the default value of a setting.
82102
* @param key - The key of the setting to get.
83103
* @returns The default value of the setting.
84104
*/
85-
function getDefaultValue<K extends keyof Settings>(key: K): Settings[K] {
86-
const param = settingsById.value[key]
105+
function getDefaultValue<K extends keyof Settings>(
106+
key: K
107+
): Settings[K] | undefined {
108+
// Assertion: settingsById is not typed.
109+
const param = getSettingById(key)
110+
111+
if (param === undefined) return
87112

88113
const versionedDefault = getVersionedDefaultValue(key, param)
89114

90115
if (versionedDefault) {
91116
return versionedDefault
92117
}
93118

94-
return typeof param?.defaultValue === 'function'
119+
return typeof param.defaultValue === 'function'
95120
? param.defaultValue()
96-
: param?.defaultValue
121+
: param.defaultValue
97122
}
98123

99-
function getVersionedDefaultValue<K extends keyof Settings>(
100-
key: K,
101-
param: SettingParams
102-
): Settings[K] | null {
124+
function getVersionedDefaultValue<
125+
K extends keyof Settings,
126+
TValue = Settings[K]
127+
>(key: K, param: SettingParams<TValue> | undefined): TValue | null {
103128
// get default versioned value, skipping if the key is 'Comfy.InstalledVersion' to prevent infinite loop
104-
if (param?.defaultsByInstallVersion && key !== 'Comfy.InstalledVersion') {
129+
const defaultsByInstallVersion = param?.defaultsByInstallVersion
130+
if (defaultsByInstallVersion && key !== 'Comfy.InstalledVersion') {
105131
const installedVersion = get('Comfy.InstalledVersion')
106132

107133
if (installedVersion) {
108-
const sortedVersions = Object.keys(param.defaultsByInstallVersion).sort(
109-
(a, b) => compareVersions(a, b)
134+
const sortedVersions = Object.keys(defaultsByInstallVersion).sort(
135+
(a, b) => compareVersions(b, a)
110136
)
111137

112-
for (const version of sortedVersions.reverse()) {
138+
for (const version of sortedVersions) {
113139
// Ensure the version is in a valid format before comparing
114-
if (!isValidVersionFormat(version)) {
140+
if (!isSemVer(version)) {
115141
continue
116142
}
117143

118144
if (compareVersions(installedVersion, version) >= 0) {
119-
const versionedDefault = param.defaultsByInstallVersion[version]
145+
const versionedDefault = defaultsByInstallVersion[version]
120146
return typeof versionedDefault === 'function'
121147
? versionedDefault()
122148
: versionedDefault
@@ -128,12 +154,6 @@ export const useSettingStore = defineStore('setting', () => {
128154
return null
129155
}
130156

131-
function isValidVersionFormat(
132-
version: string
133-
): version is `${number}.${number}.${number}` {
134-
return /^\d+\.\d+\.\d+$/.test(version)
135-
}
136-
137157
/**
138158
* Register a setting.
139159
* @param setting - The setting to register.

src/types/settingTypes.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ export interface Setting {
3232
render: () => HTMLElement
3333
}
3434

35-
export interface SettingParams extends FormItem {
35+
export interface SettingParams<TValue = unknown> extends FormItem {
3636
id: keyof Settings
3737
defaultValue: any | (() => any)
38-
defaultsByInstallVersion?: Record<
39-
`${number}.${number}.${number}`,
40-
any | (() => any)
41-
>
38+
defaultsByInstallVersion?: Record<`${number}.${number}.${number}`, TValue>
4239
onChange?: (newValue: any, oldValue?: any) => void
4340
// By default category is id.split('.'). However, changing id to assign
4441
// new category has poor backward compatibility. Use this field to overwrite

src/utils/formatUtil.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,10 @@ export const downloadUrlToHfRepoUrl = (url: string): string => {
386386
}
387387
}
388388

389-
export const isSemVer = (version: string) => {
390-
const regex = /^(\d+)\.(\d+)\.(\d+)$/
389+
export const isSemVer = (
390+
version: string
391+
): version is `${number}.${number}.${number}` => {
392+
const regex = /^\d+\.\d+\.\d+$/
391393
return regex.test(version)
392394
}
393395

0 commit comments

Comments
 (0)