Skip to content

Commit

Permalink
refactor: remove nextObject helper, inline code in cusror class
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 15, 2020
1 parent bb359a1 commit f8694f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
26 changes: 24 additions & 2 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const CursorState = require('./core/cursor').CursorState;
const Map = require('./core').BSON.Map;
const maybePromise = require('./utils').maybePromise;
const executeOperation = require('./operations/execute_operation');
const formattedOrderClause = require('./utils').formattedOrderClause;

const each = require('./operations/cursor_ops').each;
const CountOperation = require('./operations/count');
const nextObject = require('./operations/common_functions').nextObject;

/**
* @fileOverview The **Cursor** class is an internal class that embodies a cursor on MongoDB
Expand Down Expand Up @@ -193,6 +193,10 @@ class Cursor extends CoreCursor {
* @return {Promise} returns Promise if no callback passed
*/
hasNext(callback) {
if (this.s.state === CursorState.CLOSED || (this.isDead && this.isDead())) {
throw MongoError.create({ message: 'Cursor is closed', driver: true });
}

return maybePromise(callback, cb => {
const cursor = this;
if (cursor.isNotified()) {
Expand Down Expand Up @@ -221,7 +225,25 @@ class Cursor extends CoreCursor {
*/
next(callback) {
return maybePromise(callback, cb => {
nextObject(this, cb);
const cursor = this;
if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead())) {
cb(MongoError.create({ message: 'Cursor is closed', driver: true }));
return;
}

if (cursor.s.state === CursorState.INIT && cursor.cmd.sort) {
try {
cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
} catch (err) {
return cb(err);
}
}

cursor._next((err, doc) => {
if (err) return cb(err);
cursor.s.state = CursorState.OPEN;
cb(null, doc);
});
});
}

Expand Down
37 changes: 1 addition & 36 deletions lib/operations/cursor_ops.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const buildCountCommand = require('./collection_ops').buildCountCommand;
const formattedOrderClause = require('../utils').formattedOrderClause;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const push = Array.prototype.push;
Expand Down Expand Up @@ -117,40 +116,6 @@ function loop(cursor, callback) {
return loop;
}

/**
* Get the next available document from the cursor. Returns null if no more documents are available.
*
* @method
* @param {Cursor} cursor The Cursor instance from which to get the next document.
* @param {Cursor~resultCallback} [callback] The result callback.
*/
function next(cursor, callback) {
nextObject(cursor, callback);
}

// Get the next available document from the cursor, returns null if no more documents are available.
function nextObject(cursor, callback) {
if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead()))
return handleCallback(
callback,
MongoError.create({ message: 'Cursor is closed', driver: true })
);
if (cursor.s.state === CursorState.INIT && cursor.cmd.sort) {
try {
cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
} catch (err) {
return handleCallback(callback, err);
}
}

// Get the next object
cursor._next((err, doc) => {
cursor.s.state = CursorState.OPEN;
if (err) return handleCallback(callback, err);
handleCallback(callback, null, doc);
});
}

/**
* Returns an array of documents. See Cursor.prototype.toArray for more information.
*
Expand Down Expand Up @@ -200,4 +165,4 @@ function toArray(cursor, callback) {
fetchDocs();
}

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

0 comments on commit f8694f5

Please sign in to comment.