From ccf4afa256fc26838a1386b364f415776a47ef4e Mon Sep 17 00:00:00 2001 From: Tobie Langel Date: Wed, 8 Sep 2010 14:28:31 +0200 Subject: [PATCH] Do not emit WriteStream's drain event before ws.write has been called. --- lib/fs.js | 7 ++++++- test/simple/test-fs-write-stream.js | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index 74c56f64df..dcd41db628 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -840,7 +840,10 @@ WriteStream.prototype.flush = function () { var self = this; var args = this._queue.shift(); - if (!args) return self.emit('drain'); + if (!args) { + if (this.drainable) { self.emit('drain'); } + return; + } this.busy = true; @@ -896,6 +899,8 @@ WriteStream.prototype.write = function (data) { throw new Error('stream not writeable'); } + this.drainable = true; + var cb; if (typeof(arguments[arguments.length-1]) == 'function') { cb = arguments[arguments.length-1]; diff --git a/test/simple/test-fs-write-stream.js b/test/simple/test-fs-write-stream.js index 541d819229..6679536f50 100644 --- a/test/simple/test-fs-write-stream.js +++ b/test/simple/test-fs-write-stream.js @@ -17,3 +17,12 @@ var file = path.join(common.fixturesDir, "write.txt"); stream.destroy(); })(); +(function() { + var stream = fs.createWriteStream(file); + + stream.addListener('drain', function () { + assert.fail('"drain" event must not be emitted before stream.write() has been called at least once.') + }); + stream.destroy(); +})(); +