@@ -20,6 +20,8 @@ import { expect } from 'chai';
20
20
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence' ;
21
21
import { Persistence } from '../../../src/local/persistence' ;
22
22
import { PersistencePromise } from '../../../src/local/persistence_promise' ;
23
+ import { DbTarget , DbTargetKey } from '../../../src/local/indexeddb_schema' ;
24
+ import { TargetId } from '../../../src/core/types' ;
23
25
24
26
let persistence : Persistence ;
25
27
@@ -48,27 +50,63 @@ describe('IndexedDbTransaction', () => {
48
50
afterEach ( ( ) => persistence . shutdown ( ) ) ;
49
51
50
52
genericTransactionTests ( ) ;
53
+
54
+ it ( 'only invokes onCommittedListener once with retries' , async ( ) => {
55
+ let runCount = 0 ;
56
+ let commitCount = 0 ;
57
+ await persistence . runTransaction (
58
+ 'onCommitted' ,
59
+ 'readwrite-idempotent' ,
60
+ txn => {
61
+ const targetsStore = IndexedDbPersistence . getStore <
62
+ DbTargetKey ,
63
+ { targetId : TargetId }
64
+ > ( txn , DbTarget . store ) ;
65
+
66
+ txn . addOnCommittedListener ( ( ) => {
67
+ ++ commitCount ;
68
+ } ) ;
69
+
70
+ ++ runCount ;
71
+ if ( runCount == 1 ) {
72
+ // Trigger a unique key violation
73
+ return targetsStore
74
+ . add ( { targetId : 1 } )
75
+ . next ( ( ) => targetsStore . add ( { targetId : 1 } ) ) ;
76
+ } else {
77
+ return PersistencePromise . resolve ( 0 ) ;
78
+ }
79
+ }
80
+ ) ;
81
+
82
+ expect ( runCount ) . to . be . equal ( 2 ) ;
83
+ expect ( commitCount ) . to . be . equal ( 1 ) ;
84
+ } ) ;
51
85
} ) ;
52
86
53
87
function genericTransactionTests ( ) : void {
54
88
it ( 'invokes onCommittedListener when transaction succeeds' , async ( ) => {
55
89
let onCommitted = false ;
56
- await persistence . runTransaction ( 'onCommitted' , 'readonly' , txn => {
57
- txn . addOnCommittedListener ( ( ) => {
58
- onCommitted = true ;
59
- } ) ;
90
+ await persistence . runTransaction (
91
+ 'onCommitted' ,
92
+ 'readonly-idempotent' ,
93
+ txn => {
94
+ txn . addOnCommittedListener ( ( ) => {
95
+ onCommitted = true ;
96
+ } ) ;
60
97
61
- expect ( onCommitted ) . to . be . false ;
62
- return PersistencePromise . resolve ( ) ;
63
- } ) ;
98
+ expect ( onCommitted ) . to . be . false ;
99
+ return PersistencePromise . resolve ( ) ;
100
+ }
101
+ ) ;
64
102
65
103
expect ( onCommitted ) . to . be . true ;
66
104
} ) ;
67
105
68
106
it ( 'does not invoke onCommittedListener when transaction fails' , async ( ) => {
69
107
let onCommitted = false ;
70
108
await persistence
71
- . runTransaction ( 'onCommitted' , 'readonly' , txn => {
109
+ . runTransaction ( 'onCommitted' , 'readonly-idempotent ' , txn => {
72
110
txn . addOnCommittedListener ( ( ) => {
73
111
onCommitted = true ;
74
112
} ) ;
0 commit comments