mholt/PapaParse

There is an issue with .pause/.resume

Open

#164 创建于 2015年1月27日

在 GitHub 查看
 (19 评论) (0 反应) (0 负责人)JavaScript (11,877 star) (1,157 fork)batch import
confirmed bughelp wanted

描述

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;
                         }

贡献者指南