Skip to content

Commit 1d19a63

Browse files
committed
Sort sibling nodes again when a delta with an updated workflow is received
1 parent dd1c293 commit 1d19a63

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/components/cylc/gscan/index.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,45 @@ function updateWorkflow (workflow, gscan, options) {
127127
throw new Error(`Updated node [${workflow.id}] not found in workflow lookup`)
128128
}
129129
mergeWith(existingData.node, workflow, mergeWithCustomizer)
130-
Vue.set(gscan.lookup, existingData.id, existingData)
131-
// FIXME: we need to sort its parent again!
130+
const hierarchical = options.hierarchical || true
131+
if (hierarchical) {
132+
updateHierarchicalWorkflow(existingData, gscan.lookup, gscan.tree, options)
133+
}
132134
// TODO: create workflow hierarchy (from workflow object), then iterate
133135
// it and use lookup to fetch the existing node. Finally, combine
134136
// the gscan states (latestStateTasks & stateTotals).
137+
Vue.set(gscan.lookup, existingData.id, existingData)
138+
}
139+
140+
function updateHierarchicalWorkflow (existingData, lookup, tree, options) {
141+
// We need to sort its parent again.
142+
const workflowNameParts = parseWorkflowNameParts(existingData.id)
143+
const nodesIds = getWorkflowNamePartsNodesIds(workflowNameParts)
144+
// Discard the last since it's the workflow ID that we already have
145+
// in the `existingData` object. Now if not empty, we have our parent.
146+
nodesIds.pop()
147+
const parentId = nodesIds.length > 0 ? nodesIds.pop() : null
148+
const parent = parentId ? lookup[parentId] : tree
149+
if (!parent) {
150+
throw new Error(`Invalid orphan hierarchical node: ${existingData.id}`)
151+
}
152+
const siblings = parent.children
153+
// Where is this node at the moment?
154+
const currentIndex = siblings.findIndex(node => node.id === existingData.id)
155+
// Where should it be now?
156+
const sortedIndex = sortedIndexBy(
157+
parent.children,
158+
existingData,
159+
(n) => n.name,
160+
sortWorkflowNamePartNodeOrWorkflowNode
161+
)
162+
// If it is not where it is, we need to add it to its correct location.
163+
if (currentIndex !== sortedIndex) {
164+
// siblings.splice(currentIndex, 1)
165+
// siblings.splice(sortedIndex, 0, existingData)
166+
Vue.delete(siblings, currentIndex)
167+
Vue.set(siblings, sortedIndex, existingData)
168+
}
135169
}
136170

137171
// -- Pruned

src/components/cylc/gscan/nodes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ function getWorkflowNamePartsNodesIds (workflowNameParts) {
148148
return prefix
149149
})
150150
nodesIds.push(workflowNameParts.workflowId)
151+
return nodesIds
151152
}
152153

153154
/**

0 commit comments

Comments
 (0)