Skip to content

Commit c383178

Browse files
Use SimpleDb transaction
1 parent 761e4e2 commit c383178

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ import {
7575
SimpleDbTransaction
7676
} from './simple_db';
7777
import { DocumentLike, WindowLike } from '../util/types';
78-
import { ignoreIfPrimaryLeaseLoss } from './local_store';
7978

8079
const LOG_TAG = 'IndexedDbPersistence';
8180

@@ -681,11 +680,22 @@ export class IndexedDbPersistence implements Persistence {
681680
}
682681
this.detachVisibilityHandler();
683682
this.detachWindowUnloadHook();
684-
await this.runTransaction('shutdown', 'readwrite', txn =>
685-
this.releasePrimaryLeaseIfHeld(txn).next(() =>
686-
this.removeClientMetadata(txn)
687-
)
688-
).catch(e => ignoreIfPrimaryLeaseLoss(e));
683+
684+
// Use `SimpleDb.runTransaction` directly to avoid failing if another tab
685+
// has obtained the primary lease.
686+
await this.simpleDb.runTransaction(
687+
'readwrite',
688+
[DbPrimaryClient.store, DbClientMetadata.store],
689+
simpleDbTxn => {
690+
const persistenceTransaction = new IndexedDbTransaction(
691+
simpleDbTxn,
692+
ListenSequence.INVALID
693+
);
694+
return this.releasePrimaryLeaseIfHeld(persistenceTransaction).next(() =>
695+
this.removeClientMetadata(persistenceTransaction)
696+
);
697+
}
698+
);
689699
this.simpleDb.close();
690700

691701
// Remove the entry marking the client as zombied from LocalStorage since

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

-8
Original file line numberDiff line numberDiff line change
@@ -840,12 +840,4 @@ describeSpec('Persistence Recovery', ['no-ios', 'no-android'], () => {
840840
})
841841
.userUnlistens(query1);
842842
});
843-
844-
specTest('Terminate (with recovery)', [], () => {
845-
return spec()
846-
.failDatabaseTransactions('shutdown')
847-
.shutdown({ expectFailure: true })
848-
.recoverDatabase()
849-
.shutdown();
850-
});
851843
});

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,10 @@ export class SpecBuilder {
436436
return this;
437437
}
438438

439-
shutdown(options?: { expectFailure?: boolean }): this {
439+
shutdown(): this {
440440
this.nextStep();
441441
this.currentStep = {
442-
shutdown: options ?? true,
442+
shutdown: true,
443443
expectedState: {
444444
activeTargets: {},
445445
activeLimboDocs: [],

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

+11-22
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,7 @@ abstract class TestRunner {
370370
} else if ('restart' in step) {
371371
return this.doRestart();
372372
} else if ('shutdown' in step) {
373-
return typeof step.shutdown === 'object'
374-
? this.doShutdown(step.shutdown)
375-
: this.doShutdown();
373+
return this.doShutdown();
376374
} else if ('applyClientState' in step) {
377375
// PORTING NOTE: Only used by web multi-tab tests.
378376
return this.doApplyClientState(step.applyClientState!);
@@ -716,22 +714,14 @@ abstract class TestRunner {
716714
await this.remoteStore.enableNetwork();
717715
}
718716

719-
private async doShutdown(options?: {
720-
expectFailure?: boolean;
721-
}): Promise<void> {
722-
try {
723-
await this.remoteStore.shutdown();
724-
await this.sharedClientState.shutdown();
725-
// We don't delete the persisted data here since multi-clients may still
726-
// be accessing it. Instead, we manually remove it at the end of the
727-
// test run.
728-
await this.persistence.shutdown();
729-
expect(options?.expectFailure).to.not.be.true;
730-
731-
this.started = false;
732-
} catch (e) {
733-
expect(options?.expectFailure).to.be.true;
734-
}
717+
private async doShutdown(): Promise<void> {
718+
await this.remoteStore.shutdown();
719+
await this.sharedClientState.shutdown();
720+
// We don't delete the persisted data here since multi-clients may still
721+
// be accessing it. Instead, we manually remove it at the end of the
722+
// test run.
723+
await this.persistence.shutdown();
724+
this.started = false;
735725
}
736726

737727
private async doClearPersistence(): Promise<void> {
@@ -1270,8 +1260,7 @@ export type PersistenceAction =
12701260
| 'Get new document changes'
12711261
| 'Synchronize last document change read time'
12721262
| 'updateClientMetadataAndTryBecomePrimary'
1273-
| 'getHighestListenSequenceNumber'
1274-
| 'shutdown';
1263+
| 'getHighestListenSequenceNumber';
12751264

12761265
/**
12771266
* Union type for each step. The step consists of exactly one `field`
@@ -1351,7 +1340,7 @@ export interface SpecStep {
13511340
restart?: true;
13521341

13531342
/** Shut down the client and close it network connection. */
1354-
shutdown?: true | { expectFailure?: boolean };
1343+
shutdown?: true;
13551344

13561345
/**
13571346
* Optional list of expected events.

0 commit comments

Comments
 (0)