14
14
* You should have received a copy of the GNU General Public License
15
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
+ import Vue from 'vue'
18
+ import { mergeWith } from 'lodash'
19
+ import { sortedIndexBy } from '@/components/cylc/common/sort'
20
+ import { mergeWithCustomizer } from '@/components/cylc/common/merge'
21
+ import { sortWorkflowNamePartNodeOrWorkflowNode } from '@/components/cylc/gscan/sort'
22
+ import { createWorkflowNode } from '@/components/cylc/gscan/nodes'
17
23
18
24
/**
19
25
* @typedef {Object } GScan
25
31
* @typedef {Object<String, TreeNode> } Lookup
26
32
*/
27
33
28
- import { sortedIndexBy } from '@/components/cylc/common/sort'
29
- import { sortWorkflowNamePartNodeOrWorkflowNode } from '@/components/cylc/gscan/sort'
30
-
31
34
/**
32
35
* @param {TreeNode } workflow
33
36
* @param {GScan } gscan
@@ -36,18 +39,24 @@ import { sortWorkflowNamePartNodeOrWorkflowNode } from '@/components/cylc/gscan/
36
39
function addWorkflow ( workflow , gscan , options ) {
37
40
const hierarchical = options . hierarchical || true
38
41
if ( hierarchical ) {
39
- addHierarchicalWorkflow ( workflow , gscan . lookup , gscan . tree , options )
42
+ const workflowNode = createWorkflowNode ( workflow , hierarchical )
43
+ addHierarchicalWorkflow ( workflowNode , gscan . lookup , gscan . tree , options )
40
44
} else {
41
45
gscan . lookup [ workflow . id ] = workflow
42
46
gscan . tree . push ( workflow )
43
47
}
44
48
}
45
49
46
50
/**
51
+ * This function is private. It receives a lookup and tree instead of a GScan object (as in other
52
+ * functions of this module). This is required as we apply recursion for adding nodes into the tree,
53
+ * but we replace the tree and pass only a sub-tree.
54
+ *
47
55
* @param workflow
48
56
* @param {Lookup } lookup
49
57
* @param {Array<TreeNode> } tree
50
58
* @param {* } options
59
+ * @private
51
60
*/
52
61
function addHierarchicalWorkflow ( workflow , lookup , tree , options ) {
53
62
if ( ! lookup [ workflow . id ] ) {
@@ -78,24 +87,34 @@ function addHierarchicalWorkflow (workflow, lookup, tree, options) {
78
87
// we will have to merge the hierarchies
79
88
const existingNode = lookup [ workflow . id ]
80
89
// TODO: combine states summaries?
81
- // Copy array since we will iterate it, and modify existingNode.children
82
90
if ( existingNode . children ) {
91
+ // Copy array since we will iterate it, and modify existingNode.children
83
92
const children = [ ...workflow . children ]
84
93
for ( const child of children ) {
85
94
// Recursion
86
95
addHierarchicalWorkflow ( child , lookup , existingNode . children , options )
87
96
}
97
+ } else {
98
+ // Here we have an existing workflow node. Let's merge it.
99
+ mergeWith ( existingNode , workflow , mergeWithCustomizer )
88
100
}
89
101
}
90
102
}
91
103
92
104
/**
93
- * @param {TreeNode } workflow
105
+ * @param {WorkflowGraphQLData } workflow
94
106
* @param {GScan } gscan
95
107
* @param {* } options
96
108
*/
97
109
function updateWorkflow ( workflow , gscan , options ) {
98
-
110
+ // We don't care whether it is hierarchical or not here, since we can quickly
111
+ // access the node via the GScan lookup.
112
+ const existingData = gscan . lookup [ workflow . id ]
113
+ if ( ! existingData ) {
114
+ throw new Error ( `Updated node [${ workflow . id } ] not found in workflow lookup` )
115
+ }
116
+ mergeWith ( existingData . node , workflow , mergeWithCustomizer )
117
+ Vue . set ( gscan . lookup , existingData . id , existingData )
99
118
}
100
119
101
120
/**
0 commit comments