fixed callback error

This commit is contained in:
AJ ONeal 2011-02-03 23:57:11 -07:00
parent 4df55b7ced
commit c1d25bf637
2 changed files with 148 additions and 15 deletions

View File

@ -4,6 +4,8 @@
// Array.prototype.forEachAsync(next, item, i, collection) // Array.prototype.forEachAsync(next, item, i, collection)
require('futures/forEachAsync'); require('futures/forEachAsync');
function noop() {}
var fs = require('fs'), var fs = require('fs'),
EventEmitter = require('events').EventEmitter, EventEmitter = require('events').EventEmitter,
TypeEmitter = require('./node-type-emitter'); TypeEmitter = require('./node-type-emitter');
@ -18,14 +20,15 @@
function walk() { function walk() {
fs.readdir(curpath, function(err, files) { fs.readdir(curpath, function(err, files) {
if (err) { if (err) {
emitter.emit('error', curpath, err); emitter.emit('directoryError', curpath, { error: err }, noop);
//emitter.emit('error', curpath, { error: err });
} }
// XXX bug was here. next() was omitted // XXX bug was here. next() was omitted
if (!files || 0 == files.length) { if (!files || 0 == files.length) {
return next(); return next();
} }
var stats = [],
fnodeGroups = TypeEmitter.createNodeGroups(); var fnodeGroups = TypeEmitter.createNodeGroups();
// TODO could allow user to selectively stat // TODO could allow user to selectively stat
// and don't stat if there are no stat listeners // and don't stat if there are no stat listeners
@ -33,22 +36,25 @@
files.forEachAsync(function (cont, file) { files.forEachAsync(function (cont, file) {
emitter.emit('name', curpath, file); emitter.emit('name', curpath, file);
fs.lstat(curpath + '/' + file, function (err, stat) { fs.lstat(curpath + '/' + file, function (err, stat) {
if (err) { stat = stat || {};
emitter.emit('error', curpath, err);
}
if (!stat) {
cont();
}
stat.name = file; stat.name = file;
stats.push(stat); if (err) {
//emitter.emit('stat', curpath, file, stat); stat.error = err;
//emitter.emit('error', curpath, stat);
emitter.emit('nodeError', curpath, stat, noop);
fnodeGroups.errors.push(stat);
cont();
} else {
TypeEmitter.sortFnodesByType(stat, fnodeGroups); TypeEmitter.sortFnodesByType(stat, fnodeGroups);
TypeEmitter.emitNodeType(emitter, curpath, stat, cont); TypeEmitter.emitNodeType(emitter, curpath, stat, cont);
}
}); });
}).then(function () { }).then(function () {
var dirs = [] if (fnodeGroups.errors.length) {
//emitter.emit('stats', curpath, files, stats); emitter.emit('errors', curpath, fnodeGroups.errors);
}
TypeEmitter.emitNodeTypeGroups(emitter, curpath, fnodeGroups, function () { TypeEmitter.emitNodeTypeGroups(emitter, curpath, fnodeGroups, function () {
var dirs = [];
fnodeGroups.directories.forEach(function (stat) { fnodeGroups.directories.forEach(function (stat) {
dirs.push(stat.name); dirs.push(stat.name);
}); });

127
profile/walk-test.js Executable file
View File

@ -0,0 +1,127 @@
#!/usr/bin/env node
(function () {
"use strict";
var walk = require('../lib/walk'),
count = 0,
saneCount = 0;
function sort(a,b) {
a= a.toLowerCase();
b= b.toLowerCase();
if (a > b) return -1;
if (a < b) return 1;
else return 0;
}
process.argv.forEach(function(startpath, index) {
if (index > 1) {
emitter = walk(startpath);
// Non-`stat`ed Nodes
emitter.on('name', function (path, file, stat) {
saneCount += 1;
//console.log( ["[", count, "] ", path, '/', file].join('') )
console.log( [path, '/', file].join('') )
});
/*
emitter.on('names', function (path, files, stats) {
files.sort(sort);
//console.log('sort: ' + files.join(' ; '));
});
*/
// Single `stat`ed Nodes
emitter.on('error', function (path, err, next) {
// ignore
});
emitter.on('directoryError', function (path, stats, next) {
next();
});
emitter.on('nodeError', function (path, stats, next) {
next();
});
/*
emitter.on('node', function (path, stat, next) {
count += 1;
console.log( [path, '/', stat.name].join('') )
//console.log( ["[", count, "] ", path, '/', stat.name].join('') )
next();
});
*/
emitter.on('file', function (path, stat, next) {
count += 1;
console.log( [path, '/', stat.name].join('') )
//console.log( ["[", count, "] ", path, '/', stat.name].join('') )
next();
});
emitter.on('directory', function (path, stat, next) {
count += 1;
console.log( [path, '/', stat.name].join('') )
next();
});
emitter.on('symbolicLink', function (path, stat, next) {
count += 1;
console.log( [path, '/', stat.name].join('') )
next();
});
/*
*/
/*
emitter.on('blockDevice', function (path, stat, next) {
next();
});
emitter.on('characterDevice', function (path, stat, next) {
next();
});
emitter.on('FIFO', function (path, stat, next) {
next();
});
emitter.on('socket', function (path, stat, next) {
next();
});
*/
// Grouped `stat`ed Nodes
emitter.on('errors', function (path, stats, next) {
next();
});
/*
emitter.on('nodes', function (path, stats, next) {
next();
});
*/
emitter.on('files', function (path, stats, next) {
next();
});
emitter.on('directories', function (path, stats, next) {
//delete stats[1];
next();
});
emitter.on('symbolicLinks', function (path, stats, next) {
next();
});
/*
emitter.on('blockDevices', function (path, stats, next) {
next();
});
emitter.on('characterDevices', function (path, stats, next) {
next();
});
emitter.on('FIFOs', function (path, stats, next) {
next();
});
emitter.on('sockets', function (path, stats, next) {
next();
});
*/
// The end of all things
emitter.on('end', function () {
console.log("The eagle has landed. [" + count + " == " + saneCount + "]");
});
}
});
}());