[Cleanup] Reroute and serialisation (#767)

- Fixes root cause of minor in-memory-only corruption when deleting
reroutes from output slots
  - Already automatically corrected via serialisation
- If `reroutes` or `links` are empty arrays, removes them from newer
object-based serialised output entirely
- Minor refactors
- Removes unused code
- Fixes some serialisation types
- Adds `ReadonlyLinkNetwork` interface
This commit is contained in:
filtered
2025-03-14 01:40:12 +11:00
committed by GitHub
parent bcaeccfc45
commit 2e9f877bbd
5 changed files with 22 additions and 20 deletions

View File

@@ -118,7 +118,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
nodes_executing: boolean[] = []
nodes_actioning: (string | boolean)[] = []
nodes_executedAction: string[] = []
extra: Record<any, any> = {}
extra: Record<string, unknown> = {}
inputs: Dictionary<IGraphInput> = {}
outputs: Dictionary<IGraphInput> = {}
@@ -1471,14 +1471,14 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
const linkArray = [...this._links.values()]
const links = linkArray.map(x => x.serialize())
if (reroutes.length) {
if (reroutes?.length) {
// Link parent IDs cannot go in 0.4 schema arrays
extra.linkExtensions = linkArray
.filter(x => x.parentId !== undefined)
.map(x => ({ id: x.id, parentId: x.parentId }))
}
extra.reroutes = reroutes.length ? reroutes : undefined
extra.reroutes = reroutes?.length ? reroutes : undefined
return {
last_node_id: state.lastNodeId,
last_link_id: state.lastLinkId,
@@ -1499,7 +1499,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
* Mutating the properties of the return object may result in changes to your graph.
* It is intended for use with {@link structuredClone} or {@link JSON.stringify}.
*/
asSerialisable(options?: { sortNodes: boolean }): Required<SerialisableGraph> {
asSerialisable(options?: { sortNodes: boolean }): SerialisableGraph & Required<Pick<SerialisableGraph, "nodes" | "groups" | "extra">> {
const { config, state, extra } = this
const nodeList = !LiteGraph.use_uuids && options?.sortNodes
@@ -1510,10 +1510,10 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
const nodes = nodeList.map(node => node.serialize())
const groups = this._groups.map(x => x.serialize())
const links = [...this._links.values()].map(x => x.asSerialisable())
const reroutes = [...this.reroutes.values()].map(x => x.asSerialisable())
const links = this._links.size ? [...this._links.values()].map(x => x.asSerialisable()) : undefined
const reroutes = this.reroutes.size ? [...this.reroutes.values()].map(x => x.asSerialisable()) : undefined
const data: Required<SerialisableGraph> = {
const data: ReturnType<typeof this.asSerialisable> = {
version: LGraph.serialisedSchemaVersion,
config,
state,
@@ -1542,11 +1542,11 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
if (!data) return
if (!keep_old) this.clear()
const { extra } = data
let reroutes: SerialisableReroute[] | undefined
// TODO: Determine whether this should this fall back to 0.4.
if (data.version === 0.4) {
const { extra } = data
// Deprecated - old schema version, links are arrays
if (Array.isArray(data.links)) {
for (const linkData of data.links) {

View File

@@ -2578,7 +2578,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
input.link = null
// remove the link from the links pool
graph._links.delete(link_id)
link_info.disconnect(graph)
graph._version++
// link_info hasn't been modified so its ok
@@ -2625,7 +2625,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
)
}
// remove the link from the links pool
graph._links.delete(link_id)
link_info.disconnect(graph)
this.onConnectionsChange?.(
NodeSlotType.OUTPUT,

View File

@@ -67,9 +67,6 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
/** The ID ({@link LLink.id}) of every link using this reroute */
linkIds: Set<LinkId>
/** The averaged angle of every link through this reroute. */
otherAngle: number = 0
/** Cached cos */
cos: number = 0
sin: number = 0
@@ -95,7 +92,6 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
* @todo Calculate on change instead.
*/
#lastRenderTime: number = -Infinity
#buffer: Point = this.#malloc.subarray(2, 4)
/** @inheritdoc */
get origin_id(): NodeId | undefined {
@@ -272,7 +268,7 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
const pos = LLink.findNextReroute(network, link, id)?.pos ??
network.getNodeById(link.target_id)
?.getConnectionPos(true, link.target_slot, this.#buffer)
?.getInputPos(link.target_slot)
if (!pos) continue
// TODO: Store points/angles, check if changed, skip calcs.
@@ -297,12 +293,10 @@ export class Reroute implements Positionable, LinkSegment, Serialisable<Serialis
const cos = Math.cos(originDiff)
const sin = Math.sin(originDiff)
this.otherAngle = originDiff
this.cos = cos
this.sin = sin
this.controlPoint[0] = dist * -cos
this.controlPoint[1] = dist * -sin
return
}
/**

View File

@@ -111,10 +111,16 @@ export interface IPinnable {
unpin(): void
}
export interface ReadonlyLinkNetwork {
readonly links: ReadonlyMap<LinkId, LLink>
readonly reroutes: ReadonlyMap<RerouteId, Reroute>
getNodeById(id: NodeId): LGraphNode | null
}
/**
* Contains a list of links, reroutes, and nodes.
*/
export interface LinkNetwork {
export interface LinkNetwork extends ReadonlyLinkNetwork {
readonly links: Map<LinkId, LLink>
readonly reroutes: Map<RerouteId, Reroute>
getNodeById(id: NodeId): LGraphNode | null

View File

@@ -37,7 +37,7 @@ export interface SerialisableGraph {
nodes?: ISerialisedNode[]
links?: SerialisableLLink[]
reroutes?: SerialisableReroute[]
extra?: Record<any, any>
extra?: Dictionary<unknown>
}
export type ISerialisableNodeInput = Omit<INodeInputSlot, "_layoutElement" | "widget"> & {
@@ -86,7 +86,9 @@ export interface ISerialisedGraph {
groups: ISerialisedGroup[]
config: LGraphConfig
version: typeof LiteGraph.VERSION
extra?: Record<string, unknown>
extra?: Dictionary<unknown> & {
reroutes?: SerialisableReroute[]
}
}
/** Serialised LGraphGroup */