From bebfe3131e58828978c843a3bfd0c6aec110ffc3 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 9 May 2016 18:30:16 +0300 Subject: [PATCH] some refactoring + made browse-walk draw a dir in sync (fixed unstable ordering)... Signed-off-by: Alex A. Naanou --- ui (gen4)/lib/widget/browse-walk.js | 90 +++++++++++++++++------------ 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/ui (gen4)/lib/widget/browse-walk.js b/ui (gen4)/lib/widget/browse-walk.js index f6fae1ae..4ba3dde8 100755 --- a/ui (gen4)/lib/widget/browse-walk.js +++ b/ui (gen4)/lib/widget/browse-walk.js @@ -91,6 +91,18 @@ function(path, make){ // XXX expose these as config... var fullpath = false + // sync version of stat... + var stat = function(path){ + return new Promise(function(resolve, reject){ + try { + resolve(fs.statSync(path)) + } catch(err){ + reject(err) + } + }) + } + + /* var stat = function(path){ return new Promise(function(resolve, reject){ fs.stat.call(fs, path, function(err, res){ @@ -98,6 +110,7 @@ function(path, make){ }) }) } + */ // get the drive list on windows... if(os.type() == 'Windows_NT' && path == '/'){ @@ -164,48 +177,51 @@ function(path, make){ make(fullpath ? path + '../' : '../')) } - files.map(function(file){ - return stat(path +'/'+ file) - .catch(function(err){ - make(fullpath - ? path +'/'+ file - : file, null, true) - }) - .then(function(res){ - // can't read stat... (XXX ???) - if(res == null){ + // XXX split out the making stage after the stat stage to + // be able to sort suff correctly... + files + .map(function(file){ + return stat(path +'/'+ file) + .catch(function(err){ make(fullpath ? path +'/'+ file : file, null, true) - return - } + }) + .then(function(res){ + // can't read stat... (XXX ???) + if(res == null){ + make(fullpath + ? path +'/'+ file + : file, null, true) + return + } - var dir = res.isDirectory() - var elem = res && make(fullpath - ? path +'/'+ file - : file + (dir ? '/' : ''), - null, - that.options.disableFiles && !dir) + var dir = res.isDirectory() + var elem = res && make(fullpath + ? path +'/'+ file + : file + (dir ? '/' : ''), + null, + that.options.disableFiles && !dir) - // count the number of files... - // NOTE: we do not care how long it will take - // so we'll not wait... - res && dir && _countDirFiles(path, file, elem) - }) - // NOTE: we are not using promise.all(..) here because it - // triggers BEFORE the first make(..) is called... - // ...not sure I fully understand why... - .then(function(){ - // NOTE: this will get called for all results - // including ones that generate errors, not - // sure if this is a bug in .denodeify(..) - // or by-design though... - res.push(file) - if(res.length == files.length){ - resolve() - } - }) - }) + // count the number of files... + // NOTE: we do not care how long it will take + // so we'll not wait... + res && dir && _countDirFiles(path, file, elem) + }) + // NOTE: we are not using promise.all(..) here because it + // triggers BEFORE the first make(..) is called... + // ...not sure I fully understand why... + .then(function(){ + // NOTE: this will get called for all results + // including ones that generate errors, not + // sure if this is a bug in .denodeify(..) + // or by-design though... + res.push(file) + if(res.length == files.length){ + resolve() + } + }) + }) }) }) }