Browse Source

Missing vuex store

luis 7 năm trước cách đây
mục cha
commit
f2bce7b35e

+ 1 - 1
.gitignore

@@ -1,2 +1,2 @@
 DIST
-store
+/store

+ 57 - 0
browser/vue-flow/src/store/actions.js

@@ -0,0 +1,57 @@
+import m from './mutation-types'
+import log from './log'
+import flowService from '../services/flowservice'
+
+export default {
+  [m.DOCUMENT_SYNC] (ctx) {
+    log('document_sync')
+    flowService.documentUpdate(ctx.getters.nodeData, ctx.state.route.params.sessId)
+  },
+  // Node update full document state somehow
+  [m.DOCUMENT_UPDATE] ({commit}, nodeData) {
+    flowService.documentUpdate(nodeData)
+    // Map the nodes
+    // WEBSOCKET
+    commit(m.DOCUMENT_UPDATE, nodeData)
+  },
+  [m.NODE_RAISE] ({commit}, nodes) {
+    commit(m.NODE_RAISE, nodes)
+  },
+  [m.NODE_UPDATE] ({commit}, nodes) {
+    // WEBSOCKET
+    commit(m.NODE_UPDATE, nodes)
+    flowService.nodeUpdate(nodes)
+  },
+
+  [m.NODE_ADD] (ctx, node) {
+    // WEBSOCKET
+    ctx.commit(m.NODE_ADD, node)
+    ctx.dispatch(m.DOCUMENT_SYNC)
+  },
+  [m.NODE_REMOVE] (ctx, nodes) {
+    ctx.commit(m.NODE_REMOVE, nodes)
+    flowService.nodeRemove(nodes)
+    ctx.dispatch(m.DOCUMENT_SYNC)
+  },
+  [m.NODE_INSPECT] ({commit}, nodeId) {
+    commit(m.NODE_INSPECT, nodeId)
+  },
+  [m.LINK_ADD] (ctx, link) {
+    ctx.commit(m.LINK_ADD, link)
+    ctx.dispatch(m.DOCUMENT_SYNC)
+  },
+  [m.LINK_REMOVE] (ctx, link) {
+    ctx.commit(m.LINK_REMOVE, link)
+    // flowService.linkRemove(link)
+    ctx.dispatch(m.DOCUMENT_SYNC)
+  },
+  [m.TRIGGER_ADD] (ctx, trigger) {
+    ctx.commit(m.TRIGGER_ADD, trigger)
+    ctx.dispatch(m.DOCUMENT_SYNC)
+  },
+  [m.TRIGGER_REMOVE] (ctx, trigger) {
+    ctx.commit(m.TRIGGER_REMOVE, trigger)
+    ctx.dispatch(m.DOCUMENT_SYNC)
+  }
+
+}

+ 54 - 0
browser/vue-flow/src/store/default-registry.js

@@ -0,0 +1,54 @@
+export default{
+  'Input': {
+    categories: ['core'],
+    output: {type: 'interface {}'},
+    style: { color: '#686', shape: 'circle' },
+    props: {} // should be sent in the node
+  },
+  'Variable': {
+    categories: ['core'],
+    output: {type: 'interface {}'},
+    style: { color: '#88a', shape: 'circle' },
+    props: {init: ''}
+  },
+  'Const': {
+    categories: ['core'],
+    output: {type: 'interface {}'},
+    style: { color: '#555' }
+    // , props: {value: ''}
+  },
+  'Notify': {
+    categories: ['flow-web'],
+    inputs: [{type: 'interface {}'}, {type: 'string', name: 'msg'}],
+    output: {type: 'interface {}'},
+    style: {color: '#665'}
+  },
+  'Log': {
+    categories: ['flow-web'],
+    output: {type: 'io.Writer'},
+    style: {color: '#665'}
+  }
+}
+
+/* {
+        // Fixed default stuff
+        'Test': {
+          group: 'Generic',
+          output: 'any',
+          style: {
+            shape: 'thing'
+          }
+        },
+
+        'MatMul': { group: 'Machine learning', inputs: [ '[]float32', '[]float32' ], output: '[]float32', style: { color: '#a44', textColor: 'white' } },
+        'Activator': { group: 'Machine learning', inputs: [ '[]float32' ], output: '[]float32', style: { color: '#a44', textColor: 'white', shape: 'circle' } },
+
+        'test': { group: 'Text', inputs: [ '[]float32', 'string' ], output: 'string', style: {'color': '#a93'} },
+        'reverse': { group: 'Text', inputs: [ 'string' ], output: 'string', style: {'color': '#a93'} },
+
+        'fetch': { group: 'json', output: 'json', style: {'color': '#99a'} },
+        'jsonExtract': { group: 'json', inputs: ['json'], output: 'string', style: {'color': '#99a'} },
+
+        'string': { group: 'Visualization', inputs: ['string'], style: {'color': '#9a9'} },
+        'lineGraph': { group: 'Visualization', inputs: ['[]float32', '[]float32'], style: {'color': '#9a9'} }
+      }, */

+ 8 - 0
browser/vue-flow/src/store/getters.js

@@ -0,0 +1,8 @@
+export default {
+  nodeData: state => state.nodeData,
+  nodeById: state => id => {
+    return state.nodeData.nodes.find(n => n.id === id)
+  },
+  registry: state => state.registry,
+  activity: state => state.activity
+}

+ 19 - 0
browser/vue-flow/src/store/index.js

@@ -0,0 +1,19 @@
+import Vuex from 'vuex'
+import Vue from 'vue'
+import actions from './actions'
+import state from './state'
+import getters from './getters'
+import mutations from './mutations'
+
+import plugin from './ws'
+
+Vue.use(Vuex)
+
+export default new Vuex.Store({
+  strict: true,
+  state,
+  getters,
+  actions,
+  mutations,
+  plugins: [plugin]
+})

+ 7 - 0
browser/vue-flow/src/store/log.js

@@ -0,0 +1,7 @@
+const debug = 1
+let log = () => {}
+if (debug) {
+  log = console.log.bind(console.log, '%cSTORE:', 'color:#00a', (Math.random() * 1000).toFixed())
+}
+
+export default log

+ 16 - 0
browser/vue-flow/src/store/mutation-types.js

@@ -0,0 +1,16 @@
+var actions = [
+  'REGISTRY_UPDATE',
+  'DOCUMENT_UPDATE', 'DOCUMENT_SYNC',
+  'ACTIVITY_UPDATE',
+  'NODE_RAISE', 'NODE_UPDATE', 'NODE_ADD', 'NODE_REMOVE', 'NODE_INSPECT',
+  'LINK_ADD', 'LINK_REMOVE',
+  'TRIGGER_ADD', 'TRIGGER_REMOVE'
+]
+
+const obj = {}
+
+for (let k of actions) {
+  obj[k] = k
+}
+
+module.exports = obj

+ 70 - 0
browser/vue-flow/src/store/mutations.js

@@ -0,0 +1,70 @@
+import Vue from 'vue'
+
+// import m from './mutation-types'
+import m from './mutation-types'
+
+export default {
+  [m.REGISTRY_UPDATE] (state, registry) {
+    state.registry = registry
+  },
+  [m.DOCUMENT_UPDATE] (state, nodeData) {
+    state.nodeData = nodeData
+  },
+  [m.ACTIVITY_UPDATE] (state, activity) {
+    state.activity = activity
+  },
+  [m.NODE_RAISE] (state, nodes) {
+    for (let k in nodes) {
+      let ni = state.nodeData.nodes.findIndex(n => n.id === nodes[k].id)
+      const node = state.nodeData.nodes[ni]
+      state.nodeData.nodes.splice(ni, 1)
+      state.nodeData.nodes.push(node) // put in last
+    }
+  },
+  [m.NODE_UPDATE] (state, nodes) {
+    // If array
+    for (let k in nodes) {
+      const node = nodes[k]
+      const ni = state.nodeData.nodes.findIndex(n => n.id === node.id)
+      Vue.set(state.nodeData.nodes, ni, node)
+      if (node.id === state.nodeInspect.id) {
+        state.nodeInspect = node
+      }
+    }
+  },
+  [m.NODE_ADD] (state, node) {
+    state.nodeData.nodes.push(node)
+  },
+  [m.NODE_REMOVE] (state, nodes) {
+    for (let k in nodes) {
+      const node = nodes[k]
+      const ni = state.nodeData.nodes.findIndex(n => n.id === node.id)
+      state.nodeData.links = state.nodeData.links
+        .filter(l => l.from !== node.id && l.to !== node.id)
+      state.nodeData.triggers = state.nodeData.triggers
+        .filter(l => l.from !== node.id && l.to !== node.id)
+      state.nodeData.nodes.splice(ni, 1)
+    }
+  },
+  [m.NODE_INSPECT] (state, nodeId) {
+    const node = state.nodeData.nodes.find(n => n.id === nodeId)
+    state.nodeInspect = node
+  },
+  [m.LINK_ADD] (state, link) {
+    state.nodeData.links.push(link)
+  },
+  [m.LINK_REMOVE] (state, link) {
+    const i = state.nodeData.links.findIndex(l => l === link)
+    if (i === -1) return
+    state.nodeData.links.splice(i, 1)
+  },
+
+  [m.TRIGGER_ADD] (state, trigger) {
+    state.nodeData.triggers.push(trigger)
+  },
+  [m.TRIGGER_REMOVE] (state, trigger) {
+    const i = state.nodeData.triggers.findIndex(l => l === trigger)
+    if (i === -1) return
+    state.nodeData.triggers.splice(i, 1)
+  }
+}

+ 14 - 0
browser/vue-flow/src/store/state.js

@@ -0,0 +1,14 @@
+export default {
+  // Contain ids of nodes
+  nodeListOrder: [],
+  // document
+  nodeData: {
+    nodes: [],
+    links: [],
+    triggers: []
+  },
+  registry: {},
+  activity: {},
+
+  nodeInspect: {}
+}

+ 36 - 0
browser/vue-flow/src/store/ws.js

@@ -0,0 +1,36 @@
+import defRegistry from './default-registry'
+import m from './mutation-types'
+import flowService from '@/services/flowservice'
+
+export default store => {
+  store.subscribe(mut => {
+    // console.log('I changed -- perform the connection somehow', mut)
+  })
+
+  flowService.on('document', (v) => {
+    store.commit(m.DOCUMENT_UPDATE, v.data)
+  })
+  flowService.on('nodeUpdate', (v) => {
+    store.commit(m.NODE_UPDATE, v.data)
+  })
+
+  flowService.on('registry', (v) => {
+    let res = {}
+    for (let k of Object.keys(v.data)) {
+      const e = v.data[k]
+      res[k] = {
+        categories: e.categories,
+        inputs: e.inputs,
+        inputDesc: e.inputDesc,
+
+        output: e.output,
+        outputDesC: e.outputDesc,
+        style: e.extra && e.extra.style
+      }
+    }
+    store.commit(m.REGISTRY_UPDATE, Object.assign({}, defRegistry, res))
+  })
+  flowService.on('nodeActivity', (v) => {
+    store.commit(m.ACTIVITY_UPDATE, v.data || {})
+  })
+}