Better handling of first connect, now using promises

This commit is contained in:
Joel Collins 2019-03-21 16:26:44 +00:00
parent 7fd0791108
commit 45fb185d3c

View file

@ -56,38 +56,50 @@ export default new Vuex.Store({
actions: { actions: {
firstConnect(context) { firstConnect(context) {
// Reset the state when reconnecting starts // Reset the state when reconnecting starts
context.dispatch('resetState'); context.dispatch('resetState')
// Mark as loading // Mark as loading
context.commit('changeWaiting', true); context.commit('changeWaiting', true)
// Do requests // Do requests
context.dispatch('updateConfig') context.dispatch('updateConfig')
context.dispatch('updateState') .then (() => {
context.dispatch('updateState')
})
}, },
updateConfig(context, uri=`${context.getters.uri}/config`) { updateConfig(context, uri=`${context.getters.uri}/config`) {
context.dispatch('waitingState') context.dispatch('waitingState')
axios.get(uri) var sendRequest = function(resolve, reject) {
.then(response => { axios.get(uri)
context.commit('commitConfig', response.data); .then(response => {
context.dispatch('connectedState') context.commit('commitConfig', response.data)
}) context.dispatch('connectedState')
.catch(error => { resolve()
context.dispatch('handleHTTPError', error); })
}) .catch(error => {
context.dispatch('handleHTTPError', error)
reject(new Error(error))
})
}
return new Promise(sendRequest)
}, },
updateState(context, uri=`${context.getters.uri}/state`) { updateState(context, uri=`${context.getters.uri}/state`) {
context.dispatch('waitingState') context.dispatch('waitingState')
axios.get(uri) var sendRequest = function(resolve, reject) {
.then(response => { axios.get(uri)
context.commit('commitState', response.data); .then(response => {
context.dispatch('connectedState') context.commit('commitState', response.data)
}) context.dispatch('connectedState')
.catch(error => { resolve()
context.dispatch('handleHTTPError', error); })
}) .catch(error => {
context.dispatch('handleHTTPError', error)
reject(new Error(error))
})
}
return new Promise(sendRequest)
}, },
pollTask(context, [taskId, timeout, interval]) { pollTask(context, [taskId, timeout, interval]) {
@ -99,22 +111,22 @@ export default new Vuex.Store({
axios.get(`${context.getters.uri}/task/${taskId}`) axios.get(`${context.getters.uri}/task/${taskId}`)
.then(response => { .then(response => {
console.log(response.data.status) console.log(response.data.status)
var result = response.data.status; var result = response.data.status
// If the task ends with success // If the task ends with success
if(result == 'success') { if(result == 'success') {
resolve(response.data.return); resolve(response.data.return)
} }
// If task ends with an error // If task ends with an error
else if (result == 'error') { else if (result == 'error') {
reject(new Error(response.data.return)); reject(new Error(response.data.return))
} }
// If the condition isn't met but the timeout hasn't elapsed, go again // If the condition isn't met but the timeout hasn't elapsed, go again
else if (Number(new Date()) < endTime) { else if (Number(new Date()) < endTime) {
setTimeout(checkCondition, interval, resolve, reject); setTimeout(checkCondition, interval, resolve, reject)
} }
// Didn't match and too much time, reject! // Didn't match and too much time, reject!
else { else {
reject(new Error('Polling timed out')); reject(new Error('Polling timed out'))
} }
}) })
@ -140,26 +152,26 @@ export default new Vuex.Store({
}, },
resetState(context) { resetState(context) {
context.commit('changeWaiting', false); context.commit('changeWaiting', false)
context.commit('changeAvailable', true); context.commit('changeAvailable', true)
context.commit('commitError', null); context.commit('commitError', null)
context.commit('commitConfig', {}); context.commit('commitConfig', {})
context.commit('commitState', {}); context.commit('commitState', {})
}, },
waitingState(context) { waitingState(context) {
context.commit('changeWaiting', true); context.commit('changeWaiting', true)
}, },
connectedState(context) { connectedState(context) {
context.commit('changeWaiting', false); context.commit('changeWaiting', false)
context.commit('changeAvailable', true); context.commit('changeAvailable', true)
}, },
errorState(context, msg) { errorState(context, msg) {
context.commit('changeWaiting', false); context.commit('changeWaiting', false)
context.commit('changeAvailable', false); context.commit('changeAvailable', false)
context.commit('commitError', msg); context.commit('commitError', msg)
UIkit.notification({message: `<span uk-icon=\'icon: warning\'></span> ${msg}`, status: 'danger'}) UIkit.notification({message: `<span uk-icon=\'icon: warning\'></span> ${msg}`, status: 'danger'})
} }
}, },