add 10 second timeout to lbrytv status call

This commit is contained in:
seanyesmunt 2020-03-16 17:57:57 -04:00 committed by Sean Yesmunt
parent b5bb4b46d7
commit b005ed76fe
2 changed files with 21 additions and 1 deletions

View file

@ -23,6 +23,7 @@ import useIsMobile from 'effects/use-is-mobile';
// @if TARGET='web'
import OpenInAppLink from 'component/openInAppLink';
import YoutubeWelcome from 'component/youtubeWelcome';
import fetchWithTimeout from 'util/fetch';
// @endif
export const MAIN_WRAPPER_CLASS = 'main-wrapper';
@ -251,8 +252,9 @@ function App(props: Props) {
}, [syncError, pathname, isAuthenticated]);
// @if TARGET='web'
// This should all be moved into lbrytv/component/...
useEffect(() => {
fetch(`${LBRY_TV_API}/internal/status`)
fetchWithTimeout(10000, fetch(`${LBRY_TV_API}/internal/status`))
.then(response => response.json())
.then(status => {
if (status.general_state !== 'ok') {
@ -260,6 +262,7 @@ function App(props: Props) {
}
})
.catch(err => {
console.log('err', err);
setCurrentlyDegradedPerformance(true);
});
}, []);

17
ui/util/fetch.js Normal file
View file

@ -0,0 +1,17 @@
export default function fetchWithTimeout(ms, promise) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error('promise timeout'));
}, ms);
promise.then(
res => {
clearTimeout(timeoutId);
resolve(res);
},
err => {
clearTimeout(timeoutId);
reject(err);
}
);
});
}