File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,15 @@ function($Mixer, $log) {
5757
5858 const deferred = this . $$defer ( ) ;
5959
60+ // If user is already logged in resolve the promise immediately to prevent an
61+ // unnecessary computation
62+ if ( this . currentUser ) {
63+ deferred . resolve ( this . currentUser ) ;
64+ // Keep the schema of the promise consistent
65+ deferred . promise . stop = angular . noop ;
66+ return deferred . promise ;
67+ }
68+
6069 // Note the promise is being fulfilled in the next event loop to avoid
6170 // nested computations, otherwise the outer computation will cancel the
6271 // inner one once the scope has been destroyed which will lead to subscription
@@ -84,9 +93,8 @@ function($Mixer, $log) {
8493 return this . $$afterFlush ( deferred . reject , error ) ;
8594 } ) ;
8695
87- const promise = deferred . promise ;
88- promise . stop = computation . stop . bind ( computation ) ;
89- return promise ;
96+ deferred . promise . stop = computation . stop . bind ( computation ) ;
97+ return deferred . promise ;
9098 } ;
9199
92100 // Calls a function with the provided args after flush
Original file line number Diff line number Diff line change @@ -193,6 +193,19 @@ describe('angular-meteor.auth', function() {
193193 scope . $$afterFlush ( '$$throttledDigest' ) ;
194194 } ) ;
195195 } ) ;
196+
197+ it ( 'should resolve promise immediately if user is already logged in' , function ( done ) {
198+ Accounts . login ( 'tempUser' ) . onEnd ( function ( ) {
199+ var spy = jasmine . createSpy ( ) . and . returnValue ( true ) ;
200+
201+ scope . $awaitUser ( spy ) . then ( function ( user ) {
202+ expect ( user . username ) . toEqual ( 'tempUser' ) ;
203+ done ( )
204+ } ) ;
205+
206+ scope . $$throttledDigest ( ) ;
207+ } ) ;
208+ } ) ;
196209 } ) ;
197210
198211 describe ( '$waitForUser()' , function ( ) {
You can’t perform that action at this time.
0 commit comments