Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
ed190535
Commit
ed190535
authored
9 years ago
by
Allen Gilliland
Browse files
Options
Downloads
Patches
Plain Diff
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
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
resources/frontend_client/app/services.js
+15
-23
15 additions, 23 deletions
resources/frontend_client/app/services.js
with
15 additions
and
23 deletions
resources/frontend_client/app/services.js
+
15
−
23
View file @
ed190535
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment