Skip to content

Commit e5c88cd

Browse files
authored
Make handleUserChange idempotent (#2257)
1 parent c3da39c commit e5c88cd

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

packages/firestore/src/local/local_store.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,13 @@ export class LocalStore {
200200
*/
201201
// PORTING NOTE: Android and iOS only return the documents affected by the
202202
// change.
203-
handleUserChange(user: User): Promise<UserChangeResult> {
204-
return this.persistence.runTransaction(
203+
async handleUserChange(user: User): Promise<UserChangeResult> {
204+
let newMutationQueue = this.mutationQueue;
205+
let newLocalDocuments = this.localDocuments;
206+
207+
const result = await this.persistence.runTransaction(
205208
'Handle user change',
206-
'readonly',
209+
'readonly-idempotent',
207210
txn => {
208211
// Swap out the mutation queue, grabbing the pending mutation batches
209212
// before and after.
@@ -213,17 +216,16 @@ export class LocalStore {
213216
.next(promisedOldBatches => {
214217
oldBatches = promisedOldBatches;
215218

216-
this.mutationQueue = this.persistence.getMutationQueue(user);
219+
newMutationQueue = this.persistence.getMutationQueue(user);
217220

218221
// Recreate our LocalDocumentsView using the new
219222
// MutationQueue.
220-
this.localDocuments = new LocalDocumentsView(
223+
newLocalDocuments = new LocalDocumentsView(
221224
this.remoteDocuments,
222-
this.mutationQueue,
225+
newMutationQueue,
223226
this.persistence.getIndexManager()
224227
);
225-
this.queryEngine.setLocalDocumentsView(this.localDocuments);
226-
return this.mutationQueue.getAllMutationBatches(txn);
228+
return newMutationQueue.getAllMutationBatches(txn);
227229
})
228230
.next(newBatches => {
229231
const removedBatchIds: BatchId[] = [];
@@ -248,7 +250,7 @@ export class LocalStore {
248250

249251
// Return the set of all (potentially) changed documents and the list
250252
// of mutation batch IDs that were affected by change.
251-
return this.localDocuments
253+
return newLocalDocuments
252254
.getDocuments(txn, changedKeys)
253255
.next(affectedDocuments => {
254256
return {
@@ -260,7 +262,14 @@ export class LocalStore {
260262
});
261263
}
262264
);
265+
266+
this.mutationQueue = newMutationQueue;
267+
this.localDocuments = newLocalDocuments;
268+
this.queryEngine.setLocalDocumentsView(this.localDocuments);
269+
270+
return result;
263271
}
272+
264273
/* Accept locally generated Mutations and commit them to storage. */
265274
localWrite(mutations: Mutation[]): Promise<LocalWriteResult> {
266275
const localWriteTime = Timestamp.now();

0 commit comments

Comments
 (0)