Skip to content
This repository has been archived by the owner on Dec 9, 2023. It is now read-only.

Commit

Permalink
reduce promise layers
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed Aug 19, 2020
1 parent 801fdfa commit 556ee44
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions connectionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,19 +689,16 @@ function afterConnectValidated(
connectionMode,
serverUrl,
verifyLocalAuthentication,
options,
resolve) {
options) {

options = options || {};

if (verifyLocalAuthentication && server.AccessToken) {

validateAuthentication(instance, server, serverUrl).then((fullSystemInfo) => {
return validateAuthentication(instance, server, serverUrl).then((fullSystemInfo) => {

afterConnectValidated(instance, server, credentials, fullSystemInfo || systemInfo, connectionMode, serverUrl, false, options, resolve);
return afterConnectValidated(instance, server, credentials, fullSystemInfo || systemInfo, connectionMode, serverUrl, false, options);
});

return;
}

updateServerInfo(server, systemInfo);
Expand Down Expand Up @@ -737,47 +734,48 @@ function afterConnectValidated(
result.ApiClient.updateServerInfo(server, serverUrl);

const resolveActions = function () {
resolve(result);

events.trigger(instance, 'connected', [result]);

return Promise.resolve(result);
};

if (result.State === 'SignedIn') {
afterConnected(instance, result.ApiClient, options);

onLocalUserSignIn(instance, server, serverUrl).then(resolveActions, resolveActions);
return onLocalUserSignIn(instance, server, serverUrl).then(resolveActions, resolveActions);
}
else {
resolveActions();
return resolveActions();
}
}

function onSuccessfulConnection(instance, server, systemInfo, connectionMode, serverUrl, options, resolve) {
function onSuccessfulConnection(instance, server, systemInfo, connectionMode, serverUrl, options) {

const credentials = instance.credentialProvider().credentials();
options = options || {};
if (credentials.ConnectAccessToken && options.enableAutoLogin !== false) {

ensureConnectUser(instance, credentials).then(() => {
return ensureConnectUser(instance, credentials).then(() => {

if (server.ExchangeToken) {
addAuthenticationInfoFromConnect(instance, server, systemInfo, serverUrl, credentials).then(() => {
return addAuthenticationInfoFromConnect(instance, server, systemInfo, serverUrl, credentials).then(() => {

afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options, resolve);
return afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options);

}, () => {

afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options, resolve);
return afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options);
});

} else {

afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options, resolve);
return afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options);
}
});
}
else {
afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options, resolve);
return afterConnectValidated(instance, server, credentials, systemInfo, connectionMode, serverUrl, true, options);
}
}

Expand All @@ -786,10 +784,7 @@ function resolveIfAvailable(instance, url, server, result, connectionMode, serve
const promise = instance.validateServerAddress ? instance.validateServerAddress(instance, ajax, url) : Promise.resolve();

return promise.then(() => {
return new Promise(function (resolve, reject) {

onSuccessfulConnection(instance, server, result, connectionMode, serverUrl, options, resolve);
});
return onSuccessfulConnection(instance, server, result, connectionMode, serverUrl, options);
}, () => {
console.log('minServerVersion requirement not met. Server version: ' + result.Version);
return {
Expand All @@ -805,7 +800,7 @@ export default class ConnectionManager {
appStorage,
apiClientFactory,
serverDiscoveryFn,
wakeOnLanFn,
wakeOnLan,
appName,
appVersion,
deviceName,
Expand Down Expand Up @@ -846,7 +841,7 @@ export default class ConnectionManager {
this.capabilities = capabilitiesFn;

this.apiClientFactory = apiClientFactory;
this.wakeOnLanFn = wakeOnLanFn;
this.wakeOnLan = wakeOnLan;
this.serverDiscoveryFn = serverDiscoveryFn;
this.devicePixelRatio = devicePixelRatio;
}
Expand Down Expand Up @@ -973,7 +968,7 @@ export default class ConnectionManager {

const ApiClient = this.apiClientFactory;

apiClient = new ApiClient(this.appStorage, this.wakeOnLanFn, serverUrl, this.appName(), this.appVersion(), this.deviceName(), this.deviceId(), this.devicePixelRatio);
apiClient = new ApiClient(this.appStorage, this.wakeOnLan, serverUrl, this.appName(), this.appVersion(), this.deviceName(), this.deviceId(), this.devicePixelRatio);

apiClient.rejectInsecureAddresses = this.rejectInsecureAddresses;

Expand Down

0 comments on commit 556ee44

Please sign in to comment.