Skip to content

Commit 3d82305

Browse files
committed
Move GScan data to its module, update the code of this PR to use callbacks
1 parent fadaae6 commit 3d82305

File tree

16 files changed

+214
-444
lines changed

16 files changed

+214
-444
lines changed

src/components/cylc/common/deltas.js

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -182,37 +182,8 @@ function applyDeltasPruned (pruned, lookup) {
182182
}
183183
}
184184

185-
/**
186-
* A function that simply applies the deltas to a lookup object.
187-
*
188-
* The entries in deltas will be the value of the lookup, and their ID's
189-
* will be the keys.
190-
*
191-
* This function can be used with any lookup-like structure. When
192-
* entries are updated it will merge with a customizer maintaining
193-
* the Vue reactivity.
194-
*
195-
* @param {GraphQLResponseData} data
196-
* @param {Object.<String, Object>} lookup
197-
*/
198-
export default function (data, lookup) {
199-
const added = data.deltas.added
200-
const updated = data.deltas.updated
201-
const pruned = data.deltas.pruned
202-
const errors = []
203-
if (added) {
204-
const result = applyDeltasAdded(added, lookup)
205-
errors.push(...result.errors)
206-
}
207-
if (updated) {
208-
const result = applyDeltasUpdated(updated, lookup)
209-
errors.push(...result.errors)
210-
}
211-
if (pruned) {
212-
const result = applyDeltasPruned(pruned, lookup)
213-
errors.push(...result.errors)
214-
}
215-
return {
216-
errors
217-
}
185+
export {
186+
applyDeltasAdded,
187+
applyDeltasUpdated,
188+
applyDeltasPruned
218189
}

src/components/cylc/gscan/GScan.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export default {
285285
}
286286
},
287287
computed: {
288-
...mapState('workflows', ['gscan']),
288+
...mapState('gscan', ['gscan']),
289289
// workflowNodes () {
290290
// // NOTE: In case we decide to allow the user to switch between hierarchical and flat
291291
// // gscan view, then all we need to do is just pass a boolean data-property to

src/components/cylc/gscan/callbacks.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
applyDeltasPruned
2222
} from '@/components/cylc/gscan/deltas'
2323
import DeltasCallback from '@/services/callbacks'
24+
import { clear } from '@/components/cylc/gscan/index'
2425

2526
/**
2627
* Provisional GScan callback until https://github.com/cylc/cylc-ui/pull/736
@@ -29,37 +30,41 @@ import DeltasCallback from '@/services/callbacks'
2930
class GScanCallback extends DeltasCallback {
3031
constructor () {
3132
super()
32-
this.workflows = null
33+
this.lookup = null
34+
this.gscan = null
3335
}
3436

3537
before (deltas, store, errors) {
36-
this.workflows = Object.assign({}, store.state.workflows.workflows)
38+
// If it were TS, we would use a ReadOnly type here...
39+
this.lookup = store.state.workflows.lookup
40+
const gscan = store.state.gscan.gscan
41+
this.gscan = Object.assign({}, gscan)
3742
}
3843

3944
tearDown (store, errors) {
40-
store.commit('workflows/SET_WORKFLOWS', {})
41-
this.workflows = null
45+
clear(this.gscan)
46+
store.commit('gscan/SET_GSCAN', this.gscan)
47+
this.lookup = null
48+
this.gscan = null
4249
}
4350

4451
onAdded (added, store, errors) {
45-
this.workflows = Object.assign(this.workflows, store.state.workflows.workflows)
46-
const results = applyDeltasAdded(added, this.workflows)
52+
const results = applyDeltasAdded(added, this.gscan, {})
4753
errors.push(...results.errors)
4854
}
4955

5056
onUpdated (updated, store, errors) {
51-
const results = applyDeltasUpdated(updated, this.workflows)
57+
const results = applyDeltasUpdated(updated, this.gscan, {})
5258
errors.push(...results.errors)
5359
}
5460

5561
onPruned (pruned, store, errors) {
56-
this.workflows = Object.assign(this.workflows, store.state.workflows.workflows)
57-
const results = applyDeltasPruned(pruned, this.workflows)
62+
const results = applyDeltasPruned(pruned, this.gscan, {})
5863
errors.push(...results.errors)
5964
}
6065

6166
commit (store, errors) {
62-
store.commit('workflows/SET_WORKFLOWS', this.workflows)
67+
store.commit('gscan/SET_GSCAN', this.gscan)
6368
}
6469
}
6570

src/components/cylc/gscan/deltas.js

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -105,63 +105,6 @@ function applyDeltasPruned (pruned, gscan, options) {
105105
return result
106106
}
107107

108-
const DELTAS = {
109-
added: applyDeltasAdded,
110-
updated: applyDeltasUpdated,
111-
pruned: applyDeltasPruned
112-
}
113-
114-
/**
115-
* Handle the deltas. This function receives the new set of deltas, and the GScan object.
116-
*
117-
* The GScan object contains a tree property that holds the hierarchical (or not) GScan,
118-
* and a lookup helper dictionary used for ease of access to leaf or intermediary tree
119-
* nodes.
120-
*
121-
* @param {Deltas} deltas
122-
* @param {GScan} gscan
123-
* @param {*} options
124-
* @returns {Result}
125-
*/
126-
function handleDeltas (deltas, gscan, options) {
127-
const results = {
128-
errors: []
129-
}
130-
Object.keys(DELTAS).forEach(key => {
131-
if (deltas[key]) {
132-
const handlingFunction = DELTAS[key]
133-
const result = handlingFunction(deltas[key], gscan, options)
134-
results.errors.push(...result.errors)
135-
}
136-
})
137-
return results
138-
}
139-
140-
/**
141-
* @param {GraphQLResponseData} data
142-
* @param {*} gscan
143-
* @param {*} options
144-
* @returns {Result}
145-
*/
146-
export default function (data, gscan, options) {
147-
const deltas = data.deltas
148-
try {
149-
return handleDeltas(deltas, gscan, options)
150-
} catch (error) {
151-
return {
152-
errors: [
153-
[
154-
'Unexpected error applying gscan deltas',
155-
error,
156-
deltas,
157-
gscan,
158-
options
159-
]
160-
]
161-
}
162-
}
163-
}
164-
165108
export {
166109
applyDeltasAdded,
167110
applyDeltasUpdated,

src/components/cylc/gscan/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ import {
3535
* @typedef {Object<String, TreeNode>} Lookup
3636
*/
3737

38+
/**
39+
* @param {GScan} gscan
40+
*/
41+
function clear (gscan) {
42+
['tree', 'lookup'].forEach(each => {
43+
Object.keys(gscan[each]).forEach(key => {
44+
Vue.delete(gscan[each], key)
45+
})
46+
})
47+
}
48+
3849
// --- Added
3950

4051
/**
@@ -238,6 +249,7 @@ function removeNode (id, lookup, tree) {
238249
}
239250

240251
export {
252+
clear,
241253
addWorkflow,
242254
updateWorkflow,
243255
removeWorkflow

src/components/cylc/tree/callbacks.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import DeltasCallback from '@/services/callbacks'
19+
import { clear } from '@/components/cylc/tree'
1920
import {
2021
before,
2122
after,
@@ -45,10 +46,10 @@ class TreeCallback extends DeltasCallback {
4546
}
4647

4748
before (deltas, store, errors) {
48-
const lookup = store.state.workflows.lookup
49-
const workflow = store.state.workflows.workflow
49+
// If it were TS, we would use a ReadOnly type here...
50+
this.lookup = store.state.workflows.lookup
51+
const workflow = store.state.tree.workflow
5052
this.workflow = Object.assign({}, workflow)
51-
this.lookup = Object.assign({}, lookup)
5253
const results = before(deltas, this.workflow, this.lookup)
5354
errors.push(...results.errors)
5455
}
@@ -59,9 +60,10 @@ class TreeCallback extends DeltasCallback {
5960
}
6061

6162
tearDown (store, errors) {
62-
store.commit('workflows/CLEAR_WORKFLOW')
63-
this.workflow = null
63+
clear(this.workflow)
64+
store.commit('tree/SET_WORKFLOW', this.workflow)
6465
this.lookup = null
66+
this.workflow = null
6567
}
6668

6769
onAdded (added, store, errors) {
@@ -80,7 +82,7 @@ class TreeCallback extends DeltasCallback {
8082
}
8183

8284
commit (store, errors) {
83-
store.commit('workflows/SET_WORKFLOW', this.workflow)
85+
store.commit('tree/SET_WORKFLOW', this.workflow)
8486
}
8587
}
8688

src/services/callbacks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ class DeltasCallback {
3737
tearDown (store, errors) {}
3838

3939
/**
40-
* @param {DeltasAdded|Object} added
40+
* @param {DeltasAdded} added
4141
* @param {Vuex} store
4242
* @param {Array<Object>} errors
4343
*/
4444
onAdded (added, store, errors) {}
4545

4646
/**
47-
* @param {DeltasUpdated|Object} updated
47+
* @param {DeltasUpdated} updated
4848
* @param {Vuex} store
4949
* @param {Array<Object>} errors
5050
*/
5151
onUpdated (updated, store, errors) {}
5252

5353
/**
54-
* @param {DeltasPruned|Object} pruned -
54+
* @param {DeltasPruned} pruned -
5555
* @param {Vuex} store - Vuex store
5656
* @param {Array<Object>} errors
5757
*/

src/store/gscan.module.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
const state = {
19+
/**
20+
* This is the data structure used by GScan component. The tree holds the hierarchical GScan,
21+
* and the lookup is a helper structure for quick access to nodes in the tree.
22+
*/
23+
gscan: {
24+
tree: [],
25+
lookup: {}
26+
}
27+
}
28+
29+
const mutations = {
30+
SET_GSCAN (state, data) {
31+
state.gscan = data
32+
}
33+
}
34+
35+
export const gscan = {
36+
namespaced: true,
37+
state,
38+
mutations
39+
}

src/store/options.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { app } from './app.module'
2020
import { workflows } from './workflows.module'
2121
import { user } from './user.module'
2222
import { tree } from './tree.module'
23+
import { gscan } from './gscan.module'
2324

2425
// State
2526
const state = {
@@ -82,7 +83,8 @@ export default {
8283
app,
8384
workflows,
8485
user,
85-
tree
86+
tree,
87+
gscan
8688
},
8789
actions,
8890
mutations,

0 commit comments

Comments
 (0)