From 86c37d610829e5dd38ec3d50416775d974e70063 Mon Sep 17 00:00:00 2001 From: Braden Anderson Date: Tue, 3 Feb 2015 17:08:07 -0700 Subject: [PATCH] remove references to streamer._paused and fix fake chunk callbacks --- papaparse.js | 9 +++---- tests/test-cases.js | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/papaparse.js b/papaparse.js index 5f92d22..7483c98 100755 --- a/papaparse.js +++ b/papaparse.js @@ -424,7 +424,6 @@ function ChunkStreamer(config) { this._handle = null; - this._paused = false; this._finished = false; this._input = null; this._baseIndex = 0; @@ -440,7 +439,7 @@ }; replaceConfig.call(this, config); - this.parseChunk = function(chunk) + this.parseChunk = function(chunk, isFakeChunk) { // First chunk pre-processing if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) @@ -481,10 +480,10 @@ finished: finishedIncludingPreview }); } - else if (isFunction(this._config.chunk)) + else if (isFunction(this._config.chunk) && !isFakeChunk) { this._config.chunk(results, this._handle); - if (this._paused) + if (this._handle.paused()) return; results = undefined; this._completeResults = undefined; @@ -916,7 +915,7 @@ this.resume = function() { _paused = false; - self.streamer.parseChunk(_input); + self.streamer.parseChunk(_input, true); }; this.aborted = function () diff --git a/tests/test-cases.js b/tests/test-cases.js index 03c1ae2..9086487 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1564,6 +1564,72 @@ var CUSTOM_TESTS = [ }); } }, + { + description: "Chunk functions can pause parsing", + expected: [ + [['A', 'b', 'c']] + ], + run: function(callback) { + var updates = []; + Papa.parse('A,b,c\nd,E,f\nG,h,i', { + chunkSize: 10, + chunk: function(response, handle) { + updates.push(response.data); + handle.pause(); + callback(updates); + }, + complete: function() { + callback('incorrect complete callback'); + } + }); + } + }, + { + description: "Chunk functions can resume parsing", + expected: [ + [['A', 'b', 'c']], + [['d', 'E', 'f'], ['G', 'h', 'i']] + ], + run: function(callback) { + var updates = []; + var handle = null; + var first = true; + Papa.parse('A,b,c\nd,E,f\nG,h,i', { + chunkSize: 10, + chunk: function(response, h) { + updates.push(response.data); + if (!first) return; + handle = h; + handle.pause(); + first = false; + }, + complete: function() { + callback(updates); + } + }); + setTimeout(function() { + handle.resume(); + }, 500); + } + }, + { + description: "Chunk functions can abort parsing", + expected: [ + [['A', 'b', 'c'], ['d', 'E', 'f'], ['G', 'h', 'i']] + ], + run: function(callback) { + var updates = []; + Papa.parse('A,b,c\nd,E,f\nG,h,i', { + chunk: function(response, handle) { + updates.push(response.data); + handle.abort(); + }, + complete: function(response) { + callback(updates); + } + }); + } + }, { description: "Step exposes indexes for files", expected: [6, 12, 17],