[TS] Use strict mode in LLink & Reroute (#602)

- Adds fallback colour if reroute colour is somehow nullish
This commit is contained in:
filtered
2025-02-26 05:00:33 +11:00
committed by GitHub
parent 4636367de2
commit f39e1d96e8
4 changed files with 27 additions and 18 deletions

View File

@@ -815,7 +815,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
* @param slot * @param slot
* @param data * @param data
*/ */
setOutputData(slot: number, data: unknown): void { setOutputData(slot: number, data: number | string | boolean | { toToolTip?(): string }): void {
if (!this.outputs) return if (!this.outputs) return
// this maybe slow and a niche case // this maybe slow and a niche case

View File

@@ -1,4 +1,3 @@
// @ts-strict-ignore
import type { import type {
CanvasColour, CanvasColour,
LinkNetwork, LinkNetwork,
@@ -46,9 +45,9 @@ export class LLink implements LinkSegment, Serialisable<SerialisableLLink> {
/** @inheritdoc */ /** @inheritdoc */
_centreAngle?: number _centreAngle?: number
#color?: CanvasColour #color?: CanvasColour | null
/** Custom colour for this link only */ /** Custom colour for this link only */
public get color(): CanvasColour { public get color(): CanvasColour | null | undefined {
return this.#color return this.#color
} }
@@ -108,7 +107,9 @@ export class LLink implements LinkSegment, Serialisable<SerialisableLLink> {
network: LinkNetwork, network: LinkNetwork,
linkSegment: LinkSegment, linkSegment: LinkSegment,
): Reroute[] { ): Reroute[] {
return network.reroutes.get(linkSegment.parentId) if (!linkSegment.parentId) return []
return network.reroutes
.get(linkSegment.parentId)
?.getReroutes() ?? [] ?.getReroutes() ?? []
} }
@@ -125,7 +126,9 @@ export class LLink implements LinkSegment, Serialisable<SerialisableLLink> {
linkSegment: LinkSegment, linkSegment: LinkSegment,
rerouteId: RerouteId, rerouteId: RerouteId,
): Reroute | null | undefined { ): Reroute | null | undefined {
return network.reroutes.get(linkSegment.parentId) if (!linkSegment.parentId) return
return network.reroutes
.get(linkSegment.parentId)
?.findNextReroute(rerouteId) ?.findNextReroute(rerouteId)
} }

View File

@@ -1,4 +1,3 @@
// @ts-strict-ignore
import type { import type {
CanvasColour, CanvasColour,
LinkSegment, LinkSegment,
@@ -31,12 +30,12 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
#parentId?: RerouteId #parentId?: RerouteId
/** @inheritdoc */ /** @inheritdoc */
public get parentId(): RerouteId { public get parentId(): RerouteId | undefined {
return this.#parentId return this.#parentId
} }
/** Ignores attempts to create an infinite loop. @inheritdoc */ /** Ignores attempts to create an infinite loop. @inheritdoc */
public set parentId(value: RerouteId) { public set parentId(value) {
if (value === this.id) return if (value === this.id) return
if (this.getReroutes() === null) return if (this.getReroutes() === null) return
this.#parentId = value this.#parentId = value
@@ -98,16 +97,22 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
/** @inheritdoc */ /** @inheritdoc */
get origin_id(): NodeId | undefined { get origin_id(): NodeId | undefined {
// if (!this.linkIds.size) return this.#network.deref()?.reroutes.get(this.parentId) // if (!this.linkIds.size) return this.#network.deref()?.reroutes.get(this.parentId)
return this.#network.deref() const nextId = this.linkIds.values().next().value
?.links.get(this.linkIds.values().next().value) return nextId === undefined
?.origin_id ? undefined
: this.#network.deref()
?.links.get(nextId)
?.origin_id
} }
/** @inheritdoc */ /** @inheritdoc */
get origin_slot(): number | undefined { get origin_slot(): number | undefined {
return this.#network.deref() const nextId = this.linkIds.values().next().value
?.links.get(this.linkIds.values().next().value) return nextId === undefined
?.origin_slot ? undefined
: this.#network.deref()
?.links.get(nextId)
?.origin_slot
} }
/** /**
@@ -200,6 +205,7 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
if (this.#parentId === withParentId) return this if (this.#parentId === withParentId) return this
if (visited.has(this)) return null if (visited.has(this)) return null
visited.add(this) visited.add(this)
if (this.#parentId === undefined) return
return this.#network return this.#network
.deref() .deref()
@@ -279,7 +285,7 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
*/ */
draw(ctx: CanvasRenderingContext2D): void { draw(ctx: CanvasRenderingContext2D): void {
const { pos } = this const { pos } = this
ctx.fillStyle = this._colour ctx.fillStyle = this._colour ?? "#18184d"
ctx.beginPath() ctx.beginPath()
ctx.arc(pos[0], pos[1], Reroute.radius, 0, 2 * Math.PI) ctx.arc(pos[0], pos[1], Reroute.radius, 0, 2 * Math.PI)
ctx.fill() ctx.fill()

View File

@@ -128,9 +128,9 @@ export interface LinkSegment {
_centreAngle?: number _centreAngle?: number
/** Output node ID */ /** Output node ID */
readonly origin_id: NodeId readonly origin_id: NodeId | undefined
/** Output slot index */ /** Output slot index */
readonly origin_slot: number readonly origin_slot: number | undefined
} }
export interface IInputOrOutput { export interface IInputOrOutput {