diff --git a/assets/js/embed.js b/assets/js/embed.js index 283bc06d..1f742864 100644 --- a/assets/js/embed.js +++ b/assets/js/embed.js @@ -1,5 +1,5 @@ -function get_playlist(plid, timeouts = 1) { - if (timeouts >= 10) { +function get_playlist(plid, retries = 5) { + if (retries <= 0) { console.log('Failed to pull playlist'); return; } @@ -18,7 +18,6 @@ function get_playlist(plid, timeouts = 1) { xhr.responseType = 'json'; xhr.timeout = 20000; xhr.open('GET', plid_url, true); - xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { @@ -51,10 +50,17 @@ function get_playlist(plid, timeouts = 1) { } } + xhr.onerror = function () { + console.log('Pulling playlist failed... ' + retries + '/5'); + setTimeout(function () { get_playlist(plid, retries - 1) }, 1000); + } + xhr.ontimeout = function () { - console.log('Pulling playlist timed out... ' + timeouts + '/10'); - get_playlist(plid, timeouts++); + console.log('Pulling playlist failed... ' + retries + '/5'); + get_playlist(plid, retries - 1); } + + xhr.send(); } if (video_data.plid) { diff --git a/assets/js/notifications.js b/assets/js/notifications.js index 7a112350..4e7be42d 100644 --- a/assets/js/notifications.js +++ b/assets/js/notifications.js @@ -1,15 +1,14 @@ var notifications, delivered; -function get_subscriptions(callback, timeouts = 1) { - if (timeouts >= 10) { - return +function get_subscriptions(callback, retries = 5) { + if (retries <= 0) { + return; } var xhr = new XMLHttpRequest(); xhr.responseType = 'json'; xhr.timeout = 20000; xhr.open('GET', '/api/v1/auth/subscriptions', true); - xhr.send(null); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { @@ -20,10 +19,17 @@ function get_subscriptions(callback, timeouts = 1) { } } + xhr.onerror = function () { + console.log('Pulling subscriptions failed... ' + retries + '/5'); + setTimeout(function () { get_subscriptions(callback, retries - 1) }, 1000); + } + xhr.ontimeout = function () { - console.log('Pulling subscriptions timed out... ' + timeouts + '/10'); - get_subscriptions(callback, timeouts++); + console.log('Pulling subscriptions failed... ' + retries + '/5'); + get_subscriptions(callback, retries - 1); } + + xhr.send(); } function create_notification_stream(subscriptions) { @@ -77,7 +83,7 @@ function create_notification_stream(subscriptions) { notifications.onerror = function (event) { console.log('Something went wrong with notifications, trying to reconnect...'); notifications.close(); - get_subscriptions(create_notification_stream); + setTimeout(function () { get_subscriptions(create_notification_stream) }, 100); } notifications.ontimeout = function (event) { @@ -97,6 +103,7 @@ window.addEventListener('load', function (e) { } else { setTimeout(function () { if (!localStorage.getItem('stream')) { + notifications = true; get_subscriptions(create_notification_stream); localStorage.setItem('stream', true); } @@ -110,6 +117,7 @@ window.addEventListener('load', function (e) { } else { setTimeout(function () { if (!localStorage.getItem('stream')) { + notifications = true; get_subscriptions(create_notification_stream); localStorage.setItem('stream', true); } diff --git a/assets/js/player.js b/assets/js/player.js index ae9a8b25..37dd1dcf 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -218,7 +218,6 @@ if (!video_data.params.listen && video_data.params.annotations) { xhr.responseType = 'text'; xhr.timeout = 60000; xhr.open('GET', '/api/v1/annotations/' + video_data.id, true); - xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { @@ -251,6 +250,8 @@ if (!video_data.params.listen && video_data.params.annotations) { window.open(path, '_blank'); } }); + + xhr.send(); } // Since videojs-share can sometimes be blocked, we defer it until last diff --git a/assets/js/subscribe_widget.js b/assets/js/subscribe_widget.js index 7a7f806d..1201a554 100644 --- a/assets/js/subscribe_widget.js +++ b/assets/js/subscribe_widget.js @@ -7,8 +7,8 @@ if (subscribe_button.getAttribute('data-type') === 'subscribe') { subscribe_button.onclick = unsubscribe; } -function subscribe(timeouts = 1) { - if (timeouts >= 10) { +function subscribe(retries = 5) { + if (retries <= 0) { console.log('Failed to subscribe.'); return; } @@ -20,7 +20,6 @@ function subscribe(timeouts = 1) { xhr.timeout = 20000; xhr.open('POST', url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - xhr.send('csrf_token=' + subscribe_data.csrf_token); var fallback = subscribe_button.innerHTML; subscribe_button.onclick = unsubscribe; @@ -35,14 +34,21 @@ function subscribe(timeouts = 1) { } } + xhr.onerror = function () { + console.log('Subscribing failed... ' + retries + '/5'); + setTimeout(function () { subscribe(retries - 1) }, 1000); + } + xhr.ontimeout = function () { - console.log('Subscribing timed out... ' + timeouts + '/10'); - subscribe(timeouts++); + console.log('Subscribing failed... ' + retries + '/5'); + subscribe(retries - 1); } + + xhr.send('csrf_token=' + subscribe_data.csrf_token); } -function unsubscribe(timeouts = 1) { - if (timeouts >= 10) { +function unsubscribe(retries = 5) { + if (retries <= 0) { console.log('Failed to subscribe'); return; } @@ -54,7 +60,6 @@ function unsubscribe(timeouts = 1) { xhr.timeout = 20000; xhr.open('POST', url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - xhr.send('csrf_token=' + subscribe_data.csrf_token); var fallback = subscribe_button.innerHTML; subscribe_button.onclick = subscribe; @@ -69,8 +74,15 @@ function unsubscribe(timeouts = 1) { } } + xhr.onerror = function () { + console.log('Unsubscribing failed... ' + retries + '/5'); + setTimeout(function () { unsubscribe(retries - 1) }, 1000); + } + xhr.ontimeout = function () { - console.log('Unsubscribing timed out... ' + timeouts + '/10'); - unsubscribe(timeouts++); + console.log('Unsubscribing failed... ' + retries + '/5'); + unsubscribe(retries - 1); } + + xhr.send('csrf_token=' + subscribe_data.csrf_token); } diff --git a/assets/js/themes.js b/assets/js/themes.js index 178ff112..826c6597 100644 --- a/assets/js/themes.js +++ b/assets/js/themes.js @@ -9,10 +9,11 @@ toggle_theme.addEventListener('click', function () { xhr.responseType = 'json'; xhr.timeout = 20000; xhr.open('GET', url, true); - xhr.send(); set_mode(dark_mode); localStorage.setItem('dark_mode', dark_mode); + + xhr.send(); }); window.addEventListener('storage', function (e) { diff --git a/assets/js/watch.js b/assets/js/watch.js index 80da3ee6..fb4ab4b4 100644 --- a/assets/js/watch.js +++ b/assets/js/watch.js @@ -109,10 +109,10 @@ function number_with_separator(val) { return val; } -function get_playlist(plid, timeouts = 1) { +function get_playlist(plid, retries = 5) { playlist = document.getElementById('playlist'); - if (timeouts >= 10) { + if (retries <= 0) { console.log('Failed to pull playlist'); playlist.innerHTML = ''; return; @@ -136,7 +136,6 @@ function get_playlist(plid, timeouts = 1) { xhr.responseType = 'json'; xhr.timeout = 20000; xhr.open('GET', plid_url, true); - xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { @@ -174,20 +173,31 @@ function get_playlist(plid, timeouts = 1) { } } + xhr.onerror = function () { + playlist = document.getElementById('playlist'); + playlist.innerHTML = + '