Skip to content
Snippets Groups Projects
Commit ed190535 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

tidy up some of our AppState promise code and make it easier to reason and a bit more concise.

parent b84b2f56
No related branches found
No related tags found
No related merge requests found
......@@ -13,12 +13,12 @@ CorvusServices.factory('AppState', ['$rootScope', '$routeParams', '$q', '$locati
// 2. appstate:organization
var initPromise;
var currentUserPromise;
var service = {
model: {
setupToken: null,
currentUserPromise: null,
currentUser: null,
currentOrgSlug: null,
currentOrg: null,
......@@ -32,8 +32,7 @@ CorvusServices.factory('AppState', ['$rootScope', '$routeParams', '$q', '$locati
initPromise = deferred.promise;
// just make sure we grab the current user
service.model.currentUserPromise = service.refreshCurrentUser();
service.model.currentUserPromise.then(function (user) {
service.refreshCurrentUser().then(function (user) {
deferred.resolve();
}, function (error) {
deferred.resolve();
......@@ -44,7 +43,7 @@ CorvusServices.factory('AppState', ['$rootScope', '$routeParams', '$q', '$locati
},
clearState: function() {
service.model.currentUserPromise = null;
currentUserPromise = null;
service.model.currentUser = null;
service.model.currentOrgSlug = null;
service.model.currentOrg = null;
......@@ -62,10 +61,9 @@ CorvusServices.factory('AppState', ['$rootScope', '$routeParams', '$q', '$locati
},
refreshCurrentUser: function() {
var deferred = $q.defer();
// this is meant to be called once on app startup
User.current(function(result) {
var userRefresh = User.current(function (result) {
service.model.currentUser = result;
// add isMember(orgSlug) method to the object
......@@ -103,13 +101,15 @@ CorvusServices.factory('AppState', ['$rootScope', '$routeParams', '$q', '$locati
$rootScope.$broadcast('appstate:user', result);
deferred.resolve(result);
}, function(error) {
}, function (error) {
console.log('unable to get current user', error);
deferred.reject(error);
});
return deferred.promise;
// NOTE: every time we refresh the user we update our current promise to ensure that
// we can guarantee we've resolved the current user
currentUserPromise = userRefresh.$promise;
return currentUserPromise;
},
switchOrg: function(org_slug) {
......@@ -167,19 +167,11 @@ CorvusServices.factory('AppState', ['$rootScope', '$routeParams', '$q', '$locati
// this code is here to ensure that we have resolved our currentUser BEFORE we execute any other
// code meant to establish app context based on the current route
if (service.model.currentUserPromise) {
// we have an outstanding promise for getting current user, so wait for that first
service.model.currentUserPromise.then(function(user) {
service.model.currentUserPromise = null;
service.routeChangedImpl(event);
}, function(error) {
service.model.currentUserPromise = null;
service.routeChangedImpl(event);
});
} else {
// we must already have the user, so carry on
currentUserPromise.then(function (user) {
service.routeChangedImpl(event);
}
}, function (error) {
service.routeChangedImpl(event);
});
},
routeChangedImpl: function(event) {
......@@ -273,7 +265,7 @@ CorvusServices.factory('AppState', ['$rootScope', '$routeParams', '$q', '$locati
// login just took place, so lets force a refresh of the current user
$rootScope.$on("appstate:login", function(event, session_id) {
service.model.currentUserPromise = service.refreshCurrentUser();
service.refreshCurrentUser();
});
// logout just took place, do some cleanup
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment