From 79fa61fa330e1dd904550ebee5af340d4c3f2768 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 11 Dec 2015 15:25:04 -0500 Subject: [PATCH] connectivity fixes --- dashboard-ui/apiclient/apiclient.js | 51 ++++++----- dashboard-ui/apiclient/connectionmanager.js | 29 +++++-- dashboard-ui/scripts/itemdetailpage.js | 12 +-- dashboard-ui/scripts/itemlistpage.js | 10 ++- dashboard-ui/scripts/mediacontroller.js | 10 +-- dashboard-ui/scripts/site.js | 94 ++++++++++----------- 6 files changed, 116 insertions(+), 90 deletions(-) diff --git a/dashboard-ui/apiclient/apiclient.js b/dashboard-ui/apiclient/apiclient.js index 99acc3572..f9d79e38a 100644 --- a/dashboard-ui/apiclient/apiclient.js +++ b/dashboard-ui/apiclient/apiclient.js @@ -58,25 +58,32 @@ return serverInfo; }; - var currentUserPromise; + var currentUser; /** * Gets or sets the current user id. */ self.getCurrentUser = function () { - var promise = currentUserPromise; + if (currentUser) { + return new Promise(function (resolve, reject) { - if (promise == null) { - - promise = self.getUser(self.getCurrentUserId()).catch(function (err) { - currentUserPromise = null; - throw err; + resolve(currentUser); }); - - currentUserPromise = promise; } - return promise; + var userId = self.getCurrentUserId(); + + if (!userId) { + return new Promise(function (resolve, reject) { + + reject(); + }); + } + + return self.getUser(userId).then(function (user) { + currentUser = user; + return user; + }); }; /** @@ -112,7 +119,7 @@ }; self.setAuthenticationInfo = function (accessKey, userId) { - currentUserPromise = null; + currentUser = null; serverInfo.AccessToken = accessKey; serverInfo.UserId = userId; @@ -209,18 +216,22 @@ return fetch(request.url, fetchRequest); } + return fetchWithTimeout(request.url, fetchRequest, request.timeout); + } + + function fetchWithTimeout(url, options, timeoutMs) { + return new Promise(function (resolve, reject) { - var timeout = setTimeout(reject, request.timeout); + var timeout = setTimeout(reject, timeoutMs); - fetch(request.url, fetchRequest).then(function (response) { + fetch(url, options).then(function (response) { clearTimeout(timeout); resolve(response); }, function (error) { clearTimeout(timeout); throw error; }); - }); } @@ -330,7 +341,7 @@ var timeout = connectionMode == MediaBrowser.ConnectionMode.Local ? 7000 : 15000; - fetch(url + "/system/info/public", { + fetchWithTimeout(url + "/system/info/public", { method: 'GET', accept: 'application/json' @@ -338,7 +349,7 @@ // Commenting this out since the fetch api doesn't have a timeout option yet //timeout: timeout - }).then(function () { + }, timeout).then(function () { logger.log("Reconnect succeeded to " + url); @@ -357,7 +368,7 @@ setTimeout(function () { tryReconnectInternal(resolve, reject, newConnectionMode, currentRetryCount + 1); - }, 500); + }, 300); } else { reject(); @@ -371,7 +382,7 @@ setTimeout(function () { tryReconnectInternal(resolve, reject, self.serverInfo().LastConnectionMode, 0); - }, 500); + }, 300); }); } @@ -551,14 +562,14 @@ function onWebSocketMessage(msg) { if (msg.MessageType === "UserDeleted") { - currentUserPromise = null; + currentUser = null; } else if (msg.MessageType === "UserUpdated" || msg.MessageType === "UserConfigurationUpdated") { var user = msg.Data; if (user.Id == self.getCurrentUserId()) { - currentUserPromise = null; + currentUser = null; } } diff --git a/dashboard-ui/apiclient/connectionmanager.js b/dashboard-ui/apiclient/connectionmanager.js index e67da395f..f1fd5d594 100644 --- a/dashboard-ui/apiclient/connectionmanager.js +++ b/dashboard-ui/apiclient/connectionmanager.js @@ -117,18 +117,31 @@ return fetch(request.url, fetchRequest); } + return fetchWithTimeout(request.url, fetchRequest, request.timeout); + } + + function fetchWithTimeout(url, options, timeoutMs) { + + logger.log('fetchWithTimeout: timeoutMs: ' + timeoutMs + ', url: ' + url); + return new Promise(function (resolve, reject) { - var timeout = setTimeout(reject, request.timeout); + var timeout = setTimeout(reject, timeoutMs); - fetch(request.url, fetchRequest).then(function (response) { + fetch(url, options).then(function (response) { clearTimeout(timeout); + + logger.log('fetchWithTimeout: succeeded connecting to url: ' + url); + resolve(response); }, function (error) { + clearTimeout(timeout); + + logger.log('fetchWithTimeout: timed out connecting to url: ' + url); + throw error; }); - }); } @@ -155,10 +168,12 @@ request.headers = request.headers || {}; - logger.log('Requesting url without automatic networking: ' + request.url); + logger.log('ConnectionManager requesting url: ' + request.url); return getFetchPromise(request).then(function (response) { + logger.log('ConnectionManager response status: ' + response.status + ', url: ' + request.url); + if (response.status < 400) { if (request.dataType == 'json' || request.headers.accept == 'application/json') { @@ -170,8 +185,10 @@ return Promise.reject(response); } - }, function (error) { - throw error; + }, function (err) { + + logger.log('ConnectionManager request failed to url: ' + request.url); + throw err; }); } diff --git a/dashboard-ui/scripts/itemdetailpage.js b/dashboard-ui/scripts/itemdetailpage.js index e1f90177e..fc06de7ac 100644 --- a/dashboard-ui/scripts/itemdetailpage.js +++ b/dashboard-ui/scripts/itemdetailpage.js @@ -238,11 +238,13 @@ setPeopleHeader(page, item); - $(page).trigger('displayingitem', [{ - - item: item, - context: context - }]); + page.dispatchEvent(new CustomEvent("displayingitem", { + detail: { + item: item, + context: context + }, + bubbles: true + })); Dashboard.hideLoadingMsg(); } diff --git a/dashboard-ui/scripts/itemlistpage.js b/dashboard-ui/scripts/itemlistpage.js index 70df1527f..619bbfff8 100644 --- a/dashboard-ui/scripts/itemlistpage.js +++ b/dashboard-ui/scripts/itemlistpage.js @@ -196,10 +196,12 @@ LibraryMenu.setTitle(name); - $(page).trigger('displayingitem', [{ - - item: item - }]); + page.dispatchEvent(new CustomEvent("displayingitem", { + detail: { + item: item + }, + bubbles: true + })); LibraryBrowser.setLastRefreshed(page); Dashboard.hideLoadingMsg(); diff --git a/dashboard-ui/scripts/mediacontroller.js b/dashboard-ui/scripts/mediacontroller.js index 01b173c20..76cc3469b 100644 --- a/dashboard-ui/scripts/mediacontroller.js +++ b/dashboard-ui/scripts/mediacontroller.js @@ -1008,8 +1008,7 @@ $(apiClient).off("websocketmessage", onWebSocketMessageReceived).on("websocketmessage", onWebSocketMessageReceived); } - Dashboard.ready(function () { - + MediaController.init = function () { if (window.ApiClient) { initializeApiClient(window.ApiClient); } @@ -1017,7 +1016,7 @@ $(ConnectionManager).on('apiclientcreated', function (e, apiClient) { initializeApiClient(apiClient); }); - }); + }; function onCastButtonClicked() { @@ -1027,7 +1026,6 @@ document.addEventListener('headercreated', function () { $('.btnCast').off('click', onCastButtonClicked).on('click', onCastButtonClicked); - }); pageClassOn('pagebeforeshow', "page", function () { @@ -1035,11 +1033,11 @@ var page = this; currentDisplayInfo = null; - }); - pageClassOn('displayingitem', "libraryPage", function (e, info) { + pageClassOn('displayingitem', "libraryPage", function (e) { + var info = e.detail.info; currentDisplayInfo = info; mirrorIfEnabled(info); diff --git a/dashboard-ui/scripts/site.js b/dashboard-ui/scripts/site.js index 67991348a..b625c117e 100644 --- a/dashboard-ui/scripts/site.js +++ b/dashboard-ui/scripts/site.js @@ -767,32 +767,31 @@ var Dashboard = { }); } - if (!Dashboard.getPluginSecurityInfoPromise) { + var cachedInfo = Dashboard.pluginSecurityInfo; + if (cachedInfo) { + return new Promise(function (resolve, reject) { - var deferred = $.Deferred(); - - // Don't let this blow up the dashboard when it fails - apiClient.ajax({ - type: "GET", - url: apiClient.getUrl("Plugins/SecurityInfo"), - dataType: 'json', - - error: function () { - // Don't show normal dashboard errors - } - - }).then(function (result) { - deferred.resolveWith(null, [result]); + resolve(cachedInfo); }); - - Dashboard.getPluginSecurityInfoPromise = deferred; } - return Dashboard.getPluginSecurityInfoPromise; + return apiClient.ajax({ + type: "GET", + url: apiClient.getUrl("Plugins/SecurityInfo"), + dataType: 'json', + + error: function () { + // Don't show normal dashboard errors + } + + }).then(function (result) { + Dashboard.pluginSecurityInfo = result; + return result; + }); }, resetPluginSecurityInfo: function () { - Dashboard.getPluginSecurityInfoPromise = null; + Dashboard.pluginSecurityInfo = null; }, ensureHeader: function (page) { @@ -1466,11 +1465,6 @@ var Dashboard = { } }, - ready: function (fn) { - - Dashboard.initPromise.then(fn); - }, - loadExternalPlayer: function () { var deferred = DeferredBuilder.Deferred(); @@ -1648,7 +1642,11 @@ var AppInfo = {}; if (!Dashboard.isServerlessPage()) { if (server && server.UserId && server.AccessToken) { + Dashboard.showLoadingMsg(); + ConnectionManager.connectToServer(server).then(function (result) { + Dashboard.showLoadingMsg(); + if (result.State == MediaBrowser.ConnectionState.SignedIn) { window.ApiClient = result.ApiClient; } @@ -1871,7 +1869,7 @@ var AppInfo = {}; define('native-promise-only', [bowerPath + '/native-promise-only/lib/npo.src']); } - function init(promiseResolve, hostingAppInfo) { + function init(hostingAppInfo) { if (Dashboard.isRunningInCordova() && browserInfo.android) { define("appstorage", ["cordova/android/appstorage"]); @@ -1978,7 +1976,7 @@ var AppInfo = {}; AppInfo[i] = hostingAppInfo[i]; } - initAfterDependencies(promiseResolve); + initAfterDependencies(); }); } @@ -1990,7 +1988,7 @@ var AppInfo = {}; }); } - function initAfterDependencies(promiseResolve) { + function initAfterDependencies() { var drawer = document.querySelector('.mainDrawerPanel'); drawer.classList.remove('mainDrawerPanelPreInit'); @@ -2075,6 +2073,8 @@ var AppInfo = {}; Promise.all(promises).then(function () { + MediaController.init(); + document.title = Globalize.translateDocument(document.title, 'html'); var mainDrawerPanelContent = document.querySelector('.mainDrawerPanelContent'); @@ -2111,17 +2111,17 @@ var AppInfo = {}; // Don't like having to use jQuery here, but it takes care of making sure that embedded script executes $(mainDrawerPanelContent).html(Globalize.translateDocument(newHtml, 'html')); - onAppReady(promiseResolve); + onAppReady(); }); return; } - onAppReady(promiseResolve); + onAppReady(); }); }); } - function onAppReady(promiseResolve) { + function onAppReady() { var deps = []; @@ -2178,7 +2178,6 @@ var AppInfo = {}; $.mobile.filterHtml = Dashboard.filterHtml; $.mobile.initializePage(); - promiseResolve(); var postInitDependencies = []; @@ -2407,30 +2406,27 @@ var AppInfo = {}; require(initialDependencies, function (isMobile) { - Dashboard.initPromise = new Promise(function (resolve, reject) { + function onWebComponentsReady() { - function onWebComponentsReady() { + var polymerDependencies = []; - var polymerDependencies = []; + require(polymerDependencies, function () { - require(polymerDependencies, function () { - - getHostingAppInfo().then(function (hostingAppInfo) { - init(resolve, hostingAppInfo); - }); + getHostingAppInfo().then(function (hostingAppInfo) { + init(hostingAppInfo); }); - } + }); + } - setBrowserInfo(isMobile); - setAppInfo(); - setDocumentClasses(); + setBrowserInfo(isMobile); + setAppInfo(); + setDocumentClasses(); - if (supportsNativeWebComponents) { - onWebComponentsReady(); - } else { - document.addEventListener('WebComponentsReady', onWebComponentsReady); - } - }); + if (supportsNativeWebComponents) { + onWebComponentsReady(); + } else { + document.addEventListener('WebComponentsReady', onWebComponentsReady); + } }); })();