Enum support
This commit is contained in:
@@ -2,41 +2,34 @@ import { debounce } from '$lib/utils';
|
||||
import { toHashMap } from '@litegraph-ts/core';
|
||||
import { get, writable } from 'svelte/store';
|
||||
import type { Writable } from 'svelte/store';
|
||||
import { defaultConfig, type ConfigState, type ConfigDefAny, CONFIG_DEFS_BY_NAME } from './configDefs';
|
||||
import { defaultConfig, type ConfigState, type ConfigDefAny, CONFIG_DEFS_BY_NAME, validateConfigOption } from './configDefs';
|
||||
|
||||
type ConfigStateOps = {
|
||||
getBackendURL: () => string,
|
||||
load: (data: any) => ConfigState
|
||||
loadDefault: () => ConfigState
|
||||
setConfigOption: (def: ConfigDefAny, v: any) => boolean
|
||||
|
||||
load: (data: any, runOnChanged?: boolean) => ConfigState
|
||||
loadDefault: (runOnChanged?: boolean) => ConfigState
|
||||
setConfigOption: (def: ConfigDefAny, v: any, runOnChanged: boolean) => boolean
|
||||
validateConfigOption: (def: ConfigDefAny, v: any) => boolean
|
||||
onChange: <K extends keyof ConfigState>(optionName: K, callback: ConfigOnChangeCallback<ConfigState[K]>) => void
|
||||
runOnChangedEvents: () => void,
|
||||
}
|
||||
|
||||
export type WritableConfigStateStore = Writable<ConfigState> & ConfigStateOps;
|
||||
const store: Writable<ConfigState> = writable({ ...defaultConfig })
|
||||
const callbacks: Record<string, ConfigOnChangeCallback<any>[]> = {}
|
||||
let changedOptions: Partial<Record<keyof ConfigState, [any, any]>> = {}
|
||||
|
||||
function getBackendURL(): string {
|
||||
const state = get(store);
|
||||
return `${window.location.protocol}//${state.comfyUIHostname}:${state.comfyUIPort}`
|
||||
}
|
||||
|
||||
function validateConfigOption(def: ConfigDefAny, v: any): boolean {
|
||||
switch (def.type) {
|
||||
case "boolean":
|
||||
return typeof v === "boolean";
|
||||
case "number":
|
||||
return typeof v === "number";
|
||||
case "string":
|
||||
return typeof v === "string";
|
||||
case "string[]":
|
||||
return Array.isArray(v) && v.every(vs => typeof vs === "string");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function setConfigOption(def: ConfigDefAny, v: any): boolean {
|
||||
function setConfigOption(def: ConfigDefAny, v: any, runOnChanged: boolean): boolean {
|
||||
let valid = false;
|
||||
store.update(state => {
|
||||
const oldValue = state[def.name]
|
||||
|
||||
valid = validateConfigOption(def, v);
|
||||
|
||||
if (!valid) {
|
||||
@@ -47,12 +40,33 @@ function setConfigOption(def: ConfigDefAny, v: any): boolean {
|
||||
state[def.name] = v
|
||||
}
|
||||
|
||||
const changed = oldValue != state[def.name];
|
||||
if (changed) {
|
||||
if (runOnChanged) {
|
||||
if (callbacks[def.name]) {
|
||||
for (const callback of callbacks[def.name]) {
|
||||
callback(state[def.name], oldValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (changedOptions[def.name] == null) {
|
||||
changedOptions[def.name] = [oldValue, state[def.name]];
|
||||
}
|
||||
else {
|
||||
changedOptions[def.name][1] = state[def.name]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
})
|
||||
return valid;
|
||||
}
|
||||
|
||||
function load(data: any): ConfigState {
|
||||
function load(data: any, runOnChanged: boolean = false): ConfigState {
|
||||
changedOptions = {}
|
||||
|
||||
store.set({ ...defaultConfig })
|
||||
if (data != null && typeof data === "object") {
|
||||
for (const [k, v] of Object.entries(data)) {
|
||||
@@ -62,15 +76,36 @@ function load(data: any): ConfigState {
|
||||
continue;
|
||||
}
|
||||
|
||||
setConfigOption(def, v);
|
||||
setConfigOption(def, v, runOnChanged);
|
||||
}
|
||||
}
|
||||
|
||||
return get(store);
|
||||
}
|
||||
|
||||
function loadDefault() {
|
||||
return load(null);
|
||||
function loadDefault(runOnChanged: boolean = false) {
|
||||
return load(null, runOnChanged);
|
||||
}
|
||||
|
||||
export type ConfigOnChangeCallback<V> = (value: V, oldValue?: V) => void;
|
||||
|
||||
function onChange<K extends keyof ConfigState>(optionName: K, callback: ConfigOnChangeCallback<ConfigState[K]>) {
|
||||
callbacks[optionName] ||= []
|
||||
callbacks[optionName].push(callback)
|
||||
}
|
||||
|
||||
function runOnChangedEvents() {
|
||||
console.debug("Running changed events for config...")
|
||||
for (const [optionName, [oldValue, newValue]] of Object.entries(changedOptions)) {
|
||||
const def = CONFIG_DEFS_BY_NAME[optionName]
|
||||
if (callbacks[optionName]) {
|
||||
console.debug("Running callback!", optionName, oldValue, newValue)
|
||||
for (const callback of callbacks[def.name]) {
|
||||
callback(newValue, oldValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
changedOptions = {}
|
||||
}
|
||||
|
||||
const configStateStore: WritableConfigStateStore =
|
||||
@@ -80,6 +115,8 @@ const configStateStore: WritableConfigStateStore =
|
||||
validateConfigOption,
|
||||
setConfigOption,
|
||||
load,
|
||||
loadDefault
|
||||
loadDefault,
|
||||
onChange,
|
||||
runOnChangedEvents
|
||||
}
|
||||
export default configStateStore;
|
||||
|
||||
Reference in New Issue
Block a user