Skip to content

Commit

Permalink
Small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Mar 26, 2013
1 parent 767c283 commit f4d02e7
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions buffered-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ BufferedStream.prototype.pause = function () {
* Resumes emitting data events.
*/
BufferedStream.prototype.resume = function () {
if (this.paused) flushOnNextTick(this);
if (this.paused) flushSoon(this);
this.paused = false;
};

Expand All @@ -103,16 +103,18 @@ BufferedStream.prototype.resume = function () {
* stream is full and should not be written to further until drained, true
* otherwise.
*/
BufferedStream.prototype.write = function (chunk, encoding) {
BufferedStream.prototype.write = function (chunk) {
if (!this.writable) throw new Error('Stream is not writable');
if (this.ended) throw new Error('Stream is already ended');

if (typeof chunk === 'string') chunk = new Buffer(chunk, encoding);
if (typeof chunk === 'string') {
chunk = new Buffer(chunk, arguments[1]);
}

this._buffer.push(chunk);
this.size += chunk.length;

flushOnNextTick(this);
flushSoon(this);

if (this.full) {
this._wasFull = true;
Expand All @@ -128,18 +130,21 @@ BufferedStream.prototype.write = function (chunk, encoding) {
* scheduled to be flushed, the end event will fire immediately. Otherwise, it
* will fire after the next flush.
*/
BufferedStream.prototype.end = function (chunk, encoding) {
BufferedStream.prototype.end = function (chunk) {
if (this.ended) throw new Error('Stream is already ended');

if (chunk != null) this.write(chunk, encoding);
if (chunk != null) {
this.write(chunk, arguments[1]);
}

this.ended = true;

// Trigger the flush cycle one last time to emit any data that
// was written before end was called.
flushOnNextTick(this);
flushSoon(this);
};

function flushOnNextTick(stream) {
function flushSoon(stream) {
if (stream._flushing) return;
stream._flushing = true;

Expand All @@ -163,8 +168,7 @@ function flush(stream) {
if (!stream._buffer) return;

var chunk;
while (stream._buffer.length) {
chunk = stream._buffer.shift();
while (chunk = stream._buffer.shift()) {
stream.size -= chunk.length;

if (stream.encoding) {
Expand Down

0 comments on commit f4d02e7

Please sign in to comment.