[Refactor] Move Widget.beforeQueued invocation from graphToPrompt to queuePrompt (#2667)

This commit is contained in:
Chenlei Hu
2025-02-21 14:18:11 -05:00
committed by GitHub
parent 40deb19634
commit 40da43861e
3 changed files with 23 additions and 24 deletions

View File

@@ -39,7 +39,7 @@ import {
import { ExtensionManager } from '@/types/extensionTypes'
import { ColorAdjustOptions, adjustColor } from '@/utils/colorUtil'
import { graphToPrompt } from '@/utils/executionUtil'
import { isImageNode } from '@/utils/litegraphUtil'
import { executeWidgetsCallback, isImageNode } from '@/utils/litegraphUtil'
import { deserialiseAndCreate } from '@/utils/vintageClipboard'
import { type ComfyApi, api } from './api'
@@ -1260,7 +1260,7 @@ export class ComfyApp {
return '(unknown error)'
}
async queuePrompt(number, batchCount = 1) {
async queuePrompt(number: number, batchCount: number = 1): Promise<boolean> {
this.#queueItems.push({ number, batchCount })
// Only have one action process the items so each one gets a unique seed correctly
@@ -1276,8 +1276,11 @@ export class ComfyApp {
;({ number, batchCount } = this.#queueItems.pop())
for (let i = 0; i < batchCount; i++) {
const p = await this.graphToPrompt()
// Allow widgets to run callbacks before a prompt has been queued
// e.g. random seed before every gen
executeWidgetsCallback(this.graph.nodes, 'beforeQueued')
const p = await this.graphToPrompt()
try {
const res = await api.queuePrompt(number, p)
this.lastNodeErrors = res.node_errors
@@ -1303,19 +1306,12 @@ export class ComfyApp {
break
}
for (const n of p.workflow.nodes) {
const node = this.graph.getNodeById(n.id)
if (node.widgets) {
for (const widget of node.widgets) {
// Allow widgets to run callbacks after a prompt has been queued
// e.g. random seed after every gen
if (widget.afterQueued) {
widget.afterQueued()
}
}
}
}
// Allow widgets to run callbacks after a prompt has been queued
// e.g. random seed after every gen
executeWidgetsCallback(
p.workflow.nodes.map((n) => this.graph.getNodeById(n.id)),
'afterQueued'
)
this.canvas.draw(true, true)
await this.ui.queue.update()
}

View File

@@ -15,14 +15,6 @@ export const graphToPrompt = async (
const { sortNodes = false } = options
for (const outerNode of graph.computeExecutionOrder(false)) {
if (outerNode.widgets) {
for (const widget of outerNode.widgets) {
// Allow widgets to run callbacks before a prompt has been queued
// e.g. random seed before every gen
widget.beforeQueued?.()
}
}
const innerNodes = outerNode.getInnerNodes
? outerNode.getInnerNodes()
: [outerNode]

View File

@@ -42,3 +42,14 @@ export const getItemsColorOption = (items: unknown[]): ColorOption | null => {
? _.head(colorOptions)!
: null
}
export function executeWidgetsCallback(
nodes: LGraphNode[],
callbackName: 'onRemove' | 'beforeQueued' | 'afterQueued'
) {
for (const node of nodes) {
for (const widget of node.widgets ?? []) {
widget[callbackName]?.()
}
}
}