Browse Source

Rework #166.

pull/481/head
Gabe Gorelick 7 years ago committed by Sergi Almacellas Abellana
parent
commit
dfc5b7c421
  1. 7
      papaparse.js
  2. 35
      tests/node-tests.js
  3. 7
      tests/test-cases.js

7
papaparse.js

@ -425,6 +425,7 @@
{ {
this._handle = null; this._handle = null;
this._finished = false; this._finished = false;
this._completed = false;
this._input = null; this._input = null;
this._baseIndex = 0; this._baseIndex = 0;
this._partialLine = ''; this._partialLine = '';
@ -483,7 +484,7 @@
else if (isFunction(this._config.chunk) && !isFakeChunk) else if (isFunction(this._config.chunk) && !isFakeChunk)
{ {
this._config.chunk(results, this._handle); this._config.chunk(results, this._handle);
if (this._handle.paused()) if (this._handle.paused() || this._handle.aborted())
return; return;
results = undefined; results = undefined;
this._completeResults = undefined; this._completeResults = undefined;
@ -495,8 +496,10 @@
this._completeResults.meta = results.meta; this._completeResults.meta = results.meta;
} }
if (finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) if (!this._completed && finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) {
this._config.complete(this._completeResults, this._input); this._config.complete(this._completeResults, this._input);
this._completed = true;
}
if (!finishedIncludingPreview && (!results || !results.meta.paused)) if (!finishedIncludingPreview && (!results || !results.meta.paused))
this._nextChunk(); this._nextChunk();

35
tests/node-tests.js

@ -57,5 +57,40 @@ describe('PapaParse', function() {
done(); done();
}, },
}); });
it('should support pausing and resuming on same tick when streaming', function(done) {
var rows = [];
Papa.parse(fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'), {
chunk: function(results, parser) {
rows = rows.concat(results.data);
parser.pause();
parser.resume();
},
error: function(err) {
done(new Error(err));
},
complete: function() {
assert.deepEqual(rows[0], [
'Grant',
'Dyer',
'Donec.elementum@orciluctuset.example',
'2013-11-23T02:30:31-08:00',
'2014-05-31T01:06:56-07:00',
'Magna Ut Associates',
'ljenkins'
]);
assert.deepEqual(rows[7], [
'Talon',
'Salinas',
'posuere.vulputate.lacus@Donecsollicitudin.example',
'2015-01-31T09:19:02-08:00',
'2014-12-17T04:59:18-08:00',
'Aliquam Iaculis Incorporate',
'Phasellus@Quisquetincidunt.example'
]);
done();
}
});
});
}); });
}); });

7
tests/test-cases.js

@ -1579,7 +1579,7 @@ var CUSTOM_TESTS = [
callback(updates); callback(updates);
}, },
complete: function() { complete: function() {
callback('incorrect complete callback'); callback(new Error('incorrect complete callback'));
} }
}); });
} }
@ -1615,14 +1615,17 @@ var CUSTOM_TESTS = [
{ {
description: "Chunk functions can abort parsing", description: "Chunk functions can abort parsing",
expected: [ expected: [
[['A', 'b', 'c'], ['d', 'E', 'f'], ['G', 'h', 'i']] [['A', 'b', 'c']]
], ],
run: function(callback) { run: function(callback) {
var updates = []; var updates = [];
Papa.parse('A,b,c\nd,E,f\nG,h,i', { Papa.parse('A,b,c\nd,E,f\nG,h,i', {
chunkSize: 1,
chunk: function(response, handle) { chunk: function(response, handle) {
if (response.data.length) {
updates.push(response.data); updates.push(response.data);
handle.abort(); handle.abort();
}
}, },
complete: function(response) { complete: function(response) {
callback(updates); callback(updates);

Loading…
Cancel
Save