-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (35 loc) · 1.08 KB
/
index.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
38
39
40
41
42
43
44
45
'use strict';
const Readable = require('stream').Readable;
const inherits = require('util').inherits;
function StreamFromIterator(iterator, options) {
if (!(this instanceof StreamFromIterator)) {
return new StreamFromIterator(iterator, options);
}
Readable.call(this, options);
this.__iterator = iterator;
this.__lastResult = {done: false};
}
inherits(StreamFromIterator, Readable);
StreamFromIterator.obj = function (iterator, options) {
options = options || {};
options.objectMode = true;
return new StreamFromIterator(iterator, options);
};
StreamFromIterator.prototype._read = function () {
let needMoreData;
let value;
while (!this.__lastResult.done && needMoreData !== false) {
if (!this.__iterator || typeof this.__iterator.next !== 'function') {
// throw TypeError:
this.__lastResult = this.__iterator.next();
}
try {
this.__lastResult = this.__iterator.next();
value = this.__lastResult.value;
} catch (err) {
this.emit('error', err);
}
needMoreData = this.push(this.__lastResult.done ? null : value);
}
};
module.exports = StreamFromIterator;