diff --git a/lib/Executions.js b/lib/Executions.js index 18e7cee3..c889aeb2 100644 --- a/lib/Executions.js +++ b/lib/Executions.js @@ -22,6 +22,7 @@ module.exports = class Executions { await this.db.executions.delete(executionName) } await this.db.executions.put(exec) + await this.load(exec.executionName) } /** @@ -115,7 +116,7 @@ module.exports = class Executions { ) if (!['NOAUTH', 'STATE_MACHINE_NOT_FOUND'].includes(data.status)) { - await this.storeFromServerRequest({ ...data, executionTitle: title }) + await this.storeFromServerRequest({ ...data, executionTitle: title, executionInput: input }) } return data @@ -126,7 +127,7 @@ module.exports = class Executions { * @param {object} execDesc - Execution description which was the result of the request. */ async storeFromServerRequest (execDesc) { - const { ctx, status, executionName, executionTitle, stateMachineName } = execDesc + const { ctx, status, executionName, executionTitle, executionInput, stateMachineName } = execDesc const date = new Date() const stateMachine = { name: stateMachineName } @@ -140,6 +141,7 @@ module.exports = class Executions { const res = { executionTitle, + executionInput, status, executionName, stateMachine, diff --git a/lib/Tymly-sdk.js b/lib/Tymly-sdk.js index 9e9a4c16..fe7b8b59 100644 --- a/lib/Tymly-sdk.js +++ b/lib/Tymly-sdk.js @@ -14,6 +14,7 @@ const Info = require('./Info') const Route = require('./Route') const Tasks = require('./Tasks') const Downloads = require('./Downloads') +const WebSocket = require('./WebSocket') const database = require('./database') const { v1: uuidv1 } = require('uuid') const shasum = require('shasum') @@ -78,6 +79,7 @@ module.exports = class TymlyClient { this.watching = new Watching(this) this.suggestions = new Suggestions(this) this.download = new Downloads(this) + this.websocket = new WebSocket(this) await Promise.all(USER_QUERY_KEYS.map(k => this[k].load())) @@ -329,6 +331,7 @@ module.exports = class TymlyClient { * Closes down running services. */ async destroy () { + this.websocket.disconnect() this.options.store.commit('app/clear', {}) this.options.store.commit('tracker/clear', {}) this.haveRunUserQuery = false diff --git a/lib/WebSocket.js b/lib/WebSocket.js new file mode 100644 index 00000000..1ced5fd8 --- /dev/null +++ b/lib/WebSocket.js @@ -0,0 +1,65 @@ +class _WebSocket { + constructor (client) { + this.store = client.options.store + this.token = client.options.auth.token + this.ws = this.store.state.app.ws + this.connect() + } + + get url () { + let url = process.env.TYMLY_API_BASE_URL.replace(/^http/, 'ws') + if (url[url.length - 1] !== '/') url += '/' + + const params = [ + `token=Bearer ${this.token}` + ] + + return url + `?${params.join('&')}` + } + + subscribe (subscriptions) { + this.connect() + this.ws.send(JSON.stringify({ event: 'subscribe', subscriptions })) + } + + unsubscribe (subscriptions) { + this.connect() + this.ws.send(JSON.stringify({ event: 'unsubscribe', subscriptions })) + } + + connect () { + if (this.ws) return + + console.log('Opening websocket connection') + + this.ws = new WebSocket(this.url) + this.ws.onopen = event => { + this.ws.onmessage = msg => { + // do not update if came from same source - msg.srcElement? + + let event = '' + let message = {} + + try { + const obj = JSON.parse(msg.data) + event = obj.key + message = obj.message + } catch (e) { } + + this.store.commit('app/wsMessage', { event, message }) + } + } + this.store.commit('app/ws', this.ws) + } + + disconnect () { + if (this.ws === null) return + + console.log('Closing websocket connection') + this.ws.close() + this.ws = null + this.store.commit('app/ws', this.ws) + } +} + +module.exports = _WebSocket