Skip to content

Commit

Permalink
fix(findit) dirs, first could be overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jul 14, 2015
1 parent 934af4e commit 05474a7
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions lib/findit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,54 @@
'use strict';

if (typeof module !== 'undefined' && module.exports)
module.exports = new FindIt();
module.exports = findit;
else
global.findit = new FindIt();
global.findit = findit;

function FindIt() {
var dirs, first;
function findit(entry) {
var emitter = Emitify();

function findit(entry) {
var emitter = Emitify();
setTimeout(function() {
new FindIt(emitter, entry);
}, 0);

return emitter;
}

function FindIt(emitter, entry) {
this._dirs = 0;
this._first = true;

this._find(emitter, enry);
}

FindIt.prototype._find = function(emitter, entry) {
var self = this;

if (entry.isFile) {
emitter.emit('file', entry.fullPath, entry);

setTimeout(function() {
dirs = 0;
first = true;
find(emitter, entry);
}, 0);
if (self._first)
emitter.emit('end');
} else {
if (self._first)
self._first = false;

return emitter;
}

function find(emitter, entry) {
if (entry.isFile) {
emitter.emit('file', entry.fullPath, entry);

if (first)
emitter.emit('end');
} else {
if (first)
first = false;

emitter.emit('directory', entry.fullPath, entry);

++dirs;

entry.createReader()
.readEntries(function(entries) {
[].filter.call(entries, function(entry) {
find(emitter, entry);
});

--dirs;

if (!dirs)
emitter.emit('end');
emitter.emit('directory', entry.fullPath, entry);

++self._dirs;

entry.createReader()
.readEntries(function(entries) {
[].filter.call(entries, function(entry) {
find(emitter, entry);
});
}
}

return findit;

--self._dirs;

if (!self._dirs)
emitter.emit('end');
});
}
}
})(this);

0 comments on commit 05474a7

Please sign in to comment.