Skip to content

Commit 3697830

Browse files
authored
fix: clear cache and update edges after renderPorts (#5036)
* fix: clear cache and update edges after renderPorts * fix: correct type
1 parent 591f862 commit 3697830

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

__tests__/view/node/index.spec.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('NodeView', () => {
119119

120120
describe('confirmUpdate', () => {
121121
beforeEach(() => {
122-
vi.spyOn(nodeView, 'hasAction').mockReturnValue(false)
122+
vi.spyOn(nodeView, 'hasAction').mockReturnValue(0)
123123
vi.spyOn(nodeView, 'removeAction').mockReturnValue(0)
124124
vi.spyOn(nodeView, 'handleAction').mockReturnValue(0)
125125
vi.spyOn(nodeView, 'getFlag').mockReturnValue(1)
@@ -137,7 +137,7 @@ describe('NodeView', () => {
137137

138138
it('should handle ports action', () => {
139139
vi.spyOn(nodeView, 'hasAction').mockImplementation(
140-
(flag: any, action: any) => action === 'ports',
140+
(flag: any, action: any) => (action === 'ports' ? 1 : 0),
141141
)
142142

143143
const result = nodeView.confirmUpdate(1)
@@ -149,7 +149,7 @@ describe('NodeView', () => {
149149

150150
it('should handle render action', () => {
151151
vi.spyOn(nodeView, 'hasAction').mockImplementation(
152-
(flag: any, action: any) => action === 'render',
152+
(flag: any, action: any) => (action === 'render' ? 1 : 0),
153153
)
154154

155155
const result = nodeView.confirmUpdate(1)
@@ -160,7 +160,7 @@ describe('NodeView', () => {
160160
})
161161

162162
it('should handle individual actions when not rendering', () => {
163-
vi.spyOn(nodeView, 'hasAction').mockReturnValue(false)
163+
vi.spyOn(nodeView, 'hasAction').mockReturnValue(0)
164164

165165
const result = nodeView.confirmUpdate(1)
166166

@@ -216,6 +216,31 @@ describe('NodeView', () => {
216216
})
217217
})
218218

219+
describe('ports and edges', () => {
220+
it('should request connected edge update after rendering ports', () => {
221+
const node2 = graph.addNode({
222+
x: 300,
223+
y: 100,
224+
width: 80,
225+
height: 40,
226+
markup: [{ tagName: 'rect', selector: 'body' }],
227+
})
228+
const edge = graph.addEdge({
229+
source: { cell: node.id },
230+
target: { cell: node2.id },
231+
})
232+
233+
const edgeView = edge.findView(graph) as any
234+
const requestSpy = vi
235+
.spyOn(graph.renderer as any, 'requestViewUpdate')
236+
.mockImplementation(() => {})
237+
vi.spyOn(graph.renderer as any, 'isViewMounted').mockReturnValue(true)
238+
;(nodeView as any).renderPorts()
239+
240+
expect(requestSpy).toHaveBeenCalledWith(edgeView, expect.any(Number))
241+
})
242+
})
243+
219244
describe('transform methods', () => {
220245
beforeEach(() => {
221246
vi.spyOn(nodeView, 'rotate').mockImplementation(() => {})
@@ -292,7 +317,6 @@ describe('NodeView', () => {
292317

293318
it('should clean ports cache', () => {
294319
;(nodeView as any).portsCache['port1'] = {} as any
295-
296320
;(nodeView as any).cleanPortsCache()
297321

298322
expect((nodeView as any).portsCache).toEqual({})
@@ -590,7 +614,6 @@ describe('NodeView', () => {
590614
vi.spyOn(node, 'getParent').mockReturnValue(null)
591615
// @ts-expect-error
592616
vi.spyOn(nodeView, 'notify').mockImplementation(() => {})
593-
594617
;(nodeView as any).prepareEmbedding(event)
595618

596619
expect(nodeView.notify).toHaveBeenCalledWith(
@@ -665,7 +688,6 @@ describe('NodeView', () => {
665688
vi.spyOn(nodeView, 'setEventData').mockImplementation(() => {})
666689
vi.spyOn(node, 'getPosition').mockReturnValue({ x: 100, y: 200 })
667690
vi.spyOn(nodeView as any, 'getRestrictArea').mockReturnValue(null)
668-
669691
;(nodeView as any).startNodeDragging(event, 150, 250)
670692

671693
expect(nodeView.setEventData).toHaveBeenCalled()
@@ -677,7 +699,6 @@ describe('NodeView', () => {
677699
vi.spyOn(nodeView as any, 'notifyUnhandledMouseDown').mockImplementation(
678700
() => {},
679701
)
680-
681702
;(nodeView as any).startNodeDragging(event, 150, 250)
682703

683704
expect((nodeView as any).notifyUnhandledMouseDown).toHaveBeenCalledWith(
@@ -694,7 +715,6 @@ describe('NodeView', () => {
694715
vi.spyOn(nodeView as any, 'notifyUnhandledMouseDown').mockImplementation(
695716
() => {},
696717
)
697-
698718
;(nodeView as any).startNodeDragging(event, 150, 250)
699719

700720
expect((nodeView as any).notifyUnhandledMouseDown).toHaveBeenCalledWith(
@@ -719,7 +739,6 @@ describe('NodeView', () => {
719739
vi.spyOn(graph, 'getGridSize').mockReturnValue(10)
720740
vi.spyOn(node, 'setPosition').mockImplementation(() => {})
721741
vi.spyOn(graph.options.embedding, 'enabled', 'get').mockReturnValue(false)
722-
723742
;(nodeView as any).dragNode(event, 150, 250)
724743

725744
expect(nodeView.addClass).toHaveBeenCalledWith('node-moving')
@@ -740,7 +759,6 @@ describe('NodeView', () => {
740759
autoScroll: vi.fn(),
741760
}
742761
vi.spyOn(graph, 'getPlugin').mockReturnValue(mockScroller)
743-
744762
;(nodeView as any).autoScrollGraph(100, 200)
745763

746764
expect(mockScroller.autoScroll).toHaveBeenCalledWith(100, 200)
@@ -760,8 +778,7 @@ describe('NodeView', () => {
760778
const event = { type: 'mousemove' } as any
761779

762780
vi.spyOn(graph, 'getPlugin').mockReturnValue(mockSelection)
763-
vi.spyOn(nodeView, 'notify').mockImplementation(() => {})
764-
781+
vi.spyOn(nodeView, 'notify').mockImplementation(() => nodeView)
765782
;(nodeView as any).notifyNodeMove('node:move', event, 100, 200, node)
766783

767784
expect(nodeView.notify).toHaveBeenCalledWith(
@@ -847,7 +864,7 @@ describe('NodeView', () => {
847864
mockCandidate,
848865
])
849866
vi.spyOn(nodeView, 'clearEmbedding').mockImplementation(() => {})
850-
vi.spyOn(nodeView, 'notify').mockImplementation(() => {})
867+
vi.spyOn(nodeView, 'notify').mockImplementation(() => nodeView)
851868
vi.spyOn(graph, 'snapToGrid').mockReturnValue({ x: 150, y: 250 })
852869
vi.spyOn(graph, 'findViewByCell').mockReturnValue(nodeView)
853870
vi.spyOn(node, 'getParent').mockReturnValue(null)

src/view/node/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,32 @@ export class NodeView<
311311
})
312312

313313
this.updatePorts()
314+
this.cleanCache()
315+
this.updateConnectedEdges()
316+
}
317+
318+
protected updateConnectedEdges() {
319+
const graph = this.graph
320+
const node = this.cell
321+
const edges = graph.model.getConnectedEdges(node)
322+
for (let i = 0, n = edges.length; i < n; i += 1) {
323+
const edge = edges[i]
324+
const edgeView = edge.findView(graph) as EdgeView
325+
if (!edgeView || !graph.renderer.isViewMounted(edgeView)) {
326+
continue
327+
}
328+
const actions = ['update']
329+
if (edge.getSourceCell() === node) {
330+
actions.push('source')
331+
}
332+
if (edge.getTargetCell() === node) {
333+
actions.push('target')
334+
}
335+
graph.renderer.requestViewUpdate(
336+
edgeView,
337+
edgeView.getFlag(actions as any),
338+
)
339+
}
314340
}
315341

316342
protected appendPorts(ports: Port[], zIndex: number, refs: Element[]) {

0 commit comments

Comments
 (0)