From 7d38f6bdc7ed04980e65df23b2ddab82d8b6da92 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Fri, 2 May 2025 02:50:26 +1000 Subject: [PATCH] Fix regression in legacy widget-values-as-function (#990) Resolves issue with KJNodes get/set nodes. ![image](https://github.com/user-attachments/assets/ecbf99d4-b2d3-48bf-a34c-65d0bf451b4c) Downstream consumer: https://github.com/kijai/ComfyUI-KJNodes/blob/c3dc82108a2a86c17094107ead61d63f8c76200e/web/js/setgetnodes.js#L401-L404 --- src/widgets/ComboWidget.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/widgets/ComboWidget.ts b/src/widgets/ComboWidget.ts index 4de0a816c..a19910ad2 100644 --- a/src/widgets/ComboWidget.ts +++ b/src/widgets/ComboWidget.ts @@ -1,3 +1,4 @@ +import type { LGraphNode } from "@/LGraphNode" import type { IComboWidget, IWidgetOptions } from "@/types/widgets" import { clamp, LiteGraph } from "@/litegraph" @@ -5,7 +6,13 @@ import { clamp, LiteGraph } from "@/litegraph" import { BaseSteppedWidget } from "./BaseSteppedWidget" import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget" -type Values = string[] | Record +/** + * This is used as an (invalid) assertion to resolve issues with legacy duck-typed values. + * + * Function style in use by: + * https://github.com/kijai/ComfyUI-KJNodes/blob/c3dc82108a2a86c17094107ead61d63f8c76200e/web/js/setgetnodes.js#L401-L404 + */ +type Values = string[] | Record | ((widget: ComboWidget, node: LGraphNode) => string[]) function toArray(values: Values): string[] { return Array.isArray(values) ? values : Object.keys(values) @@ -24,12 +31,11 @@ export class ComboWidget extends BaseSteppedWidget implements IComboWidget { this.value = widget.value } - #getValues(): Values { + #getValues(node: LGraphNode): Values { const { values } = this.options if (values == null) throw new Error("[ComboWidget]: values is required") return typeof values === "function" - // @ts-expect-error handle () => string[] type that is not typed in IWidgetOptions ? values(this, node) : values } @@ -65,7 +71,7 @@ export class ComboWidget extends BaseSteppedWidget implements IComboWidget { } #tryChangeValue(delta: number, options: WidgetEventOptions): void { - const values = this.#getValues() + const values = this.#getValues(options.node) const indexedValues = toArray(values) // avoids double click event @@ -195,7 +201,7 @@ export class ComboWidget extends BaseSteppedWidget implements IComboWidget { if (x > width - 40) return this.incrementValue({ e, node, canvas }) // Otherwise, show dropdown menu - const values = this.#getValues() + const values = this.#getValues(node) const values_list = toArray(values) // Handle center click - show dropdown menu