@@ -18,6 +18,16 @@ import {
1818import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
1919import { toLinkId } from '@/types/linkId'
2020import { toRerouteId } from '@/types/rerouteId'
21+ import {
22+ createLGraphState ,
23+ mintGroupId ,
24+ mintNodeId ,
25+ mintRerouteId ,
26+ observeGroupId ,
27+ observeNodeId ,
28+ observeRerouteId
29+ } from './idAllocation'
30+ import type { LGraphState } from './idAllocation'
2131import { useLinkStore } from '@/stores/linkStore'
2232import { useNodeDataStore } from '@/stores/nodeDataStore'
2333import { useRerouteStore } from '@/stores/rerouteStore'
@@ -44,7 +54,6 @@ import type { DragAndScaleState } from './DragAndScale'
4454import { LGraphCanvas } from './LGraphCanvas'
4555import { Rectangle } from './infrastructure/Rectangle'
4656import { LGraphGroup } from './LGraphGroup'
47- import { toGroupId } from '@/types/groupId'
4857import {
4958 LGraphNode ,
5059 registerNodeState ,
@@ -144,22 +153,11 @@ function isLGraphTriggerAction(action: string): action is LGraphTriggerAction {
144153 return validTriggerActions . has ( action as LGraphTriggerAction )
145154}
146155
147- function nextNodeId ( state : LGraphState ) : NodeId {
148- return toNodeId ( ++ state . lastNodeId )
149- }
150-
151156function numericNodeId ( id : NodeId ) : number | null {
152157 const numericId = Number ( id )
153158 return Number . isInteger ( numericId ) ? numericId : null
154159}
155160
156- function syncLastNodeId ( state : LGraphState , id : NodeId ) : void {
157- const numericId = numericNodeId ( id )
158- if ( numericId !== null && state . lastNodeId < numericId ) {
159- state . lastNodeId = numericId
160- }
161- }
162-
163161export type RendererType = 'LG' | 'Vue' | 'Vue-corrected'
164162
165163/**
@@ -168,13 +166,7 @@ export type RendererType = 'LG' | 'Vue' | 'Vue-corrected'
168166 */
169167export type SubgraphId = UUID
170168
171- export interface LGraphState {
172- /** Counter, not an id — brand at the point a group is constructed. */
173- lastGroupId : number
174- lastNodeId : number
175- lastLinkId : LinkId
176- lastRerouteId : RerouteId
177- }
169+ export type { LGraphState } from './idAllocation'
178170
179171type ParamsArray < T , K extends MethodNames < T > > = Parameters <
180172 Extract < T [ K ] , ( ...args : never [ ] ) => unknown >
@@ -349,12 +341,7 @@ export class LGraph
349341 list_of_graphcanvas : LGraphCanvas [ ] | null
350342 status : number = LGraph . STATUS_STOPPED
351343
352- private _state : LGraphState = {
353- lastGroupId : 0 ,
354- lastNodeId : 0 ,
355- lastLinkId : toLinkId ( 0 ) ,
356- lastRerouteId : toRerouteId ( 0 )
357- }
344+ private _state : LGraphState = createLGraphState ( )
358345
359346 get state ( ) : LGraphState {
360347 return this . _state
@@ -516,12 +503,7 @@ export class LGraph
516503 this . id = zeroUuid
517504 this . revision = 0
518505
519- this . state = {
520- lastGroupId : 0 ,
521- lastNodeId : 0 ,
522- lastLinkId : toLinkId ( 0 ) ,
523- lastRerouteId : toRerouteId ( 0 )
524- }
506+ this . state = createLGraphState ( )
525507
526508 // used to detect changes
527509 this . _version = - 1
@@ -1078,9 +1060,8 @@ export class LGraph
10781060 // groups
10791061 if ( node instanceof LGraphGroup ) {
10801062 // Assign group ID
1081- if ( node . id == null || node . id === - 1 )
1082- node . id = toGroupId ( ++ state . lastGroupId )
1083- if ( node . id > state . lastGroupId ) state . lastGroupId = node . id
1063+ if ( node . id == null || node . id === - 1 ) node . id = mintGroupId ( state )
1064+ observeGroupId ( state , node . id )
10841065
10851066 this . _groups . push ( node )
10861067 this . setDirtyCanvas ( true )
@@ -1097,7 +1078,7 @@ export class LGraph
10971078 console . warn (
10981079 'LiteGraph: there is already a node with this ID, changing it'
10991080 )
1100- node . id = nextNodeId ( state )
1081+ node . id = mintNodeId ( state )
11011082 }
11021083
11031084 if ( this . _nodes . length >= LiteGraph . MAX_NUMBER_OF_NODES ) {
@@ -1106,9 +1087,9 @@ export class LGraph
11061087
11071088 // give him an id
11081089 if ( node . id == null || node . id === UNASSIGNED_NODE_ID ) {
1109- node . id = nextNodeId ( state )
1090+ node . id = mintNodeId ( state )
11101091 } else {
1111- syncLastNodeId ( state , node . id )
1092+ observeNodeId ( state , node . id )
11121093 }
11131094
11141095 // Set ghost flag before registration so the node state carries it
@@ -1627,12 +1608,8 @@ export class LGraph
16271608 floating
16281609 } : OptionalProps < SerialisableReroute , 'id' > ) : Reroute {
16291610 const rerouteId =
1630- id === undefined
1631- ? toRerouteId ( Number ( this . state . lastRerouteId ) + 1 )
1632- : toRerouteId ( id )
1633- if ( rerouteId > this . state . lastRerouteId ) {
1634- this . state . lastRerouteId = rerouteId
1635- }
1611+ id === undefined ? mintRerouteId ( this . state ) : toRerouteId ( id )
1612+ observeRerouteId ( this . state , rerouteId )
16361613
16371614 const existingReroute = this . reroutes . get ( rerouteId )
16381615 const reroute = existingReroute ?? new Reroute ( rerouteId , this , pos )
@@ -1655,8 +1632,7 @@ export class LGraph
16551632 if ( ! ( before instanceof LLink ) && ! ( before instanceof Reroute ) ) {
16561633 return
16571634 }
1658- const rerouteId = toRerouteId ( Number ( this . state . lastRerouteId ) + 1 )
1659- this . state . lastRerouteId = rerouteId
1635+ const rerouteId = mintRerouteId ( this . state )
16601636 const chainLinks =
16611637 before instanceof Reroute
16621638 ? [
@@ -2116,7 +2092,7 @@ export class LGraph
21162092 }
21172093 }
21182094
2119- const newNodeId = nextNodeId ( this . state )
2095+ const newNodeId = mintNodeId ( this . state )
21202096 nodeIdMap . set ( toNodeId ( n_info . id ) , newNodeId )
21212097 node . id = newNodeId
21222098 n_info . id = newNodeId
@@ -2219,7 +2195,7 @@ export class LGraph
22192195 // Shared definitions may survive, so unpacked groups need fresh layout
22202196 // ids, like the reroutes below.
22212197 for ( const groupInfo of groups ) {
2222- groupInfo . id = ++ this . rootGraph . state . lastGroupId
2198+ groupInfo . id = mintGroupId ( this . rootGraph . state )
22232199 const group = new LGraphGroup ( groupInfo . title , groupInfo . id )
22242200 this . add ( group , true )
22252201 group . configure ( groupInfo )
@@ -2291,8 +2267,7 @@ export class LGraph
22912267 const rerouteIdMap = new Map < RerouteId , RerouteId > ( )
22922268 const oldReroutes = subgraphNode . subgraph . reroutes
22932269 for ( const reroute of oldReroutes . values ( ) ) {
2294- const migratedId = toRerouteId ( Number ( this . state . lastRerouteId ) + 1 )
2295- this . state . lastRerouteId = migratedId
2270+ const migratedId = mintRerouteId ( this . state )
22962271 const migratedReroute = new Reroute ( migratedId , this , [
22972272 reroute . pos [ 0 ] + offsetX ,
22982273 reroute . pos [ 1 ] + offsetY
0 commit comments