Skip to content

Commit 92154b7

Browse files
committed
feat: enhance PortComponent with memoization and improve code formatting in store files
1 parent d509f19 commit 92154b7

4 files changed

Lines changed: 38 additions & 36 deletions

File tree

apps/chaingraph-frontend/src/components/flow/nodes/ChaingraphNode/PortComponent.tsx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import type {
2222
import type {
2323
PortContextValue,
2424
} from '@/components/flow/nodes/ChaingraphNode/ports/context/PortContext'
25-
import { memo } from 'react'
2625
import { AnyPort } from 'components/flow/nodes/ChaingraphNode/ports/AnyPort/AnyPort'
26+
import { memo } from 'react'
2727
import { BooleanPort } from '@/components/flow/nodes/ChaingraphNode/ports/BooleanPort/BooleanPort'
2828
import { NumberPort } from '@/components/flow/nodes/ChaingraphNode/ports/NumberPort/NumberPort'
2929

@@ -46,6 +46,35 @@ export interface PortProps {
4646
context: PortContextValue
4747
}
4848

49+
/**
50+
* Memoized PortComponent with custom comparison
51+
* Only re-renders when port data actually changes, not on parent node position updates
52+
*/
53+
export const PortComponent = memo(PortComponentInner, (prev, next) => {
54+
// Different port ID = must re-render
55+
if (prev.port.id !== next.port.id) {
56+
return false
57+
}
58+
59+
// Different node = must re-render
60+
if (prev.node.id !== next.node.id) {
61+
return false
62+
}
63+
64+
// Node version changed = port data might have changed
65+
if (prev.node.getVersion() !== next.node.getVersion()) {
66+
return false
67+
}
68+
69+
// Context reference changed (operations are stable, but check anyway)
70+
if (prev.context !== next.context) {
71+
return false
72+
}
73+
74+
// All checks passed - skip re-render
75+
return true
76+
})
77+
4978
function PortComponentInner(props: PortProps) {
5079
const {
5180
node,
@@ -118,32 +147,3 @@ function PortComponentInner(props: PortProps) {
118147
}
119148
}
120149
}
121-
122-
/**
123-
* Memoized PortComponent with custom comparison
124-
* Only re-renders when port data actually changes, not on parent node position updates
125-
*/
126-
export const PortComponent = memo(PortComponentInner, (prev, next) => {
127-
// Different port ID = must re-render
128-
if (prev.port.id !== next.port.id) {
129-
return false
130-
}
131-
132-
// Different node = must re-render
133-
if (prev.node.id !== next.node.id) {
134-
return false
135-
}
136-
137-
// Node version changed = port data might have changed
138-
if (prev.node.getVersion() !== next.node.getVersion()) {
139-
return false
140-
}
141-
142-
// Context reference changed (operations are stable, but check anyway)
143-
if (prev.context !== next.context) {
144-
return false
145-
}
146-
147-
// All checks passed - skip re-render
148-
return true
149-
})

apps/chaingraph-frontend/src/store/nodes/computed/port-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export const $nodePortContexts = combine(
186186

187187
// Iterate over ALL nodes, not just those in portEdgesMap
188188
Object.keys(nodes).forEach((nodeId) => {
189-
const nodePortEdges = portEdgesMap[nodeId] || {} // Empty object if no edges
189+
const nodePortEdges = portEdgesMap[nodeId] || {} // Empty object if no edges
190190

191191
// Get or create operations for this node
192192
let operations = operationsCache.get(nodeId)

apps/chaingraph-frontend/src/store/nodes/computed/port-edges.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import type { EdgeData } from '@/store/edges/types'
1010
import { combine } from 'effector'
1111
import { $edges } from '@/store/edges'
12-
import { $nodes } from '../stores'
1312

1413
export interface PortEdgesMap {
1514
[portId: string]: EdgeData[]

apps/chaingraph-frontend/src/store/nodes/stores.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ export const $nodePositions = nodesDomain.createStore<Record<string, Position>>(
382382
.on(updateNodePositionOnly, (state, { nodeId, position }) => {
383383
const current = state[nodeId]
384384
if (current?.x === position.x && current?.y === position.y) {
385-
return state // Same reference - no cascade
385+
return state // Same reference - no cascade
386386
}
387387
return { ...state, [nodeId]: position }
388388
})
@@ -413,7 +413,8 @@ export const $nodePositions = nodesDomain.createStore<Record<string, Position>>(
413413
// Node CRUD operations - sync initial positions
414414
.on(addNode, (state, node) => {
415415
const position = node.metadata.ui?.position
416-
if (!position) return state
416+
if (!position)
417+
return state
417418
return { ...state, [node.id]: position }
418419
})
419420
.on(addNodes, (state, nodes) => {
@@ -428,7 +429,8 @@ export const $nodePositions = nodesDomain.createStore<Record<string, Position>>(
428429
})
429430
.on(updateNode, (state, node) => {
430431
const position = node.metadata.ui?.position
431-
if (!position) return state
432+
if (!position)
433+
return state
432434
const current = state[node.id]
433435
if (current?.x === position.x && current?.y === position.y) {
434436
return state
@@ -437,7 +439,8 @@ export const $nodePositions = nodesDomain.createStore<Record<string, Position>>(
437439
})
438440
// UI updates that include position changes
439441
.on(updateNodeUILocal, (state, { nodeId, ui }) => {
440-
if (!ui?.position) return state
442+
if (!ui?.position)
443+
return state
441444
const current = state[nodeId]
442445
if (current?.x === ui.position.x && current?.y === ui.position.y) {
443446
return state

0 commit comments

Comments
 (0)