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

View File

@@ -15,14 +15,6 @@ export const graphToPrompt = async (
const { sortNodes = false } = options const { sortNodes = false } = options
for (const outerNode of graph.computeExecutionOrder(false)) { 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 const innerNodes = outerNode.getInnerNodes
? outerNode.getInnerNodes() ? outerNode.getInnerNodes()
: [outerNode] : [outerNode]

View File

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