Skip to content

Commit

Permalink
Move GScan data to its module, update the code of this PR to use call…
Browse files Browse the repository at this point in the history
…backs
  • Loading branch information
kinow committed Oct 18, 2021
1 parent fadaae6 commit 3d82305
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 444 deletions.
37 changes: 4 additions & 33 deletions src/components/cylc/common/deltas.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,37 +182,8 @@ function applyDeltasPruned (pruned, lookup) {
}
}

/**
* A function that simply applies the deltas to a lookup object.
*
* The entries in deltas will be the value of the lookup, and their ID's
* will be the keys.
*
* This function can be used with any lookup-like structure. When
* entries are updated it will merge with a customizer maintaining
* the Vue reactivity.
*
* @param {GraphQLResponseData} data
* @param {Object.<String, Object>} lookup
*/
export default function (data, lookup) {
const added = data.deltas.added
const updated = data.deltas.updated
const pruned = data.deltas.pruned
const errors = []
if (added) {
const result = applyDeltasAdded(added, lookup)
errors.push(...result.errors)
}
if (updated) {
const result = applyDeltasUpdated(updated, lookup)
errors.push(...result.errors)
}
if (pruned) {
const result = applyDeltasPruned(pruned, lookup)
errors.push(...result.errors)
}
return {
errors
}
export {
applyDeltasAdded,
applyDeltasUpdated,
applyDeltasPruned
}
2 changes: 1 addition & 1 deletion src/components/cylc/gscan/GScan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export default {
}
},
computed: {
...mapState('workflows', ['gscan']),
...mapState('gscan', ['gscan']),
// workflowNodes () {
// // NOTE: In case we decide to allow the user to switch between hierarchical and flat
// // gscan view, then all we need to do is just pass a boolean data-property to
Expand Down
25 changes: 15 additions & 10 deletions src/components/cylc/gscan/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
applyDeltasPruned
} from '@/components/cylc/gscan/deltas'
import DeltasCallback from '@/services/callbacks'
import { clear } from '@/components/cylc/gscan/index'

/**
* Provisional GScan callback until https://github.com/cylc/cylc-ui/pull/736
Expand All @@ -29,37 +30,41 @@ import DeltasCallback from '@/services/callbacks'
class GScanCallback extends DeltasCallback {
constructor () {
super()
this.workflows = null
this.lookup = null
this.gscan = null
}

before (deltas, store, errors) {
this.workflows = Object.assign({}, store.state.workflows.workflows)
// If it were TS, we would use a ReadOnly type here...
this.lookup = store.state.workflows.lookup
const gscan = store.state.gscan.gscan
this.gscan = Object.assign({}, gscan)
}

tearDown (store, errors) {
store.commit('workflows/SET_WORKFLOWS', {})
this.workflows = null
clear(this.gscan)
store.commit('gscan/SET_GSCAN', this.gscan)
this.lookup = null
this.gscan = null
}

onAdded (added, store, errors) {
this.workflows = Object.assign(this.workflows, store.state.workflows.workflows)
const results = applyDeltasAdded(added, this.workflows)
const results = applyDeltasAdded(added, this.gscan, {})
errors.push(...results.errors)
}

onUpdated (updated, store, errors) {
const results = applyDeltasUpdated(updated, this.workflows)
const results = applyDeltasUpdated(updated, this.gscan, {})
errors.push(...results.errors)
}

onPruned (pruned, store, errors) {
this.workflows = Object.assign(this.workflows, store.state.workflows.workflows)
const results = applyDeltasPruned(pruned, this.workflows)
const results = applyDeltasPruned(pruned, this.gscan, {})
errors.push(...results.errors)
}

commit (store, errors) {
store.commit('workflows/SET_WORKFLOWS', this.workflows)
store.commit('gscan/SET_GSCAN', this.gscan)
}
}

Expand Down
57 changes: 0 additions & 57 deletions src/components/cylc/gscan/deltas.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,63 +105,6 @@ function applyDeltasPruned (pruned, gscan, options) {
return result
}

const DELTAS = {
added: applyDeltasAdded,
updated: applyDeltasUpdated,
pruned: applyDeltasPruned
}

/**
* Handle the deltas. This function receives the new set of deltas, and the GScan object.
*
* The GScan object contains a tree property that holds the hierarchical (or not) GScan,
* and a lookup helper dictionary used for ease of access to leaf or intermediary tree
* nodes.
*
* @param {Deltas} deltas
* @param {GScan} gscan
* @param {*} options
* @returns {Result}
*/
function handleDeltas (deltas, gscan, options) {
const results = {
errors: []
}
Object.keys(DELTAS).forEach(key => {
if (deltas[key]) {
const handlingFunction = DELTAS[key]
const result = handlingFunction(deltas[key], gscan, options)
results.errors.push(...result.errors)
}
})
return results
}

/**
* @param {GraphQLResponseData} data
* @param {*} gscan
* @param {*} options
* @returns {Result}
*/
export default function (data, gscan, options) {
const deltas = data.deltas
try {
return handleDeltas(deltas, gscan, options)
} catch (error) {
return {
errors: [
[
'Unexpected error applying gscan deltas',
error,
deltas,
gscan,
options
]
]
}
}
}

export {
applyDeltasAdded,
applyDeltasUpdated,
Expand Down
12 changes: 12 additions & 0 deletions src/components/cylc/gscan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ import {
* @typedef {Object<String, TreeNode>} Lookup
*/

/**
* @param {GScan} gscan
*/
function clear (gscan) {
['tree', 'lookup'].forEach(each => {
Object.keys(gscan[each]).forEach(key => {
Vue.delete(gscan[each], key)
})
})
}

// --- Added

/**
Expand Down Expand Up @@ -238,6 +249,7 @@ function removeNode (id, lookup, tree) {
}

export {
clear,
addWorkflow,
updateWorkflow,
removeWorkflow
Expand Down
14 changes: 8 additions & 6 deletions src/components/cylc/tree/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import DeltasCallback from '@/services/callbacks'
import { clear } from '@/components/cylc/tree'
import {
before,
after,
Expand Down Expand Up @@ -45,10 +46,10 @@ class TreeCallback extends DeltasCallback {
}

before (deltas, store, errors) {
const lookup = store.state.workflows.lookup
const workflow = store.state.workflows.workflow
// If it were TS, we would use a ReadOnly type here...
this.lookup = store.state.workflows.lookup
const workflow = store.state.tree.workflow
this.workflow = Object.assign({}, workflow)
this.lookup = Object.assign({}, lookup)
const results = before(deltas, this.workflow, this.lookup)
errors.push(...results.errors)
}
Expand All @@ -59,9 +60,10 @@ class TreeCallback extends DeltasCallback {
}

tearDown (store, errors) {
store.commit('workflows/CLEAR_WORKFLOW')
this.workflow = null
clear(this.workflow)
store.commit('tree/SET_WORKFLOW', this.workflow)
this.lookup = null
this.workflow = null
}

onAdded (added, store, errors) {
Expand All @@ -80,7 +82,7 @@ class TreeCallback extends DeltasCallback {
}

commit (store, errors) {
store.commit('workflows/SET_WORKFLOW', this.workflow)
store.commit('tree/SET_WORKFLOW', this.workflow)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ class DeltasCallback {
tearDown (store, errors) {}

/**
* @param {DeltasAdded|Object} added
* @param {DeltasAdded} added
* @param {Vuex} store
* @param {Array<Object>} errors
*/
onAdded (added, store, errors) {}

/**
* @param {DeltasUpdated|Object} updated
* @param {DeltasUpdated} updated
* @param {Vuex} store
* @param {Array<Object>} errors
*/
onUpdated (updated, store, errors) {}

/**
* @param {DeltasPruned|Object} pruned -
* @param {DeltasPruned} pruned -
* @param {Vuex} store - Vuex store
* @param {Array<Object>} errors
*/
Expand Down
39 changes: 39 additions & 0 deletions src/store/gscan.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (C) NIWA & British Crown (Met Office) & Contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

const state = {
/**
* This is the data structure used by GScan component. The tree holds the hierarchical GScan,
* and the lookup is a helper structure for quick access to nodes in the tree.
*/
gscan: {
tree: [],
lookup: {}
}
}

const mutations = {
SET_GSCAN (state, data) {
state.gscan = data
}
}

export const gscan = {
namespaced: true,
state,
mutations
}
4 changes: 3 additions & 1 deletion src/store/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { app } from './app.module'
import { workflows } from './workflows.module'
import { user } from './user.module'
import { tree } from './tree.module'
import { gscan } from './gscan.module'

// State
const state = {
Expand Down Expand Up @@ -82,7 +83,8 @@ export default {
app,
workflows,
user,
tree
tree,
gscan
},
actions,
mutations,
Expand Down
Loading

0 comments on commit 3d82305

Please sign in to comment.