Skip to content

Commit f06cff1

Browse files
Sort the initial set of changes
1 parent e7589ca commit f06cff1

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/firestore/src/core/view_snapshot.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,15 @@ export class ViewSnapshot {
161161
changeSet.track({ type: ChangeType.Added, doc });
162162
});
163163

164+
const changes = changeSet.getChanges();
165+
// We don't need to compare the change type since all documents are added.
166+
changes.sort((d1, d2) => query.docComparator(d1.doc, d2.doc));
167+
164168
return new ViewSnapshot(
165169
query,
166170
documents,
167171
DocumentSet.emptySet(documents),
168-
changeSet.getChanges(),
172+
changes,
169173
fromCache,
170174
hasPendingWrites,
171175
/* syncStateChanged */ true,

packages/firestore/test/unit/specs/orderby_spec.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,24 @@ describeSpec('OrderBy:', [], () => {
5656
.expectEvents(query1, { hasPendingWrites: true, added: [doc2b] })
5757
);
5858
});
59+
60+
specTest('orderBy applies to existing documents', ['exclusive'], () => {
61+
const query = Query.atPath(path('collection')).addOrderBy(
62+
orderBy('sort', 'asc')
63+
);
64+
const docA = doc('collection/a', 0, { key: 'a', sort: 2 });
65+
const docB = doc('collection/b', 1001, { key: 'b', sort: 1 });
66+
67+
return spec()
68+
.withGCEnabled(false)
69+
.userListens(query)
70+
.watchAcksFull(query, 1000, docA, docB)
71+
.expectEvents(query, { added: [docB, docA] })
72+
.userUnlistens(query)
73+
.watchRemoves(query)
74+
.userListens(query, 'resume-token-1000')
75+
.expectEvents(query, { added: [docB, docA], fromCache: true })
76+
.watchAcksFull(query, 1000)
77+
.expectEvents(query, {});
78+
});
5979
});

0 commit comments

Comments
 (0)