From c82c3c24f723fb308b1bb2d936eee46e49e48a08 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 16 Sep 2025 17:33:02 -0700 Subject: [PATCH] [refactor] convert Rectangle from Float64Array to Array inheritance - addresses review feedback Replace Float64Array inheritance with Array to remove legacy typed array usage. Maintains compatibility with array indexing (rect[0], rect[1], etc.) and property access (rect.x, rect.y, etc.) while adding .set() and .subarray() methods for compatibility. Co-authored-by: webfiltered --- .../litegraph/src/infrastructure/Rectangle.ts | 60 ++++++++----------- src/lib/litegraph/src/interfaces.ts | 12 +--- 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/src/lib/litegraph/src/infrastructure/Rectangle.ts b/src/lib/litegraph/src/infrastructure/Rectangle.ts index 65e0802d5c..4403848b6f 100644 --- a/src/lib/litegraph/src/infrastructure/Rectangle.ts +++ b/src/lib/litegraph/src/infrastructure/Rectangle.ts @@ -4,44 +4,49 @@ import type { ReadOnlyPoint, ReadOnlyRect, ReadOnlySize, - ReadOnlyTypedArray, Size } from '@/lib/litegraph/src/interfaces' import { isInRectangle } from '@/lib/litegraph/src/measure' /** - * A rectangle, represented as a float64 array of 4 numbers: [x, y, width, height]. + * A rectangle, represented as an array of 4 numbers: [x, y, width, height]. * - * This class is a subclass of Float64Array, and so has all the methods of that class. Notably, - * {@link Rectangle.from} can be used to convert a {@link ReadOnlyRect}. Typing of this however, - * is broken due to the base TS lib returning Float64Array rather than `this`. - * - * Sub-array properties ({@link Float64Array.subarray}): - * - {@link pos}: The position of the top-left corner of the rectangle. - * - {@link size}: The size of the rectangle. + * This class extends Array and provides both array access (rect[0], rect[1], etc.) + * and convenient property access (rect.x, rect.y, rect.width, rect.height). */ -export class Rectangle extends Float64Array { - #pos: Point | undefined - #size: Size | undefined - +export class Rectangle extends Array { constructor( x: number = 0, y: number = 0, width: number = 0, height: number = 0 ) { - super(4) - + super() this[0] = x this[1] = y this[2] = width this[3] = height + this.length = 4 } static override from([x, y, width, height]: ReadOnlyRect): Rectangle { return new Rectangle(x, y, width, height) } + /** Set all values from an array (for TypedArray compatibility) */ + set(values: ArrayLike): void { + this[0] = values[0] ?? 0 + this[1] = values[1] ?? 0 + this[2] = values[2] ?? 0 + this[3] = values[3] ?? 0 + } + + /** Create a subarray (for TypedArray compatibility) */ + subarray(begin: number = 0, end?: number): number[] { + const endIndex = end ?? this.length + return this.slice(begin, endIndex) + } + /** * Creates a new rectangle positioned at the given centre, with the given width/height. * @param centre The centre of the rectangle, as an `[x, y]` point @@ -65,23 +70,11 @@ export class Rectangle extends Float64Array { : new Rectangle(rect[0], rect[1], rect[2], rect[3]) } - override subarray( - begin: number = 0, - end?: number - ): Float64Array { - const byteOffset = begin << 3 - const length = end === undefined ? end : end - begin - return new Float64Array(this.buffer, byteOffset, length) - } - /** - * A reference to the position of the top-left corner of this rectangle. - * - * Updating the values of the returned object will update this rectangle. + * The position of the top-left corner of this rectangle. */ get pos(): Point { - this.#pos ??= this.subarray(0, 2) - return this.#pos! + return [this[0], this[1]] } set pos(value: ReadOnlyPoint) { @@ -90,13 +83,10 @@ export class Rectangle extends Float64Array { } /** - * A reference to the size of this rectangle. - * - * Updating the values of the returned object will update this rectangle. + * The size of this rectangle. */ get size(): Size { - this.#size ??= this.subarray(2, 4) - return this.#size! + return [this[2], this[3]] } set size(value: ReadOnlySize) { @@ -471,7 +461,7 @@ export class Rectangle extends Float64Array { } export type ReadOnlyRectangle = Omit< - ReadOnlyTypedArray, + Readonly, | 'setHeightBottomAnchored' | 'setWidthRightAnchored' | 'resizeTopLeft' diff --git a/src/lib/litegraph/src/interfaces.ts b/src/lib/litegraph/src/interfaces.ts index ad45096cc3..290091c021 100644 --- a/src/lib/litegraph/src/interfaces.ts +++ b/src/lib/litegraph/src/interfaces.ts @@ -225,18 +225,10 @@ export interface IFoundSlot extends IInputOrOutput { } /** A point represented as `[x, y]` co-ordinates */ -export type Point = - | [x: number, y: number] - | Float32Array - | Float64Array - | number[] +export type Point = [x: number, y: number] /** A size represented as `[width, height]` */ -export type Size = - | [width: number, height: number] - | Float32Array - | Float64Array - | number[] +export type Size = [width: number, height: number] /** A very firm array */ type ArRect = [x: number, y: number, width: number, height: number]