1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-25 01:20:30 +00:00

Update wiki.readFiles() to call the callback just once

Now we accumulate the results and just pass them to the callback once.
This commit is contained in:
Jermolene 2013-12-23 08:55:11 +00:00
parent 10c25c1692
commit 12b4cc5d3e
2 changed files with 14 additions and 12 deletions

View File

@ -90,8 +90,8 @@ DropZoneWidget.prototype.handleDropEvent = function(event) {
// Try to import the various data types we understand // Try to import the various data types we understand
this.importData(dataTransfer); this.importData(dataTransfer);
// Import any files in the drop // Import any files in the drop
this.wiki.readFiles(dataTransfer.files,function(tiddlerFields) { this.wiki.readFiles(dataTransfer.files,function(tiddlerFieldsArray) {
self.dispatchEvent({type: "tw-import-tiddlers", param: JSON.stringify([tiddlerFields])}); self.dispatchEvent({type: "tw-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray)});
}); });
// Tell the browser that we handled the drop // Tell the browser that we handled the drop
event.preventDefault(); event.preventDefault();

View File

@ -1024,16 +1024,23 @@ exports.getTiddlerText = function(title,defaultText) {
}; };
/* /*
Read an array of browser File objects, invoking callback(tiddlerFields) for each loaded file Read an array of browser File objects, invoking callback(tiddlerFieldsArray) once they're all read
*/ */
exports.readFiles = function(files,callback) { exports.readFiles = function(files,callback) {
var result = [],
outstanding = files.length;
for(var f=0; f<files.length; f++) { for(var f=0; f<files.length; f++) {
this.readFile(files[f],callback); this.readFile(files[f],function(tiddlerFieldsArray) {
result.push.apply(result,tiddlerFieldsArray);
if(--outstanding === 0) {
callback(result);
}
});
}; };
}; };
/* /*
Read a browser File object, invoking callback(tiddlerFields) with the tiddler fields object Read a browser File object, invoking callback(tiddlerFieldsArray) with an array of tiddler fields objects
*/ */
exports.readFile = function(file,callback) { exports.readFile = function(file,callback) {
// Get the type, falling back to the filename extension // Get the type, falling back to the filename extension
@ -1063,15 +1070,10 @@ exports.readFile = function(file,callback) {
var commaPos = event.target.result.indexOf(","); var commaPos = event.target.result.indexOf(",");
if(commaPos !== -1) { if(commaPos !== -1) {
tiddlerFields.text = event.target.result.substr(commaPos+1); tiddlerFields.text = event.target.result.substr(commaPos+1);
callback(tiddlerFields); callback([tiddlerFields]);
} }
} else { } else {
var tiddlers = self.deserializeTiddlers(type,event.target.result,tiddlerFields); callback(self.deserializeTiddlers(type,event.target.result,tiddlerFields));
if(tiddlers) {
$tw.utils.each(tiddlers,function(tiddlerFields) {
callback(tiddlerFields);
});
}
} }
}; };
// Kick off the read // Kick off the read