Skip to content

Commit

Permalink
Add and test for close event on Cursor
Browse files Browse the repository at this point in the history
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 pull-stream/stream-to-pull-stream#12.
  • Loading branch information
Eli Skeggs authored and saintedlama committed Jun 4, 2018
1 parent 80d1cd4 commit cb72692
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
}
12 changes: 10 additions & 2 deletions test/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});


Expand All @@ -245,4 +253,4 @@ class ToArrayStream extends Writable {

cb();
}
}
}

0 comments on commit cb72692

Please sign in to comment.