Remove app.storageLocation handling (#1731)

This commit is contained in:
Chenlei Hu
2024-11-28 10:35:08 -08:00
committed by GitHub
parent 7f9c70386f
commit b0085114d7
5 changed files with 35 additions and 79 deletions

View File

@@ -85,46 +85,34 @@ class ManageTemplates extends ComfyDialog {
async load() { async load() {
let templates = [] let templates = []
if (app.storageLocation === 'server') { if (app.isNewUserSession) {
if (app.isNewUserSession) { // New user so migrate existing templates
// New user so migrate existing templates
const json = localStorage.getItem(id)
if (json) {
templates = JSON.parse(json)
}
await api.storeUserData(file, json, { stringify: false })
} else {
const res = await api.getUserData(file)
if (res.status === 200) {
try {
templates = await res.json()
} catch (error) {}
} else if (res.status !== 404) {
console.error(res.status + ' ' + res.statusText)
}
}
} else {
const json = localStorage.getItem(id) const json = localStorage.getItem(id)
if (json) { if (json) {
templates = JSON.parse(json) templates = JSON.parse(json)
} }
await api.storeUserData(file, json, { stringify: false })
} else {
const res = await api.getUserData(file)
if (res.status === 200) {
try {
templates = await res.json()
} catch (error) {}
} else if (res.status !== 404) {
console.error(res.status + ' ' + res.statusText)
}
} }
return templates ?? [] return templates ?? []
} }
async store() { async store() {
if (app.storageLocation === 'server') { const templates = JSON.stringify(this.templates, undefined, 4)
const templates = JSON.stringify(this.templates, undefined, 4) localStorage.setItem(id, templates) // Backwards compatibility
localStorage.setItem(id, templates) // Backwards compatibility try {
try { await api.storeUserData(file, templates, { stringify: false })
await api.storeUserData(file, templates, { stringify: false }) } catch (error) {
} catch (error) { console.error(error)
console.error(error) useToastStore().addAlert(error.message)
useToastStore().addAlert(error.message)
}
} else {
localStorage.setItem(id, JSON.stringify(this.templates))
} }
} }

View File

@@ -36,7 +36,6 @@ import {
LGraphNode, LGraphNode,
LiteGraph LiteGraph
} from '@comfyorg/litegraph' } from '@comfyorg/litegraph'
import { StorageLocation } from '@/types/settingTypes'
import { ExtensionManager } from '@/types/extensionTypes' import { ExtensionManager } from '@/types/extensionTypes'
import { import {
ComfyNodeDefImpl, ComfyNodeDefImpl,
@@ -144,7 +143,6 @@ export class ComfyApp {
progress: { value: number; max: number } | null progress: { value: number; max: number } | null
configuringGraph: boolean configuringGraph: boolean
isNewUserSession: boolean isNewUserSession: boolean
storageLocation: StorageLocation
ctx: CanvasRenderingContext2D ctx: CanvasRenderingContext2D
bodyTop: HTMLElement bodyTop: HTMLElement
bodyLeft: HTMLElement bodyLeft: HTMLElement
@@ -180,6 +178,14 @@ export class ComfyApp {
return ComfyWidgets return ComfyWidgets
} }
/**
* @deprecated storageLocation is always 'server' since
* https://github.com/comfyanonymous/ComfyUI/commit/53c8a99e6c00b5e20425100f6680cd9ea2652218
*/
get storageLocation() {
return 'server'
}
constructor() { constructor() {
this.vueAppReady = false this.vueAppReady = false
this.ui = new ComfyUI(this) this.ui = new ComfyUI(this)
@@ -1699,30 +1705,12 @@ export class ComfyApp {
) )
} }
async #migrateSettings() {
this.isNewUserSession = true
// Store all current settings
const settings = Object.keys(this.ui.settings).reduce((p, n) => {
const v = localStorage[`Comfy.Settings.${n}`]
if (v) {
try {
p[n] = JSON.parse(v)
} catch (error) {}
}
return p
}, {})
await api.storeSettings(settings)
}
async #setUser() { async #setUser() {
const userConfig = await api.getUserConfig() const userConfig = await api.getUserConfig()
this.storageLocation = userConfig.storage
if (typeof userConfig.migrated == 'boolean') { if (typeof userConfig.migrated == 'boolean') {
// Single user mode migrated true/false for if the default user is created // Single user mode migrated true/false for if the default user is created
if (!userConfig.migrated && this.storageLocation === 'server') { if (!userConfig.migrated) {
// Default user not created yet this.isNewUserSession = true
await this.#migrateSettings()
} }
return return
} }
@@ -1747,7 +1735,7 @@ export class ComfyApp {
if (created) { if (created) {
api.user = user api.user = user
await this.#migrateSettings() this.isNewUserSession = true
} }
} }

View File

@@ -55,15 +55,11 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
} }
async load() { async load() {
if (this.app.storageLocation === 'browser') { this.settingsValues = await api.getSettings()
this.settingsValues = localStorage
} else {
this.settingsValues = await api.getSettings()
}
// Trigger onChange for any settings added before load // Trigger onChange for any settings added before load
for (const id in this.settingsLookup) { for (const id in this.settingsLookup) {
const compatId = this.getId(id) const compatId = id
this.settingsValues[compatId] = this.tryMigrateDeprecatedValue( this.settingsValues[compatId] = this.tryMigrateDeprecatedValue(
id, id,
this.settingsValues[compatId] this.settingsValues[compatId]
@@ -74,25 +70,11 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
} }
} }
getId(id: string) {
if (this.app.storageLocation === 'browser') {
id = 'Comfy.Settings.' + id
}
return id
}
getSettingValue<K extends keyof Settings>( getSettingValue<K extends keyof Settings>(
id: K, id: K,
defaultValue?: Settings[K] defaultValue?: Settings[K]
): Settings[K] { ): Settings[K] {
let value = this.settingsValues[this.getId(id)] let value = this.settingsValues[id]
if (value != null) {
if (this.app.storageLocation === 'browser') {
try {
value = JSON.parse(value)
} catch (error) {}
}
}
return (value ?? defaultValue) as Settings[K] return (value ?? defaultValue) as Settings[K]
} }
@@ -111,7 +93,7 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
localStorage['Comfy.Settings.' + id] = json // backwards compatibility for extensions keep setting in storage localStorage['Comfy.Settings.' + id] = json // backwards compatibility for extensions keep setting in storage
let oldValue = this.getSettingValue(id, undefined) let oldValue = this.getSettingValue(id, undefined)
this.settingsValues[this.getId(id)] = value this.settingsValues[id] = value
if (id in this.settingsLookup) { if (id in this.settingsLookup) {
this.settingsLookup[id].onChange?.(value, oldValue) this.settingsLookup[id].onChange?.(value, oldValue)

View File

@@ -430,7 +430,7 @@ export const zSystemStats = z.object({
devices: z.array(zDeviceStats) devices: z.array(zDeviceStats)
}) })
const zUser = z.object({ const zUser = z.object({
storage: z.enum(['server', 'browser']), storage: z.enum(['server']),
migrated: z.boolean(), migrated: z.boolean(),
users: z.record(z.string(), z.unknown()) users: z.record(z.string(), z.unknown())
}) })

View File

@@ -1,7 +1,5 @@
import { Settings } from './apiTypes' import { Settings } from './apiTypes'
export type StorageLocation = 'browser' | 'server'
export type SettingInputType = export type SettingInputType =
| 'boolean' | 'boolean'
| 'number' | 'number'