Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/Executions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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 }
Expand All @@ -140,6 +141,7 @@ module.exports = class Executions {

const res = {
executionTitle,
executionInput,
status,
executionName,
stateMachine,
Expand Down
3 changes: 3 additions & 0 deletions lib/Tymly-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()))

Expand Down Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions lib/WebSocket.js
Original file line number Diff line number Diff line change
@@ -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