incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From deedu...@apache.org
Subject git commit: Minor File API cleanup.
Date Wed, 29 Feb 2012 15:51:05 GMT
Updated Branches:
  refs/heads/master 51f6f57a4 -> a1bf7477b


Minor File API cleanup.

 - Check for existance of error/success callback.
 - Wrap additional error codes in FileError object.
 - Minor linting cleanup.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/a1bf7477
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/a1bf7477
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/a1bf7477

Branch: refs/heads/master
Commit: a1bf7477b163c0ae16a3050dabe5e57b9b2cac5d
Parents: 51f6f57
Author: Drew Walters <deedubbu@gmail.com>
Authored: Wed Feb 29 09:50:28 2012 -0600
Committer: Drew Walters <deedubbu@gmail.com>
Committed: Wed Feb 29 09:50:28 2012 -0600

----------------------------------------------------------------------
 lib/plugin/DirectoryEntry.js            |   12 ++--
 lib/plugin/DirectoryReader.js           |    4 +-
 lib/plugin/Entry.js                     |   91 +++++++++++++------------
 lib/plugin/FileEntry.js                 |    8 +-
 lib/plugin/FileReader.js                |    1 -
 lib/plugin/FileWriter.js                |    1 -
 lib/plugin/blackberry/app.js            |    2 +-
 lib/plugin/requestFileSystem.js         |   45 +++++++------
 lib/plugin/resolveLocalFileSystemURI.js |   22 ++++---
 9 files changed, 97 insertions(+), 89 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/DirectoryEntry.js
----------------------------------------------------------------------
diff --git a/lib/plugin/DirectoryEntry.js b/lib/plugin/DirectoryEntry.js
index 0511fc4..e9949d0 100644
--- a/lib/plugin/DirectoryEntry.js
+++ b/lib/plugin/DirectoryEntry.js
@@ -5,7 +5,7 @@ var utils = require('cordova/utils'),
 
 /**
  * An interface representing a directory on the file system.
- * 
+ *
  * {boolean} isFile always false (readonly)
  * {boolean} isDirectory always true (readonly)
  * {DOMString} name of the directory, excluding the path leading to it (readonly)
@@ -34,11 +34,11 @@ DirectoryEntry.prototype.createReader = function() {
  * @param {Function} errorCallback is called with a FileError
  */
 DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, errorCallback)
{
-    var win = function(result) {
+    var win = typeof successCallback !== 'function' ? null : function(result) {
         var entry = new DirectoryEntry(result.name, result.fullPath);
         successCallback(entry);
     };
-    var fail = function(code) {
+    var fail = typeof errorCallback !== 'function' ? null : function(code) {
         errorCallback(new FileError(code));
     };
     exec(win, fail, "File", "getDirectory", [this.fullPath, path, options]);
@@ -51,7 +51,7 @@ DirectoryEntry.prototype.getDirectory = function(path, options, successCallback,
  * @param {Function} errorCallback is called with a FileError
  */
 DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCallback) {
-    var fail = function(code) {
+    var fail = typeof errorCallback !== 'function' ? null : function(code) {
         errorCallback(new FileError(code));
     };
     exec(successCallback, fail, "File", "removeRecursively", [this.fullPath]);
@@ -66,12 +66,12 @@ DirectoryEntry.prototype.removeRecursively = function(successCallback,
errorCall
  * @param {Function} errorCallback is called with a FileError
  */
 DirectoryEntry.prototype.getFile = function(path, options, successCallback, errorCallback)
{
-    var win = function(result) {
+    var win = typeof successCallback !== 'function' ? null : function(result) {
         var FileEntry = require('cordova/plugin/FileEntry');
         var entry = new FileEntry(result.name, result.fullPath);
         successCallback(entry);
     };
-    var fail = function(code) {
+    var fail = typeof errorCallback !== 'function' ? null : function(code) {
         errorCallback(new FileError(code));
     };
     exec(win, fail, "File", "getFile", [this.fullPath, path, options]);

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/DirectoryReader.js
----------------------------------------------------------------------
diff --git a/lib/plugin/DirectoryReader.js b/lib/plugin/DirectoryReader.js
index 8f106b4..90e67a4 100644
--- a/lib/plugin/DirectoryReader.js
+++ b/lib/plugin/DirectoryReader.js
@@ -14,7 +14,7 @@ function DirectoryReader(path) {
  * @param {Function} errorCallback is called with a FileError
  */
 DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) {
-    var win = function(result) {
+    var win = typeof successCallback !== 'function' ? null : function(result) {
         var retVal = [];
         for (var i=0; i<result.length; i++) {
             var entry = null;
@@ -32,7 +32,7 @@ DirectoryReader.prototype.readEntries = function(successCallback, errorCallback)
         }
         successCallback(retVal);
     };
-    var fail = function(code) {
+    var fail = typeof errorCallback !== 'function' ? null : function(code) {
         errorCallback(new FileError(code));
     };
     exec(win, fail, "File", "readEntries", [this.path]);

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/Entry.js
----------------------------------------------------------------------
diff --git a/lib/plugin/Entry.js b/lib/plugin/Entry.js
index b4ce74b..f748dcc 100644
--- a/lib/plugin/Entry.js
+++ b/lib/plugin/Entry.js
@@ -4,7 +4,7 @@ var exec = require('cordova/exec'),
 
 /**
  * Represents a file or directory on the local file system.
- * 
+ *
  * @param isFile
  *            {boolean} true if Entry is a file (readonly)
  * @param isDirectory
@@ -26,26 +26,27 @@ function Entry(isFile, isDirectory, name, fullPath, fileSystem) {
 
 /**
  * Look up the metadata of the entry.
- * 
+ *
  * @param successCallback
  *            {Function} is called with a Metadata object
  * @param errorCallback
  *            {Function} is called with a FileError
  */
 Entry.prototype.getMetadata = function(successCallback, errorCallback) {
-  var success = function(lastModified) {
+  var success = typeof successCallback !== 'function' ? null : function(lastModified) {
       var metadata = new Metadata(lastModified);
       successCallback(metadata);
   };
-  var fail = function(code) {
+  var fail = typeof errorCallback !== 'function' ? null : function(code) {
       errorCallback(new FileError(code));
   };
+
   exec(success, fail, "File", "getMetadata", [this.fullPath]);
 };
 
 /**
  * Move a file or directory to a new location.
- * 
+ *
  * @param parent
  *            {DirectoryEntry} the directory to which to move this entry
  * @param newName
@@ -56,50 +57,49 @@ Entry.prototype.getMetadata = function(successCallback, errorCallback)
{
  *            {Function} called with a FileError
  */
 Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) {
+    var fail = function(code) {
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(code));
+        }
+    };
     // user must specify parent Entry
     if (!parent) {
-        errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+        fail(FileError.NOT_FOUND_ERR);
         return;
     }
     // source path
     var srcPath = this.fullPath,
         // entry name
         name = newName || this.name,
-        // destination path
-        dstPath,
         success = function(entry) {
-            var result; 
-
             if (entry) {
-                // create appropriate Entry object
-                result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name,
entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath);
-                try {
-                    successCallback(result);
-                }
-                catch (e) {
-                    console.log('Error invoking callback: ' + e);
+                if (typeof successCallback === 'function') {
+                    // create appropriate Entry object
+                    var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name,
entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath);
+                    try {
+                        successCallback(result);
+                    }
+                    catch (e) {
+                        console.log('Error invoking callback: ' + e);
+                    }
                 }
-            } 
+            }
             else {
                 // no Entry object returned
-                errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+                fail(FileError.NOT_FOUND_ERR);
             }
-        },
-        fail = function(code) {
-            errorCallback(new FileError(code));
         };
 
-
     // copy
     exec(success, fail, "File", "moveTo", [srcPath, parent.fullPath, name]);
 };
 
 /**
  * Copy a directory to a different location.
- * 
- * @param parent 
+ *
+ * @param parent
  *            {DirectoryEntry} the directory to which to copy the entry
- * @param newName 
+ * @param newName
  *            {DOMString} new name of the entry, defaults to the current name
  * @param successCallback
  *            {Function} called with the new Entry object
@@ -107,9 +107,15 @@ Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallbac
  *            {Function} called with a FileError
  */
 Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) {
+    var fail = function(code) {
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(code));
+        }
+    };
+
     // user must specify parent Entry
     if (!parent) {
-        errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+        fail(FileError.NOT_FOUND_ERR);
         return;
     }
 
@@ -119,25 +125,22 @@ Entry.prototype.copyTo = function(parent, newName, successCallback,
errorCallbac
         name = newName || this.name,
         // success callback
         success = function(entry) {
-            var result; 
-
             if (entry) {
-                // create appropriate Entry object
-                result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name,
entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath);
-                try {
-                    successCallback(result);
-                }
-                catch (e) {
-                    console.log('Error invoking callback: ' + e);
+                if (typeof successCallback === 'function') {
+                    // create appropriate Entry object
+                    var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name,
entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath);
+                    try {
+                        successCallback(result);
+                    }
+                    catch (e) {
+                        console.log('Error invoking callback: ' + e);
+                    }
                 }
             }
             else {
                 // no Entry object returned
-                errorCallback(new FileError(FileError.NOT_FOUND_ERR));
+                fail(FileError.NOT_FOUND_ERR);
             }
-        },
-        fail = function(code) {
-            errorCallback(new FileError(code));
         };
 
     // copy
@@ -168,13 +171,13 @@ Entry.prototype.toURI = function(mimeType) {
  * Remove a file or directory. It is an error to attempt to delete a
  * directory that is not empty. It is an error to attempt to delete a
  * root directory of a file system.
- * 
+ *
  * @param successCallback {Function} called with no parameters
  * @param errorCallback {Function} called with a FileError
  */
 Entry.prototype.remove = function(successCallback, errorCallback) {
-    var fail = function(code) {
-      errorCallback(new FileError(code));
+    var fail = typeof errorCallback !== 'function' ? null : function(code) {
+        errorCallback(new FileError(code));
     };
     exec(successCallback, fail, "File", "remove", [this.fullPath]);
 };
@@ -186,7 +189,7 @@ Entry.prototype.remove = function(successCallback, errorCallback) {
  * @param errorCallback {Function} called with a FileError
  */
 Entry.prototype.getParent = function(successCallback, errorCallback) {
-    var fail = function(code) {
+    var fail = typeof errorCallback !== 'function' ? null : function(code) {
         errorCallback(new FileError(code));
     };
     exec(successCallback, fail, "File", "getParent", [this.fullPath]);

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/FileEntry.js
----------------------------------------------------------------------
diff --git a/lib/plugin/FileEntry.js b/lib/plugin/FileEntry.js
index c8aaded..cbe22c4 100644
--- a/lib/plugin/FileEntry.js
+++ b/lib/plugin/FileEntry.js
@@ -7,7 +7,7 @@ var utils = require('cordova/utils'),
 
 /**
  * An interface representing a file on the file system.
- * 
+ *
  * {boolean} isFile always true (readonly)
  * {boolean} isDirectory always false (readonly)
  * {DOMString} name of the file, excluding the path leading to it (readonly)
@@ -49,12 +49,12 @@ FileEntry.prototype.createWriter = function(successCallback, errorCallback)
{
  * @param {Function} errorCallback is called with a FileError
  */
 FileEntry.prototype.file = function(successCallback, errorCallback) {
-    var win = function(f) {
+    var win = typeof successCallback !== 'function' ? null : function(f) {
         var file = new File(f.name, f.fullPath, f.type, f.lastModifiedDate, f.size);
         successCallback(file);
     };
-    var fail = function(e) {
-        errorCallback(new FileError(e));
+    var fail = typeof errorCallback !== 'function' ? null : function(code) {
+        errorCallback(new FileError(code));
     };
     exec(win, fail, "File", "getFileMetadata", [this.fullPath]);
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/FileReader.js
----------------------------------------------------------------------
diff --git a/lib/plugin/FileReader.js b/lib/plugin/FileReader.js
index 611e28e..c5e5fe3 100644
--- a/lib/plugin/FileReader.js
+++ b/lib/plugin/FileReader.js
@@ -200,7 +200,6 @@ FileReader.prototype.readAsDataURL = function(file) {
         },
         // Error callback
         function(e) {
-            var evt;
             // If DONE (cancelled), then don't do anything
             if (me.readyState === FileReader.DONE) {
                 return;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/FileWriter.js
----------------------------------------------------------------------
diff --git a/lib/plugin/FileWriter.js b/lib/plugin/FileWriter.js
index f727f4a..e0e5626 100644
--- a/lib/plugin/FileWriter.js
+++ b/lib/plugin/FileWriter.js
@@ -94,7 +94,6 @@ FileWriter.prototype.write = function(text) {
     exec(
         // Success callback
         function(r) {
-            var evt;
             // If DONE (cancelled), then don't do anything
             if (me.readyState === FileWriter.DONE) {
                 return;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/blackberry/app.js
----------------------------------------------------------------------
diff --git a/lib/plugin/blackberry/app.js b/lib/plugin/blackberry/app.js
index 6314352..6c58e30 100644
--- a/lib/plugin/blackberry/app.js
+++ b/lib/plugin/blackberry/app.js
@@ -43,7 +43,7 @@ module.exports = {
       }
 
       // allow Cordova JavaScript Extension opportunity to cleanup
-      manager.destroy()
+      manager.destroy();
 
       // exit the app
       blackberry.app.exit();

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/requestFileSystem.js
----------------------------------------------------------------------
diff --git a/lib/plugin/requestFileSystem.js b/lib/plugin/requestFileSystem.js
index 1394d74..afee5d7 100644
--- a/lib/plugin/requestFileSystem.js
+++ b/lib/plugin/requestFileSystem.js
@@ -10,28 +10,31 @@ var FileError = require('cordova/plugin/FileError'),
  * @param errorCallback  invoked if error occurs retrieving file system
  */
 var requestFileSystem = function(type, size, successCallback, errorCallback) {
-  if (type < 0 || type > 3) {
-    if (typeof errorCallback === "function") {
-      errorCallback(new FileError(FileError.SYNTAX_ERR));
-    }
-  } else {
-    // if successful, return a FileSystem object
-    var success = function(file_system) {
-      if (file_system) {
-        // grab the name and root from the file system object
-        var result = new FileSystem(file_system.name, file_system.root);
-        successCallback(result);
-      } 
-      else {
-        // no FileSystem object returned
-        errorCallback(new FileError(FileError.NOT_FOUND_ERR));
-      }
+    var fail = function(code) {
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(code));
+        }
     };
-    var fail = function(e) {
-        errorCallback(new FileError(e));
-    };   
-    exec(success, fail, "File", "requestFileSystem", [type, size]);
-  }
+
+    if (type < 0 || type > 3) {
+        fail(FileError.SYNTAX_ERR);
+    } else {
+        // if successful, return a FileSystem object
+        var success = function(file_system) {
+            if (file_system) {
+                if (typeof successCallback === 'function') {
+                    // grab the name and root from the file system object
+                    var result = new FileSystem(file_system.name, file_system.root);
+                    successCallback(result);
+                }
+            }
+            else {
+                // no FileSystem object returned
+                fail(FileError.NOT_FOUND_ERR);
+            }
+        };
+        exec(success, fail, "File", "requestFileSystem", [type, size]);
+    }
 };
 
 module.exports = requestFileSystem;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a1bf7477/lib/plugin/resolveLocalFileSystemURI.js
----------------------------------------------------------------------
diff --git a/lib/plugin/resolveLocalFileSystemURI.js b/lib/plugin/resolveLocalFileSystemURI.js
index 879a1ff..fbf849b 100644
--- a/lib/plugin/resolveLocalFileSystemURI.js
+++ b/lib/plugin/resolveLocalFileSystemURI.js
@@ -4,27 +4,31 @@ var DirectoryEntry = require('cordova/plugin/DirectoryEntry'),
 
 /**
  * Look up file system Entry referred to by local URI.
- * @param {DOMString} uri  URI referring to a local file or directory 
+ * @param {DOMString} uri  URI referring to a local file or directory
  * @param successCallback  invoked with Entry object corresponding to URI
  * @param errorCallback    invoked if error occurs retrieving file system entry
  */
 module.exports = function(uri, successCallback, errorCallback) {
     // error callback
     var fail = function(error) {
-        errorCallback(new FileError(error));
+        if (typeof errorCallback === 'function') {
+            errorCallback(new FileError(error));
+        }
     };
     // if successful, return either a file or directory entry
     var success = function(entry) {
         var result;
 
         if (entry) {
-            // create appropriate Entry object
-            result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath)
: new FileEntry(entry.name, entry.fullPath);
-            try {
-                successCallback(result);
-            }
-            catch (e) {
-                console.log('Error invoking callback: ' + e);
+            if (typeof successCallback === 'function') {
+                // create appropriate Entry object
+                result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath)
: new FileEntry(entry.name, entry.fullPath);
+                try {
+                    successCallback(result);
+                }
+                catch (e) {
+                    console.log('Error invoking callback: ' + e);
+                }
             }
         }
         else {


Mime
View raw message