Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/traverseFileTree.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
function loopFiles(item, callback) {
const dirReader = item.createReader();
let fileList = [];

function sequence() {
dirReader.readEntries((entries) => {
const entryList = Array.prototype.slice.apply(entries);
fileList = fileList.concat(entryList);

// Check if all the file has been viewed
const isFinished = !entryList.length;

if (isFinished) {
callback(fileList);
} else {
sequence();
}
});
}

sequence();
}

const traverseFileTree = (files, callback, isAccepted) => {
const _traverseFileTree = (item, path) => {
path = path || '';
Expand All @@ -8,12 +31,10 @@ const traverseFileTree = (files, callback, isAccepted) => {
}
});
} else if (item.isDirectory) {
const dirReader = item.createReader();

dirReader.readEntries((entries) => {
for (const entrieItem of entries) {
_traverseFileTree(entrieItem, `${path}${item.name}/`);
}
loopFiles(item, (entries) => {
entries.forEach((entryItem) => {
_traverseFileTree(entryItem, `${path}${item.name}/`);
});
});
}
};
Expand Down
10 changes: 9 additions & 1 deletion tests/uploader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ const makeFileSystemEntry = (item) => {
handle(new Item(item.name));
},
createReader: () => {
let first = true;
return {
readEntries: handle => handle(item.children.map(makeFileSystemEntry)),
readEntries(handle) {
if (!first) {
return [];
}

first = false;
return handle(item.children.map(makeFileSystemEntry));
},
};
},
};
Expand Down