This commit is contained in:
2026-03-13 00:19:14 +01:00
parent 2ff7e1f5f2
commit 2d8f13aebb
5 changed files with 58 additions and 39 deletions

View File

@@ -4,7 +4,7 @@
* - **AbstractNodeProps<TData>** — Typed props (id, data, width?, height?, selected?) for your node.
* - **useAbstractNode(id, data)** — Flow context plus helpers: nodes, edges, setNodes, setEdges,
* updateData(partial), incomingEdges, outgoingEdges, sourceIds, targetIds. Calling updateData()
* also reports this node as a trigger for connection path (lifecycle "trigger").
* also marks this node as a connection-path trigger so edges update on data changes.
* - **createAbstractNodeComponent(displayName, Component)** — Wraps with memo + nodePropsAreEqual.
*
* **Node lifecycle / connection status:** Nodes that can be updating, paused, or in error should
@@ -15,7 +15,7 @@
* component, then export const MyNode = createAbstractNodeComponent('MyNode', MyNodeComponent).
*/
import React, { useCallback, useContext, useMemo } from 'react'
import React, { useCallback, useContext, useMemo, useRef } from 'react'
import { GraphContext, ConnectionPathContext } from './flowContext'
import { nodePropsAreEqual } from './flowUtils'
import type { AppNode } from './nodeTypes'
@@ -82,12 +82,13 @@ export function useAbstractNode<TData = Record<string, unknown>>(
): AbstractNodeContext<TData> {
const graphCtx = useContext(GraphContext)
const pathCtx = useContext(ConnectionPathContext)
const addTriggerRef = useRef(pathCtx?.addConnectionPathTrigger)
addTriggerRef.current = pathCtx?.addConnectionPathTrigger
const nodes = graphCtx?.graphRef?.current?.nodes ?? []
const edges = graphCtx?.edges ?? []
const setNodes = graphCtx?.setNodes
const setEdges = graphCtx?.setEdges
const addConnectionPathTrigger = pathCtx?.addConnectionPathTrigger
const updateData = useCallback(
(partial: Partial<TData>) => {
if (!setNodes) return
@@ -96,9 +97,9 @@ export function useAbstractNode<TData = Record<string, unknown>>(
n.id === id ? { ...n, data: { ...(n.data as object), ...partial } } : n
) as AppNode[]
)
addConnectionPathTrigger?.(id)
addTriggerRef.current?.(id)
},
[id, setNodes, addConnectionPathTrigger]
[id, setNodes]
)
const incomingEdges = useMemo(

View File

@@ -54,36 +54,39 @@ export function useSyncConnectionStatus(
state: NodeConnectionStatusState
): void {
const ctx = useContext(ConnectionPathContext)
const ctxRef = useRef(ctx)
ctxRef.current = ctx
const { updating, error, paused } = state
const prevRef = useRef({ updating: false, error: false, paused: false })
useEffect(() => {
const pathCtx = ctxRef.current
const prev = prevRef.current
const nowUpdating = Boolean(updating)
const nowError = Boolean(error)
const nowPaused = Boolean(paused)
if (prev.updating !== nowUpdating) {
if (nowUpdating) ctx?.startConnectionPathUpdate?.(nodeId)
else ctx?.endConnectionPathUpdate?.(nodeId)
if (nowUpdating) pathCtx?.startConnectionPathUpdate?.(nodeId)
else pathCtx?.endConnectionPathUpdate?.(nodeId)
prev.updating = nowUpdating
}
if (prev.error !== nowError) {
if (nowError) ctx?.addConnectionPathError?.(nodeId)
else ctx?.removeConnectionPathError?.(nodeId)
if (nowError) pathCtx?.addConnectionPathError?.(nodeId)
else pathCtx?.removeConnectionPathError?.(nodeId)
prev.error = nowError
}
if (prev.paused !== nowPaused) {
if (nowPaused) ctx?.addConnectionPathPausedNode?.(nodeId)
else ctx?.removeConnectionPathPausedNode?.(nodeId)
if (nowPaused) pathCtx?.addConnectionPathPausedNode?.(nodeId)
else pathCtx?.removeConnectionPathPausedNode?.(nodeId)
prev.paused = nowPaused
}
return () => {
if (prevRef.current.updating) ctx?.endConnectionPathUpdate?.(nodeId)
if (prevRef.current.error) ctx?.removeConnectionPathError?.(nodeId)
if (prevRef.current.paused) ctx?.removeConnectionPathPausedNode?.(nodeId)
if (prevRef.current.updating) pathCtx?.endConnectionPathUpdate?.(nodeId)
if (prevRef.current.error) pathCtx?.removeConnectionPathError?.(nodeId)
if (prevRef.current.paused) pathCtx?.removeConnectionPathPausedNode?.(nodeId)
prevRef.current = { updating: false, error: false, paused: false }
}
}, [nodeId, updating, error, paused, ctx])
}, [nodeId, updating, error, paused])
}