Skip to content

Commit a1cf3d1

Browse files
committed
resolve comments
1 parent 016cc8f commit a1cf3d1

File tree

5 files changed

+57
-62
lines changed

5 files changed

+57
-62
lines changed

packages/firestore/src/core/event_manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class QueryListener {
372372
return false;
373373
}
374374

375-
// Raise data from cache if we have any documents or we are offline
375+
// Raise data from cache if we have any documents or resume token, or we are offline.
376376
return (
377377
!snap.docs.isEmpty() ||
378378
snap.resumeToken.approximateByteSize() > 0 ||

packages/firestore/src/core/view_snapshot.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ export class ViewSnapshot {
188188
!this.mutatedKeys.isEqual(other.mutatedKeys) ||
189189
!queryEquals(this.query, other.query) ||
190190
!this.docs.isEqual(other.docs) ||
191-
!this.oldDocs.isEqual(other.oldDocs) ||
192-
this.resumeToken !== other.resumeToken
191+
!this.oldDocs.isEqual(other.oldDocs)
193192
) {
194193
return false;
195194
}

packages/firestore/test/integration/api/query.test.ts

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ apiDescribe('Queries', (persistence: boolean) => {
12811281
};
12821282

12831283
return withTestCollection(persistence, testDocs, async coll => {
1284-
await getDocs(query(coll)); // Populate the cache
1284+
await getDocs(query(coll)); // Populate the cache.
12851285
const snapshot = await getDocs(
12861286
query(coll, where('map.nested', '==', 'foo'))
12871287
);
@@ -1291,12 +1291,12 @@ apiDescribe('Queries', (persistence: boolean) => {
12911291

12921292
// Reproduces https://github.com/firebase/firebase-js-sdk/issues/5873
12931293
// eslint-disable-next-line no-restricted-properties
1294-
(persistence ? describe : describe.skip)('Caching empty results ', () => {
1295-
it('can cache empty query results', () => {
1294+
(persistence ? describe : describe.skip)('Caching empty results', () => {
1295+
it('can raises initial snapshot from cache, even if it is empty', () => {
12961296
return withTestCollection(persistence, {}, async coll => {
1297-
const snapshot1 = await getDocs(coll); // Populate the cache
1297+
const snapshot1 = await getDocs(coll); // Populate the cache.
12981298
expect(snapshot1.metadata.fromCache).to.be.false;
1299-
expect(toDataArray(snapshot1)).to.deep.equal([]); // Precondition check
1299+
expect(toDataArray(snapshot1)).to.deep.equal([]); // Precondition check.
13001300

13011301
// Add a snapshot listener whose first event should be raised from cache.
13021302
const storeEvent = new EventsAccumulator<QuerySnapshot>();
@@ -1307,73 +1307,25 @@ apiDescribe('Queries', (persistence: boolean) => {
13071307
});
13081308
});
13091309

1310-
it('can empty cached collection and raise snapshot from it', () => {
1310+
it('can raises initial snapshot from cache, even if it has become empty', () => {
13111311
const testDocs = {
13121312
a: { key: 'a' }
13131313
};
13141314
return withTestCollection(persistence, testDocs, async coll => {
1315-
// Populate the cache
1315+
// Populate the cache.
13161316
const snapshot1 = await getDocs(coll);
13171317
expect(snapshot1.metadata.fromCache).to.be.false;
13181318
expect(toDataArray(snapshot1)).to.deep.equal([{ key: 'a' }]);
1319-
//empty the collection
1319+
// Empty the collection.
13201320
void deleteDoc(doc(coll, 'a'));
13211321

1322-
// Add a snapshot listener whose first event should be raised from cache.
13231322
const storeEvent = new EventsAccumulator<QuerySnapshot>();
13241323
onSnapshot(coll, storeEvent.storeEvent);
13251324
const snapshot2 = await storeEvent.awaitEvent();
13261325
expect(snapshot2.metadata.fromCache).to.be.true;
13271326
expect(toDataArray(snapshot2)).to.deep.equal([]);
13281327
});
13291328
});
1330-
1331-
it('can raise snapshot from a cached collection which was emptied offline', () => {
1332-
const testDocs = {
1333-
a: { key: 'a' }
1334-
};
1335-
return withTestCollection(
1336-
persistence,
1337-
testDocs,
1338-
async (coll, firestore) => {
1339-
await getDocs(coll); // Populate the cache
1340-
const storeEvent = new EventsAccumulator<QuerySnapshot>();
1341-
onSnapshot(coll, storeEvent.storeEvent);
1342-
await storeEvent.awaitEvent();
1343-
1344-
await disableNetwork(firestore);
1345-
void deleteDoc(doc(coll, 'a'));
1346-
await enableNetwork(firestore);
1347-
1348-
const snapshot = await storeEvent.awaitEvent();
1349-
expect(snapshot.metadata.fromCache).to.be.true;
1350-
expect(toDataArray(snapshot)).to.deep.equal([]);
1351-
}
1352-
);
1353-
});
1354-
1355-
it('can register a listener and empty cache offline, and raise snaoshot from it when came back online', () => {
1356-
const testDocs = {
1357-
a: { key: 'a' }
1358-
};
1359-
return withTestCollection(
1360-
persistence,
1361-
testDocs,
1362-
async (coll, firestore) => {
1363-
await getDocs(coll); // Populate the cache
1364-
await disableNetwork(firestore);
1365-
const storeEvent = new EventsAccumulator<QuerySnapshot>();
1366-
onSnapshot(coll, storeEvent.storeEvent);
1367-
await storeEvent.awaitEvent();
1368-
void deleteDoc(doc(coll, 'a'));
1369-
await enableNetwork(firestore);
1370-
1371-
const snapshot = await storeEvent.awaitEvent();
1372-
expect(snapshot.metadata.fromCache).to.be.true;
1373-
expect(toDataArray(snapshot)).to.deep.equal([]);
1374-
}
1375-
);
1376-
});
13771329
});
13781330
});
13791331

packages/firestore/test/unit/api/database.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
} from '../../../src';
2626
import { EmulatorAuthCredentialsProvider } from '../../../src/api/credentials';
2727
import { User } from '../../../src/auth/user';
28+
import { encodeBase64 } from '../../../src/platform/base64';
29+
import { ByteString } from '../../../src/util/byte_string';
2830
import {
2931
collectionReference,
3032
documentReference,
@@ -179,6 +181,48 @@ describe('QuerySnapshot', () => {
179181
).to.be.false;
180182
});
181183

184+
it('resume token should not effect querySnapshot equality', () => {
185+
const resumeToken1 = ByteString.fromBase64String(
186+
encodeBase64('ResumeToken1')
187+
);
188+
const resumeToken1Copy = ByteString.fromBase64String(
189+
encodeBase64('ResumeToken1')
190+
);
191+
const resumeToken2 = ByteString.fromBase64String(
192+
encodeBase64('ResumeToken2')
193+
);
194+
195+
const snapshot1 = querySnapshot(
196+
'foo',
197+
{},
198+
{ a: { a: 1 } },
199+
keys(),
200+
false,
201+
false,
202+
resumeToken1
203+
);
204+
const snapshot1Copy = querySnapshot(
205+
'foo',
206+
{},
207+
{ a: { a: 1 } },
208+
keys(),
209+
false,
210+
false,
211+
resumeToken1Copy
212+
);
213+
const snapshot2 = querySnapshot(
214+
'foo',
215+
{},
216+
{ a: { a: 1 } },
217+
keys(),
218+
false,
219+
false,
220+
resumeToken2
221+
);
222+
expect(snapshotEqual(snapshot1, snapshot1Copy)).to.be.true;
223+
expect(snapshotEqual(snapshot1, snapshot2)).to.be.true;
224+
});
225+
182226
it('JSON.stringify() does not throw', () => {
183227
JSON.stringify(
184228
querySnapshot('foo', {}, { a: { a: 1 } }, keys(), false, false)

packages/firestore/test/util/api_helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ export function querySnapshot(
131131
docsToAdd: { [key: string]: JsonObject<unknown> },
132132
mutatedKeys: DocumentKeySet,
133133
fromCache: boolean,
134-
syncStateChanged: boolean
134+
syncStateChanged: boolean,
135+
resumeToken?: ByteString
135136
): QuerySnapshot {
136137
const query: InternalQuery = newQueryForPath(pathFrom(path));
137138
let oldDocuments: DocumentSet = new DocumentSet();
@@ -145,7 +146,6 @@ export function querySnapshot(
145146
newDocuments = newDocuments.add(docToAdd);
146147
documentChanges.push({ type: ChangeType.Added, doc: docToAdd });
147148
});
148-
const resumeToken = ByteString.EMPTY_BYTE_STRING;
149149
const viewSnapshot: ViewSnapshot = new ViewSnapshot(
150150
query,
151151
newDocuments,
@@ -155,7 +155,7 @@ export function querySnapshot(
155155
fromCache,
156156
syncStateChanged,
157157
false,
158-
resumeToken
158+
resumeToken ?? ByteString.EMPTY_BYTE_STRING
159159
);
160160
const db = firestore();
161161
return new QuerySnapshot(

0 commit comments

Comments
 (0)