Skip to content

Commit

Permalink
correcting example
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Mar 29, 2012
1 parent d219c73 commit 1900257
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
30 changes: 21 additions & 9 deletions article.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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;
Expand Down
25 changes: 19 additions & 6 deletions random_stream.js
Expand Up @@ -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.