From 8fc4b74aed29bf65dec693826c5149a25204b417 Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Sun, 28 May 2023 15:08:33 -0500 Subject: [PATCH] Config state temp --- src/lib/stores/configState.ts | 117 ++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 19 deletions(-) diff --git a/src/lib/stores/configState.ts b/src/lib/stores/configState.ts index 347bd78..5da2dab 100644 --- a/src/lib/stores/configState.ts +++ b/src/lib/stores/configState.ts @@ -2,31 +2,100 @@ import { debounce } from '$lib/utils'; import { get, writable } from 'svelte/store'; import type { Writable } from 'svelte/store'; -export type ConfigState = { - /** Backend domain for ComfyUI */ - comfyUIHostname: string, +type ConfigDefType = "boolean" | "number" | "string" | "string[]"; - /** Backend port for ComfyUI */ - comfyUIPort: number, +// A simple parameter description interface +interface ConfigDef { + // The `IdType` is necessary to get a stricter type + // parameter instead of a generic `id: string;`. This will be needed + // later when we infer the type of the `config` object. + name: IdType; - /** Strip user state even if saving to local storage */ - alwaysStripUserState: boolean, + type: TypeType, - /** When saving, always prompt for a name to save the workflow as */ - promptForWorkflowName: boolean, + description?: string, - /** When closing the tab, open the confirmation window if there's unsaved changes */ - confirmWhenUnloadingUnsavedChanges: boolean, - - /** Basenames of templates that can be loaded from public/templates. Saves LocalStorage space. */ - builtInTemplates: string[], - - /** Cache loading of built-in resources to save network use */ - cacheBuiltInResources: boolean + defaultValue: ValueType, } +type ConfigDefBoolean = ConfigDef; +type ConfigDefNumber = ConfigDef; +type ConfigDefString = ConfigDef; +type ConfigDefStringArray = ConfigDef; + +// Configuration parameters ------------------------------------ + +const defComfyUIHostname: ConfigDefString<"comfyUIHostname"> = { + name: "comfyUIHostname", + type: "string", + defaultValue: "localhost", + description: "Backend domain for ComfyUI", +}; + +const defComfyUIPort: ConfigDefNumber<"comfyUIPort"> = { + name: "comfyUIPort", + type: "number", + defaultValue: 8188, + description: "Backend port for ComfyUI", +}; + +const defAlwaysStripUserState: ConfigDefBoolean<"alwaysStripUserState"> = { + name: "alwaysStripUserState", + type: "boolean", + defaultValue: false, + description: "Strip user state even if saving to local storage" +}; + +const defPromptForWorkflowName: ConfigDefBoolean<"promptForWorkflowName"> = { + name: "promptForWorkflowName", + type: "boolean", + defaultValue: false, + description: "When saving, always prompt for a name to save the workflow as", +}; + +const defConfirmWhenUnloadingUnsavedChanges: ConfigDefBoolean<"confirmWhenUnloadingUnsavedChanges"> = { + name: "confirmWhenUnloadingUnsavedChanges", + type: "boolean", + defaultValue: true, + description: "When closing the tab, open the confirmation window if there's unsaved changes" +}; + +const defBuiltInTemplates: ConfigDefStringArray<"builtInTemplates"> = { + name: "builtInTemplates", + type: "string[]", + defaultValue: [], + description: "Basenames of templates that can be loaded from public/templates. Saves LocalStorage space.", +}; + +const defCacheBuiltInResources: ConfigDefBoolean<"cacheBuiltInResources"> = { + name: "cacheBuiltInResources", + type: "boolean", + defaultValue: true, + description: "Cache loading of built-in resources to save network use" +}; + +export const CONFIG_DEFS = [ + defComfyUIHostname, + defComfyUIPort, + defAlwaysStripUserState, + defPromptForWorkflowName, + defConfirmWhenUnloadingUnsavedChanges, + defBuiltInTemplates, + defCacheBuiltInResources, +] as const; + +type Config>>> = { + [K in T[number]["name"]]: Extract["defaultValue"] +} extends infer O + ? { [P in keyof O]: O[P] } + : never; + +type ConfigState = Config + type ConfigStateOps = { - getBackendURL: () => string + getBackendURL: () => string, + save: () => void, + load: () => ConfigState } export type WritableConfigStateStore = Writable & ConfigStateOps; @@ -46,9 +115,19 @@ function getBackendURL(): string { return `${window.location.protocol}//${state.comfyUIHostname}:${state.comfyUIPort}` } +function save() { + +} + +function load(): ConfigState { + +} + const configStateStore: WritableConfigStateStore = { ...store, - getBackendURL + getBackendURL, + save, + load } export default configStateStore;