-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement Readable instead of Stream #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ | |
* Module dependencies. | ||
*/ | ||
|
||
var Stream = require('stream').Stream; | ||
var Readable = require('stream').Readable; | ||
var util = require('util'); | ||
|
||
/** | ||
* Initialize a `PNGStream` with the given `canvas`. | ||
|
@@ -30,32 +31,59 @@ var Stream = require('stream').Stream; | |
*/ | ||
|
||
var PNGStream = module.exports = function PNGStream(canvas, sync) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a: if (!(this instanceof PNGStream)) {
return new PNGStream(canvas, sync)
} To avoid pollution of the global namespace if invoked without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer throwing an error since this is the default behaviour with ES6 classes which we probably will want to upgrade the code base to at some point. Also, you can always stop throwing an exception without a major bump but going the other way is more complicated. This is the exact error the Node throws: throw new TypeError("Class constructors cannot be invoked without 'new'") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Addressed. GitHub isn't hiding this because the diff is below the comment.) |
||
var self = this | ||
, method = sync | ||
if (!(this instanceof PNGStream)) { | ||
throw new TypeError("Class constructors cannot be invoked without 'new'"); | ||
} | ||
|
||
Readable.call(this); | ||
|
||
var self = this; | ||
var method = sync | ||
? 'streamPNGSync' | ||
: 'streamPNG'; | ||
this.sync = sync; | ||
this.canvas = canvas; | ||
|
||
// TODO: implement async | ||
if ('streamPNG' === method) method = 'streamPNGSync'; | ||
this.method = method; | ||
}; | ||
|
||
util.inherits(PNGStream, Readable); | ||
|
||
var PNGStream = module.exports = function PNGStream(canvas, sync) { | ||
Readable.call(this); | ||
|
||
var self = this; | ||
var method = sync | ||
? 'streamPNGSync' | ||
: 'streamPNG'; | ||
this.sync = sync; | ||
this.canvas = canvas; | ||
this.readable = true; | ||
|
||
// TODO: implement async | ||
if ('streamPNG' == method) method = 'streamPNGSync'; | ||
if ('streamPNG' === method) method = 'streamPNGSync'; | ||
this.method = method; | ||
}; | ||
|
||
util.inherits(PNGStream, Readable); | ||
|
||
function noop() {} | ||
|
||
PNGStream.prototype._read = function _read() { | ||
// For now we're not controlling the c++ code's data emission, so we only | ||
// call canvas.streamPNGSync once and let it emit data at will. | ||
this._read = noop; | ||
var self = this; | ||
process.nextTick(function(){ | ||
canvas[method](function(err, chunk, len){ | ||
self.canvas[self.method](function(err, chunk, len){ | ||
if (err) { | ||
self.emit('error', err); | ||
self.readable = false; | ||
} else if (len) { | ||
self.emit('data', chunk, len); | ||
self.push(chunk); | ||
} else { | ||
self.emit('end'); | ||
self.readable = false; | ||
self.push(null); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Inherit from `EventEmitter`. | ||
*/ | ||
|
||
PNGStream.prototype.__proto__ = Stream.prototype; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should depend from
readable-stream
, it is better practice and yields more stability across node versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does require pulling in another dependency though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering the fact that most of the stream utilities depends on readable-stream, it's not really problematic (the users will do depend on it anyway). The only issue might be in the browser, but I don't think this can be used there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(in browserify the stream module is based on readable-stream so it's no big deal)