@@ -192,8 +192,9 @@ static bool PushBackIfMissing(const T& entry, std::vector<T>* v) {
192192// Store a unique listener of type T in a listener_vector and unique auth
193193// object in auth_vector. Both vectors must be in sync, i.e addition must
194194// either succeed or fail otherwise this method asserts.
195+ // Return whether the listener is added.
195196template <typename T>
196- static void AddListener (T listener, std::vector<T>* listener_vector, Auth* auth,
197+ static bool AddListener (T listener, std::vector<T>* listener_vector, Auth* auth,
197198 std::vector<Auth*>* auth_vector, Mutex* mutex) {
198199 MutexLock lock (*mutex);
199200 // Add to array of listeners if not already there.
@@ -203,26 +204,38 @@ static void AddListener(T listener, std::vector<T>* listener_vector, Auth* auth,
203204
204205 // The listener and Auth should either point at each other or not point
205206 // at each other.
206- FIREBASE_ASSERT_RETURN_VOID ( listener_added == auth_added);
207+ FIREBASE_ASSERT_RETURN ( false , listener_added == auth_added);
207208 (void )auth_added;
208- (void )listener_added;
209+
210+ return listener_added;
209211}
210212
211213void Auth::AddAuthStateListener (AuthStateListener* listener) {
212214 if (!auth_data_) return ;
213- AddListener (listener, &auth_data_->listeners , this , &listener->auths_ ,
214- &auth_data_->listeners_mutex );
215+ bool added = AddListener (listener, &auth_data_->listeners , this ,
216+ &listener->auths_ , &auth_data_->listeners_mutex );
217+ // If the listener is registered successfully and persistent cache has been
218+ // loaded, trigger OnAuthStateChanged() immediately. Otherwise, wait until
219+ // the cache is loaded, through AuthStateListener event
220+ if (added && auth_data_->persistent_cache_loaded ) {
221+ listener->OnAuthStateChanged (this );
222+ }
215223}
216224
217225void Auth::AddIdTokenListener (IdTokenListener* listener) {
218226 if (!auth_data_) return ;
219- int listener_count = auth_data_->id_token_listeners .size ();
220- AddListener (listener, &auth_data_->id_token_listeners , this ,
221- &listener->auths_ , &auth_data_->listeners_mutex );
227+ bool added = AddListener (listener, &auth_data_->id_token_listeners , this ,
228+ &listener->auths_ , &auth_data_->listeners_mutex );
222229 // AddListener is valid even if the listener is already registered.
223230 // This makes sure that we only increase the reference count if a listener
224231 // was actually added.
225- if (auth_data_->id_token_listeners .size () > listener_count) {
232+ if (added) {
233+ // If the listener is registered successfully and persistent cache has been
234+ // loaded, trigger OnAuthStateChanged() immediately. Otherwise, wait until
235+ // the cache is loaded, through AuthStateListener event
236+ if (auth_data_->persistent_cache_loaded ) {
237+ listener->OnIdTokenChanged (this );
238+ }
226239 EnableTokenAutoRefresh (auth_data_);
227240 }
228241}
@@ -321,10 +334,15 @@ static inline bool VectorContains(const T& entry, const std::vector<T>& v) {
321334 /* Notify the listener. */ \
322335 listener->notification_method (auth_data->auth ); \
323336 } \
337+ \
338+ /* Auth should have loaded persistent cache if exists when the listener */ \
339+ /* event is triggered for the first time. */ \
340+ auth_data->persistent_cache_loaded = true ; \
324341 }
325342
326343AUTH_NOTIFY_LISTENERS (NotifyAuthStateListeners, " Auth state" , listeners,
327344 OnAuthStateChanged);
345+
328346AUTH_NOTIFY_LISTENERS (NotifyIdTokenListeners, " ID token" , id_token_listeners,
329347 OnIdTokenChanged);
330348
0 commit comments