Improve OAuth polling

This commit is contained in:
JonnyWong16 2018-09-30 21:05:12 -07:00
parent dbc53ca710
commit a94207691f

View file

@ -611,7 +611,6 @@ function PlexOAuth(success, error, pre) {
if (typeof pre === "function") { if (typeof pre === "function") {
pre() pre()
} }
clearTimeout(polling);
closePlexOAuthWindow(); closePlexOAuthWindow();
plex_oauth_window = PopupCenter('', 'Plex-OAuth', 600, 700); plex_oauth_window = PopupCenter('', 'Plex-OAuth', 600, 700);
$(plex_oauth_window.document.body).html(plex_oauth_loader); $(plex_oauth_window.document.body).html(plex_oauth_loader);
@ -621,9 +620,9 @@ function PlexOAuth(success, error, pre) {
const code = data.code; const code = data.code;
plex_oauth_window.location = 'https://app.plex.tv/auth/#!?clientID=' + x_plex_headers['X-Plex-Client-Identifier'] + '&code=' + code; plex_oauth_window.location = 'https://app.plex.tv/auth/#!?clientID=' + x_plex_headers['X-Plex-Client-Identifier'] + '&code=' + code;
polling = pin;
(function poll() { (function poll() {
polling = setTimeout(function () {
$.ajax({ $.ajax({
url: 'https://plex.tv/api/v2/pins/' + pin, url: 'https://plex.tv/api/v2/pins/' + pin,
type: 'GET', type: 'GET',
@ -636,22 +635,21 @@ function PlexOAuth(success, error, pre) {
} }
} }
}, },
error: function () { error: function (jqXHR, textStatus, errorThrown) {
if (textStatus !== "timeout") {
closePlexOAuthWindow(); closePlexOAuthWindow();
if (typeof error === "function") { if (typeof error === "function") {
error() error()
} }
},
complete: function () {
if (!plex_oauth_window.closed){
poll();
} else {
clearTimeout(polling);
} }
}, },
timeout: 1000 complete: function () {
if (!plex_oauth_window.closed && polling === pin){
setTimeout(function() {poll()}, 1000);
}
},
timeout: 10000
}); });
}, 1000);
})(); })();
}, function () { }, function () {
closePlexOAuthWindow(); closePlexOAuthWindow();