Skip to content

Commit 48642ad

Browse files
Add runTransaction parameter
1 parent 8c2bbc3 commit 48642ad

File tree

5 files changed

+534
-411
lines changed

5 files changed

+534
-411
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

+52-41
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ export class IndexedDbPersistence implements Persistence {
321321
.then(() => {
322322
return this.simpleDb.runTransaction(
323323
'readonly',
324+
/* idempotent= */ false,
324325
[DbTargetGlobal.store],
325326
txn => {
326327
return getHighestListenSequenceNumber(txn).next(
@@ -344,8 +345,11 @@ export class IndexedDbPersistence implements Persistence {
344345
}
345346

346347
private startRemoteDocumentCache(): Promise<void> {
347-
return this.simpleDb.runTransaction('readonly', ALL_STORES, txn =>
348-
this.remoteDocumentCache.start(txn)
348+
return this.simpleDb.runTransaction(
349+
'readonly',
350+
/* idempotent= */ false,
351+
ALL_STORES,
352+
txn => this.remoteDocumentCache.start(txn)
349353
);
350354
}
351355

@@ -391,47 +395,52 @@ export class IndexedDbPersistence implements Persistence {
391395
* primary lease.
392396
*/
393397
private updateClientMetadataAndTryBecomePrimary(): Promise<void> {
394-
return this.simpleDb.runTransaction('readwrite', ALL_STORES, txn => {
395-
const metadataStore = clientMetadataStore(txn);
396-
return metadataStore
397-
.put(
398-
new DbClientMetadata(
399-
this.clientId,
400-
Date.now(),
401-
this.networkEnabled,
402-
this.inForeground
398+
return this.simpleDb.runTransaction(
399+
'readwrite',
400+
/* idempotent= */ false,
401+
ALL_STORES,
402+
txn => {
403+
const metadataStore = clientMetadataStore(txn);
404+
return metadataStore
405+
.put(
406+
new DbClientMetadata(
407+
this.clientId,
408+
Date.now(),
409+
this.networkEnabled,
410+
this.inForeground
411+
)
403412
)
404-
)
405-
.next(() => {
406-
if (this.isPrimary) {
407-
return this.verifyPrimaryLease(txn).next(success => {
408-
if (!success) {
409-
this.isPrimary = false;
410-
this.queue.enqueueAndForget(() =>
411-
this.primaryStateListener(false)
412-
);
413-
}
414-
});
415-
}
416-
})
417-
.next(() => this.canActAsPrimary(txn))
418-
.next(canActAsPrimary => {
419-
const wasPrimary = this.isPrimary;
420-
this.isPrimary = canActAsPrimary;
421-
422-
if (wasPrimary !== this.isPrimary) {
423-
this.queue.enqueueAndForget(() =>
424-
this.primaryStateListener(this.isPrimary)
425-
);
426-
}
413+
.next(() => {
414+
if (this.isPrimary) {
415+
return this.verifyPrimaryLease(txn).next(success => {
416+
if (!success) {
417+
this.isPrimary = false;
418+
this.queue.enqueueAndForget(() =>
419+
this.primaryStateListener(false)
420+
);
421+
}
422+
});
423+
}
424+
})
425+
.next(() => this.canActAsPrimary(txn))
426+
.next(canActAsPrimary => {
427+
const wasPrimary = this.isPrimary;
428+
this.isPrimary = canActAsPrimary;
429+
430+
if (wasPrimary !== this.isPrimary) {
431+
this.queue.enqueueAndForget(() =>
432+
this.primaryStateListener(this.isPrimary)
433+
);
434+
}
427435

428-
if (wasPrimary && !this.isPrimary) {
429-
return this.releasePrimaryLeaseIfHeld(txn);
430-
} else if (this.isPrimary) {
431-
return this.acquireOrExtendPrimaryLease(txn);
432-
}
433-
});
434-
});
436+
if (wasPrimary && !this.isPrimary) {
437+
return this.releasePrimaryLeaseIfHeld(txn);
438+
} else if (this.isPrimary) {
439+
return this.acquireOrExtendPrimaryLease(txn);
440+
}
441+
});
442+
}
443+
);
435444
}
436445

437446
private verifyPrimaryLease(
@@ -646,6 +655,7 @@ export class IndexedDbPersistence implements Persistence {
646655
this.detachWindowUnloadHook();
647656
await this.simpleDb.runTransaction(
648657
'readwrite',
658+
/* idempotent= */ false,
649659
[DbPrimaryClient.store, DbClientMetadata.store],
650660
txn => {
651661
return this.releasePrimaryLeaseIfHeld(txn).next(() =>
@@ -678,6 +688,7 @@ export class IndexedDbPersistence implements Persistence {
678688
getActiveClients(): Promise<ClientId[]> {
679689
return this.simpleDb.runTransaction(
680690
'readonly',
691+
/* idempotent= */ false,
681692
[DbClientMetadata.store],
682693
txn => {
683694
return clientMetadataStore(txn)

packages/firestore/test/unit/local/encoded_resource_path.test.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ function runTransaction<T>(
216216
transaction: SimpleDbTransaction
217217
) => PersistencePromise<T>
218218
): Promise<T> {
219-
return db.runTransaction<T>('readwrite', ['test'], txn => {
220-
return fn(txn.store<string, boolean>('test'), txn);
221-
});
219+
return db.runTransaction<T>(
220+
'readwrite',
221+
/* idempotent= */ false,
222+
['test'],
223+
txn => {
224+
return fn(txn.store<string, boolean>('test'), txn);
225+
}
226+
);
222227
}

0 commit comments

Comments
 (0)