diff --git a/History.md b/History.md index b7c59cf0..b445c14c 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +3.0.2 / 2016-07-06 +================== + - Fix find cursor close when already paused + 3.0.1 / 2016-07-06 ================== - Fix find cursor pause and resume diff --git a/lib/collection.js b/lib/collection.js index 5cfa4158..0db5d28e 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -362,12 +362,15 @@ Collection.prototype.find = function (query, opts, fn) { } }) - cursor.on('end', function () { + function done () { if (fn) { fn() } resolve() - }) + } + + cursor.on('close', done) + cursor.on('end', done) cursor.on('error', function (err) { if (fn) { diff --git a/package.json b/package.json index 3eca179f..85a1f534 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "monk", - "version": "3.0.1", + "version": "3.0.2", "main": "lib/monk.js", "tags": [ "mongodb", diff --git a/test/collection.js b/test/collection.js index 7511519c..efaa3b84 100644 --- a/test/collection.js +++ b/test/collection.js @@ -237,7 +237,6 @@ test('find > should allow stream cursor destroy', (t) => { return users.insert([{ cursor: true }, { cursor: true }, { cursor: true }, { cursor: true }]).then(() => { return users.find(query) .each((doc, {close}) => { - console.log(found) t.not(doc.cursor, null) found++ if (found === 2) close() @@ -253,6 +252,29 @@ test('find > should allow stream cursor destroy', (t) => { }) }) +test('find > should allow stream cursor destroy even when paused', (t) => { + const query = { cursor: { $exists: true } } + let found = 0 + return users.insert([{ cursor: true }, { cursor: true }, { cursor: true }, { cursor: true }]).then(() => { + return users.find(query) + .each((doc, {close, pause, resume}) => { + pause() + t.not(doc.cursor, null) + found++ + if (found === 2) return close() + resume() + }) + .then(() => { + return new Promise((resolve) => { + setTimeout(() => { + t.is(found, 2) + resolve() + }, 100) + }) + }) + }) +}) + test('find > stream pause and continue', (t) => { const query = { stream: 4 } return users.insert([{ stream: 4 }, { stream: 4 }, { stream: 4 }, { stream: 4 }]).then(() => {