Browse Source

Fix for Firefox quirk: streaming in worker now uses FileReaderSync (closes #76)

pull/82/head
Matthew Holt 11 years ago
parent
commit
553f5d7e39
  1. 18
      papaparse.js

18
papaparse.js

@ -547,13 +547,22 @@
var reader, nextChunk, slice; var reader, nextChunk, slice;
var handle = new ParserHandle(copy(config)); var handle = new ParserHandle(copy(config));
// FileReader is better than FileReaderSync (even in worker) - see http://stackoverflow.com/q/24708649/1048862
// But Firefox is a pill, too - see issue #76: https://github.com/mholt/PapaParse/issues/76
var usingAsyncReader = typeof FileReader === 'function';
this.stream = function(file) this.stream = function(file)
{ {
var slice = file.slice || file.webkitSlice || file.mozSlice; var slice = file.slice || file.webkitSlice || file.mozSlice;
reader = new FileReader(); // Better than FileReaderSync (even in worker). See: http://stackoverflow.com/q/24708649/1048862 if (usingAsyncReader)
{
reader = new FileReader(); // Preferred method of reading files, even in workers
reader.onload = chunkLoaded; reader.onload = chunkLoaded;
reader.onerror = chunkError; reader.onerror = chunkError;
}
else
reader = new FileReaderSync(); // Hack for running in a web worker in Firefox
nextChunk(); // Starts streaming nextChunk(); // Starts streaming
@ -567,12 +576,15 @@
{ {
var end = Math.min(start + config.chunkSize, file.size); var end = Math.min(start + config.chunkSize, file.size);
var txt = reader.readAsText(slice.call(file, start, end), config.encoding); var txt = reader.readAsText(slice.call(file, start, end), config.encoding);
start += config.chunkSize; if (!usingAsyncReader)
return txt; chunkLoaded({ target: { result: txt } }); // mimic the async signature
} }
function chunkLoaded(event) function chunkLoaded(event)
{ {
// Very important to increment start each time before handling results
start += config.chunkSize;
// Rejoin the line we likely just split in two by chunking the file // Rejoin the line we likely just split in two by chunking the file
aggregate += partialLine + event.target.result; aggregate += partialLine + event.target.result;
partialLine = ""; partialLine = "";

Loading…
Cancel
Save