diff --git a/packages/mongo-livedata/mongo_driver.js b/packages/mongo-livedata/mongo_driver.js index 5c28ff8871..72cd5cb25b 100644 --- a/packages/mongo-livedata/mongo_driver.js +++ b/packages/mongo-livedata/mongo_driver.js @@ -490,7 +490,7 @@ _Mongo.prototype._observe = function (cursorDescription, ordered, callbacks) { // take place. liveResultsSet = new LiveResultsSet( cursorDescription, - self._createSynchronousCursor(cursorDescription), + self, ordered, function () { delete self._liveResultsSets[observeKey]; @@ -516,15 +516,19 @@ _Mongo.prototype._observe = function (cursorDescription, ordered, callbacks) { return observeHandle; }; -var LiveResultsSet = function (cursorDescription, synchronousCursor, ordered, +var LiveResultsSet = function (cursorDescription, mongoHandle, ordered, stopCallback) { var self = this; self._cursorDescription = cursorDescription; - self._synchronousCursor = synchronousCursor; + self._mongoHandle = mongoHandle; self._ordered = ordered; self._stopCallbacks = [stopCallback]; + // This constructor cannot yield, so we don't create the synchronousCursor yet + // (since that can yield). + self._synchronousCursor = null; + // previous results snapshot. on each poll cycle, diffs against // results drives the callbacks. self._results = ordered ? [] : {}; @@ -679,7 +683,12 @@ _.extend(LiveResultsSet.prototype, { self._pendingWrites = []; // Get the new query results. (These calls can yield.) - self._synchronousCursor.rewind(); + if (self._synchronousCursor) { + self._synchronousCursor.rewind(); + } else { + self._synchronousCursor = self._mongoHandle._createSynchronousCursor( + self._cursorDescription); + } var newResults = self._synchronousCursor.getRawObjects(self._ordered); var oldResults = self._results;