@@ -192,8 +192,9 @@ static bool PushBackIfMissing(const T& entry, std::vector<T>* v) {
192
192
// Store a unique listener of type T in a listener_vector and unique auth
193
193
// object in auth_vector. Both vectors must be in sync, i.e addition must
194
194
// either succeed or fail otherwise this method asserts.
195
+ // Return whether the listener is added.
195
196
template <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,
197
198
std::vector<Auth*>* auth_vector, Mutex* mutex) {
198
199
MutexLock lock (*mutex);
199
200
// 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,
203
204
204
205
// The listener and Auth should either point at each other or not point
205
206
// at each other.
206
- FIREBASE_ASSERT_RETURN_VOID ( listener_added == auth_added);
207
+ FIREBASE_ASSERT_RETURN ( false , listener_added == auth_added);
207
208
(void )auth_added;
208
- (void )listener_added;
209
+
210
+ return listener_added;
209
211
}
210
212
211
213
void Auth::AddAuthStateListener (AuthStateListener* listener) {
212
214
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
+ }
215
223
}
216
224
217
225
void Auth::AddIdTokenListener (IdTokenListener* listener) {
218
226
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 );
222
229
// AddListener is valid even if the listener is already registered.
223
230
// This makes sure that we only increase the reference count if a listener
224
231
// 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
+ }
226
239
EnableTokenAutoRefresh (auth_data_);
227
240
}
228
241
}
@@ -321,10 +334,15 @@ static inline bool VectorContains(const T& entry, const std::vector<T>& v) {
321
334
/* Notify the listener. */ \
322
335
listener->notification_method (auth_data->auth ); \
323
336
} \
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 ; \
324
341
}
325
342
326
343
AUTH_NOTIFY_LISTENERS (NotifyAuthStateListeners, " Auth state" , listeners,
327
344
OnAuthStateChanged);
345
+
328
346
AUTH_NOTIFY_LISTENERS (NotifyIdTokenListeners, " ID token" , id_token_listeners,
329
347
OnIdTokenChanged);
330
348
0 commit comments