update promises

This commit is contained in:
Luke Pulverenti 2015-11-29 17:53:05 -05:00
parent cc53792556
commit 4a80f8d99b

View File

@ -52,13 +52,12 @@
return list1;
}
function resolveWithFailure(deferred) {
function resolveFailure(resolve) {
deferred.resolveWith(null, [
{
resolve({
State: MediaBrowser.ConnectionState.Unavailable,
ConnectUser: self.connectUser()
}]);
});
}
function updateServerInfo(server, systemInfo) {
@ -862,7 +861,7 @@
logger.log('Begin connectToServers, with ' + servers.length + ' servers');
var deferred = DeferredBuilder.Deferred();
return new Promise(function (resolve, reject) {
if (servers.length == 1) {
@ -876,7 +875,7 @@
}
logger.log('resolving connectToServers with result.State: ' + result.State);
deferred.resolveWith(null, [result]);
resolve(result);
});
@ -889,30 +888,28 @@
if (result.State == MediaBrowser.ConnectionState.SignedIn) {
deferred.resolveWith(null, [result]);
resolve(result);
} else {
deferred.resolveWith(null, [
{
resolve({
Servers: servers,
State: (!servers.length && !self.connectUser()) ? MediaBrowser.ConnectionState.ConnectSignIn : MediaBrowser.ConnectionState.ServerSelection,
ConnectUser: self.connectUser()
}]);
});
}
});
} else {
deferred.resolveWith(null, [
{
resolve({
Servers: servers,
State: (!servers.length && !self.connectUser()) ? MediaBrowser.ConnectionState.ConnectSignIn : MediaBrowser.ConnectionState.ServerSelection,
ConnectUser: self.connectUser()
}]);
});
}
}
return deferred.promise();
});
};
function beginWakeServer(server) {
@ -929,7 +926,7 @@
self.connectToServer = function (server, options) {
var deferred = DeferredBuilder.Deferred();
return new Promise(function (resolve, reject) {
var tests = [];
@ -945,9 +942,8 @@
var wakeOnLanSendTime = new Date().getTime();
options = options || {};
testNextConnectionMode(tests, 0, server, wakeOnLanSendTime, options, deferred);
return deferred.promise();
testNextConnectionMode(tests, 0, server, wakeOnLanSendTime, options, resolve);
});
};
function stringEqualsIgnoreCase(str1, str2) {
@ -955,12 +951,12 @@
return (str1 || '').toLowerCase() == (str2 || '').toLowerCase();
}
function testNextConnectionMode(tests, index, server, wakeOnLanSendTime, options, deferred) {
function testNextConnectionMode(tests, index, server, wakeOnLanSendTime, options, resolve) {
if (index >= tests.length) {
logger.log('Tested all connection modes. Failing server connection.');
resolveWithFailure(deferred);
resolveFailure(resolve);
return;
}
@ -985,7 +981,7 @@
}
if (skipTest || !address) {
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, deferred);
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, resolve);
return;
}
@ -994,7 +990,7 @@
tryConnect(address, timeout).then(function (result) {
logger.log('calling onSuccessfulConnection with connection mode ' + mode + ' with server ' + server.Name);
onSuccessfulConnection(server, result, mode, options, deferred);
onSuccessfulConnection(server, result, mode, options, resolve);
}, function () {
@ -1006,16 +1002,16 @@
// TODO: Implement delay and retry
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, deferred);
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, resolve);
} else {
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, deferred);
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, resolve);
}
});
}
function onSuccessfulConnection(server, systemInfo, connectionMode, options, deferred) {
function onSuccessfulConnection(server, systemInfo, connectionMode, options, resolve) {
var credentials = credentialProvider.credentials();
if (credentials.ConnectAccessToken) {
@ -1025,31 +1021,31 @@
if (server.ExchangeToken) {
addAuthenticationInfoFromConnect(server, connectionMode, credentials).then(function () {
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, deferred);
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, resolve);
}, function () {
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, deferred);
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, resolve);
});
} else {
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, deferred);
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, resolve);
}
});
}
else {
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, deferred);
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, resolve);
}
}
function afterConnectValidated(server, credentials, systemInfo, connectionMode, verifyLocalAuthentication, options, deferred) {
function afterConnectValidated(server, credentials, systemInfo, connectionMode, verifyLocalAuthentication, options, resolve) {
if (verifyLocalAuthentication && server.AccessToken) {
validateAuthentication(server, connectionMode).then(function () {
afterConnectValidated(server, credentials, systemInfo, connectionMode, false, options, deferred);
afterConnectValidated(server, credentials, systemInfo, connectionMode, false, options, resolve);
});
return;
@ -1085,7 +1081,7 @@
afterConnected(result.ApiClient, options);
}
deferred.resolveWith(null, [result]);
resolve(result);
Events.trigger(self, 'connected', [result]);
}
@ -1108,18 +1104,18 @@
self.connectToAddress = function (address) {
var deferred = DeferredBuilder.Deferred();
return new Promise(function (resolve, reject) {
if (!address) {
deferred.reject();
return deferred.promise();
reject();
return;
}
address = normalizeAddress(address);
function onFail() {
logger.log('connectToAddress ' + address + ' failed');
resolveWithFailure(deferred);
resolveFailure(resolve);
}
tryConnect(address, defaultTimeout).then(function (publicInfo) {
@ -1132,15 +1128,11 @@
};
updateServerInfo(server, publicInfo);
self.connectToServer(server).then(function (result) {
deferred.resolveWith(null, [result]);
self.connectToServer(server).then(resolve, onFail);
}, onFail);
}, onFail);
return deferred.promise();
});
};
self.loginToConnect = function (username, password) {