(
set: (fn: (state: SceneState) => Partial<SceneState>) => void,
get: () => SceneState,
changes: { create?: NodeCreateOp[]; update?: NodeUpdateOp[]; delete?: NodeDeleteOp[] },
)
| 761 | } |
| 762 | |
| 763 | export const applyNodeChangesAction = ( |
| 764 | set: (fn: (state: SceneState) => Partial<SceneState>) => void, |
| 765 | get: () => SceneState, |
| 766 | changes: { create?: NodeCreateOp[]; update?: NodeUpdateOp[]; delete?: NodeDeleteOp[] }, |
| 767 | ) => { |
| 768 | if (get().readOnly) return |
| 769 | |
| 770 | const createOps = changes.create ?? [] |
| 771 | const updateOps = changes.update ?? [] |
| 772 | const deleteOps = changes.delete ?? [] |
| 773 | const nodesToMarkDirty = new Set<AnyNodeId>() |
| 774 | const parentsToMarkDirty = new Set<AnyNodeId>() |
| 775 | |
| 776 | set((state) => { |
| 777 | const nextNodes = { ...state.nodes } |
| 778 | const nextCollections = { ...state.collections } |
| 779 | const nextRootIds = [...state.rootNodeIds] |
| 780 | let resolvedRootIds = nextRootIds |
| 781 | |
| 782 | for (const { id, data } of updateOps) { |
| 783 | const currentNode = nextNodes[id] |
| 784 | if (!currentNode) continue |
| 785 | const updatedNode = parseUpdatedNode(currentNode, data) |
| 786 | |
| 787 | if (data.parentId !== undefined && data.parentId !== currentNode.parentId) { |
| 788 | const oldParentId = currentNode.parentId as AnyNodeId | null |
| 789 | if (oldParentId && nextNodes[oldParentId]) { |
| 790 | const oldParent = nextNodes[oldParentId] as AnyContainerNode |
| 791 | nextNodes[oldParent.id] = { |
| 792 | ...oldParent, |
| 793 | children: oldParent.children.filter((childId) => childId !== id), |
| 794 | } as AnyNode |
| 795 | parentsToMarkDirty.add(oldParent.id) |
| 796 | } |
| 797 | |
| 798 | const newParentId = data.parentId as AnyNodeId | null |
| 799 | if (newParentId && nextNodes[newParentId]) { |
| 800 | const newParent = nextNodes[newParentId] as AnyContainerNode |
| 801 | nextNodes[newParent.id] = { |
| 802 | ...newParent, |
| 803 | children: Array.from(new Set([...newParent.children, id])), |
| 804 | } as AnyNode |
| 805 | parentsToMarkDirty.add(newParent.id) |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | nextNodes[id] = updatedNode |
| 810 | nodesToMarkDirty.add(id) |
| 811 | } |
| 812 | |
| 813 | for (const { node, parentId } of createOps) { |
| 814 | const effectiveParentId = parentId ?? (node.parentId as AnyNodeId | null) ?? null |
| 815 | const newNode = parseCreatedNode(node, effectiveParentId) |
| 816 | |
| 817 | nextNodes[newNode.id as AnyNodeId] = newNode |
| 818 | nodesToMarkDirty.add(newNode.id as AnyNodeId) |
| 819 | |
| 820 | if (effectiveParentId && nextNodes[effectiveParentId]) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…