|
386 | 386 | /**
|
387 | 387 | * If `auto` flag is not set, use this function to start the job manually.
|
388 | 388 | * Will resume parsing instead if parsing was paused previously.
|
389 |
| - * Only return results if `raw` is being parsed. |
390 |
| - * @return {Array[]|Object[]|undefined} |
| 389 | + * return results if `raw` is being parsed, otherwise return a Promise to the results |
| 390 | + * |
| 391 | + * @return {Array[]|Object[]|Promise} |
391 | 392 | */
|
392 | 393 | start() {
|
393 | 394 | if (this.stream && this._parser) return this.resume();
|
|
425 | 426 | }
|
426 | 427 |
|
427 | 428 | _parseRaw(raw, _config, run) {
|
428 |
| - if (!raw || !this._attached || !run) return; |
| 429 | + if (!raw || !run) return; |
429 | 430 | var results = Papa.parse(raw, _config);
|
430 | 431 | this._syncProperties(results);
|
431 | 432 | return results;
|
432 | 433 | }
|
433 | 434 |
|
434 | 435 | _parseFile(file, _config, run) {
|
435 |
| - if (!file || !this._attached || !run) return; |
436 |
| - var config = Object.assign( |
437 |
| - {complete: this._complete.bind(this)}, |
438 |
| - this._config |
439 |
| - ); |
440 |
| - |
441 |
| - Papa.parse(file, config); |
| 436 | + if (!file || !run) return; |
| 437 | + return new Promise((resolve, reject) => { |
| 438 | + let config = Object.assign( |
| 439 | + {complete: this._complete(this, resolve, reject)}, |
| 440 | + this._config |
| 441 | + ); |
| 442 | + |
| 443 | + Papa.parse(file, config); |
| 444 | + }); |
442 | 445 | }
|
443 | 446 |
|
444 | 447 | _parseUrl(url, _config, run) {
|
445 |
| - if (!url || !this._attached || !run) return; |
446 |
| - var config = Object.assign( |
447 |
| - {download: true, complete: this._complete.bind(this)}, |
448 |
| - this._config |
449 |
| - ); |
450 |
| - |
451 |
| - Papa.parse(url, config); |
| 448 | + if (!url || !run) return; |
| 449 | + return new Promise((resolve, reject) => { |
| 450 | + let config = Object.assign( |
| 451 | + {download: true, complete: this._complete(this, resolve, reject)}, |
| 452 | + this._config |
| 453 | + ); |
| 454 | + |
| 455 | + Papa.parse(url, config); |
| 456 | + }); |
452 | 457 | }
|
453 | 458 |
|
454 | 459 | _syncProperties(results) {
|
|
467 | 472 | this.dispatchEvent(new CustomEvent('stream', {detail: results}));
|
468 | 473 | }
|
469 | 474 |
|
470 |
| - _complete(results) { |
471 |
| - this._syncProperties(results); |
| 475 | + _complete(self, resolve, reject) { |
| 476 | + return function(results) { |
| 477 | + this._syncProperties(results); |
472 | 478 |
|
473 |
| - if (this.errors && this.errors.length > 0) |
474 |
| - return this.dispatchEvent(new CustomEvent('errored', {detail: results})); |
| 479 | + if (this.errors && this.errors.length > 0) { |
| 480 | + this.dispatchEvent(new CustomEvent('errored', {detail: results})); |
| 481 | + return reject(results); |
| 482 | + } |
| 483 | + |
| 484 | + if (results && results.meta && results.meta.aborted) { |
| 485 | + this.dispatchEvent(new CustomEvent('aborted', {detail: results})); |
| 486 | + return reject(results); |
| 487 | + } |
475 | 488 |
|
476 |
| - if (results && results.meta && results.meta.aborted) |
477 |
| - return this.dispatchEvent(new CustomEvent('aborted', {detail: results})); |
| 489 | + if (results) { |
| 490 | + this.dispatchEvent(new CustomEvent('completed', {detail: results})); |
| 491 | + } |
478 | 492 |
|
479 |
| - if (results) |
480 |
| - this.dispatchEvent(new CustomEvent('completed', {detail: results})); |
| 493 | + resolve(results); |
| 494 | + }.bind(self); |
481 | 495 | }
|
482 | 496 | }
|
483 | 497 |
|
|
0 commit comments