Put file stream methods into prototype, small style fixes

This commit is contained in:
Ryan Dahl 2010-04-08 10:37:10 -07:00
parent 3819920d77
commit 7faf7d5c8d

121
lib/fs.js
View File

@ -492,16 +492,32 @@ var FileReadStream = fs.FileReadStream = function(path, options) {
options = options || {}; options = options || {};
for (var i in options) this[i] = options[i]; for (var i in options) this[i] = options[i];
var var self = this;
self = this,
buffer = null;
function read() { fs.open(this.path, this.flags, this.mode, function(err, fd) {
if (!self.readable || self.paused) { if (err) {
self.emit('error', err);
self.readable = false;
return; return;
} }
fs.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) { self.fd = fd;
self.emit('open', fd);
self._read();
});
};
sys.inherits(FileReadStream, events.EventEmitter);
FileReadStream.prototype._read = function () {
var self = this;
if (!self.readable || self.paused) return;
fs.read(self.fd,
self.bufferSize,
undefined,
self.encoding,
function(err, data, bytesRead) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);
self.readable = false; self.readable = false;
@ -516,7 +532,7 @@ var FileReadStream = fs.FileReadStream = function(path, options) {
// do not emit events if the stream is paused // do not emit events if the stream is paused
if (self.paused) { if (self.paused) {
buffer = data; self.buffer = data;
return; return;
} }
@ -526,23 +542,13 @@ var FileReadStream = fs.FileReadStream = function(path, options) {
} }
self.emit('data', data); self.emit('data', data);
read(); self._read();
}); });
} };
fs.open(this.path, this.flags, this.mode, function(err, fd) {
if (err) {
self.emit('error', err);
self.readable = false;
return;
}
self.fd = fd; FileReadStream.prototype.forceClose = function (cb) {
self.emit('open', fd); var self = this;
read();
});
this.forceClose = function(cb) {
this.readable = false; this.readable = false;
function close() { function close() {
@ -569,22 +575,24 @@ var FileReadStream = fs.FileReadStream = function(path, options) {
} }
}; };
this.pause = function() {
FileReadStream.prototype.pause = function() {
this.paused = true; this.paused = true;
}; };
this.resume = function() {
FileReadStream.prototype.resume = function() {
this.paused = false; this.paused = false;
if (buffer !== null) { if (this.buffer) {
self.emit('data', buffer); this.emit('data', this.buffer);
buffer = null; this.buffer = null;
} }
read(); this._read();
}; };
};
sys.inherits(FileReadStream, events.EventEmitter);
fs.createWriteStream = function(path, options) { fs.createWriteStream = function(path, options) {
return new FileWriteStream(path, options); return new FileWriteStream(path, options);
@ -604,31 +612,33 @@ var FileWriteStream = fs.FileWriteStream = function(path, options) {
options = options || {}; options = options || {};
for (var i in options) this[i] = options[i]; for (var i in options) this[i] = options[i];
var this.busy = false;
self = this, this._queue = [];
queue = [],
busy = false;
queue.push([fs.open, this.path, this.flags, this.mode, undefined]); this._queue.push([fs.open, this.path, this.flags, this.mode, undefined]);
this.flush();
};
sys.inherits(FileWriteStream, events.EventEmitter);
function flush() {
if (busy) {
return;
}
var args = queue.shift();
if (!args) {
return self.emit('drain');
}
busy = true; FileWriteStream.prototype.flush = function () {
if (this.busy) return;
var self = this;
var args = this._queue.shift();
if (!args) return self.emit('drain');
this.busy = true;
var var
method = args.shift(), method = args.shift(),
cb = args.pop(); cb = args.pop();
var self = this;
args.push(function(err) { args.push(function(err) {
busy = false; self.busy = false;
if (err) { if (err) {
self.writeable = false; self.writeable = false;
@ -657,7 +667,7 @@ var FileWriteStream = fs.FileWriteStream = function(path, options) {
cb(null, arguments[1]); cb(null, arguments[1]);
} }
flush(); self.flush();
}); });
// Inject the file pointer // Inject the file pointer
@ -668,23 +678,27 @@ var FileWriteStream = fs.FileWriteStream = function(path, options) {
method.apply(this, args); method.apply(this, args);
}; };
this.write = function(data, cb) {
FileWriteStream.prototype.write = function(data, cb) {
if (!this.writeable) { if (!this.writeable) {
throw new Error('stream not writeable'); throw new Error('stream not writeable');
} }
queue.push([fs.write, data, undefined, this.encoding, cb]); this._queue.push([fs.write, data, undefined, this.encoding, cb]);
flush(); this.flush();
return false; return false;
}; };
this.close = function(cb) {
FileWriteStream.prototype.close = function (cb) {
this.writeable = false; this.writeable = false;
queue.push([fs.close, cb]); this._queue.push([fs.close, cb]);
flush(); this.flush();
}; };
this.forceClose = function(cb) {
FileWriteStream.prototype.forceClose = function (cb) {
this.writeable = false; this.writeable = false;
fs.close(self.fd, function(err) { fs.close(self.fd, function(err) {
if (err) { if (err) {
@ -703,6 +717,3 @@ var FileWriteStream = fs.FileWriteStream = function(path, options) {
}); });
}; };
flush();
};
sys.inherits(FileWriteStream, events.EventEmitter);