@ -7,7 +7,7 @@
{
{
"use strict" ;
"use strict" ;
var IS _WORKER = ! global . document , SCRIPT _PATH ;
var IS _WORKER = ! global . document , IS _SYNC = false , AUTO _ SCRIPT_PATH ;
var workers = { } , workerIdCounter = 0 ;
var workers = { } , workerIdCounter = 0 ;
// A configuration object from which to draw default settings
// A configuration object from which to draw default settings
@ -39,6 +39,8 @@
global . Papa . BYTE _ORDER _MARK = "\ufeff" ;
global . Papa . BYTE _ORDER _MARK = "\ufeff" ;
global . Papa . BAD _DELIMITERS = [ "\r" , "\n" , "\"" , global . Papa . BYTE _ORDER _MARK ] ;
global . Papa . BAD _DELIMITERS = [ "\r" , "\n" , "\"" , global . Papa . BYTE _ORDER _MARK ] ;
global . Papa . WORKERS _SUPPORTED = ! ! global . Worker ;
global . Papa . WORKERS _SUPPORTED = ! ! global . Worker ;
// Must be set externally if using workers and Papa Parse is loaded asynchronously
global . Papa . SCRIPT _PATH = null ;
// Configurable chunk sizes for local and remote files, respectively
// Configurable chunk sizes for local and remote files, respectively
global . Papa . LocalChunkSize = 1024 * 1024 * 10 ; // 10 MB
global . Papa . LocalChunkSize = 1024 * 1024 * 10 ; // 10 MB
@ -147,9 +149,17 @@
if ( IS _WORKER )
if ( IS _WORKER )
{
global . onmessage = workerThreadReceivedMessage ;
global . onmessage = workerThreadReceivedMessage ;
}
else if ( Papa . WORKERS _SUPPORTED )
else if ( Papa . WORKERS _SUPPORTED )
SCRIPT _PATH = getScriptPath ( ) ;
{
AUTO _SCRIPT _PATH = getScriptPath ( ) ;
// Check if the script was loaded synchronously
document . body . addEventListener ( 'load' , function ( ) {
IS _SYNC = true ;
} , true ) ;
}
@ -157,7 +167,7 @@
function CsvToJson ( _input , _config )
function CsvToJson ( _input , _config )
{
{
var config = IS _WORKER ? _config : copyAndValidateConfig ( _config ) ;
var config = IS _WORKER ? _config : copyAndValidateConfig ( _config ) ;
var useWorker = config . worker && Papa . WORKERS _SUPPORTED && SCRIPT _PATH ;
var useWorker = config . worker && Papa . WORKERS _SUPPORTED && ( Papa . SCRIPT _PATH || AUTO _SCRIPT _PATH ) ;
if ( useWorker )
if ( useWorker )
{
{
@ -1349,16 +1359,20 @@
// the script path here. See: https://github.com/mholt/PapaParse/issues/87#issuecomment-57885358
// the script path here. See: https://github.com/mholt/PapaParse/issues/87#issuecomment-57885358
function getScriptPath ( )
function getScriptPath ( )
{
{
var id = "worker" + String ( Math . random ( ) ) . substr ( 2 ) ;
var scripts = document . getElementsByTagName ( 'script' ) ;
document . write ( '<script id="' + id + '"></script>' ) ;
return scripts . length ? scripts [ scripts . length - 1 ] . src : '' ;
return document . getElementById ( id ) . previousSibling . src ;
}
}
function newWorker ( )
function newWorker ( )
{
{
if ( ! Papa . WORKERS _SUPPORTED )
if ( ! Papa . WORKERS _SUPPORTED )
return false ;
return false ;
var w = new global . Worker ( SCRIPT _PATH ) ;
if ( ! IS _SYNC && Papa . SCRIPT _PATH === null )
throw new Error (
'Script path cannot be determined automatically when Papa Parse is loaded asynchronously. ' +
'You need to set Papa.SCRIPT_PATH manually.'
) ;
var w = new global . Worker ( Papa . SCRIPT _PATH || AUTO _SCRIPT _PATH ) ;
w . onmessage = mainThreadReceivedMessage ;
w . onmessage = mainThreadReceivedMessage ;
w . id = workerIdCounter ++ ;
w . id = workerIdCounter ++ ;
workers [ w . id ] = w ;
workers [ w . id ] = w ;