From 45fb185d3c256891a3977f390987b0d2b97b4b70 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Thu, 21 Mar 2019 16:26:44 +0000 Subject: [PATCH] Better handling of first connect, now using promises --- src/store.js | 82 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/src/store.js b/src/store.js index 90935f11..4331e16b 100644 --- a/src/store.js +++ b/src/store.js @@ -56,38 +56,50 @@ export default new Vuex.Store({ actions: { firstConnect(context) { // Reset the state when reconnecting starts - context.dispatch('resetState'); + context.dispatch('resetState') // Mark as loading - context.commit('changeWaiting', true); + context.commit('changeWaiting', true) // Do requests context.dispatch('updateConfig') - context.dispatch('updateState') + .then (() => { + context.dispatch('updateState') + }) }, updateConfig(context, uri=`${context.getters.uri}/config`) { context.dispatch('waitingState') - axios.get(uri) - .then(response => { - context.commit('commitConfig', response.data); - context.dispatch('connectedState') - }) - .catch(error => { - context.dispatch('handleHTTPError', error); - }) + var sendRequest = function(resolve, reject) { + axios.get(uri) + .then(response => { + context.commit('commitConfig', response.data) + context.dispatch('connectedState') + resolve() + }) + .catch(error => { + context.dispatch('handleHTTPError', error) + reject(new Error(error)) + }) + } + return new Promise(sendRequest) }, updateState(context, uri=`${context.getters.uri}/state`) { context.dispatch('waitingState') - axios.get(uri) - .then(response => { - context.commit('commitState', response.data); - context.dispatch('connectedState') - }) - .catch(error => { - context.dispatch('handleHTTPError', error); - }) + var sendRequest = function(resolve, reject) { + axios.get(uri) + .then(response => { + context.commit('commitState', response.data) + context.dispatch('connectedState') + resolve() + }) + .catch(error => { + context.dispatch('handleHTTPError', error) + reject(new Error(error)) + }) + } + return new Promise(sendRequest) }, pollTask(context, [taskId, timeout, interval]) { @@ -99,22 +111,22 @@ export default new Vuex.Store({ axios.get(`${context.getters.uri}/task/${taskId}`) .then(response => { console.log(response.data.status) - var result = response.data.status; + var result = response.data.status // If the task ends with success if(result == 'success') { - resolve(response.data.return); + resolve(response.data.return) } // If task ends with an 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 else if (Number(new Date()) < endTime) { - setTimeout(checkCondition, interval, resolve, reject); + setTimeout(checkCondition, interval, resolve, reject) } // Didn't match and too much time, reject! else { - reject(new Error('Polling timed out')); + reject(new Error('Polling timed out')) } }) @@ -140,26 +152,26 @@ export default new Vuex.Store({ }, resetState(context) { - context.commit('changeWaiting', false); - context.commit('changeAvailable', true); - context.commit('commitError', null); - context.commit('commitConfig', {}); - context.commit('commitState', {}); + context.commit('changeWaiting', false) + context.commit('changeAvailable', true) + context.commit('commitError', null) + context.commit('commitConfig', {}) + context.commit('commitState', {}) }, waitingState(context) { - context.commit('changeWaiting', true); + context.commit('changeWaiting', true) }, connectedState(context) { - context.commit('changeWaiting', false); - context.commit('changeAvailable', true); + context.commit('changeWaiting', false) + context.commit('changeAvailable', true) }, errorState(context, msg) { - context.commit('changeWaiting', false); - context.commit('changeAvailable', false); - context.commit('commitError', msg); + context.commit('changeWaiting', false) + context.commit('changeAvailable', false) + context.commit('commitError', msg) UIkit.notification({message: ` ${msg}`, status: 'danger'}) } },