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