I wrote this example of IS.File.directoryIterator
for Firefox.
Can you please help me to review it so if an error occurs it is caught? I'm not so great with promise and defer. But this code works. I'm just worried I may not catch all errors in case one occurs.
//start - helper function
function enumChildEntries(pathToDir, delegate, max_depth, depth, specialCase_runDelegateOnRoot) {
var deferred_enumEntries = Promise.defer();
if (specialCase_runDelegateOnRoot && !depth /* meaning depth is either 0, undefined, or null so then obviously pathToDir is root which is what dev passed into the func to start at */) {
var entry = {isDir:true, name:OS.Path.basename(pathToDir), path:pathToDir};
var rez_delegate = delegate(entry);
} else {
var rez_delegate = false;
}
if (rez_delegate) {
return deferred_enumEntries.resolve(entry);
} else {
if (max_depth >= 0) {
var typeofDepth = typeof depth;
if (typeofDepth != 'number') {
depth = 0;
} else {
depth++;
}
if (depth > max_depth) {
'Reached depth of max_depth, ' + max_depth + '! Will not enumChildEntries on path of directory of: "' + pathToDir + '"'
return deferred_enumEntries.resolve('Reached depth of max_depth, ' + max_depth + '! Will not enumChildEntries on path of directory of: "' + pathToDir + '"');
} else {
console.log('will enum as depth is:', depth, 'and max_depth not yet equalled:', max_depth)
}
}
var iterrator = new OS.File.DirectoryIterator(pathToDir);
var subdirs = [];
var promise_batch = iterrator.nextBatch();
return promise_batch.then(
function(aVal) {
console.info('Fullfilled - promise_batch - ', aVal);
for (var i=0; i<aVal.length; i++) {
if (aVal[i].isDir) {
subdirs.push(aVal[i])
}
rez_delegate_e = delegate(aVal[i]);
if (rez_delegate_e) {
return deferred_enumEntries.resolve(aVal[i]);
}
}
var promiseArr_itrSubdirs = [];
for (var i=0; i<subdirs.length; i++) {
promiseArr_itrSubdirs.push(enumChildEntries(subdirs[i].path, delegate, max_depth, depth));
}
var promiseAll_itrSubdirs = Promise.all(promiseArr_itrSubdirs);
return promiseAll_itrSubdirs.then(
function(aVal) {
console.log('Fullfilled - promiseArr_itrSubdirs - ', aVal);
},
function(aReason) {
console.error('Rejected - promiseArr_itrSubdirs - ', aReason);
return deferred_enumEntires.reject({rejectorOf_promiseName:'promiseArr_itrSubdirs', aReason:aReason, pathToDir:pathToDir});
}
);
},
function(aReason) {
console.error('Rejected - promise_batch - ', aReason);
return deferred_enumEntires.reject({rejectorOf_promiseName:'promise_batch', aReason:aReason});
}
);
}
console.error('i thought it would never get here but it did, wat the hek');
}
// end - helper function
/************ start usage **************/
var totalEntriesEnummed = 0; //meaning total number of entries ran delegate on, includes root dir
function delegate_handleEntry(entry) {
// return true to make enumeration stop
totalEntriesEnummed++;
console.info('entry:', entry);
}
var pathToTarget = OS.Constants.Path.desktopDir;
var promise_enumEntries = enumChildEntries(pathToTarget, delegate_handleEntry, -1 /* get all files, for just 1 level set to 0, 2 levels set 1 etc*/, null, false);
promise_enumEntries.then(
function(aVal) {
// on resolve, aVal is undefined if it went through all possible entries
console.log('Fullfilled - promise_enumEntries - ', aVal);
console.info('totalEntriesEnummed:', totalEntriesEnummed)
},
function(aReason) {
console.error('Rejected - promise_enumEntries - ', aReason);
throw {rejectorOf_promiseName:'promise_enumEntries', aReason:aReason};
}
).catch(
function(aCatch) {
console.error('Caught - promise_enumEntries - ', aCatch);
throw aCatch;
}
);