[Major Refactor] Use pinia store to manage setting & nodeDef (#202)

* Node def store and settings tore

* Fix initial values

* Remove legacy setting listen

* Fix searchbox test
This commit is contained in:
Chenlei Hu
2024-07-24 11:49:09 -04:00
committed by GitHub
parent 84d8c5fc16
commit b73fe80761
10 changed files with 186 additions and 84 deletions

View File

@@ -0,0 +1,68 @@
import { ComfyNodeDef } from "@/types/apiTypes";
import { defineStore } from "pinia";
export const SYSTEM_NODE_DEFS: ComfyNodeDef[] = [
{
name: "PrimitiveNode",
display_name: "Primitive",
category: "utils",
input: { required: {}, optional: {} },
output: ["*"],
output_name: ["connect to widget input"],
output_is_list: [false],
python_module: "nodes",
description: "Primitive values like numbers, strings, and booleans.",
},
{
name: "Reroute",
display_name: "Reroute",
category: "utils",
input: { required: { "": ["*"] }, optional: {} },
output: ["*"],
output_name: [""],
output_is_list: [false],
python_module: "nodes",
description: "Reroute the connection to another node.",
},
{
name: "Note",
display_name: "Note",
category: "utils",
input: { required: {}, optional: {} },
output: [],
output_name: [],
output_is_list: [],
python_module: "nodes",
description: "Node that add notes to your project",
},
];
const SYSTEM_NODE_DEFS_BY_NAME = SYSTEM_NODE_DEFS.reduce((acc, nodeDef) => {
acc[nodeDef.name] = nodeDef;
return acc;
}, {}) as Record<string, ComfyNodeDef>;
interface State {
nodeDefsByName: Record<string, ComfyNodeDef>;
}
export const useNodeDefStore = defineStore("nodeDef", {
state: (): State => ({
nodeDefsByName: SYSTEM_NODE_DEFS_BY_NAME,
}),
getters: {
nodeDefs(state) {
return Object.values(state.nodeDefsByName);
},
},
actions: {
addNodeDef(nodeDef: ComfyNodeDef) {
this.nodeDefsByName[nodeDef.name] = nodeDef;
},
addNodeDefs(nodeDefs: ComfyNodeDef[]) {
for (const nodeDef of nodeDefs) {
this.addNodeDef(nodeDef);
}
},
},
});

View File

@@ -0,0 +1,41 @@
/**
* TODO: Migrate scripts/ui/settings.ts here
*
* Currently the reactive settings act as a proxy of the legacy settings.
* Every time a setting is changed, the settingStore dispatch the change to the
* legacy settings. Every time the legacy settings are changed, the legacy
* settings directly updates the settingStore.settingValues.
*/
import { app } from "@/scripts/app";
import { ComfySettingsDialog } from "@/scripts/ui/settings";
import { defineStore } from "pinia";
interface State {
settingValues: Record<string, any>;
}
export const useSettingStore = defineStore("setting", {
state: (): State => ({
settingValues: {},
}),
actions: {
addSettings(settings: ComfySettingsDialog) {
for (const id in settings.settingsLookup) {
const value = settings.getSettingValue(id);
this.settingValues[id] = value;
}
},
set(key: string, value: any) {
this.settingValues[key] = value;
app.ui.settings.setSettingValue(key, value);
},
get(key: string) {
return (
this.settingValues[key] ?? app.ui.settings.getSettingDefaultValue(key)
);
},
},
});