Skip to content

Commit 65a9ddf

Browse files
committed
Move onSnapshot handers to the queue so that a call to flush will call the onSnapshot callbacks no mater where the flush is called from.
1 parent 57643cc commit 65a9ddf

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

src/firestore-document.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ function MockFirestoreDocument(path, data, parent, name, CollectionReference) {
2323
if (parent) parent.children[this.id] = this;
2424
this.data = null;
2525
this._dataChanged(_.cloneDeep(data) || null);
26-
this.onSnapshotSubscribers = [];
2726
}
2827

2928
MockFirestoreDocument.prototype.flush = function (delay) {
30-
var self = this;
31-
_.forEach(this.onSnapshotSubscribers, function (subscriber) {
32-
self._defer('onSnapshot', _.toArray(arguments), subscriber);
33-
});
3429
this.queue.flush(delay);
3530
return this;
3631
};
@@ -224,20 +219,18 @@ MockFirestoreDocument.prototype.onSnapshot = function (optionsOrObserverOrOnNext
224219
// compare the current state to the one from when this function was created
225220
// and send the data to the callback if different.
226221
if (err === null) {
227-
if (JSON.stringify(this.data) !== JSON.stringify(context.data) || includeMetadataChanges) {
222+
if (JSON.stringify(self.data) !== JSON.stringify(context.data) || includeMetadataChanges) {
228223
onNext(new DocumentSnapshot(self.id, self.ref, self._getData()));
229-
context.data = this._getData();
224+
context.data = self._getData();
230225
}
231226
} else {
232227
onError(err);
233228
}
234229
};
235-
this.onSnapshotSubscribers.push(onSnapshot);
230+
var unsubscribe = this.queue.onPostFlush(onSnapshot);
236231

237232
// return the unsubscribe function
238-
return function () {
239-
self.onSnapshotSubscribers.pop(onSnapshot);
240-
};
233+
return unsubscribe;
241234
};
242235

243236

src/firestore-query.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ function MockFirestoreQuery(path, data, parent, name) {
2323
this.orderedDirections = [];
2424
this.limited = 0;
2525
this._setData(data);
26-
this.onSnapshotSubscribers = [];
2726
}
2827

2928
MockFirestoreQuery.prototype.flush = function (delay) {
30-
var self = this;
31-
_.forEach(this.onSnapshotSubscribers, function (subscriber) {
32-
self._defer('onSnapshot', _.toArray(arguments), subscriber);
33-
});
3429
this.queue.flush(delay);
3530
return this;
3631
};
@@ -163,7 +158,7 @@ MockFirestoreQuery.prototype.onSnapshot = function (optionsOrObserverOrOnNext, o
163158
// compare the current state to the one from when this function was created
164159
// and send the data to the callback if different.
165160
if (err === null) {
166-
this.get().then(function (querySnapshot) {
161+
self.get().then(function (querySnapshot) {
167162
var results = self._results();
168163
if (JSON.stringify(results) !== JSON.stringify(context.data) || includeMetadataChanges) {
169164
onNext(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id), results));
@@ -175,12 +170,10 @@ MockFirestoreQuery.prototype.onSnapshot = function (optionsOrObserverOrOnNext, o
175170
onError(err);
176171
}
177172
};
178-
this.onSnapshotSubscribers.push(onSnapshot);
173+
var unsubscribe = this.queue.onPostFlush(onSnapshot);
179174

180175
// return the unsubscribe function
181-
return function () {
182-
self.onSnapshotSubscribers.pop(onSnapshot);
183-
};
176+
return unsubscribe;
184177
};
185178

186179
MockFirestoreQuery.prototype._results = function () {

src/queue.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var EventEmitter = require('events').EventEmitter;
66

77
function FlushQueue () {
88
this.events = [];
9+
this.postFlushListeners = [];
910
}
1011

1112
FlushQueue.prototype.push = function () {
@@ -23,6 +24,14 @@ FlushQueue.prototype.push = function () {
2324
}));
2425
};
2526

27+
FlushQueue.prototype.onPostFlush = function(subscriber) {
28+
this.postFlushListeners.push(subscriber);
29+
var self = this;
30+
return function() {
31+
self.postFlushListeners.pop(subscriber);
32+
};
33+
};
34+
2635
FlushQueue.prototype.flushing = false;
2736

2837
FlushQueue.prototype.flush = function (delay) {
@@ -33,6 +42,9 @@ FlushQueue.prototype.flush = function (delay) {
3342
}
3443
function process () {
3544
self.flushing = true;
45+
_.forEach(self.postFlushListeners, function (subscriber) {
46+
self.push(subscriber);
47+
});
3648
while (self.events.length) {
3749
self.events[0].run();
3850
}

test/unit/firestore-collection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ describe('MockFirestoreCollection', function () {
412412
expect(err).to.equal(error);
413413
done();
414414
});
415+
collection.doc('a').update({name: 'A'}, {setMerge: true});
415416
collection.flush();
416417
});
417418

test/unit/firestore-document.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ describe('MockFirestoreDocument', function () {
471471
done();
472472
});
473473
doc.update({newTitle: 'A new title'}, {setMerge: true});
474-
doc.flush();
474+
db.flush();
475475
});
476476

477477
it('returns error if error occured', function (done) {
@@ -483,6 +483,7 @@ describe('MockFirestoreDocument', function () {
483483
expect(err).to.equal(error);
484484
done();
485485
});
486+
doc.update({name: 'A'}, {setMerge: true});
486487
doc.flush();
487488
});
488489

0 commit comments

Comments
 (0)