@@ -229,26 +229,23 @@ export class IndexedDbPersistence implements Persistence {
229
229
) ;
230
230
}
231
231
232
- if ( currentPrimary != null ) {
233
- if ( this . isPrimary ) {
234
- // If we are the primary lease holder, we refresh the client
235
- // metadata and the primary lease after the default interval.
236
- return this . acquireOrExtendPrimaryLease ( txn ) . next (
237
- ( ) => CLIENT_METADATA_REFRESH_INTERVAL_MS
238
- ) ;
239
- } else {
240
- // If another client currently holds the lease, we use a custom
241
- // refresh interval and schedule a lease refresh immediately after
242
- // the current lease is expected to expire.
243
- const remainingLifetimeMs =
244
- Date . now ( ) -
245
- ( currentPrimary . leaseTimestampMs +
246
- CLIENT_METADATA_MAX_AGE_MS ) ;
247
- return Math . min (
248
- MINIMUM_REFRESH_INTERVAL_MS ,
249
- remainingLifetimeMs + 1
250
- ) ;
251
- }
232
+ if ( this . isPrimary ) {
233
+ // If we are the primary lease holder, we refresh the client
234
+ // metadata and the primary lease after the default interval.
235
+ return this . acquireOrExtendPrimaryLease ( txn ) . next (
236
+ ( ) => CLIENT_METADATA_REFRESH_INTERVAL_MS
237
+ ) ;
238
+ } else if ( currentPrimary != null ) {
239
+ // If another client currently holds the lease, we use a custom
240
+ // refresh interval and schedule a lease refresh immediately after
241
+ // the current lease is expected to expire.
242
+ const remainingLifetimeMs =
243
+ Date . now ( ) -
244
+ ( currentPrimary . leaseTimestampMs + CLIENT_METADATA_MAX_AGE_MS ) ;
245
+ return Math . min (
246
+ MINIMUM_REFRESH_INTERVAL_MS ,
247
+ remainingLifetimeMs + 1
248
+ ) ;
252
249
} else {
253
250
// If there is no current leaseholder, but we are not ourselves
254
251
// eligible to directly obtain the lease (based on our foreground
@@ -316,20 +313,17 @@ export class IndexedDbPersistence implements Persistence {
316
313
return PersistencePromise . resolve ( this . isLocalClient ( currentPrimary ) ) ;
317
314
}
318
315
316
+ let canActAsPrimary = this . inForeground ;
317
+
319
318
// Check if this client is eligible for a primary lease based on its
320
319
// visibility state and the visibility state of all active clients. A
321
320
// client can obtain the primary lease if it is either in the foreground
322
321
// or if this client and all other clients are in the background.
323
- return PersistencePromise . resolve ( true )
322
+ return PersistencePromise . resolve ( )
324
323
. next ( ( ) => {
325
- if ( this . inForeground ) {
326
- return true ;
327
- }
328
-
329
- let canActAsPrimary = true ;
330
-
331
- return clientMetadataStore ( txn )
332
- . iterate ( ( key , value , control ) => {
324
+ if ( ! canActAsPrimary ) {
325
+ canActAsPrimary = true ;
326
+ return clientMetadataStore ( txn ) . iterate ( ( key , value , control ) => {
333
327
if ( this . clientKey !== value . clientId ) {
334
328
if (
335
329
this . isWithinMaxAge ( value . updateTimeMs ) &&
@@ -339,12 +333,10 @@ export class IndexedDbPersistence implements Persistence {
339
333
control . done ( ) ;
340
334
}
341
335
}
342
- } )
343
- . next ( ( ) => {
344
- return canActAsPrimary ;
345
336
} ) ;
337
+ }
346
338
} )
347
- . next ( canActAsPrimary => {
339
+ . next ( ( ) => {
348
340
log . debug (
349
341
LOG_TAG ,
350
342
`Client ${
0 commit comments