Skip to content

Commit f82fc64

Browse files
WIP
1 parent 891ec44 commit f82fc64

File tree

5 files changed

+53
-72
lines changed

5 files changed

+53
-72
lines changed

packages/firestore/src/core/bundle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { LoadBundleTaskProgress } from '@firebase/firestore-types';
1919

20-
import { MutableDocumentMap } from '../model/collections';
20+
import { DocumentMap } from '../model/collections';
2121
import { DocumentKey } from '../model/document_key';
2222
import { MutableDocument } from '../model/mutable_document';
2323
import { BundledDocumentMetadata as ProtoBundledDocumentMetadata } from '../protos/firestore_bundle_proto';
@@ -46,7 +46,7 @@ export type BundledDocuments = BundledDocument[];
4646
export class BundleLoadResult {
4747
constructor(
4848
readonly progress: LoadBundleTaskProgress,
49-
readonly changedDocs: MutableDocumentMap
49+
readonly changedDocs: DocumentMap
5050
) {}
5151
}
5252

packages/firestore/src/local/local_store_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export interface LocalWriteResult {
100100

101101
/** The result of a user-change operation in the local store. */
102102
export interface UserChangeResult {
103-
readonly affectedDocuments: MutableDocumentMap;
103+
readonly affectedDocuments: DocumentMap;
104104
readonly removedBatchIds: BatchId[];
105105
readonly addedBatchIds: BatchId[];
106106
}

packages/firestore/src/model/mutation.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,16 +512,12 @@ function applyPatchMutationToLocalView(
512512
/**
513513
* Returns a FieldPath/Value map with the content of the PatchMutation.
514514
*/
515-
function getPatch(mutation: PatchMutation): Map<FieldPath, ProtoValue> {
516-
const result = new Map<FieldPath, ProtoValue>();
515+
function getPatch(mutation: PatchMutation): Map<FieldPath, ProtoValue | null> {
516+
const result = new Map<FieldPath, ProtoValue | null>();
517517
mutation.fieldMask.fields.forEach(fieldPath => {
518518
if (!fieldPath.isEmpty()) {
519519
const newValue = mutation.data.field(fieldPath);
520-
if (newValue !== null) {
521-
result.set(fieldPath, newValue);
522-
} else {
523-
result.delete(fieldPath);
524-
}
520+
result.set(fieldPath, newValue);
525521
}
526522
});
527523
return result;

packages/firestore/test/unit/local/local_store.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class LocalStoreTester {
413413
);
414414
for (const keyString of keyStrings) {
415415
const returned = this.lastChanges.get(key(keyString));
416-
expect(returned?.isNoDocument()).to.be.true;
416+
expect(returned?.isFoundDocument()).to.be.false;
417417
}
418418
this.lastChanges = null;
419419
});
@@ -438,7 +438,7 @@ class LocalStoreTester {
438438
toNotContain(keyStr: string): LocalStoreTester {
439439
this.promiseChain = this.promiseChain.then(() =>
440440
localStoreReadDocument(this.localStore, key(keyStr)).then(result => {
441-
expect(result).to.be.null;
441+
expect(result.isValidDocument()).to.be.false;
442442
})
443443
);
444444
return this;

packages/firestore/test/unit/model/mutation.test.ts

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,25 @@ describe('Mutation', () => {
5858

5959
it('can apply sets to documents', () => {
6060
const docData = { foo: 'foo-value', baz: 'baz-value' };
61-
const baseDoc = doc('collection/key', 0, docData);
61+
const document = doc('collection/key', 0, docData);
6262

6363
const set = setMutation('collection/key', { bar: 'bar-value' });
64-
const setDoc = applyMutationToLocalView(set, baseDoc, timestamp);
65-
expect(setDoc).to.deep.equal(
64+
applyMutationToLocalView(set, document, timestamp);
65+
expect(document).to.deep.equal(
6666
doc('collection/key', 0, { bar: 'bar-value' }).setHasLocalMutations()
6767
);
6868
});
6969

7070
it('can apply patches to documents', () => {
7171
const docData = { foo: { bar: 'bar-value' }, baz: 'baz-value' };
7272

73-
const baseDoc = doc('collection/key', 0, docData);
73+
const document = doc('collection/key', 0, docData);
7474
const patch = patchMutation('collection/key', {
7575
'foo.bar': 'new-bar-value'
7676
});
7777

78-
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
79-
expect(patchedDoc).to.deep.equal(
78+
applyMutationToLocalView(patch, document, timestamp);
79+
expect(document).to.deep.equal(
8080
doc('collection/key', 0, {
8181
foo: { bar: 'new-bar-value' },
8282
baz: 'baz-value'
@@ -87,15 +87,15 @@ describe('Mutation', () => {
8787
it('can apply patches with merges to missing documents', () => {
8888
const timestamp = Timestamp.now();
8989

90-
const baseDoc = deletedDoc('collection/key', 0);
90+
const document = deletedDoc('collection/key', 0);
9191
const patch = patchMutation(
9292
'collection/key',
9393
{ 'foo.bar': 'new-bar-value' },
9494
Precondition.none()
9595
);
9696

97-
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
98-
expect(patchedDoc).to.deep.equal(
97+
applyMutationToLocalView(patch, document, timestamp);
98+
expect(document).to.deep.equal(
9999
doc('collection/key', 0, {
100100
foo: { bar: 'new-bar-value' }
101101
}).setHasLocalMutations()
@@ -105,48 +105,48 @@ describe('Mutation', () => {
105105
it('can apply patches with merges to null documents', () => {
106106
const timestamp = Timestamp.now();
107107

108-
const baseDoc = MutableDocument.newInvalidDocument(key('collection/key'));
108+
const document = MutableDocument.newInvalidDocument(key('collection/key'));
109109
const patch = patchMutation(
110110
'collection/key',
111111
{ 'foo.bar': 'new-bar-value' },
112112
Precondition.none()
113113
);
114114

115-
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
116-
expect(patchedDoc).to.deep.equal(
115+
applyMutationToLocalView(patch, document, timestamp);
116+
expect(document).to.deep.equal(
117117
doc('collection/key', 0, {
118118
foo: { bar: 'new-bar-value' }
119119
}).setHasLocalMutations()
120120
);
121121
});
122122

123123
it('will delete values from the field-mask', () => {
124-
const baseDoc = doc('collection/key', 0, {
124+
const document = doc('collection/key', 0, {
125125
foo: { bar: 'bar-value', baz: 'baz-value' }
126126
});
127127
const patch = patchMutation('collection/key', {
128128
'foo.bar': FieldValue.delete()
129129
});
130130

131-
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
132-
expect(patchedDoc).to.deep.equal(
131+
applyMutationToLocalView(patch, document, timestamp);
132+
expect(document).to.deep.equal(
133133
doc('collection/key', 0, {
134134
foo: { baz: 'baz-value' }
135135
}).setHasLocalMutations()
136136
);
137137
});
138138

139139
it('will patch a primitive value', () => {
140-
const baseDoc = doc('collection/key', 0, {
140+
const document = doc('collection/key', 0, {
141141
foo: 'foo-value',
142142
baz: 'baz-value'
143143
});
144144
const patch = patchMutation('collection/key', {
145145
'foo.bar': 'new-bar-value'
146146
});
147147

148-
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
149-
expect(patchedDoc).to.deep.equal(
148+
applyMutationToLocalView(patch, document, timestamp);
149+
expect(document).to.deep.equal(
150150
doc('collection/key', 0, {
151151
foo: { bar: 'new-bar-value' },
152152
baz: 'baz-value'
@@ -155,25 +155,21 @@ describe('Mutation', () => {
155155
});
156156

157157
it('patching a NoDocument yields a NoDocument', () => {
158-
const baseDoc = deletedDoc('collection/key', 0);
158+
const document = deletedDoc('collection/key', 0);
159159
const patch = patchMutation('collection/key', { foo: 'bar' });
160-
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
161-
expect(patchedDoc).to.deep.equal(baseDoc);
160+
applyMutationToLocalView(patch, document, timestamp);
161+
expect(document).to.deep.equal(deletedDoc('collection/key', 0));
162162
});
163163

164164
it('can apply local serverTimestamp transforms to documents', () => {
165165
const docData = { foo: { bar: 'bar-value' }, baz: 'baz-value' };
166166

167-
const baseDoc = doc('collection/key', 0, docData);
167+
const document = doc('collection/key', 0, docData);
168168
const transform = patchMutation('collection/key', {
169169
'foo.bar': FieldValue.serverTimestamp()
170170
});
171171

172-
const transformedDoc = applyMutationToLocalView(
173-
transform,
174-
baseDoc,
175-
timestamp
176-
);
172+
applyMutationToLocalView(transform, document, timestamp);
177173

178174
// Server timestamps aren't parsed, so we manually insert it.
179175
const data = wrapObject({
@@ -183,7 +179,7 @@ describe('Mutation', () => {
183179
data.set(field('foo.bar'), serverTimestamp(timestamp, null));
184180
const expectedDoc = doc('collection/key', 0, data).setHasLocalMutations();
185181

186-
expect(transformedDoc).to.deep.equal(expectedDoc);
182+
expect(document).to.deep.equal(expectedDoc);
187183
});
188184

189185
// NOTE: This is more a test of UserDataReader code than Mutation code but
@@ -352,7 +348,7 @@ describe('Mutation', () => {
352348
it('can apply server-acked serverTimestamp transform to documents', () => {
353349
const docData = { foo: { bar: 'bar-value' }, baz: 'baz-value' };
354350

355-
const baseDoc = doc('collection/key', 0, docData);
351+
const document = doc('collection/key', 0, docData);
356352
const transform = patchMutation('collection/key', {
357353
'foo.bar': FieldValue.serverTimestamp()
358354
});
@@ -365,13 +361,9 @@ describe('Mutation', () => {
365361
}
366362
}
367363
]);
368-
const transformedDoc = applyMutationToRemoteDocument(
369-
transform,
370-
baseDoc,
371-
mutationResult
372-
);
364+
applyMutationToRemoteDocument(transform, document, mutationResult);
373365

374-
expect(transformedDoc).to.deep.equal(
366+
expect(document).to.deep.equal(
375367
doc('collection/key', 1, {
376368
foo: { bar: timestamp.toDate() },
377369
baz: 'baz-value'
@@ -381,21 +373,17 @@ describe('Mutation', () => {
381373

382374
it('can apply server-acked array transforms to document', () => {
383375
const docData = { array1: [1, 2], array2: ['a', 'b'] };
384-
const baseDoc = doc('collection/key', 0, docData);
376+
const document = doc('collection/key', 0, docData);
385377
const transform = setMutation('collection/key', {
386378
array1: FieldValue.arrayUnion(2, 3),
387379
array2: FieldValue.arrayRemove('a', 'c')
388380
});
389381

390382
// Server just sends null transform results for array operations.
391383
const mutationResult = new MutationResult(version(1), [null, null]);
392-
const transformedDoc = applyMutationToRemoteDocument(
393-
transform,
394-
baseDoc,
395-
mutationResult
396-
);
384+
applyMutationToRemoteDocument(transform, document, mutationResult);
397385

398-
expect(transformedDoc).to.deep.equal(
386+
expect(document).to.deep.equal(
399387
doc('collection/key', 1, {
400388
array1: [1, 2, 3],
401389
array2: ['b']
@@ -466,51 +454,47 @@ describe('Mutation', () => {
466454

467455
it('can apply server-acked numeric add transform to document', () => {
468456
const docData = { sum: 1 };
469-
const baseDoc = doc('collection/key', 0, docData);
457+
const document = doc('collection/key', 0, docData);
470458
const transform = setMutation('collection/key', {
471459
sum: FieldValue.increment(2)
472460
});
473461

474462
const mutationResult = new MutationResult(version(1), [
475463
{ integerValue: 3 }
476464
]);
477-
const transformedDoc = applyMutationToRemoteDocument(
478-
transform,
479-
baseDoc,
480-
mutationResult
481-
);
465+
applyMutationToRemoteDocument(transform, document, mutationResult);
482466

483-
expect(transformedDoc).to.deep.equal(
467+
expect(document).to.deep.equal(
484468
doc('collection/key', 1, { sum: 3 }).setHasCommittedMutations()
485469
);
486470
});
487471

488472
it('can apply deletes to documents', () => {
489-
const baseDoc = doc('collection/key', 0, { foo: 'bar' });
473+
const document = doc('collection/key', 0, { foo: 'bar' });
490474

491475
const mutation = deleteMutation('collection/key');
492-
const result = applyMutationToLocalView(mutation, baseDoc, Timestamp.now());
493-
expect(result).to.deep.equal(deletedDoc('collection/key', 0));
476+
applyMutationToLocalView(mutation, document, Timestamp.now());
477+
expect(document).to.deep.equal(deletedDoc('collection/key', 0));
494478
});
495479

496480
it('can apply sets with mutation results', () => {
497-
const baseDoc = doc('collection/key', 0, { foo: 'bar' });
481+
const document = doc('collection/key', 0, { foo: 'bar' });
498482

499483
const docSet = setMutation('collection/key', { foo: 'new-bar' });
500484
const setResult = mutationResult(4);
501-
const setDoc = applyMutationToRemoteDocument(docSet, baseDoc, setResult);
502-
expect(setDoc).to.deep.equal(
485+
applyMutationToRemoteDocument(docSet, document, setResult);
486+
expect(document).to.deep.equal(
503487
doc('collection/key', 4, { foo: 'new-bar' }).setHasCommittedMutations()
504488
);
505489
});
506490

507491
it('will apply patches with mutation results', () => {
508-
const baseDoc = doc('collection/key', 0, { foo: 'bar' });
492+
const document = doc('collection/key', 0, { foo: 'bar' });
509493

510494
const mutation = patchMutation('collection/key', { foo: 'new-bar' });
511495
const result = mutationResult(5);
512-
const patchedDoc = applyMutationToRemoteDocument(mutation, baseDoc, result);
513-
expect(patchedDoc).to.deep.equal(
496+
applyMutationToRemoteDocument(mutation, document, result);
497+
expect(document).to.deep.equal(
514498
doc('collection/key', 5, { foo: 'new-bar' }).setHasCommittedMutations()
515499
);
516500
});
@@ -521,8 +505,9 @@ describe('Mutation', () => {
521505
mutationResult: MutationResult,
522506
expected: MutableDocument
523507
): void {
524-
applyMutationToRemoteDocument(mutation, base, mutationResult);
525-
expect(base).to.deep.equal(expected);
508+
const documentCopy = base.clone();
509+
applyMutationToRemoteDocument(mutation, documentCopy, mutationResult);
510+
expect(documentCopy).to.deep.equal(expected);
526511
}
527512

528513
it('transitions versions correctly', () => {

0 commit comments

Comments
 (0)