Skip to content

Instantly share code, notes, and snippets.

@creationix
Created October 28, 2009 18:08
Show Gist options
  • Save creationix/220669 to your computer and use it in GitHub Desktop.
Save creationix/220669 to your computer and use it in GitHub Desktop.
patch for file.js in node.js
From 57e3be04890597d1757d629c7c2e63a9ae5b3ead Mon Sep 17 00:00:00 2001
From: Tim Caswell <tim@creationix.com>
Date: Wed, 28 Oct 2009 11:49:22 -0500
Subject: [PATCH] DRY up the open, write, read, and close methods on the File prototype.
---
lib/file.js | 28 ++++++++--------------------
1 files changed, 8 insertions(+), 20 deletions(-)
diff --git a/lib/file.js b/lib/file.js
index 1ac89a8..dd34278 100644
--- a/lib/file.js
+++ b/lib/file.js
@@ -108,17 +108,16 @@ proto._maybeDispatch = function () {
debugObject(self.currentAction);
args = self.currentAction.args || [];
+ method = self.currentAction.method;
+
- if (self.currentAction.method !== "open") {
+ if (method !== "open") {
args.unshift(self.fd);
}
- method = self.currentAction.method;
-
if (!args[3] && (method === "read" || method === "write")) {
args[3] = self.encoding;
}
-
promise = node.fs[method].apply(self, args);
userPromise = self.currentAction.promise;
@@ -144,21 +143,10 @@ proto._queueAction = function (method, args) {
return userPromise;
};
-// FIXME the following can probably be DRY'd up with some fancy getter
-// stuff.
-proto.open = function (filename, flags, mode) {
- return this._queueAction("open", [filename, flags, mode]);
-};
+(["open", "write", "read", "close"]).forEach(function (name) {
+ proto[name] = function () {
+ return this._queueAction(name, Array.prototype.slice.call(arguments, 0));
+ };
+});
-proto.write = function (data, pos, encoding) {
- return this._queueAction("write", [data, pos, encoding]);
-};
-
-proto.read = function (length, pos, encoding) {
- return this._queueAction("read", [length, pos, encoding]);
-};
-
-proto.close = function () {
- return this._queueAction("close");
-};
--
1.6.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment