Skip to content

Commit 62a306a

Browse files
Add onCommitted listeners for transactions
1 parent c3da39c commit 62a306a

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,17 @@ export class IndexedDbPersistence implements Persistence {
758758
? 'readwrite-idempotent'
759759
: 'readwrite';
760760

761+
let persistenceTransaction: PersistenceTransaction;
762+
761763
// Do all transactions as readwrite against all object stores, since we
762764
// are the only reader/writer.
763-
return this.simpleDb.runTransaction(
764-
simpleDbMode,
765-
ALL_STORES,
766-
simpleDbTxn => {
765+
return this.simpleDb
766+
.runTransaction(simpleDbMode, ALL_STORES, simpleDbTxn => {
767+
persistenceTransaction = new IndexedDbTransaction(
768+
simpleDbTxn,
769+
this.listenSequence.next()
770+
);
771+
767772
if (mode === 'readwrite-primary') {
768773
// While we merely verify that we have (or can acquire) the lease
769774
// immediately, we wait to extend the primary lease until after
@@ -785,12 +790,7 @@ export class IndexedDbPersistence implements Persistence {
785790
PRIMARY_LEASE_LOST_ERROR_MSG
786791
);
787792
}
788-
return transactionOperation(
789-
new IndexedDbTransaction(
790-
simpleDbTxn,
791-
this.listenSequence.next()
792-
)
793-
);
793+
return transactionOperation(persistenceTransaction);
794794
})
795795
.next(result => {
796796
return this.acquireOrExtendPrimaryLease(simpleDbTxn).next(
@@ -799,13 +799,14 @@ export class IndexedDbPersistence implements Persistence {
799799
});
800800
} else {
801801
return this.verifyAllowTabSynchronization(simpleDbTxn).next(() =>
802-
transactionOperation(
803-
new IndexedDbTransaction(simpleDbTxn, this.listenSequence.next())
804-
)
802+
transactionOperation(persistenceTransaction)
805803
);
806804
}
807-
}
808-
);
805+
})
806+
.then(result => {
807+
persistenceTransaction.raiseOnCommittedEvent();
808+
return result;
809+
});
809810
}
810811

811812
/**

packages/firestore/src/local/memory_persistence.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export class MemoryPersistence implements Persistence {
180180
this.referenceDelegate.onTransactionStarted();
181181
return transactionOperation(txn)
182182
.next(result => {
183+
txn.raiseOnCommittedEvent();
183184
return this.referenceDelegate
184185
.onTransactionCommitted(txn)
185186
.next(() => result);
@@ -203,8 +204,10 @@ export class MemoryPersistence implements Persistence {
203204
* Memory persistence is not actually transactional, but future implementations
204205
* may have transaction-scoped state.
205206
*/
206-
export class MemoryTransaction implements PersistenceTransaction {
207-
constructor(readonly currentSequenceNumber: ListenSequenceNumber) {}
207+
export class MemoryTransaction extends PersistenceTransaction {
208+
constructor(readonly currentSequenceNumber: ListenSequenceNumber) {
209+
super();
210+
}
208211
}
209212

210213
export class MemoryEagerDelegate implements ReferenceDelegate {

packages/firestore/src/local/persistence.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,25 @@ import { RemoteDocumentCache } from './remote_document_cache';
2929
import { ClientId } from './shared_client_state';
3030

3131
/**
32-
* Opaque interface representing a persistence transaction.
32+
* A base class representing a persistence transaction, encapsulating both the
33+
* transaction's sequence numbers as well as a list of onCommitted listeners.
3334
*
3435
* When you call Persistence.runTransaction(), it will create a transaction and
3536
* pass it to your callback. You then pass it to any method that operates
3637
* on persistence.
3738
*/
3839
export abstract class PersistenceTransaction {
40+
private readonly onCommittedListeners: Array<() => {}> = [];
41+
3942
abstract readonly currentSequenceNumber: ListenSequenceNumber;
43+
44+
addOnCommittedListener(listener: () => {}): void {
45+
this.onCommittedListeners.push(listener);
46+
}
47+
48+
raiseOnCommittedEvent(): void {
49+
this.onCommittedListeners.forEach(listener => listener());
50+
}
4051
}
4152

4253
/** The different modes supported by `IndexedDbPersistence.runTransaction()`. */

0 commit comments

Comments
 (0)