-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Readable instead of Stream.
- Loading branch information
Showing
5 changed files
with
104 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
zbjornson
Author
Collaborator
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is this supposed to be redefined here, or is it a merge resolution issue?