From e96581a2b976e14cb7ce78c099c64c49b245678a Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sat, 7 Dec 2024 10:25:41 +1100 Subject: [PATCH] Optimise positionableItems getters - No change to internal functionality - Replaces forced spread of all items on every property access with generator function - Consumers that require an array can very cleanly spread into one --- src/LGraph.ts | 7 +++++-- src/LGraphCanvas.ts | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/LGraph.ts b/src/LGraph.ts index e6cba49..830c91e 100644 --- a/src/LGraph.ts +++ b/src/LGraph.ts @@ -121,8 +121,11 @@ export class LGraph implements LinkNetwork, Serialisable { } /** @returns All items on the canvas that can be selected */ - get positionableItems(): Positionable[] { - return [...this._nodes, ...this._groups, ...this.reroutes.values()] + *positionableItems(): Generator { + for (const node of this._nodes) yield node + for (const group of this._groups) yield group + for (const reroute of this.reroutes.values()) yield reroute + return } #reroutes = new Map() diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index 77cc394..a8407e9 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -3958,8 +3958,8 @@ export class LGraphCanvas { return this.graph.empty } - get positionableItems(): Positionable[] { - return this.graph.positionableItems + get positionableItems() { + return this.graph.positionableItems() } /** @@ -8420,7 +8420,7 @@ export class LGraphCanvas { * If nothing is selected, the view is fitted around all items in the graph. */ fitViewToSelectionAnimated(options: AnimationOptions = {}) { - const items: Positionable[] = this.selectedItems.size + const items = this.selectedItems.size ? Array.from(this.selectedItems) : this.positionableItems this.animateToBounds(createBounds(items), options)