[Refactor] Prefer for..of to incremental for loops (#640)

- Converts incremental for loops to for..of loops
- Improves readability
This commit is contained in:
filtered
2025-02-28 03:28:10 +11:00
committed by GitHub
parent be8dc6867e
commit 38dd7b7089

View File

@@ -712,13 +712,14 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
if (this.properties) o.properties = LiteGraph.cloneObject(this.properties) if (this.properties) o.properties = LiteGraph.cloneObject(this.properties)
if (this.widgets && this.serialize_widgets) { const { widgets } = this
if (widgets && this.serialize_widgets) {
o.widgets_values = [] o.widgets_values = []
for (let i = 0; i < this.widgets.length; ++i) { for (const [i, widget] of widgets.entries()) {
if (this.widgets[i]) if (widget)
o.widgets_values[i] = this.widgets[i].value o.widgets_values[i] = widget.value
else else
// @ts-ignore #595 No-null // @ts-ignore #595 No-null
o.widgets_values[i] = null o.widgets_values[i] = null
} }
} }
@@ -743,19 +744,18 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
// we clone it because serialize returns shared containers // we clone it because serialize returns shared containers
const data = LiteGraph.cloneObject(this.serialize()) const data = LiteGraph.cloneObject(this.serialize())
const { inputs, outputs } = data
// remove links // remove links
if (data.inputs) { if (inputs) {
for (let i = 0; i < data.inputs.length; ++i) { for (const element of inputs) {
data.inputs[i].link = null element.link = null
} }
} }
if (data.outputs) { if (outputs) {
for (let i = 0; i < data.outputs.length; ++i) { for (const { links } of outputs) {
if (data.outputs[i].links) { if (links) links.length = 0
data.outputs[i].links.length = 0
}
} }
} }
@@ -798,10 +798,8 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
if (this.onPropertyChanged?.(name, value, prev_value) === false) if (this.onPropertyChanged?.(name, value, prev_value) === false)
this.properties[name] = prev_value this.properties[name] = prev_value
// widgets could be linked to properties
if (this.widgets) { if (this.widgets) {
for (let i = 0; i < this.widgets.length; ++i) { for (const w of this.widgets) {
const w = this.widgets[i]
if (!w) continue if (!w) continue
if (w.options.property == name) { if (w.options.property == name) {
@@ -818,12 +816,13 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* @param data * @param data
*/ */
setOutputData(slot: number, data: number | string | boolean | { toToolTip?(): string }): void { setOutputData(slot: number, data: number | string | boolean | { toToolTip?(): string }): void {
if (!this.outputs) return const { outputs } = this
if (!outputs) return
// this maybe slow and a niche case // this maybe slow and a niche case
if (slot == -1 || slot >= this.outputs.length) return if (slot == -1 || slot >= outputs.length) return
const output_info = this.outputs[slot] const output_info = outputs[slot]
if (!output_info) return if (!output_info) return
// store data in the output itself in case we want to debug // store data in the output itself in case we want to debug
@@ -832,10 +831,10 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
if (!this.graph) throw new NullGraphError() if (!this.graph) throw new NullGraphError()
// if there are connections, pass the data to the connections // if there are connections, pass the data to the connections
if (this.outputs[slot].links) { const { links } = outputs[slot]
for (let i = 0; i < this.outputs[slot].links.length; i++) { if (links) {
const link_id = this.outputs[slot].links[i] for (const id of links) {
const link = this.graph._links.get(link_id) const link = this.graph._links.get(id)
if (link) link.data = data if (link) link.data = data
} }
} }
@@ -845,9 +844,10 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* sets the output data type, useful when you want to be able to overwrite the data type * sets the output data type, useful when you want to be able to overwrite the data type
*/ */
setOutputDataType(slot: number, type: ISlotType): void { setOutputDataType(slot: number, type: ISlotType): void {
if (!this.outputs) return const { outputs } = this
if (slot == -1 || slot >= this.outputs.length) return if (!outputs || (slot == -1 || slot >= outputs.length)) return
const output_info = this.outputs[slot]
const output_info = outputs[slot]
if (!output_info) return if (!output_info) return
// store data in the output itself in case we want to debug // store data in the output itself in case we want to debug
output_info.type = type output_info.type = type
@@ -855,10 +855,10 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
if (!this.graph) throw new NullGraphError() if (!this.graph) throw new NullGraphError()
// if there are connections, pass the data to the connections // if there are connections, pass the data to the connections
if (this.outputs[slot].links) { const { links } = outputs[slot]
for (let i = 0; i < this.outputs[slot].links.length; i++) { if (links) {
const link_id = this.outputs[slot].links[i] for (const id of links) {
const link = this.graph._links.get(link_id) const link = this.graph._links.get(id)
if (!link) continue if (!link) continue
link.type = type link.type = type
} }
@@ -993,15 +993,15 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* @returns value * @returns value
*/ */
getInputOrProperty(name: string): unknown { getInputOrProperty(name: string): unknown {
if (!this.inputs || !this.inputs.length) { const { inputs } = this
if (!inputs?.length) {
return this.properties ? this.properties[name] : null return this.properties ? this.properties[name] : null
} }
if (!this.graph) throw new NullGraphError() if (!this.graph) throw new NullGraphError()
for (let i = 0, l = this.inputs.length; i < l; ++i) { for (const input of inputs) {
const input_info = this.inputs[i] if (name == input.name && input.link != null) {
if (name == input_info.name && input_info.link != null) { const link = this.graph._links.get(input.link)
const link = this.graph._links.get(input_info.link)
if (link) return link.data if (link) return link.data
} }
} }
@@ -1042,12 +1042,11 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* tells you if there is any connection in the output slots * tells you if there is any connection in the output slots
*/ */
isAnyOutputConnected(): boolean { isAnyOutputConnected(): boolean {
if (!this.outputs) return false const { outputs } = this
if (!outputs) return false
for (let i = 0; i < this.outputs.length; ++i) { for (const output of outputs) {
if (this.outputs[i].links && this.outputs[i].links?.length) { if (output.links?.length) return true
return true
}
} }
return false return false
} }
@@ -1056,18 +1055,18 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* retrieves all the nodes connected to this output slot * retrieves all the nodes connected to this output slot
*/ */
getOutputNodes(slot: number): LGraphNode[] | null { getOutputNodes(slot: number): LGraphNode[] | null {
if (!this.outputs || this.outputs.length == 0) return null const { outputs } = this
if (!outputs || outputs.length == 0) return null
if (slot >= this.outputs.length) return null if (slot >= outputs.length) return null
const output = this.outputs[slot] const { links } = outputs[slot]
if (!output.links || output.links.length == 0) return null if (!links || links.length == 0) return null
if (!this.graph) throw new NullGraphError() if (!this.graph) throw new NullGraphError()
const r: LGraphNode[] = [] const r: LGraphNode[] = []
for (let i = 0; i < output.links.length; i++) { for (const id of links) {
const link_id = output.links[i] const link = this.graph._links.get(id)
const link = this.graph._links.get(link_id)
if (link) { if (link) {
const target_node = this.graph.getNodeById(link.target_id) const target_node = this.graph.getNodeById(link.target_id)
if (target_node) { if (target_node) {
@@ -1198,14 +1197,14 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
param: unknown, param: unknown,
options: { action_call?: any }, options: { action_call?: any },
): void { ): void {
if (!this.outputs || !this.outputs.length) { const { outputs } = this
if (!outputs || !outputs.length) {
return return
} }
if (this.graph) this.graph._last_trigger_time = LiteGraph.getTime() if (this.graph) this.graph._last_trigger_time = LiteGraph.getTime()
for (let i = 0; i < this.outputs.length; ++i) { for (const [i, output] of outputs.entries()) {
const output = this.outputs[i]
if ( if (
!output || !output ||
output.type !== LiteGraph.EVENT || output.type !== LiteGraph.EVENT ||
@@ -1389,8 +1388,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* @param array of triplets like [[name,type,extra_info],[...]] * @param array of triplets like [[name,type,extra_info],[...]]
*/ */
addOutputs(array: [string, ISlotType, Partial<INodeOutputSlot>][]): void { addOutputs(array: [string, ISlotType, Partial<INodeOutputSlot>][]): void {
for (let i = 0; i < array.length; ++i) { for (const info of array) {
const info = array[i]
const o = new NodeOutputSlot({ name: info[0], type: info[1], links: null }) const o = new NodeOutputSlot({ name: info[0], type: info[1], links: null })
if (array[2]) { if (array[2]) {
for (const j in info[2]) { for (const j in info[2]) {
@@ -1415,13 +1413,14 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
*/ */
removeOutput(slot: number): void { removeOutput(slot: number): void {
this.disconnectOutput(slot) this.disconnectOutput(slot)
this.outputs.splice(slot, 1) const { outputs } = this
outputs.splice(slot, 1)
for (let i = slot; i < this.outputs.length; ++i) { for (let i = slot; i < outputs.length; ++i) {
if (!this.outputs[i] || !this.outputs[i].links) continue const output = outputs[i]
if (!output || !output.links) continue
const links = this.outputs[i].links for (const linkId of output.links) {
for (const linkId of links) {
if (!this.graph) throw new NullGraphError() if (!this.graph) throw new NullGraphError()
const link = this.graph._links.get(linkId) const link = this.graph._links.get(linkId)
@@ -1465,8 +1464,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* @param array of triplets like [[name,type,extra_info],[...]] * @param array of triplets like [[name,type,extra_info],[...]]
*/ */
addInputs(array: [string, ISlotType, Partial<INodeInputSlot>][]): void { addInputs(array: [string, ISlotType, Partial<INodeInputSlot>][]): void {
for (let i = 0; i < array.length; ++i) { for (const info of array) {
const info = array[i]
const o: INodeInputSlot = new NodeInputSlot({ name: info[0], type: info[1], link: null }) const o: INodeInputSlot = new NodeInputSlot({ name: info[0], type: info[1], link: null })
// TODO: Checking the wrong variable here - confirm no downstream consumers, then remove. // TODO: Checking the wrong variable here - confirm no downstream consumers, then remove.
if (array[2]) { if (array[2]) {
@@ -1491,13 +1489,15 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
*/ */
removeInput(slot: number): void { removeInput(slot: number): void {
this.disconnectInput(slot) this.disconnectInput(slot)
const slot_info = this.inputs.splice(slot, 1) const { inputs } = this
const slot_info = inputs.splice(slot, 1)
for (let i = slot; i < this.inputs.length; ++i) { for (let i = slot; i < inputs.length; ++i) {
if (!this.inputs[i]) continue const input = inputs[i]
if (!input) continue
if (!this.graph) throw new NullGraphError() if (!this.graph) throw new NullGraphError()
const link = this.graph._links.get(this.inputs[i].link) const link = this.graph._links.get(input.link)
if (!link) continue if (!link) continue
link.target_slot -= 1 link.target_slot -= 1
@@ -1532,9 +1532,10 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
const ctorSize = this.constructor.size const ctorSize = this.constructor.size
if (ctorSize) return [ctorSize[0], ctorSize[1]] if (ctorSize) return [ctorSize[0], ctorSize[1]]
const { inputs, outputs } = this
let rows = Math.max( let rows = Math.max(
this.inputs ? this.inputs.length : 1, inputs ? inputs.length : 1,
this.outputs ? this.outputs.length : 1, outputs ? outputs.length : 1,
) )
const size = out || new Float32Array([0, 0]) const size = out || new Float32Array([0, 0])
rows = Math.max(rows, 1) rows = Math.max(rows, 1)
@@ -1545,9 +1546,8 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
let input_width = 0 let input_width = 0
let output_width = 0 let output_width = 0
if (this.inputs) { if (inputs) {
for (let i = 0, l = this.inputs.length; i < l; ++i) { for (const input of inputs) {
const input = this.inputs[i]
const text = input.label || input.localized_name || input.name || "" const text = input.label || input.localized_name || input.name || ""
const text_width = compute_text_size(text) const text_width = compute_text_size(text)
if (input_width < text_width) if (input_width < text_width)
@@ -1555,9 +1555,8 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
} }
} }
if (this.outputs) { if (outputs) {
for (let i = 0, l = this.outputs.length; i < l; ++i) { for (const output of outputs) {
const output = this.outputs[i]
const text = output.label || output.localized_name || output.name || "" const text = output.label || output.localized_name || output.name || ""
const text_width = compute_text_size(text) const text_width = compute_text_size(text)
if (output_width < text_width) if (output_width < text_width)
@@ -1637,10 +1636,11 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
// there are several ways to define info about a property // there are several ways to define info about a property
// legacy mode // legacy mode
if (this.properties_info) { const { properties_info } = this
for (let i = 0; i < this.properties_info.length; ++i) { if (properties_info) {
if (this.properties_info[i].name == property) { for (const propInfo of properties_info) {
info = this.properties_info[i] if (propInfo.name == property) {
info = propInfo
break break
} }
} }
@@ -1844,9 +1844,10 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
getSlotInPosition(x: number, y: number): IFoundSlot | null { getSlotInPosition(x: number, y: number): IFoundSlot | null {
// search for inputs // search for inputs
const link_pos = new Float32Array(2) const link_pos = new Float32Array(2)
if (this.inputs) { const { inputs, outputs } = this
for (let i = 0, l = this.inputs.length; i < l; ++i) {
const input = this.inputs[i] if (inputs) {
for (const [i, input] of inputs.entries()) {
this.getConnectionPos(true, i, link_pos) this.getConnectionPos(true, i, link_pos)
if (isInRectangle(x, y, link_pos[0] - 10, link_pos[1] - 5, 20, 10)) { if (isInRectangle(x, y, link_pos[0] - 10, link_pos[1] - 5, 20, 10)) {
return { input, slot: i, link_pos } return { input, slot: i, link_pos }
@@ -1854,9 +1855,8 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
} }
} }
if (this.outputs) { if (outputs) {
for (let i = 0, l = this.outputs.length; i < l; ++i) { for (const [i, output] of outputs.entries()) {
const output = this.outputs[i]
this.getConnectionPos(false, i, link_pos) this.getConnectionPos(false, i, link_pos)
if (isInRectangle(x, y, link_pos[0] - 10, link_pos[1] - 5, 20, 10)) { if (isInRectangle(x, y, link_pos[0] - 10, link_pos[1] - 5, 20, 10)) {
return { output, slot: i, link_pos } return { output, slot: i, link_pos }
@@ -1919,11 +1919,12 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
findInputSlot<TReturn extends false>(name: string, returnObj?: TReturn): number findInputSlot<TReturn extends false>(name: string, returnObj?: TReturn): number
findInputSlot<TReturn extends true>(name: string, returnObj?: TReturn): INodeInputSlot findInputSlot<TReturn extends true>(name: string, returnObj?: TReturn): INodeInputSlot
findInputSlot(name: string, returnObj: boolean = false) { findInputSlot(name: string, returnObj: boolean = false) {
if (!this.inputs) return -1 const { inputs } = this
if (!inputs) return -1
for (let i = 0, l = this.inputs.length; i < l; ++i) { for (const [i, input] of inputs.entries()) {
if (name == this.inputs[i].name) { if (name == input.name) {
return !returnObj ? i : this.inputs[i] return !returnObj ? i : input
} }
} }
return -1 return -1
@@ -1938,11 +1939,12 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
findOutputSlot<TReturn extends false>(name: string, returnObj?: TReturn): number findOutputSlot<TReturn extends false>(name: string, returnObj?: TReturn): number
findOutputSlot<TReturn extends true>(name: string, returnObj?: TReturn): INodeOutputSlot findOutputSlot<TReturn extends true>(name: string, returnObj?: TReturn): INodeOutputSlot
findOutputSlot(name: string, returnObj: boolean = false) { findOutputSlot(name: string, returnObj: boolean = false) {
if (!this.outputs) return -1 const { outputs } = this
if (!outputs) return -1
for (let i = 0, l = this.outputs.length; i < l; ++i) { for (const [i, output] of outputs.entries()) {
if (name == this.outputs[i].name) { if (name == output.name) {
return !returnObj ? i : this.outputs[i] return !returnObj ? i : output
} }
} }
return -1 return -1