ws.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import defRegistry from './flow/default-registry'
  2. import flowMut from './flow/mutation-types'
  3. import chatMut from './chat/mutation-types'
  4. import flowService from '@/services/flowservice'
  5. let flow = {}
  6. let chat = {}
  7. for (let k in flowMut) { flow[k] = 'flow/' + k }
  8. for (let k in chatMut) { chat[k] = 'chat/' + k }
  9. let targetws
  10. // DEBUG PURPOSES
  11. window.dbgDisconnect = () => {
  12. flowService.close()
  13. }
  14. window.dbgReconnect = () => {
  15. flowService.connect(targetws)
  16. }
  17. export default store => {
  18. store.subscribe(mut => {
  19. // console.log('I changed -- perform the connection somehow', mut)
  20. if (mut.type === 'route/ROUTE_CHANGED') {
  21. let route = window.location.pathname
  22. // let route = mut.payload.to.path
  23. const urlParts = route.split('/')
  24. urlParts[0] = window.location.host // Substitute first '/' with host
  25. urlParts[urlParts.length - 1] = 'conn' // 'substitute last with 'conn'
  26. const urlPath = urlParts.join('/')
  27. // Add protocol
  28. targetws = 'ws://' + urlPath
  29. if (window.location.protocol === 'https:') {
  30. targetws = 'wss://' + urlPath
  31. }
  32. flowService.connect(targetws)
  33. }
  34. })
  35. // Connected
  36. flowService.connected(() => {
  37. // Allow any persisted thing to be send first if any
  38. setTimeout(() => {
  39. const match = /.*\/s:(.*)/.exec(store.state.route.path)
  40. let sessId
  41. if (match != null) {
  42. sessId = match[1]
  43. }
  44. // Make this in a service
  45. if (sessId === undefined) {
  46. flowService.sessionNew()
  47. return
  48. }
  49. store.commit(flow.SESSID_UPDATE, sessId)
  50. flowService.sessionLoad(undefined, sessId)
  51. })
  52. })
  53. flowService.on('document', (v) => {
  54. store.commit(flow.DOCUMENT_UPDATE, v.data)
  55. })
  56. flowService.on('nodeUpdate', (v) => {
  57. store.commit(flow.NODE_UPDATE, v.data)
  58. })
  59. flowService.on('registry', (v) => {
  60. let res = {}
  61. for (let k of Object.keys(v.data)) {
  62. const e = v.data[k]
  63. res[k] = {
  64. categories: e.categories,
  65. inputs: e.inputs,
  66. inputDesc: e.inputDesc,
  67. output: e.output,
  68. outputDesC: e.outputDesc,
  69. style: e.extra && e.extra.style
  70. }
  71. }
  72. store.commit(flow.REGISTRY_UPDATE, Object.assign({}, defRegistry, res))
  73. })
  74. flowService.on('nodeActivity', (v) => {
  75. store.commit(flow.ACTIVITY_UPDATE, v.data || {nodes: {}})
  76. })
  77. flowService.on('sessionNotify', (v) => {
  78. // ACTION
  79. store.dispatch(flow.NOTIFICATION_ADD, v.data)
  80. })
  81. flowService.on('sessionJoin', (v) => {
  82. // store.dispatch(flow.NOTIFICATION_ADD, 'Connected')
  83. const sessId = v.id
  84. store.dispatch(chat.CHAT_JOIN, {
  85. handle: store.state.chat.handle,
  86. sessId: sessId
  87. })
  88. store.commit(chat.EVENTS_UPDATE, [])
  89. store.commit(flow.SESSID_UPDATE, sessId)
  90. })
  91. /// // CHAT //////
  92. flowService.on('chatUserList', (v) => {
  93. store.commit(chat.USERLIST_UPDATE, v.data)
  94. })
  95. flowService.on('chatEvent', (v) => {
  96. store.commit(chat.EVENT_ADD, v.data)
  97. })
  98. }