Skip to content

Commit f8694f5

Browse files
committed
refactor: remove nextObject helper, inline code in cusror class
1 parent bb359a1 commit f8694f5

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

lib/cursor.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ const CursorState = require('./core/cursor').CursorState;
1111
const Map = require('./core').BSON.Map;
1212
const maybePromise = require('./utils').maybePromise;
1313
const executeOperation = require('./operations/execute_operation');
14+
const formattedOrderClause = require('./utils').formattedOrderClause;
1415

1516
const each = require('./operations/cursor_ops').each;
1617
const CountOperation = require('./operations/count');
17-
const nextObject = require('./operations/common_functions').nextObject;
1818

1919
/**
2020
* @fileOverview The **Cursor** class is an internal class that embodies a cursor on MongoDB
@@ -193,6 +193,10 @@ class Cursor extends CoreCursor {
193193
* @return {Promise} returns Promise if no callback passed
194194
*/
195195
hasNext(callback) {
196+
if (this.s.state === CursorState.CLOSED || (this.isDead && this.isDead())) {
197+
throw MongoError.create({ message: 'Cursor is closed', driver: true });
198+
}
199+
196200
return maybePromise(callback, cb => {
197201
const cursor = this;
198202
if (cursor.isNotified()) {
@@ -221,7 +225,25 @@ class Cursor extends CoreCursor {
221225
*/
222226
next(callback) {
223227
return maybePromise(callback, cb => {
224-
nextObject(this, cb);
228+
const cursor = this;
229+
if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead())) {
230+
cb(MongoError.create({ message: 'Cursor is closed', driver: true }));
231+
return;
232+
}
233+
234+
if (cursor.s.state === CursorState.INIT && cursor.cmd.sort) {
235+
try {
236+
cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
237+
} catch (err) {
238+
return cb(err);
239+
}
240+
}
241+
242+
cursor._next((err, doc) => {
243+
if (err) return cb(err);
244+
cursor.s.state = CursorState.OPEN;
245+
cb(null, doc);
246+
});
225247
});
226248
}
227249

lib/operations/cursor_ops.js

+1-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const buildCountCommand = require('./collection_ops').buildCountCommand;
4-
const formattedOrderClause = require('../utils').formattedOrderClause;
54
const handleCallback = require('../utils').handleCallback;
65
const MongoError = require('../core').MongoError;
76
const push = Array.prototype.push;
@@ -117,40 +116,6 @@ function loop(cursor, callback) {
117116
return loop;
118117
}
119118

120-
/**
121-
* Get the next available document from the cursor. Returns null if no more documents are available.
122-
*
123-
* @method
124-
* @param {Cursor} cursor The Cursor instance from which to get the next document.
125-
* @param {Cursor~resultCallback} [callback] The result callback.
126-
*/
127-
function next(cursor, callback) {
128-
nextObject(cursor, callback);
129-
}
130-
131-
// Get the next available document from the cursor, returns null if no more documents are available.
132-
function nextObject(cursor, callback) {
133-
if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead()))
134-
return handleCallback(
135-
callback,
136-
MongoError.create({ message: 'Cursor is closed', driver: true })
137-
);
138-
if (cursor.s.state === CursorState.INIT && cursor.cmd.sort) {
139-
try {
140-
cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
141-
} catch (err) {
142-
return handleCallback(callback, err);
143-
}
144-
}
145-
146-
// Get the next object
147-
cursor._next((err, doc) => {
148-
cursor.s.state = CursorState.OPEN;
149-
if (err) return handleCallback(callback, err);
150-
handleCallback(callback, null, doc);
151-
});
152-
}
153-
154119
/**
155120
* Returns an array of documents. See Cursor.prototype.toArray for more information.
156121
*
@@ -200,4 +165,4 @@ function toArray(cursor, callback) {
200165
fetchDocs();
201166
}
202167

203-
module.exports = { count, each, next, toArray };
168+
module.exports = { count, each, toArray };

0 commit comments

Comments
 (0)