Skip to content

Commit

Permalink
Merge pull request #1 from CodeMan99/producer-class
Browse files Browse the repository at this point in the history
Move the Producer class to module scope.
  • Loading branch information
AWinterman authored Feb 18, 2020
2 parents dd0011a + d0cbcd5 commit 9926ffe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "6"
- "8"
- "9"
25 changes: 13 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ if(Readable === undefined) {
Readable = require('readable-stream')
}

function Producer(source, options) {
Readable.call(this, options)
this._index = 0
this._source = source
}

util.inherits(Producer, Readable)

Producer.prototype._read = function() {
// evaluate the source, and push the results.
this._source(this._index++)
}

// from
//
// a stream that reads from an source.
Expand All @@ -22,18 +35,6 @@ function from(source, options) {
return from(streamify(source, options), options)
}

function Producer(source, options) {
Readable.call(this, options)
this._index = 0
}

util.inherits(Producer, Readable)

Producer.prototype._read = function() {
// evaluate the source, and push the results.
source.call(this, this._index++)
}

return new Producer(source, options)
}

Expand Down
8 changes: 7 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var assert = require('assert')
, from = require('./')
, Stream = require('stream').Stream

var random = ['hello', 'goodbye', 1, 2, 3]

Expand All @@ -17,9 +18,14 @@ var i = 0

characters.on('data', function(data) {
assert.equal(data, sentence[i++])
assert(i)
})

from(function(index) {
assert(index === 0)
assert(this instanceof Stream)
})

assert(i)



0 comments on commit 9926ffe

Please sign in to comment.