Browse Source

Using PapaParse inside a worker

Fixes #188
pull/211/head
Paul Harper 10 years ago
parent
commit
871a20db2e
  1. 17
      papaparse.js
  2. 31
      tests/test-cases.js

17
papaparse.js

@ -7,7 +7,9 @@
{ {
"use strict"; "use strict";
var IS_WORKER = (!global.document && !!global.postMessage), LOADED_SYNC = false, AUTO_SCRIPT_PATH; var IS_WORKER = !global.document && !!global.postMessage,
IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search),
LOADED_SYNC = false, AUTO_SCRIPT_PATH;
var workers = {}, workerIdCounter = 0; var workers = {}, workerIdCounter = 0;
var Papa = {}; var Papa = {};
@ -19,7 +21,7 @@
Papa.UNIT_SEP = String.fromCharCode(31); Papa.UNIT_SEP = String.fromCharCode(31);
Papa.BYTE_ORDER_MARK = "\ufeff"; Papa.BYTE_ORDER_MARK = "\ufeff";
Papa.BAD_DELIMITERS = ["\r", "\n", "\"", Papa.BYTE_ORDER_MARK]; Papa.BAD_DELIMITERS = ["\r", "\n", "\"", Papa.BYTE_ORDER_MARK];
Papa.WORKERS_SUPPORTED = !!global.Worker; Papa.WORKERS_SUPPORTED = !IS_WORKER && !!global.Worker;
Papa.SCRIPT_PATH = null; // Must be set by your code if you use workers and this lib is loaded asynchronously Papa.SCRIPT_PATH = null; // Must be set by your code if you use workers and this lib is loaded asynchronously
// Configurable chunk sizes for local and remote files, respectively // Configurable chunk sizes for local and remote files, respectively
@ -145,7 +147,7 @@
} }
if (IS_WORKER) if (IS_PAPA_WORKER)
{ {
global.onmessage = workerThreadReceivedMessage; global.onmessage = workerThreadReceivedMessage;
} }
@ -426,7 +428,7 @@
var finishedIncludingPreview = this._finished || (this._config.preview && this._rowCount >= this._config.preview); var finishedIncludingPreview = this._finished || (this._config.preview && this._rowCount >= this._config.preview);
if (IS_WORKER) if (IS_PAPA_WORKER)
{ {
global.postMessage({ global.postMessage({
results: results, results: results,
@ -462,7 +464,7 @@
{ {
if (isFunction(this._config.error)) if (isFunction(this._config.error))
this._config.error(error); this._config.error(error);
else if (IS_WORKER && this._config.error) else if (IS_PAPA_WORKER && this._config.error)
{ {
global.postMessage({ global.postMessage({
workerId: Papa.WORKER_ID, workerId: Papa.WORKER_ID,
@ -1280,7 +1282,10 @@
'Script path cannot be determined automatically when Papa Parse is loaded asynchronously. ' + 'Script path cannot be determined automatically when Papa Parse is loaded asynchronously. ' +
'You need to set Papa.SCRIPT_PATH manually.' 'You need to set Papa.SCRIPT_PATH manually.'
); );
var w = new global.Worker(Papa.SCRIPT_PATH || AUTO_SCRIPT_PATH); var workerUrl = Papa.SCRIPT_PATH || AUTO_SCRIPT_PATH;
// Append "papaworker" to the search string to tell papaparse that this is our worker.
workerUrl += (workerUrl.indexOf('?') !== -1 ? '&' : '?') + 'papaworker';
var w = new global.Worker(workerUrl);
w.onmessage = mainThreadReceivedMessage; w.onmessage = mainThreadReceivedMessage;
w.id = workerIdCounter++; w.id = workerIdCounter++;
workers[w.id] = w; workers[w.id] = w;

31
tests/test-cases.js

@ -1458,6 +1458,37 @@ var CUSTOM_TESTS = [
} }
}); });
} }
},
{
description: "Should not assume we own the worker unless papaworker is in the search string",
disabled: typeof Worker === 'undefined',
expected: [false, true, true, true, true],
run: function(callback) {
var searchStrings = [
'',
'?papaworker',
'?x=1&papaworker',
'?x=1&papaworker&y=1',
'?x=1&papaworker=1'
];
var results = searchStrings.map(function () { return false; });
var workers = [];
// Give it .5s to do something
setTimeout(function () {
workers.forEach(function (w) { w.terminate(); });
callback(results);
}, 500);
searchStrings.forEach(function (searchString, idx) {
var w = new Worker('../papaparse.js' + searchString);
workers.push(w);
w.addEventListener('message', function () {
results[idx] = true;
});
w.postMessage({input: 'a,b,c\n1,2,3'});
});
}
} }
]; ];

Loading…
Cancel
Save