Skip to content

Commit 806a86a

Browse files
Cleanup for canActAsPrimary
1 parent 010729b commit 806a86a

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

+25-33
Original file line numberDiff line numberDiff line change
@@ -229,26 +229,23 @@ export class IndexedDbPersistence implements Persistence {
229229
);
230230
}
231231

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+
);
252249
} else {
253250
// If there is no current leaseholder, but we are not ourselves
254251
// eligible to directly obtain the lease (based on our foreground
@@ -316,20 +313,17 @@ export class IndexedDbPersistence implements Persistence {
316313
return PersistencePromise.resolve(this.isLocalClient(currentPrimary));
317314
}
318315

316+
let canActAsPrimary = this.inForeground;
317+
319318
// Check if this client is eligible for a primary lease based on its
320319
// visibility state and the visibility state of all active clients. A
321320
// client can obtain the primary lease if it is either in the foreground
322321
// or if this client and all other clients are in the background.
323-
return PersistencePromise.resolve(true)
322+
return PersistencePromise.resolve()
324323
.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) => {
333327
if (this.clientKey !== value.clientId) {
334328
if (
335329
this.isWithinMaxAge(value.updateTimeMs) &&
@@ -339,12 +333,10 @@ export class IndexedDbPersistence implements Persistence {
339333
control.done();
340334
}
341335
}
342-
})
343-
.next(() => {
344-
return canActAsPrimary;
345336
});
337+
}
346338
})
347-
.next(canActAsPrimary => {
339+
.next(() => {
348340
log.debug(
349341
LOG_TAG,
350342
`Client ${

packages/firestore/test/unit/specs/spec_test_runner.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1169,11 +1169,19 @@ export async function runSpec(
11691169
}
11701170
await runners[i].start();
11711171
}
1172+
let lastStep = null;
1173+
let count = 0;
11721174
try {
1173-
let stepcnt = 0;
11741175
await sequence(steps, async step => {
1176+
++count;
1177+
lastStep = step;
11751178
return runners[step.clientIndex || 0].run(step);
11761179
});
1180+
} catch (err) {
1181+
console.warn(
1182+
`Spec test failed at step ${count}: ${JSON.stringify(lastStep)}`
1183+
);
1184+
throw err;
11771185
} finally {
11781186
for (const runner of runners) {
11791187
await runner.shutdown();

0 commit comments

Comments
 (0)