Skip to content

Commit

Permalink
fix: don't consume first document when calling hasNext on cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 15, 2020
1 parent c56ff72 commit bb359a1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 58 deletions.
28 changes: 7 additions & 21 deletions lib/cursor.js
Expand Up @@ -124,8 +124,6 @@ class Cursor extends CoreCursor {
state: CursorState.INIT,
// Promise library
promiseLibrary,
// Current doc
currentDoc: null,
// explicitlyIgnoreSession
explicitlyIgnoreSession: !!options.explicitlyIgnoreSession
};
Expand Down Expand Up @@ -197,20 +195,18 @@ class Cursor extends CoreCursor {
hasNext(callback) {
return maybePromise(callback, cb => {
const cursor = this;
if (cursor.s.currentDoc) {
return cb(null, true);
}

if (cursor.isNotified()) {
return cb(null, false);
}

nextObject(cursor, (err, doc) => {
cursor._next((err, doc) => {
if (err) return cb(err);
if (cursor.s.state === Cursor.CLOSED || cursor.isDead()) return cb(null, false);
if (!doc) return cb(null, false);
if (doc == null || cursor.s.state === Cursor.CLOSED || cursor.isDead()) {
return cb(null, false);
}

cursor.s.currentDoc = doc;
cursor.s.state = CursorState.OPEN;
cursor.cursorState.cursorIndex--;
cb(null, true);
});
});
Expand All @@ -225,17 +221,7 @@ class Cursor extends CoreCursor {
*/
next(callback) {
return maybePromise(callback, cb => {
const cursor = this;

// Return the currentDoc if someone called hasNext first
if (cursor.s.currentDoc) {
const doc = cursor.s.currentDoc;
cursor.s.currentDoc = null;
return cb(null, doc);
}

// Return the next object
nextObject(cursor, cb);
nextObject(this, cb);
});
}

Expand Down
38 changes: 1 addition & 37 deletions lib/operations/cursor_ops.js
Expand Up @@ -106,34 +106,6 @@ function each(cursor, callback) {
}
}

/**
* Check if there is any document still available in the cursor.
*
* @method
* @param {Cursor} cursor The Cursor instance on which to run.
* @param {Cursor~resultCallback} [callback] The result callback.
*/
function hasNext(cursor, callback) {
if (cursor.s.currentDoc) {
return callback(null, true);
}

if (cursor.isNotified()) {
return callback(null, false);
}

nextObject(cursor, (err, doc) => {
if (err) return callback(err, null);
if (cursor.s.state === CursorState.CLOSED || cursor.isDead()) {
return callback(null, false);
}

if (!doc) return callback(null, false);
cursor.s.currentDoc = doc;
callback(null, true);
});
}

// Trampoline emptying the number of retrieved items
// without incurring a nextTick operation
function loop(cursor, callback) {
Expand All @@ -153,14 +125,6 @@ function loop(cursor, callback) {
* @param {Cursor~resultCallback} [callback] The result callback.
*/
function next(cursor, callback) {
// Return the currentDoc if someone called hasNext first
if (cursor.s.currentDoc) {
const doc = cursor.s.currentDoc;
cursor.s.currentDoc = null;
return callback(null, doc);
}

// Return the next object
nextObject(cursor, callback);
}

Expand Down Expand Up @@ -236,4 +200,4 @@ function toArray(cursor, callback) {
fetchDocs();
}

module.exports = { count, each, hasNext, next, toArray };
module.exports = { count, each, next, toArray };

0 comments on commit bb359a1

Please sign in to comment.