Skip to content

Commit bba6e25

Browse files
committed
* Fixed that calling "onSnapshot" would sometimes not trigger the initial callback-call.
First case where it would fail: the initial-data was null and it would think "no change", despite it needing to always trigger initially. Second case where it would fail: calling onSnapshot on a document instead of a query.
1 parent 27e7f8f commit bba6e25

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/firestore-document.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,22 @@ MockFirestoreDocument.prototype.onSnapshot = function (optionsOrObserverOrOnNext
215215
var context = {
216216
data: self._getData(),
217217
};
218-
var onSnapshot = function () {
218+
var onSnapshot = function (forceTrigger) {
219219
// compare the current state to the one from when this function was created
220220
// and send the data to the callback if different.
221221
if (err === null) {
222-
if (JSON.stringify(self.data) !== JSON.stringify(context.data) || includeMetadataChanges) {
222+
if (JSON.stringify(self.data) !== JSON.stringify(context.data) || includeMetadataChanges || forceTrigger) {
223223
onNext(new DocumentSnapshot(self.id, self.ref, self._getData()));
224224
context.data = self._getData();
225225
}
226226
} else {
227227
onError(err);
228228
}
229229
};
230+
231+
// onSnapshot should always return when initially called, then
232+
// every time data changes.
233+
onSnapshot(true);
230234
var unsubscribe = this.queue.onPostFlush(onSnapshot);
231235

232236
// return the unsubscribe function

src/firestore-query.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ MockFirestoreQuery.prototype.onSnapshot = function (optionsOrObserverOrOnNext, o
154154
var context = {
155155
data: self._results(),
156156
};
157-
var onSnapshot = function () {
157+
var onSnapshot = function (forceTrigger) {
158158
// compare the current state to the one from when this function was created
159159
// and send the data to the callback if different.
160160
if (err === null) {
161161
self.get().then(function (querySnapshot) {
162162
var results = self._results();
163-
if (JSON.stringify(results) !== JSON.stringify(context.data) || includeMetadataChanges) {
163+
if (JSON.stringify(results) !== JSON.stringify(context.data) || includeMetadataChanges || forceTrigger) {
164164
onNext(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id), results));
165165
// onNext(new QuerySnapshot(self.id, self.ref, results));
166166
context.data = results;
@@ -173,7 +173,7 @@ MockFirestoreQuery.prototype.onSnapshot = function (optionsOrObserverOrOnNext, o
173173

174174
// onSnapshot should always return when initially called, then
175175
// every time data changes.
176-
onSnapshot();
176+
onSnapshot(true);
177177
var unsubscribe = this.queue.onPostFlush(onSnapshot);
178178

179179
// return the unsubscribe function

0 commit comments

Comments
 (0)