mholt/PapaParse

There is an issue with .pause/.resume

Open

#164 aberto em 27 de jan. de 2015

Ver no GitHub
 (19 comments) (0 reactions) (0 assignees)JavaScript (1.157 forks)batch import
confirmed bughelp wanted

Métricas do repositório

Stars
 (11.877 stars)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

I heavily rely on pause-resume methods. I use chunk-streaming processing and here is what I've found: https://github.com/mholt/PapaParse/blob/master/papaparse.js#L403

else if (isFunction(this._config.chunk))
{
    this._config.chunk(results, this._handle);
    if (this._paused)
        return;
    results = undefined;
}

this._paused is always false in this context. It has to be checked by this._handle.paused()

Another part of the issue is in the resume implementation, but let's look at pause method at first:

this.pause = function()
{
    _paused = true;
    _parser.abort();
    _input = _input.substr(_parser.getCharIndex());
};

_input will be always an empty string (!) if we use chunk-streaming. On resume, PapaParse will parse the empty string again and will fire callbacks with empty results.

My JavaScript code is not that good, which is why I'm creating this issue instead of a pull request, but here is my attempt to patch the issue:

@@ -367,6 +367,12 @@

                 this.parseChunk = function(chunk)
                 {
+                        if (chunk.length === 0) {
+                                // It is possible due to the pause/resume implementation.
+                                this._nextChunk();
+                                return;
+                        }
+
                         // Rejoin the line we likely just split in two by chunking the file
                         var aggregate = this._partialLine + chunk;
                         this._partialLine = "";
@@ -400,7 +406,7 @@
                         else if (isFunction(this._config.chunk))
                         {
                                 this._config.chunk(results, this._handle);
-                                if (this._paused)
+                                if (this._handle.paused())
                                         return;
                                 results = undefined;
                         }

Guia do colaborador