'use strict'; var inherits = require('inherits'); var Readable = require('readable-stream').Readable; module.exports = ReadStream; inherits(ReadStream, Readable); function ReadStream(db, opts) { if (!(this instanceof ReadStream)) { return new ReadStream(db, opts); } Readable.call(this, { objectMode: true }); opts = opts || {}; var thisOpts = this.opts = { since: 0 }; Object.keys(opts).forEach(function (key) { thisOpts[key] = opts[key]; }); this.opts.returnDocs = false; this.last = opts.since; this.db = db; this.changes = void 0; this.canceled = false; } ReadStream.prototype._read = function () { if (this.changes) { return; } var self = this; this.opts.since = this.last; this.changes = this.db.changes(this.opts).on('complete', function (resp) { self.cancel = function () {}; if (!resp.canceled) { self.push(null); } }); this.changes.on('change', function (change) { self.last = change.seq; var more = self.push(change); if (!more) { self.changes.cancel(); } }); }; ReadStream.prototype.cancel = function () { this.canceled = true; if (this.changes && typeof this.changes.cancel === 'function') { return this.changes.cancel(); } };