From 18a70ffda13d3692aee34c097ad9330756d6479b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Tue, 2 Mar 2010 23:28:00 +0100 Subject: [PATCH] Tweaks - Add 'writeable' property - Renamed pump->flush - Use sys.mixin instead of process.mixin --- lib/fs.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 02701f687f..00f6168c15 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1,3 +1,5 @@ +var sys = require('sys'); + exports.Stats = process.Stats; process.Stats.prototype._checkModeProperty = function (property) { @@ -383,13 +385,13 @@ exports.fileWriteStream = function(path, options) { var FileWriteStream = exports.FileWriteStream = function(path, options) { this.path = path; this.fd = null; - this.closed = false; + this.writeable = true; this.flags = 'w'; this.encoding = 'binary'; this.mode = 0666; - process.mixin(this, options || {}); + sys.mixin(this, options || {}); var self = this, @@ -398,7 +400,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { queue.push([fs.open, this.path, this.flags, this.mode]); - function pump() { + function flush() { if (busy) { return; } @@ -416,6 +418,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { busy = false; if (err) { + self.writeable = false; self.emit('error', err); return; } @@ -426,13 +429,13 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { self.emit('open', self.fd); } - // stop pumping after close + // stop flushing after close if (method === fs.close) { self.emit('close'); return; } - pump(); + flush(); }); // Inject the file pointer @@ -444,21 +447,21 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { }; this.write = function(data) { - if (this.closed) { - throw new Error('stream already closed'); + if (!this.writeable) { + throw new Error('stream not writeable'); } queue.push([fs.write, data, undefined, this.encoding]); - pump(); + flush(); return false; }; this.close = function() { - this.closed = true; + this.writeable = false; queue.push([fs.close,]); - pump(); + flush(); }; - pump(); + flush(); }; -FileWriteStream.prototype.__proto__ = process.EventEmitter.prototype; +FileWriteStream.prototype.__proto__ = process.EventEmitter.prototype; \ No newline at end of file