@@ -122,7 +122,7 @@ export class IndexedDbTransaction extends PersistenceTransaction {
122
122
}
123
123
124
124
// The different modes supported by `IndexedDbPersistence.runTransaction()`
125
- type TransactionMode =
125
+ type IndexedDbTransactionMode =
126
126
| 'readonly'
127
127
| 'readwrite'
128
128
| 'readwrite-primary'
@@ -330,7 +330,6 @@ export class IndexedDbPersistence implements Persistence {
330
330
. then ( ( ) => {
331
331
return this . simpleDb . runTransaction (
332
332
'readonly' ,
333
- /* idempotent= */ false ,
334
333
[ DbTargetGlobal . store ] ,
335
334
txn => {
336
335
return getHighestListenSequenceNumber ( txn ) . next (
@@ -354,11 +353,8 @@ export class IndexedDbPersistence implements Persistence {
354
353
}
355
354
356
355
private startRemoteDocumentCache ( ) : Promise < void > {
357
- return this . simpleDb . runTransaction (
358
- 'readonly' ,
359
- /* idempotent= */ false ,
360
- ALL_STORES ,
361
- txn => this . remoteDocumentCache . start ( txn )
356
+ return this . simpleDb . runTransaction ( 'readonly' , ALL_STORES , txn =>
357
+ this . remoteDocumentCache . start ( txn )
362
358
) ;
363
359
}
364
360
@@ -404,52 +400,47 @@ export class IndexedDbPersistence implements Persistence {
404
400
* primary lease.
405
401
*/
406
402
private updateClientMetadataAndTryBecomePrimary ( ) : Promise < void > {
407
- return this . simpleDb . runTransaction (
408
- 'readwrite' ,
409
- /* idempotent= */ false ,
410
- ALL_STORES ,
411
- txn => {
412
- const metadataStore = clientMetadataStore ( txn ) ;
413
- return metadataStore
414
- . put (
415
- new DbClientMetadata (
416
- this . clientId ,
417
- Date . now ( ) ,
418
- this . networkEnabled ,
419
- this . inForeground
420
- )
403
+ return this . simpleDb . runTransaction ( 'readwrite' , ALL_STORES , txn => {
404
+ const metadataStore = clientMetadataStore ( txn ) ;
405
+ return metadataStore
406
+ . put (
407
+ new DbClientMetadata (
408
+ this . clientId ,
409
+ Date . now ( ) ,
410
+ this . networkEnabled ,
411
+ this . inForeground
421
412
)
422
- . next ( ( ) => {
423
- if ( this . isPrimary ) {
424
- return this . verifyPrimaryLease ( txn ) . next ( success => {
425
- if ( ! success ) {
426
- this . isPrimary = false ;
427
- this . queue . enqueueAndForget ( ( ) =>
428
- this . primaryStateListener ( false )
429
- ) ;
430
- }
431
- } ) ;
432
- }
433
- } )
434
- . next ( ( ) => this . canActAsPrimary ( txn ) )
435
- . next ( canActAsPrimary => {
436
- const wasPrimary = this . isPrimary ;
437
- this . isPrimary = canActAsPrimary ;
438
-
439
- if ( wasPrimary !== this . isPrimary ) {
440
- this . queue . enqueueAndForget ( ( ) =>
441
- this . primaryStateListener ( this . isPrimary )
442
- ) ;
443
- }
413
+ )
414
+ . next ( ( ) => {
415
+ if ( this . isPrimary ) {
416
+ return this . verifyPrimaryLease ( txn ) . next ( success => {
417
+ if ( ! success ) {
418
+ this . isPrimary = false ;
419
+ this . queue . enqueueAndForget ( ( ) =>
420
+ this . primaryStateListener ( false )
421
+ ) ;
422
+ }
423
+ } ) ;
424
+ }
425
+ } )
426
+ . next ( ( ) => this . canActAsPrimary ( txn ) )
427
+ . next ( canActAsPrimary => {
428
+ const wasPrimary = this . isPrimary ;
429
+ this . isPrimary = canActAsPrimary ;
430
+
431
+ if ( wasPrimary !== this . isPrimary ) {
432
+ this . queue . enqueueAndForget ( ( ) =>
433
+ this . primaryStateListener ( this . isPrimary )
434
+ ) ;
435
+ }
444
436
445
- if ( wasPrimary && ! this . isPrimary ) {
446
- return this . releasePrimaryLeaseIfHeld ( txn ) ;
447
- } else if ( this . isPrimary ) {
448
- return this . acquireOrExtendPrimaryLease ( txn ) ;
449
- }
450
- } ) ;
451
- }
452
- ) ;
437
+ if ( wasPrimary && ! this . isPrimary ) {
438
+ return this . releasePrimaryLeaseIfHeld ( txn ) ;
439
+ } else if ( this . isPrimary ) {
440
+ return this . acquireOrExtendPrimaryLease ( txn ) ;
441
+ }
442
+ } ) ;
443
+ } ) ;
453
444
}
454
445
455
446
private verifyPrimaryLease (
@@ -664,7 +655,6 @@ export class IndexedDbPersistence implements Persistence {
664
655
this . detachWindowUnloadHook ( ) ;
665
656
await this . simpleDb . runTransaction (
666
657
'readwrite' ,
667
- /* idempotent= */ false ,
668
658
[ DbPrimaryClient . store , DbClientMetadata . store ] ,
669
659
txn => {
670
660
return this . releasePrimaryLeaseIfHeld ( txn ) . next ( ( ) =>
@@ -697,7 +687,6 @@ export class IndexedDbPersistence implements Persistence {
697
687
getActiveClients ( ) : Promise < ClientId [ ] > {
698
688
return this . simpleDb . runTransaction (
699
689
'readonly' ,
700
- /* idempotent= */ false ,
701
690
[ DbClientMetadata . store ] ,
702
691
txn => {
703
692
return clientMetadataStore ( txn )
@@ -762,26 +751,29 @@ export class IndexedDbPersistence implements Persistence {
762
751
763
752
runTransaction < T > (
764
753
action : string ,
765
- mode : TransactionMode ,
754
+ mode : IndexedDbTransactionMode ,
766
755
transactionOperation : (
767
756
transaction : PersistenceTransaction
768
757
) => PersistencePromise < T >
769
758
) : Promise < T > {
770
759
log . debug ( LOG_TAG , 'Starting transaction:' , action ) ;
771
760
772
- const idempotent =
773
- [
774
- 'readonly-idempotent' ,
775
- 'readwrite-idempotent' ,
776
- 'readwrite-primary-idempotent'
777
- ] . indexOf ( mode ) !== - 1 ;
778
- const readonly = [ 'readonly' , 'readonly-idempotent' ] . indexOf ( mode ) !== - 1 ;
761
+ // TODO(schmidt-sebastian): Once all transactions are idempotent, simplify
762
+ // this.
763
+ const idempotent = mode . endsWith ( 'idempotent' ) ;
764
+ const readonly = mode . startsWith ( 'readonly' ) ;
765
+ const simpleDbMode = readonly
766
+ ? idempotent
767
+ ? 'readonly-idempotent'
768
+ : 'readonly'
769
+ : idempotent
770
+ ? 'readwrite-idempotent'
771
+ : 'readwrite' ;
779
772
780
773
// Do all transactions as readwrite against all object stores, since we
781
774
// are the only reader/writer.
782
775
return this . simpleDb . runTransaction (
783
- readonly ? 'readonly' : 'readwrite' ,
784
- idempotent ,
776
+ simpleDbMode ,
785
777
ALL_STORES ,
786
778
simpleDbTxn => {
787
779
if ( mode === 'readwrite-primary' ) {
0 commit comments