Files
JeyzerMC-comfyui-flakes/web/queue.js
T
Mehdi Chaid 6ab581f040 refactor: split flake_stack.js into focused modules
Decomposes the ~3800-line flake_stack.js monolith into maintainable
modules:

- api.js        – all server API calls
- utils.js      – DOM helpers, sliders, inputs, drag indicators
- modal.js      – overlay modal infrastructure
- flake-modal.js – flake create/edit modal
- preset-modal.js – preset create/edit modal + preset option refresh
- pickers.js    – file browser, flake picker, preset picker
- widgets/      – per-node widget setups
  - flake-stack.js
  - flake-combo.js
  - flake-model-preset.js
  - flake-model-combo.js
- queue.js      – frontend combination queueing logic
- flake_stack.js – entry point, imports everything and registers extension

Fixes #59
2026-05-01 15:06:12 -04:00

109 lines
3.5 KiB
JavaScript

import { app } from "../../scripts/app.js";
export function getComboFlakes(node) {
return node.properties?._combo_flakes || [];
}
export function getComboPresets(node) {
return node.properties?._combo_presets || [];
}
export function cartesianProduct(arrays) {
if (arrays.length === 0) return [[]];
const result = [];
const head = arrays[0];
const tail = cartesianProduct(arrays.slice(1));
for (const h of head) {
for (const t of tail) {
result.push([h, ...t]);
}
}
return result;
}
const _originalQueuePrompt = app.queuePrompt;
app.queuePrompt = async function(number, batchCount = 1) {
const comboNodes = app.graph.nodes.filter(n => n.type === "FlakeCombo");
const modelComboNodes = app.graph.nodes.filter(n => n.type === "FlakeModelCombo");
if (comboNodes.length === 0 && modelComboNodes.length === 0) {
return _originalQueuePrompt.call(this, number, batchCount);
}
const optionsArrays = [];
for (const node of comboNodes) {
const flakes = getComboFlakes(node);
if (flakes.length === 0) {
window.alert("FlakeCombo node has no flakes selected.");
return;
}
optionsArrays.push(flakes.map((flake, i) => ({
node,
type: "combo",
value: flake,
index: i,
})));
}
for (const node of modelComboNodes) {
const presets = getComboPresets(node);
if (presets.length === 0) {
window.alert("FlakeModelCombo node has no presets selected.");
return;
}
optionsArrays.push(presets.map((preset, i) => ({
node,
type: "model_combo",
value: preset,
index: i,
})));
}
const combinations = cartesianProduct(optionsArrays);
if (combinations.length === 0) {
return _originalQueuePrompt.call(this, number, batchCount);
}
if (!window.confirm(`This will queue ${combinations.length} prompt(s). Continue?`)) {
return;
}
// Save original widget values (once per unique node)
const nodeOriginals = new Map();
for (const item of combinations[0]) {
if (!nodeOriginals.has(item.node.id)) {
if (item.type === "combo") {
const w = item.node.widgets?.find(w => w.name === "flakes_json");
nodeOriginals.set(item.node.id, { node: item.node, widget: w, value: w?.value });
} else {
const w = item.node.widgets?.find(w => w.name === "preset");
nodeOriginals.set(item.node.id, { node: item.node, widget: w, value: w?.value });
}
}
}
try {
for (const combination of combinations) {
for (const item of combination) {
if (item.type === "combo") {
const w = item.node.widgets?.find(w => w.name === "flakes_json");
if (w) w.value = JSON.stringify([item.value]);
} else {
const w = item.node.widgets?.find(w => w.name === "preset");
if (w) w.value = item.value;
}
}
await _originalQueuePrompt.call(this, number, 1);
}
} finally {
for (const orig of nodeOriginals.values()) {
if (orig.widget) orig.widget.value = orig.value;
}
for (const n of app.graph.nodes) {
if (n.type === "FlakeCombo") n._combo_render?.();
if (n.type === "FlakeModelCombo") n._model_combo_render?.();
}
}
};