From b0085114d77f595685786b104117591f20cb855b Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Thu, 28 Nov 2024 10:35:08 -0800 Subject: [PATCH] Remove app.storageLocation handling (#1731) --- src/extensions/core/nodeTemplates.ts | 50 +++++++++++----------------- src/scripts/app.ts | 34 ++++++------------- src/scripts/ui/settings.ts | 26 +++------------ src/types/apiTypes.ts | 2 +- src/types/settingTypes.ts | 2 -- 5 files changed, 35 insertions(+), 79 deletions(-) diff --git a/src/extensions/core/nodeTemplates.ts b/src/extensions/core/nodeTemplates.ts index db42c190e..4ede61688 100644 --- a/src/extensions/core/nodeTemplates.ts +++ b/src/extensions/core/nodeTemplates.ts @@ -85,46 +85,34 @@ class ManageTemplates extends ComfyDialog { async load() { let templates = [] - if (app.storageLocation === 'server') { - if (app.isNewUserSession) { - // 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 { + if (app.isNewUserSession) { + // 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) + } } - return templates ?? [] } async store() { - if (app.storageLocation === 'server') { - const templates = JSON.stringify(this.templates, undefined, 4) - localStorage.setItem(id, templates) // Backwards compatibility - try { - await api.storeUserData(file, templates, { stringify: false }) - } catch (error) { - console.error(error) - useToastStore().addAlert(error.message) - } - } else { - localStorage.setItem(id, JSON.stringify(this.templates)) + const templates = JSON.stringify(this.templates, undefined, 4) + localStorage.setItem(id, templates) // Backwards compatibility + try { + await api.storeUserData(file, templates, { stringify: false }) + } catch (error) { + console.error(error) + useToastStore().addAlert(error.message) } } diff --git a/src/scripts/app.ts b/src/scripts/app.ts index d5f24f4bd..f2860e486 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -36,7 +36,6 @@ import { LGraphNode, LiteGraph } from '@comfyorg/litegraph' -import { StorageLocation } from '@/types/settingTypes' import { ExtensionManager } from '@/types/extensionTypes' import { ComfyNodeDefImpl, @@ -144,7 +143,6 @@ export class ComfyApp { progress: { value: number; max: number } | null configuringGraph: boolean isNewUserSession: boolean - storageLocation: StorageLocation ctx: CanvasRenderingContext2D bodyTop: HTMLElement bodyLeft: HTMLElement @@ -180,6 +178,14 @@ export class ComfyApp { return ComfyWidgets } + /** + * @deprecated storageLocation is always 'server' since + * https://github.com/comfyanonymous/ComfyUI/commit/53c8a99e6c00b5e20425100f6680cd9ea2652218 + */ + get storageLocation() { + return 'server' + } + constructor() { this.vueAppReady = false 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() { const userConfig = await api.getUserConfig() - this.storageLocation = userConfig.storage if (typeof userConfig.migrated == 'boolean') { // Single user mode migrated true/false for if the default user is created - if (!userConfig.migrated && this.storageLocation === 'server') { - // Default user not created yet - await this.#migrateSettings() + if (!userConfig.migrated) { + this.isNewUserSession = true } return } @@ -1747,7 +1735,7 @@ export class ComfyApp { if (created) { api.user = user - await this.#migrateSettings() + this.isNewUserSession = true } } diff --git a/src/scripts/ui/settings.ts b/src/scripts/ui/settings.ts index 3676e0762..2469f237c 100644 --- a/src/scripts/ui/settings.ts +++ b/src/scripts/ui/settings.ts @@ -55,15 +55,11 @@ export class ComfySettingsDialog extends ComfyDialog { } async load() { - if (this.app.storageLocation === 'browser') { - this.settingsValues = localStorage - } else { - this.settingsValues = await api.getSettings() - } + this.settingsValues = await api.getSettings() // Trigger onChange for any settings added before load for (const id in this.settingsLookup) { - const compatId = this.getId(id) + const compatId = id this.settingsValues[compatId] = this.tryMigrateDeprecatedValue( id, this.settingsValues[compatId] @@ -74,25 +70,11 @@ export class ComfySettingsDialog extends ComfyDialog { } } - getId(id: string) { - if (this.app.storageLocation === 'browser') { - id = 'Comfy.Settings.' + id - } - return id - } - getSettingValue( id: K, defaultValue?: Settings[K] ): Settings[K] { - let value = this.settingsValues[this.getId(id)] - if (value != null) { - if (this.app.storageLocation === 'browser') { - try { - value = JSON.parse(value) - } catch (error) {} - } - } + let value = this.settingsValues[id] return (value ?? defaultValue) as Settings[K] } @@ -111,7 +93,7 @@ export class ComfySettingsDialog extends ComfyDialog { localStorage['Comfy.Settings.' + id] = json // backwards compatibility for extensions keep setting in storage let oldValue = this.getSettingValue(id, undefined) - this.settingsValues[this.getId(id)] = value + this.settingsValues[id] = value if (id in this.settingsLookup) { this.settingsLookup[id].onChange?.(value, oldValue) diff --git a/src/types/apiTypes.ts b/src/types/apiTypes.ts index e6eb5fd65..5dbdb1c06 100644 --- a/src/types/apiTypes.ts +++ b/src/types/apiTypes.ts @@ -430,7 +430,7 @@ export const zSystemStats = z.object({ devices: z.array(zDeviceStats) }) const zUser = z.object({ - storage: z.enum(['server', 'browser']), + storage: z.enum(['server']), migrated: z.boolean(), users: z.record(z.string(), z.unknown()) }) diff --git a/src/types/settingTypes.ts b/src/types/settingTypes.ts index 1985295ce..52aefdad3 100644 --- a/src/types/settingTypes.ts +++ b/src/types/settingTypes.ts @@ -1,7 +1,5 @@ import { Settings } from './apiTypes' -export type StorageLocation = 'browser' | 'server' - export type SettingInputType = | 'boolean' | 'number'