-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathto-stream.js
37 lines (28 loc) · 834 Bytes
/
to-stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var stream = require('readable-stream')
var inherits = require('inherits')
module.exports = IteratorStream
function IteratorStream (ite) {
if (!(this instanceof IteratorStream)) return new IteratorStream(ite)
stream.Readable.call(this, {objectMode: true})
this.iterator = ite
this.onread = onread.bind(null, this)
this.destroyed = false
}
inherits(IteratorStream, stream.Readable)
IteratorStream.prototype._read = function () {
this.iterator.next(this.onread)
}
IteratorStream.prototype.destroy = function (err) {
if (this.destroyed) return
this.destroyed = true
var self = this
this.iterator.destroy(function (error) {
if (!err) err = error
if (err) self.emit('error', err)
self.emit('close')
})
}
function onread (self, err, value) {
if (err) self.destroy(err)
else self.push(value)
}