From e4ff8cb9026d2eaaadf2bbf054b150a13b630b60 Mon Sep 17 00:00:00 2001 From: Rhys Mills Date: Mon, 18 Nov 2024 12:00:29 +0000 Subject: [PATCH] Restore polling in pandaReqwest, which is used on the usages tab --- public/video-ui/src/services/pandaReqwest.js | 39 ++++++++++++-------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/public/video-ui/src/services/pandaReqwest.js b/public/video-ui/src/services/pandaReqwest.js index 557165d90..9b5a9e584 100644 --- a/public/video-ui/src/services/pandaReqwest.js +++ b/public/video-ui/src/services/pandaReqwest.js @@ -9,15 +9,19 @@ const checkStatus = (res) => { } } -export const pandaFetch = (url, body) => { - return new Promise(function(resolve, reject) { +export const poll = (url, body, timeout) => { + const endTime = Number(new Date()) + timeout; + const interval = 100; + + const makeRequest = (resolve, reject) => { fetch(url, body) - .then(checkStatus) - .then(response => response.json()) - .then(res => { - resolve(res)} - ) - .catch(err => { + .then(checkStatus) + .then(response => response.json()) + .then(res => { + resolve(res)} + ) + .catch(err => { + if (Number(new Date()) < endTime) { if (err.status == 419) { const store = getStore(); const reauthUrl = store.getState().config.reauthUrl; @@ -32,15 +36,20 @@ export const pandaFetch = (url, body) => { error => { throw error; }); - } else { - reject(err); + setTimeout(makeRequest, interval, resolve, reject); } - }); - }); -} + } else { + reject(err); + } + }); + }; + + return new Promise(makeRequest); +}; -export const pandaReqwest = (reqwestBody) => { +// when `timeout` > 0, the request will be retried every 100ms until success or timeout +export const pandaReqwest = (reqwestBody, timeout = 0) => { const payload = Object.assign({ method: 'get' }, reqwestBody); if (payload.data) { @@ -53,5 +62,5 @@ export const pandaReqwest = (reqwestBody) => { } } - return pandaFetch(reqwestBody.url, payload); + return poll(reqwestBody.url, payload, timeout); }