Browse files

correcting example

  • Loading branch information...
1 parent d219c73 commit 19002579a53e74cc0cf0dd982a7369cec57a0d23 @pgte pgte committed Mar 29, 2012
Showing with 40 additions and 15 deletions.
  1. +21 −9 article.md
  2. +19 −6 random_stream.js
View
30 article.md
@@ -74,6 +74,7 @@ this.emit('error', new Error('Something went terribly wrong'));
This will enable the stream programatic clients to catch and handle errors gracefully. If there is noone listening to the "error" events, Node will throw an uncaught exception.
+
## Implementing a Readable Stream
A Readable Stream is mainly an object that emits "data" events.
@@ -165,10 +166,8 @@ On the other hand, if you originally have your data in a Buffer format, you can
```javascript
var buf = // some buffer I got
if (this.encoding) { buf = buf.toString(this.encoding); }
-```
this.emit('data', buf);
-
-If you have this
+```
### .pause()
@@ -212,32 +211,45 @@ RandomStream.prototype.encode = function(buffer) {
buffer = buffer.toString(this.encoding);
}
return buffer;
-}
+};
RandomStream.prototype.emitRandom = function() {
var buffer = new Buffer(this.options.size);
for(var i = 0; i < buffer.length; i ++) {
buffer[i] = Math.floor(Math.random() * 256);
}
this.emit('data', this.encode(buffer));
-}
+};
RandomStream.prototype.pause = function() {
if (this._interval) {
clearInterval(this._interval);
delete this._interval;
}
-}
+};
RandomStream.prototype.resume = function() {
var self = this;
+ if (this.ended) { throw new Error('Stream has ended'); }
+
if (! this._interval) {
- setInterval(function() {
- self.emitRandom();
- }, this.interval);
+ this._interval =
+ setInterval(function() {
+ self.emitRandom();
+ }, this.options.interval);
}
+};
+
+RandomStream.prototype.end = function(buf) {
+ this.ended = true;
+ if (buf) { this.write(buf); }
+ this.pause();
+};
+
+RandomStream.prototype.destroy = function() {
+ // do nothing
}
module.exports = RandomStream;
View
25 random_stream.js
@@ -27,32 +27,45 @@ RandomStream.prototype.encode = function(buffer) {
buffer = buffer.toString(this.encoding);
}
return buffer;
-}
+};
RandomStream.prototype.emitRandom = function() {
var buffer = new Buffer(this.options.size);
for(var i = 0; i < buffer.length; i ++) {
buffer[i] = Math.floor(Math.random() * 256);
}
this.emit('data', this.encode(buffer));
-}
+};
RandomStream.prototype.pause = function() {
if (this._interval) {
clearInterval(this._interval);
delete this._interval;
}
-}
+};
RandomStream.prototype.resume = function() {
var self = this;
+ if (this.ended) { throw new Error('Stream has ended'); }
+
if (! this._interval) {
- setInterval(function() {
- self.emitRandom();
- }, this.interval);
+ this._interval =
+ setInterval(function() {
+ self.emitRandom();
+ }, this.options.interval);
}
+};
+
+RandomStream.prototype.end = function(buf) {
+ this.ended = true;
+ if (buf) { this.write(buf); }
+ this.pause();
+};
+
+RandomStream.prototype.destroy = function() {
+ // do nothing
}
module.exports = RandomStream;

0 comments on commit 1900257

Please sign in to comment.