1414 * You should have received a copy of the GNU General Public License
1515 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1616 */
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'
1723
1824/**
1925 * @typedef {Object } GScan
2531 * @typedef {Object<String, TreeNode> } Lookup
2632 */
2733
28- import { sortedIndexBy } from '@/components/cylc/common/sort'
29- import { sortWorkflowNamePartNodeOrWorkflowNode } from '@/components/cylc/gscan/sort'
30-
3134/**
3235 * @param {TreeNode } workflow
3336 * @param {GScan } gscan
@@ -36,18 +39,24 @@ import { sortWorkflowNamePartNodeOrWorkflowNode } from '@/components/cylc/gscan/
3639function addWorkflow ( workflow , gscan , options ) {
3740 const hierarchical = options . hierarchical || true
3841 if ( hierarchical ) {
39- addHierarchicalWorkflow ( workflow , gscan . lookup , gscan . tree , options )
42+ const workflowNode = createWorkflowNode ( workflow , hierarchical )
43+ addHierarchicalWorkflow ( workflowNode , gscan . lookup , gscan . tree , options )
4044 } else {
4145 gscan . lookup [ workflow . id ] = workflow
4246 gscan . tree . push ( workflow )
4347 }
4448}
4549
4650/**
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+ *
4755 * @param workflow
4856 * @param {Lookup } lookup
4957 * @param {Array<TreeNode> } tree
5058 * @param {* } options
59+ * @private
5160 */
5261function addHierarchicalWorkflow ( workflow , lookup , tree , options ) {
5362 if ( ! lookup [ workflow . id ] ) {
@@ -78,24 +87,34 @@ function addHierarchicalWorkflow (workflow, lookup, tree, options) {
7887 // we will have to merge the hierarchies
7988 const existingNode = lookup [ workflow . id ]
8089 // TODO: combine states summaries?
81- // Copy array since we will iterate it, and modify existingNode.children
8290 if ( existingNode . children ) {
91+ // Copy array since we will iterate it, and modify existingNode.children
8392 const children = [ ...workflow . children ]
8493 for ( const child of children ) {
8594 // Recursion
8695 addHierarchicalWorkflow ( child , lookup , existingNode . children , options )
8796 }
97+ } else {
98+ // Here we have an existing workflow node. Let's merge it.
99+ mergeWith ( existingNode , workflow , mergeWithCustomizer )
88100 }
89101 }
90102}
91103
92104/**
93- * @param {TreeNode } workflow
105+ * @param {WorkflowGraphQLData } workflow
94106 * @param {GScan } gscan
95107 * @param {* } options
96108 */
97109function 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 )
99118}
100119
101120/**
0 commit comments