Skip to content

Commit 0ae7502

Browse files
committed
examples: add example for running a process tree
1 parent 6dd9514 commit 0ae7502

File tree

4 files changed

+239
-56
lines changed

4 files changed

+239
-56
lines changed

examples/vite/src/Layouting/LayoutingExample.vue

+51-29
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,79 @@
11
<script lang="ts" setup>
22
import dagre from 'dagre'
3-
import type { CoordinateExtent, Elements } from '@vue-flow/core'
4-
import { ConnectionMode, Panel, Position, VueFlow, isNode } from '@vue-flow/core'
3+
import { ConnectionMode, Panel, Position, VueFlow, useVueFlow } from '@vue-flow/core'
54
import { Controls } from '@vue-flow/controls'
65
76
import '@vue-flow/controls/dist/style.css'
87
9-
import initialElements from './initial-elements'
8+
import { initialEdges, initialNodes } from './initial-elements'
9+
import { useRunProcess } from './useRunProcess'
10+
import ProcessNode from './ProcessNode.vue'
1011
11-
const dagreGraph = new dagre.graphlib.Graph()
12+
const nodes = ref(initialNodes)
1213
13-
dagreGraph.setDefaultEdgeLabel(() => ({}))
14+
const edges = ref(initialEdges)
1415
15-
const nodeExtent: CoordinateExtent = [
16-
[0, -100],
17-
[1000, 500],
18-
]
16+
// we create a new graph instance, in case some nodes/edges were removed, otherwise dagre would act as if they were still there
17+
const dagreGraph = ref(new dagre.graphlib.Graph())
1918
20-
const elements = ref<Elements>(initialElements)
19+
dagreGraph.value.setDefaultEdgeLabel(() => ({}))
20+
21+
const { run } = useRunProcess()
22+
23+
const { findNode, fitView } = useVueFlow()
24+
25+
function handleLayout(direction: 'TB' | 'LR') {
26+
dagreGraph.value = new dagre.graphlib.Graph()
27+
dagreGraph.value.setDefaultEdgeLabel(() => ({}))
2128
22-
function onLayout(direction: string) {
2329
const isHorizontal = direction === 'LR'
24-
dagreGraph.setGraph({ rankdir: direction })
30+
dagreGraph.value.setGraph({ rankdir: direction })
2531
26-
elements.value.forEach((el) => {
27-
if (isNode(el)) {
28-
dagreGraph.setNode(el.id, { width: 150, height: 50 })
29-
} else {
30-
dagreGraph.setEdge(el.source, el.target)
31-
}
32-
})
32+
for (const node of nodes.value) {
33+
// if you need width+height of nodes for your layout, you can use the dimensions property of the internal node (`GraphNode` type)
34+
const graphNode = findNode(node.id)!
35+
36+
dagreGraph.value.setNode(node.id, { width: graphNode.dimensions.width || 150, height: graphNode.dimensions.height || 50 })
37+
}
3338
34-
dagre.layout(dagreGraph)
39+
for (const edge of edges.value) {
40+
dagreGraph.value.setEdge(edge.source, edge.target)
41+
}
3542
36-
elements.value.forEach((el) => {
37-
if (isNode(el)) {
38-
const nodeWithPosition = dagreGraph.node(el.id)
39-
el.targetPosition = isHorizontal ? Position.Left : Position.Top
40-
el.sourcePosition = isHorizontal ? Position.Right : Position.Bottom
41-
el.position = { x: nodeWithPosition.x, y: nodeWithPosition.y }
43+
dagre.layout(dagreGraph.value)
44+
45+
// set nodes with updated positions
46+
nodes.value = nodes.value.map((node) => {
47+
const nodeWithPosition = dagreGraph.value.node(node.id)
48+
49+
return {
50+
...node,
51+
targetPosition: isHorizontal ? Position.Left : Position.Top,
52+
sourcePosition: isHorizontal ? Position.Right : Position.Bottom,
53+
position: { x: nodeWithPosition.x, y: nodeWithPosition.y },
4254
}
4355
})
56+
57+
nextTick(() => {
58+
fitView()
59+
})
4460
}
4561
</script>
4662

4763
<template>
4864
<div class="layoutflow">
49-
<VueFlow v-model="elements" :node-extent="nodeExtent" :connection-mode="ConnectionMode.Loose" @pane-ready="onLayout('TB')">
65+
<VueFlow :nodes="nodes" :edges="edges" :connection-mode="ConnectionMode.Loose" @nodes-initialized="handleLayout('TB')">
66+
<template #node-process="props">
67+
<ProcessNode v-bind="props" />
68+
</template>
69+
5070
<Controls />
5171

5272
<Panel style="display: flex; gap: 10px" position="top-right">
53-
<button :style="{ marginRight: 10 }" @click="onLayout('TB')">vertical layout</button>
54-
<button @click="onLayout('LR')">horizontal layout</button>
73+
<button @click="handleLayout('TB')">vertical layout</button>
74+
<button @click="handleLayout('LR')">horizontal layout</button>
75+
76+
<button @click="run(nodes, dagreGraph)">Run</button>
5577
</Panel>
5678
</VueFlow>
5779
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<script setup lang="ts">
2+
import type { NodeProps } from '@vue-flow/core'
3+
import { Handle } from '@vue-flow/core'
4+
5+
const props = defineProps<NodeProps>()
6+
7+
const bgColor = toRef(() => {
8+
if (props.data.hasError) {
9+
return '#f87171'
10+
}
11+
12+
if (props.data.isFinished) {
13+
return '#10b981'
14+
}
15+
16+
if (props.data.isRunning || props.data.isSkipped) {
17+
return '#6b7280'
18+
}
19+
20+
return '#1a192b'
21+
})
22+
</script>
23+
24+
<template>
25+
<div class="process-node" :style="{ backgroundColor: bgColor }">
26+
<Handle type="target" :position="targetPosition" />
27+
<Handle type="source" :position="sourcePosition" />
28+
29+
<div style="display: flex; align-items: center; gap: 8px">
30+
<div v-if="data.isRunning" class="spinner" />
31+
<span v-else-if="data.hasError">&#x274C;</span>
32+
<span v-else-if="data.isSkipped">&#x1F6A7;</span>
33+
<span v-else>&#x1F4E6;</span>
34+
</div>
35+
</div>
36+
</template>
37+
38+
<style scoped>
39+
.process-node {
40+
padding: 10px;
41+
color: white;
42+
border: 1px solid #1a192b;
43+
border-radius: 99px;
44+
font-size: 10px;
45+
width: 15px;
46+
height: 15px;
47+
display: flex;
48+
align-items: center;
49+
justify-content: center;
50+
}
51+
52+
.spinner {
53+
border: 2px solid #f3f3f3;
54+
border-top: 2px solid #3498db;
55+
border-radius: 50%;
56+
width: 8px;
57+
height: 8px;
58+
animation: spin 1s linear infinite;
59+
}
60+
61+
@keyframes spin {
62+
0% {
63+
transform: rotate(0deg);
64+
}
65+
100% {
66+
transform: rotate(360deg);
67+
}
68+
}
69+
</style>
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,75 @@
1-
import type { Elements, XYPosition } from '@vue-flow/core'
1+
import type { Edge, Node } from '@vue-flow/core'
22

3-
const position: XYPosition = { x: 0, y: 0 }
3+
const position = { x: 0, y: 0 }
4+
const type: string = 'process'
45

5-
const elements: Elements = [
6+
export const initialNodes: Node[] = [
67
{
78
id: '1',
8-
type: 'input',
9-
label: 'input',
9+
label: 'Start',
1010
position,
11+
type,
1112
},
1213
{
1314
id: '2',
14-
label: 'node 2',
1515
position,
16+
type,
1617
},
1718
{
1819
id: '2a',
19-
label: 'node 2a',
2020
position,
21+
type,
2122
},
2223
{
2324
id: '2b',
24-
label: 'node 2b',
2525
position,
26+
type,
2627
},
2728
{
2829
id: '2c',
29-
label: 'node 2c',
3030
position,
31+
type,
3132
},
3233
{
3334
id: '2d',
34-
label: 'node 2d',
3535
position,
36+
type,
3637
},
3738
{
3839
id: '3',
39-
label: 'node 3',
4040
position,
41+
type,
4142
},
4243
{
4344
id: '4',
44-
label: 'node 4',
4545
position,
46+
type,
4647
},
4748
{
4849
id: '5',
49-
label: 'node 5',
5050
position,
51+
type,
5152
},
5253
{
5354
id: '6',
54-
type: 'output',
55-
label: 'output',
5655
position,
56+
type,
57+
},
58+
{
59+
id: '7',
60+
position,
61+
type,
5762
},
58-
{ id: '7', type: 'output', label: 'output', position: { x: 400, y: 450 } },
59-
{ id: 'e12', source: '1', target: '2', type: 'smoothstep', animated: true },
60-
{ id: 'e13', source: '1', target: '3', type: 'smoothstep', animated: true },
61-
{ id: 'e22a', source: '2', target: '2a', type: 'smoothstep', animated: true },
62-
{ id: 'e22b', source: '2', target: '2b', type: 'smoothstep', animated: true },
63-
{ id: 'e22c', source: '2', target: '2c', type: 'smoothstep', animated: true },
64-
{ id: 'e2c2d', source: '2c', target: '2d', type: 'smoothstep', animated: true },
65-
66-
{ id: 'e45', source: '4', target: '5', type: 'smoothstep', animated: true },
67-
{ id: 'e56', source: '5', target: '6', type: 'smoothstep', animated: true },
68-
{ id: 'e57', source: '5', target: '7', type: 'smoothstep', animated: true },
6963
]
7064

71-
export default elements
65+
export const initialEdges: Edge[] = [
66+
{ id: 'e1-2', source: '1', target: '2', type: 'smoothstep', animated: true },
67+
{ id: 'e1-3', source: '1', target: '3', type: 'smoothstep', animated: true },
68+
{ id: 'e2-2a', source: '2', target: '2a', type: 'smoothstep', animated: true },
69+
{ id: 'e2-2b', source: '2', target: '2b', type: 'smoothstep', animated: true },
70+
{ id: 'e2-2c', source: '2', target: '2c', type: 'smoothstep', animated: true },
71+
{ id: 'e2c-2d', source: '2c', target: '2d', type: 'smoothstep', animated: true },
72+
{ id: 'e4-5', source: '4', target: '5', type: 'smoothstep', animated: true },
73+
{ id: 'e5-6', source: '5', target: '6', type: 'smoothstep', animated: true },
74+
{ id: 'e5-7', source: '5', target: '7', type: 'smoothstep', animated: true },
75+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import type { Node } from '@vue-flow/core'
2+
import { useVueFlow } from '@vue-flow/core'
3+
4+
/**
5+
* Composable to simulate running a process tree.
6+
*
7+
* It loops through each node, pretends to run an async process, and updates the node's data indicating whether the process has finished.
8+
* When one node finishes, the next one starts.
9+
*
10+
* When a node has multiple descendants, it will run them in parallel.
11+
*/
12+
export function useRunProcess() {
13+
const { updateNodeData } = useVueFlow()
14+
15+
const running = ref(false)
16+
const executedNodes = new Set<string>()
17+
18+
async function runNode(node: { id: string }, dagreGraph: dagre.graphlib.Graph) {
19+
if (executedNodes.has(node.id)) {
20+
return
21+
}
22+
23+
executedNodes.add(node.id)
24+
25+
updateNodeData(node.id, { isRunning: true, isFinished: false, hasError: false })
26+
27+
// Simulate an async process with a random timeout between 1 and 3 seconds
28+
const delay = Math.floor(Math.random() * 2000) + 1000
29+
await new Promise((resolve) => setTimeout(resolve, delay))
30+
31+
const children = dagreGraph.successors(node.id) as unknown as string[]
32+
33+
// Randomly decide whether the node will throw an error
34+
const willThrowError = Math.random() < 0.15
35+
36+
if (willThrowError) {
37+
updateNodeData(node.id, { isRunning: false, hasError: true })
38+
39+
await skipDescendants(node.id, dagreGraph)
40+
return
41+
}
42+
43+
updateNodeData(node.id, { isRunning: false, isFinished: true })
44+
45+
// Run the process on the children in parallel
46+
await Promise.all(
47+
children.map((id) => {
48+
return runNode({ id }, dagreGraph)
49+
}),
50+
)
51+
}
52+
53+
async function run(nodes: Node[], dagreGraph: dagre.graphlib.Graph) {
54+
if (running.value) {
55+
return
56+
}
57+
58+
reset(nodes)
59+
60+
running.value = true
61+
62+
// Get all starting nodes (nodes with no predecessors)
63+
const startingNodes = nodes.filter((node) => dagreGraph.predecessors(node.id)?.length === 0)
64+
65+
// Run the process on all starting nodes in parallel
66+
await Promise.all(startingNodes.map((node) => runNode(node, dagreGraph)))
67+
68+
running.value = false
69+
executedNodes.clear()
70+
}
71+
72+
function reset(nodes: Node[]) {
73+
for (const node of nodes) {
74+
updateNodeData(node.id, { isRunning: false, isFinished: false, hasError: false, isSkipped: false })
75+
}
76+
}
77+
78+
async function skipDescendants(nodeId: string, dagreGraph: dagre.graphlib.Graph) {
79+
const children = dagreGraph.successors(nodeId) as unknown as string[]
80+
81+
for (const child of children) {
82+
updateNodeData(child, { isRunning: false, isSkipped: true })
83+
await skipDescendants(child, dagreGraph)
84+
}
85+
}
86+
87+
return { run, running }
88+
}

0 commit comments

Comments
 (0)