From cb72692f95c70cb38289d82e1bbf11e27c17a0af Mon Sep 17 00:00:00 2001 From: Eli Skeggs Date: Wed, 30 May 2018 12:35:10 -0700 Subject: [PATCH] Add and test for close event on Cursor Despite (some) documentation to the contrary, some folks assume that destroying a stream will emit a close event. This Cursor does not emit a close event, which causes some interoperability issues. Streams should probably emit the close event despite not being required to do so by the streams2 spec. See also https://github.com/pull-stream/stream-to-pull-stream/issues/12. --- lib/cursor.js | 4 +++- test/cursor.js | 12 ++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/cursor.js b/lib/cursor.js index 856a191..263e760 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -119,9 +119,11 @@ module.exports = class Cursor extends Readable { cursor = cursor[key](this._options[key]); }); + cursor.on('close', (...args) => this.emit('close', ...args)); + this.cursor = cursor; return cursor; }); } -} \ No newline at end of file +} diff --git a/test/cursor.js b/test/cursor.js index 708782d..6223a42 100644 --- a/test/cursor.js +++ b/test/cursor.js @@ -229,7 +229,15 @@ describe('cursor', function() { resolve(); }); }); - }) + }); + + it('should emit a close event when closed', async () => { + const cursor = db.a.findAsCursor(); + return new Promise((resolve) => { + cursor.once('close', resolve); + cursor.close(); + }); + }); }); @@ -245,4 +253,4 @@ class ToArrayStream extends Writable { cb(); } -} \ No newline at end of file +}