Skip to content

Commit

Permalink
Prefer undefined to explicit null, add NullGraphError
Browse files Browse the repository at this point in the history
- Adds NullGraphError to reduce boilerplate null check code
- Reference: #595
  • Loading branch information
webfiltered committed Feb 25, 2025
1 parent 8cbf799 commit f642593
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
if (index != -1) {
this._groups.splice(index, 1)
}
node.graph = null
node.graph = undefined
this._version++
this.setDirtyCanvas(true, true)
this.change()
Expand Down
4 changes: 3 additions & 1 deletion src/LGraphGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "./measure"
import { LGraphNode } from "./LGraphNode"
import { strokeShape } from "./draw"
import { NullGraphError } from "@/infrastructure/NullGraphError"

export interface IGraphGroupFlags extends Record<string, unknown> {
pinned?: true
Expand Down Expand Up @@ -51,7 +52,7 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable {
/** @deprecated See {@link _children} */
_nodes: LGraphNode[] = []
_children: Set<Positionable> = new Set()
graph: LGraph | null = null
graph?: LGraph
flags: IGraphGroupFlags = {}
selected?: boolean

Expand Down Expand Up @@ -236,6 +237,7 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable {
}

recomputeInsideNodes(): void {
if (!this.graph) throw new NullGraphError()
const { nodes, reroutes, groups } = this.graph
const children = this._children
this._nodes.length = 0
Expand Down
6 changes: 6 additions & 0 deletions src/infrastructure/NullGraphError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class NullGraphError extends Error {
constructor(message: string = "Attempted to access LGraph reference that was null or undefined.", cause?: Error) {
super(message, { cause })
this.name = "NullGraphError"
}
}

0 comments on commit f642593

Please sign in to comment.