cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [8/9] js commit: Promise to avoid deep nesting
Date Wed, 17 Oct 2012 20:38:34 GMT
Promise to avoid deep nesting

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/aa8d7ee9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/aa8d7ee9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/aa8d7ee9

Branch: refs/heads/master
Commit: aa8d7ee98fd5183c5eb734c0be237388542de833
Parents: 7beb8c2
Author: wangmingfeng <mingfengwang01@gmail.com>
Authored: Thu Oct 11 16:27:54 2012 +0800
Committer: wangmingfeng <mingfengwang01@gmail.com>
Committed: Thu Oct 11 16:27:54 2012 +0800

----------------------------------------------------------------------
 lib/windows8/plugin/windows8/FileProxy.js |  368 +++++++++++++-----------
 1 files changed, 204 insertions(+), 164 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/aa8d7ee9/lib/windows8/plugin/windows8/FileProxy.js
----------------------------------------------------------------------
diff --git a/lib/windows8/plugin/windows8/FileProxy.js b/lib/windows8/plugin/windows8/FileProxy.js
index 606d53d..d860d6f 100644
--- a/lib/windows8/plugin/windows8/FileProxy.js
+++ b/lib/windows8/plugin/windows8/FileProxy.js
@@ -30,42 +30,54 @@ module.exports = {
     getMetadata:function(success,fail,args) {
         var fullPath = args[0];
 
+        var dealFile = function (sFile) {
+            Windows.Storage.StorageFile.getFileFromPathAsync(fullPath).then(
+                function (storageFile) {
+                    return storageFile.getBasicPropertiesAsync();
+                },
+                function () {
+                    fail(FileError.NOT_READABLE_ERR);
+                }
+            // get the basic properties of the file.
+            ).then(
+                function (basicProperties) {
+                    success(basicProperties.dateModified);
+                },
+                function () {
+                    fail(FileError.NOT_READABLE_ERR);
+                }
+            );
+        };
+
+        var dealFolder = function (sFolder) {
+            Windows.Storage.StorageFolder.getFolderFromPathAsync(fullPath).then(
+                function (storageFolder) {
+                    return storageFolder.getBasicPropertiesAsync();
+                },
+                function () {
+                    fail(FileError.NOT_READABLE_ERR);
+                }
+            // get the basic properties of the folder.
+            ).then(
+                function (basicProperties) {
+                    success(basicProperties.dateModified);
+                },
+                function () {
+                    fail(FileError.NOT_FOUND_ERR);
+                }
+            );
+        };
+
         Windows.Storage.StorageFile.getFileFromPathAsync(fullPath).then(
+            // the path is file.
             function (sFile) {
-                Windows.Storage.StorageFile.getFileFromPathAsync(fullPath).done(
-                    function (storageFile) {
-                        storageFile.getBasicPropertiesAsync().then(
-                            function (basicProperties) {
-                                success(basicProperties.dateModified);
-                            },
-                            function () {
-                                fail(FileError.NOT_READABLE_ERR);
-                            }
-                        );
-                    },
-                    function () {
-                        fail(FileError.NOT_READABLE_ERR);
-                    }
-                );
+                dealFile(sFile);
             },
+            // the path is folder
             function () {
                 Windows.Storage.StorageFolder.getFolderFromPathAsync(fullPath).then(
                     function (sFolder) {
-                        Windows.Storage.StorageFolder.getFolderFromPathAsync(fullPath).done(
-                            function (storageFolder) {
-                                storageFolder.getBasicPropertiesAsync().then(
-                                    function (basicProperties) {
-                                        success(basicProperties.dateModified);
-                                    },
-                                    function () {
-                                        fail(FileError.NOT_FOUND_ERR);
-                                    }
-                                );
-                            },
-                            function () {
-                                fail(FileError.NOT_READABLE_ERR);
-                            }
-                        );
+                        dealFolder(sFolder);
                     }, function () {
                         fail(FileError.NOT_FOUND_ERR);
                     }
@@ -216,41 +228,46 @@ module.exports = {
                 Windows.Storage.StorageFolder.getFolderFromPathAsync(fullPath).then(
                     function (sFolder) {
                         var removeEntry = function () {
-                            var storageFolder = null;
-
-                            Windows.Storage.StorageFolder.getFolderFromPathAsync(fullPath).then(function
(storageFolder) {
-                                //FileSystem root can't be removed!
-                                var storageFolderPer = Windows.Storage.ApplicationData.current.localFolder;
-                                var storageFolderTem = Windows.Storage.ApplicationData.current.temporaryFolder;
-                                if (fullPath == storageFolderPer.path || fullPath == storageFolderTem.path)
{
-                                    fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
-                                    return;
-                                }
-                                storageFolder.createFileQuery().getFilesAsync().then(function
(fileList) {
+                            var storageFolderTop = null;
+
+                            Windows.Storage.StorageFolder.getFolderFromPathAsync(fullPath).then(
+                                function (storageFolder) {
+                                    // FileSystem root can't be removed!
+                                    var storageFolderPer = Windows.Storage.ApplicationData.current.localFolder;
+                                    var storageFolderTem = Windows.Storage.ApplicationData.current.temporaryFolder;
+                                    if (fullPath == storageFolderPer.path || fullPath ==
storageFolderTem.path) {
+                                        fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
+                                        return;
+                                    }
+                                    storageFolderTop = storageFolder;
+                                    return storageFolder.createFileQuery().getFilesAsync();
+                                }, function () {
+                                    fail(FileError.INVALID_MODIFICATION_ERR);
 
+                                }
+                            // check sub-files.
+                            ).then(function (fileList) {
+                                if (fileList) {
                                     if (fileList.length === 0) {
-                                        storageFolder.createFolderQuery().getFoldersAsync().then(function
(folderList) {
-
-                                            if (folderList.length === 0) {
-                                                storageFolder.deleteAsync().done(win, function
() {
-                                                    fail(FileError.INVALID_MODIFICATION_ERR);
+                                        return storageFolderTop.createFolderQuery().getFoldersAsync();
+                                    } else {
+                                        fail(FileError.INVALID_MODIFICATION_ERR);
+                                    }
+                                }
+                            // check sub-folders.  
+                            }).then(function (folderList) {
+                                if (folderList) {
+                                    if (folderList.length === 0) {
+                                        storageFolderTop.deleteAsync().done(win, function
() {
+                                            fail(FileError.INVALID_MODIFICATION_ERR);
 
-                                                });
-                                            } else {
-                                                fail(FileError.INVALID_MODIFICATION_ERR);
-                                            }
                                         });
                                     } else {
                                         fail(FileError.INVALID_MODIFICATION_ERR);
                                     }
-                                });
-
-                            }, function () {
-                                fail(FileError.INVALID_MODIFICATION_ERR);
-
+                                }
+                                
                             });
-
-
                         };
                         removeEntry();
                     }, function () {
@@ -259,7 +276,6 @@ module.exports = {
                 );
             }
         );
-
     },
 
     removeRecursively:function(successCallback,fail,args) {
@@ -276,36 +292,42 @@ module.exports = {
 
         var removeFolders = function (path) {
             return new WinJS.Promise(function (complete) {
-                Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(function
(storageFolder) {
-                    var fileListPromise = storageFolder.createFileQuery().getFilesAsync();
-                    var filePromiseArr = [];
-
-                    fileListPromise.then(function (fileList) {
-                        if (fileList !== null) {
-                            for (var i = 0; i < fileList.length; i++) {
-                                var filePromise = fileList[i].deleteAsync();
-                                filePromiseArr.push(filePromise);
+                var filePromiseArr = [];
+                var storageFolderTop = null;
+                Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(
+                    function (storageFolder) {
+                        var fileListPromise = storageFolder.createFileQuery().getFilesAsync();
+                        
+                        storageFolderTop = storageFolder;
+                        return fileListPromise;
+                    }
+                // remove all the files directly under the folder. 
+                ).then(function (fileList) {
+                    if (fileList !== null) {
+                        for (var i = 0; i < fileList.length; i++) {
+                            var filePromise = fileList[i].deleteAsync();
+                            filePromiseArr.push(filePromise);
+                        }
+                    }
+                    WinJS.Promise.join(filePromiseArr).then(function () {
+                        var folderListPromise = storageFolderTop.createFolderQuery().getFoldersAsync();
+                        return folderListPromise;
+                    // remove empty folders.
+                    }).then(function (folderList) {
+                        var folderPromiseArr = [];
+                        if (folderList.length !== 0) {
+                            for (var j = 0; j < folderList.length; j++) {
+
+                                folderPromiseArr.push(removeFolders(folderList[j].path));
                             }
+                            WinJS.Promise.join(folderPromiseArr).then(function () {
+                                storageFolderTop.deleteAsync().then(complete);
+                            });
+                        } else {
+                            storageFolderTop.deleteAsync().then(complete);
                         }
-                        WinJS.Promise.join(filePromiseArr).then(function () {
-                            var folderListPromise = storageFolder.createFolderQuery().getFoldersAsync();
-                            folderListPromise.then(function (folderList) {
-                                var folderPromiseArr = [];
-                                if (folderList.length !== 0) {
-                                    for (var j = 0; j < folderList.length; j++) {
-
-                                        folderPromiseArr.push(removeFolders(folderList[j].path));
-                                    }
-                                    WinJS.Promise.join(folderPromiseArr).then(function ()
{
-                                        storageFolder.deleteAsync().then(complete);
-                                    });
-                                } else {
-                                    storageFolder.deleteAsync().then(complete);
-                                }
-                            }, function () { });
-                       });
                     }, function () { });
-                });
+                }, function () { });;
             });
         };
         removeFolders(storageFolder.path).then(function () {
@@ -442,13 +464,13 @@ module.exports = {
                         var successCallBack = function (entry) {
                             parentPath = entry.fullPath;
                             storageFile.deleteAsync().then(function () {
-                                Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolder) {
-                                    storageFolder.createFileAsync(name).then(function (newStorageFile)
{
-                                        Windows.Storage.FileIO.writeTextAsync(newStorageFile,
fileContent).done(function () {
-                                            win(String(fileContent).length);
-                                        }, function () {
-                                            fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
-                                        });
+                                return Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath);
+                            }).then(function (storageFolder) {
+                                storageFolder.createFileAsync(name).then(function (newStorageFile)
{
+                                    Windows.Storage.FileIO.writeTextAsync(newStorageFile,
fileContent).done(function () {
+                                        win(String(fileContent).length);
+                                    }, function () {
+                                        fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
                                     });
                                 });
                             });
@@ -475,18 +497,20 @@ module.exports = {
         Windows.Storage.StorageFile.getFileFromPathAsync(srcPath).then(
             function (sFile) {
                 copyFiles = function (srcPath, parentPath) {
+                    var storageFileTop = null;
                     Windows.Storage.StorageFile.getFileFromPathAsync(srcPath).then(function
(storageFile) {
-                        Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolder) {
-                            storageFile.copyAsync(storageFolder, name, Windows.Storage.NameCollisionOption.failIfExists).then(function
(storageFile) {
+                        storageFileTop = storageFile;
+                        return Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath);
+                    }, function () {
 
-                                success(new FileEntry(storageFile.name, storageFile.path));
-                            }, function () {
+                        fail(FileError.NOT_FOUND_ERR);
+                    }).then(function (storageFolder) {
+                        storageFileTop.copyAsync(storageFolder, name, Windows.Storage.NameCollisionOption.failIfExists).then(function
(storageFile) {
 
-                                fail(FileError.INVALID_MODIFICATION_ERR);
-                            });
+                            success(new FileEntry(storageFile.name, storageFile.path));
                         }, function () {
 
-                            fail(FileError.NOT_FOUND_ERR);
+                            fail(FileError.INVALID_MODIFICATION_ERR);
                         });
                     }, function () {
 
@@ -503,37 +527,45 @@ module.exports = {
                     function (sFolder) {
                         copyFiles = function (srcPath, parentPath) {
                             return new WinJS.Promise(function (complete) {
+                                var storageFolderTop = null;
+                                var filePromiseArr = [];
+                                var fileListTop = null;
                                 Windows.Storage.StorageFolder.getFolderFromPathAsync(srcPath).then(function
(storageFolder) {
-                                    storageFolder.createFileQuery().getFilesAsync().then(function
(fileList) {
-                                        var filePromiseArr = [];
-                                        if (fileList) {
-                                            Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(targetStorageFolder) {
-                                                for (var i = 0; i < fileList.length; i++)
{
-                                                    filePromiseArr.push(fileList[i].copyAsync(targetStorageFolder));
-                                                }
-                                                WinJS.Promise.join(filePromiseArr).then(function
() {
-                                                    storageFolder.createFolderQuery().getFoldersAsync().then(function
(folderList) {
-                                                        var folderPromiseArr = [];
-                                                        if (folderList.length === 0) { complete();
}
-                                                        else {
-
-                                                            Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolderTarget) {
-                                                                var tempPromiseArr = [];
-                                                                var index = 0;
-                                                                for (var j = 0; j < folderList.length;
j++) {
-                                                                    tempPromiseArr[index++]
= storageFolderTarget.createFolderAsync(folderList[j].name).then(function (targetFolder) {
-                                                                        folderPromiseArr.push(copyFiles(folderList[j].path,
targetFolder.path));
-                                                                    });
-                                                                }
-                                                                WinJS.Promise.join(tempPromiseArr).then(function
() {
-                                                                    WinJS.Promise.join(folderPromiseArr).then(complete);
-                                                                });
-                                                            });
-                                                        }
+                                    storageFolderTop = storageFolder;
+                                    return storageFolder.createFileQuery().getFilesAsync();
+                                }).then(function (fileList) {
+                                    fileListTop = fileList;
+                                    if (fileList) {
+                                        return Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath);
+                                    }
+                                }).then(function (targetStorageFolder) {
+                                    for (var i = 0; i < fileListTop.length; i++) {
+                                        filePromiseArr.push(fileListTop[i].copyAsync(targetStorageFolder));
+                                    }
+
+                                    var coreCopy = function () {
+                                        storageFolderTop.createFolderQuery().getFoldersAsync().then(function
(folderList) {
+                                            var folderPromiseArr = [];
+                                            if (folderList.length === 0) { complete(); }
+                                            else {
+                                                Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolderTarget) {
+                                                    var tempPromiseArr = [];
+                                                    var index = 0;
+                                                    for (var j = 0; j < folderList.length;
j++) {
+                                                        tempPromiseArr[index++] = storageFolderTarget.createFolderAsync(folderList[j].name).then(function
(targetFolder) {
+                                                            folderPromiseArr.push(copyFiles(folderList[j].path,
targetFolder.path));
+                                                        });
+                                                    }
+                                                    WinJS.Promise.join(tempPromiseArr).then(function
() {
+                                                        WinJS.Promise.join(folderPromiseArr).then(complete);
                                                     });
                                                 });
-                                            });
-                                        }
+                                            }
+                                        });
+                                    }
+
+                                    WinJS.Promise.join(filePromiseArr).then(function () {
+                                        coreCopy();
                                     });
                                 });
                             });
@@ -587,15 +619,17 @@ module.exports = {
         Windows.Storage.StorageFile.getFileFromPathAsync(srcPath).then(
             function (sFile) {
                 moveFiles = function (srcPath, parentPath) {
+                    var storageFileTop = null;
                     Windows.Storage.StorageFile.getFileFromPathAsync(srcPath).then(function
(storageFile) {
-                        Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolder) {
-                            storageFile.moveAsync(storageFolder, name, Windows.Storage.NameCollisionOption.replaceExisting).then(function
() {
-                                success(new FileEntry(name, storageFile.path));
-                            }, function () {
-                                fail(FileError.INVALID_MODIFICATION_ERR);
-                            });
+                        storageFileTop = storageFile;
+                        return Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath);
+                    }, function () {
+                        fail(FileError.NOT_FOUND_ERR);
+                    }).then(function (storageFolder) {
+                        storageFileTop.moveAsync(storageFolder, name, Windows.Storage.NameCollisionOption.replaceExisting).then(function
() {
+                            success(new FileEntry(name, storageFileTop.path));
                         }, function () {
-                            fail(FileError.NOT_FOUND_ERR);
+                            fail(FileError.INVALID_MODIFICATION_ERR);
                         });
                     }, function () {
                         fail(FileError.NOT_FOUND_ERR);
@@ -616,46 +650,52 @@ module.exports = {
                     function (sFolder) {
                         moveFiles = function (srcPath, parentPath) {
                             return new WinJS.Promise(function (complete) {
+                                var storageFolderTop = null;
                                 Windows.Storage.StorageFolder.getFolderFromPathAsync(srcPath).then(function
(storageFolder) {
-                                    storageFolder.createFileQuery().getFilesAsync().then(function
(fileList) {
-                                        var filePromiseArr = [];
-                                        Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(dstStorageFolder) {
-                                            if (fileList) {
-                                                for (var i = 0; i < fileList.length; i++)
{
-                                                    filePromiseArr.push(fileList[i].moveAsync(dstStorageFolder));
-                                                }
+                                    storageFolderTop = storageFolder;
+                                    return storageFolder.createFileQuery().getFilesAsync();
+                                }).then(function (fileList) {
+                                    var filePromiseArr = [];
+                                    Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(dstStorageFolder) {
+                                        if (fileList) {
+                                            for (var i = 0; i < fileList.length; i++)
{
+                                                filePromiseArr.push(fileList[i].moveAsync(dstStorageFolder));
                                             }
-                                            WinJS.Promise.join(filePromiseArr).then(function
() {
-                                                storageFolder.createFolderQuery().getFoldersAsync().then(function
(folderList) {
-                                                    var folderPromiseArr = [];
-                                                    if (folderList.length === 0) {
-                                                        // If failed, we must cancel the
deletion of folders & files.So here wo can't delete the folder.
-                                                        complete();
-                                                    }
-                                                    else {
-                                                        Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolderTarget) {
-                                                            var tempPromiseArr = [];
-                                                            var index = 0;
-                                                            for (var j = 0; j < folderList.length;
j++) {
-                                                                tempPromiseArr[index++] =
storageFolderTarget.createFolderAsync(folderList[j].name).then(function (targetFolder) {
-                                                                    folderPromiseArr.push(moveFiles(folderList[j].path,
targetFolder.path));
-                                                                });
-                                                            }
-                                                            WinJS.Promise.join(tempPromiseArr).then(function
() {
-                                                                WinJS.Promise.join(folderPromiseArr).then(complete);
+                                        }
+                                        var coreMove = function () {
+                                            storageFolderTop.createFolderQuery().getFoldersAsync().then(function
(folderList) {
+                                                var folderPromiseArr = [];
+                                                if (folderList.length === 0) {
+                                                    // If failed, we must cancel the deletion
of folders & files.So here wo can't delete the folder.
+                                                    complete();
+                                                }
+                                                else {
+                                                    Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolderTarget) {
+                                                        var tempPromiseArr = [];
+                                                        var index = 0;
+                                                        for (var j = 0; j < folderList.length;
j++) {
+                                                            tempPromiseArr[index++] = storageFolderTarget.createFolderAsync(folderList[j].name).then(function
(targetFolder) {
+                                                                folderPromiseArr.push(moveFiles(folderList[j].path,
targetFolder.path));
                                                             });
+                                                        }
+                                                        WinJS.Promise.join(tempPromiseArr).then(function
() {
+                                                            WinJS.Promise.join(folderPromiseArr).then(complete);
                                                         });
-                                                    }
-                                                });
-                                            }, function () { });
-                                        });
+                                                    });
+                                                }
+                                            });
+                                        }
+
+                                        WinJS.Promise.join(filePromiseArr).then(function
() {
+                                            coreMove();
+                                        }, function () { });
                                     });
                                 });
                             });
                         };
                         var moveFinish = function (srcPath, parentPath) {
                             Windows.Storage.StorageFolder.getFolderFromPathAsync(srcPath).then(function
(originFolder) {
-                                Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolder) {
+                                return Windows.Storage.StorageFolder.getFolderFromPathAsync(parentPath).then(function
(storageFolder) {
                                     storageFolder.createFolderAsync(name, Windows.Storage.CreationCollisionOption.openIfExists).then(function
(newStorageFolder) {
                                         //can't move onto directory that is not empty
                                         newStorageFolder.createFileQuery().getFilesAsync().then(function
(fileList) {


Mime
View raw message