Skip to content

Commit aeec53d

Browse files
Make notifyLocalViewChanges idempotent
1 parent 5cf8e53 commit aeec53d

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

packages/firestore/src/local/indexeddb_mutation_queue.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ export class IndexedDbMutationQueue implements MutationQueue {
178178
);
179179
const dbBatch = this.serializer.toDbMutationBatch(this.userId, batch);
180180

181-
this.documentKeysByBatchId[batchId] = batch.keys();
182-
183181
const promises: Array<PersistencePromise<void>> = [];
184182
let collectionParents = new SortedSet<ResourcePath>((l, r) =>
185183
primitiveComparator(l.canonicalString(), r.canonicalString())
@@ -203,6 +201,10 @@ export class IndexedDbMutationQueue implements MutationQueue {
203201
);
204202
});
205203

204+
transaction.addOnCommittedListener(() => {
205+
this.documentKeysByBatchId[batchId] = batch.keys();
206+
});
207+
206208
return PersistencePromise.waitFor(promises).next(() => batch);
207209
});
208210
}

packages/firestore/src/local/local_store.ts

+25-18
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,19 @@ export class LocalStore {
300300
documentKeySet()
301301
);
302302

303-
return this.persistence.runTransaction(
304-
'Locally write mutations',
305-
'readwrite',
306-
txn => {
307-
// Load and apply all existing mutations. This lets us compute the
308-
// current base state for all non-idempotent transforms before applying
309-
// any additional user-provided writes.
310-
return this.localDocuments
311-
.getDocuments(txn, keys)
312-
.next(existingDocs => {
303+
let existingDocs: MaybeDocumentMap;
304+
305+
return this.persistence
306+
.runTransaction(
307+
'Locally write mutations',
308+
'readwrite-idempotent',
309+
txn => {
310+
// Load and apply all existing mutations. This lets us compute the
311+
// current base state for all non-idempotent transforms before applying
312+
// any additional user-provided writes.
313+
return this.localDocuments.getDocuments(txn, keys).next(docs => {
314+
existingDocs = docs;
315+
313316
// For non-idempotent mutations (such as `FieldValue.increment()`),
314317
// we record the base state in a separate patch mutation. This is
315318
// later used to guarantee consistent values and prevents flicker
@@ -336,15 +339,19 @@ export class LocalStore {
336339
}
337340
}
338341

339-
return this.mutationQueue
340-
.addMutationBatch(txn, localWriteTime, baseMutations, mutations)
341-
.next(batch => {
342-
const changes = batch.applyToLocalDocumentSet(existingDocs);
343-
return { batchId: batch.batchId, changes };
344-
});
342+
return this.mutationQueue.addMutationBatch(
343+
txn,
344+
localWriteTime,
345+
baseMutations,
346+
mutations
347+
);
345348
});
346-
}
347-
);
349+
}
350+
)
351+
.then(batch => {
352+
const changes = batch.applyToLocalDocumentSet(existingDocs);
353+
return { batchId: batch.batchId, changes };
354+
});
348355
}
349356

350357
/** Returns the local view of the documents affected by a mutation batch. */

0 commit comments

Comments
 (0)