From a043e7a72e9a247c0b3a575ea3e79252c58f4b2b Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sat, 22 Mar 2025 05:58:07 +1100 Subject: [PATCH] [API] Add reroute convenience methods (#812) Adds `LGraph.getReroute`, which accepts null & undefined IDs. Interface overloads result in `never` when given a _known_ `undefined` or `null` value. --- src/LGraph.ts | 11 +++++++++++ src/Reroute.ts | 4 ++++ src/interfaces.ts | 3 +++ 3 files changed, 18 insertions(+) diff --git a/src/LGraph.ts b/src/LGraph.ts index 247d33856..e88e38d55 100644 --- a/src/LGraph.ts +++ b/src/LGraph.ts @@ -1398,6 +1398,17 @@ export class LGraph implements LinkNetwork, Serialisable { } } + /** + * Returns a reroute by ID, or `undefined` if the ID is `null` or `undefined`. + * @param id ID of reroute to return + * @returns The reroute with the provided {@link id}, otherwise `undefined`. Always returns `undefined` if `id` is `null` or `undefined`. + */ + getReroute(id: null | undefined): undefined + getReroute(id: RerouteId | null | undefined): Reroute | undefined + getReroute(id: RerouteId | null | undefined): Reroute | undefined { + return id == null ? undefined : this.reroutes.get(id) + } + /** * Configures a reroute on the graph where ID is already known (probably deserialisation). * Creates the object if it does not exist. diff --git a/src/Reroute.ts b/src/Reroute.ts index fe03b6f12..152ac08a9 100644 --- a/src/Reroute.ts +++ b/src/Reroute.ts @@ -49,6 +49,10 @@ export class Reroute implements Positionable, LinkSegment, Serialisable readonly floatingLinks: ReadonlyMap getNodeById(id: NodeId): LGraphNode | null + getReroute(parentId: null | undefined): undefined + getReroute(parentId: RerouteId | null | undefined): Reroute | undefined } /** @@ -126,6 +128,7 @@ export interface LinkNetwork extends ReadonlyLinkNetwork { readonly reroutes: Map addFloatingLink(link: LLink): LLink removeReroute(id: number): unknown + removeFloatingLink(link: LLink): void } /**