`getContents` poorly supports directories with many files/subdirectories
I mostly love the current Directory Upload proposal, but I'm afraid that it may have poor support for directories that contain a vast number of files and child directories. A Directory's getContents method is defined as follows:
partial interface Directory {
Promise<sequence<(File or Directory)>> getContents ();
};
The behavior of getContents:
If the
Directoryhas files or sub-directories, aPromiseis returned that resolves with asequenceofFileand/orDirectorywhich represent a snapshot of theDirectory's contents. If theDirectoryis empty, aPromiseis returned that resolves with an emptysequence.
If a directory has several thousands (or hundreds of thousands) of items, or if it resides on a network file system, the Promise could take a very long time to resolve, and the resulting sequence could have a massive memory footprint. While the directory entries are being enumerated, no useful work (e.g., uploading files encountered so far) can be performed.
The old Chrome DirectoryReader API defined a readEntries method as follows:
interface DirectoryReader {
void readEntries (EntriesCallback successCallback, optional ErrorCallback errorCallback);
};
The behavior of the successCallback:
Called once per successful call to readEntries to deliver the next previously-unreported set of Entries in the associated Directory. If all Entries have already been returned from previous invocations of readEntries, successCallback MUST be called with a zero-length array as an argument.
In the old Chrome model, successive subsets of the children can be received incrementally, presenting more of a streaming view of the contents. This allows uploading to happen concurrently with directory scanning, and it helps to decrease memory pressure.
Is there any possibility of tweaking getContents or adding an incremental method? One possible example
partial interface Directory {
Promise<sequence<(File or Directory)>> getContents ();
DirectoryContents getIncrementalContents ();
};
partial interface DirectoryContents {
Promise<sequence<(File or Directory)>> read ();
}
Each read Promise would resolve with either a sequence containing a set of previously-unreported File/Directory entries, or with an empty sequence to indicate that there are no more entries.
To illustrate, getContents could be implemented in terms of getIncrementalContents as follows:
Directory.prototype.getContents = function() {
var incrementalContents = this.getIncrementalContents();
return new Promise(function(resolve, reject) {
var contents = [];
function readMore() {
incrementalContents.read().then(function(entries) {
if (entries.length) {
contents.push.apply(contents, entries);
readMore();
} else {
resolve(contents);
}
}, reject);
}
readMore();
});
};
I appreciate your feedback and enthusiasm around this spec!
I agree that having some way of incrementally reading directories would be ideal. In fact, the FileSystem API spec defines two methods in the Directory interface (enumerate and enumerateDeep) that return an Observable. This would enable incrementally reading directories, however, as you know, Observables are not yet available and it may take some time before they are. As part of our initial thoughts on the directory upload spec proposal, we were trying to build a solution that used today's primitives (such as Promises) and then, in the future, we would retrofit the API with Observables to provide the functionality that you described. If we provided this functionality now via getIncrementalContents and then re-introduce it in the future through Observables, we would give little incentive for developers to move to the newer APIs when they become available.
Additionally, due to a recent change in the spec where we no longer have rootDirectory returning a virtual Directory object holding all the Files and Directorys chosen, we would need to expose the same sort of incrementally reading APIs in HTMLInputElement and DataTransfer. This could be less desirable than returning a Promise for now and then in the future adding a method that returns an Observable.
@mintern 's comment above is really good. But any non-Stream based or non-language-primitive based solution is yet another bandaid for the platform. Even getFilesAndDirectories() (note: it's no longer called getContents()) is temporary till we have better platform stuff.
AsyncIterators (language, hopefully) and Streams (API) would help more. In the meantime, I think it's probably ok to not include this in the spec. I'm closing this.
I don't think you need anything from the language. You could just have a simple event emitter with events "files", "error", "end" where files will be emitted e.g. per every 10 files. Then if those other APIs ever come, it is trivial to convert event emitter to stream or observable anyway.