From 876c5df6f5bd1749df4c2f1f81fe517edd76798d Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 19 Feb 2019 17:17:30 +0000 Subject: [PATCH] Added method to poll a tasks status --- src/store.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/store.js b/src/store.js index c5c286bc..df93ab44 100644 --- a/src/store.js +++ b/src/store.js @@ -89,6 +89,40 @@ export default new Vuex.Store({ }) }, + pollTask(context, [taskId, timeout, interval]) { + var endTime = Number(new Date()) + (timeout || 30000); + interval = interval || 500; + + var checkCondition = function(resolve, reject) { + // If the condition is met, we're done! + axios.get(`${context.getters.uri}/task/${taskId}`) + .then(response => { + console.log(response.data.status) + var result = response.data.status; + // If the task ends with success + if(result == 'success') { + resolve(response.data.return); + } + // If task ends with an error + else if (result == 'error') { + 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); + } + // Didn't match and too much time, reject! + else { + reject(new Error('Polling timed out')); + } + }) + + }; + + return new Promise(checkCondition); + + }, + handleHTTPError(context, error) { var errormsg = ''; if (error.response) {