@@ -395,8 +395,10 @@ function findParsedRow({ cache, row }: { cache: Cache; row: number }):
395395 ...serialParsedRow ,
396396 } ;
397397 }
398- // TODO(SL): we know the index of the last row in the serial range, we can be more precise
399- const estimatedStart = cache . header . bytes + row * cache . averageRowBytes ;
398+ const estimatedStart =
399+ cache . header . bytes +
400+ cache . serial . end +
401+ ( row - cache . serial . validRows . length ) * cache . averageRowBytes ;
400402 // find the range containing this row
401403 const range = cache . random . find (
402404 ( r ) => estimatedStart >= r . start && estimatedStart < r . end
@@ -466,6 +468,7 @@ function fetchRange({
466468 checkSignal ( signal ) ;
467469
468470 let cursor = start ;
471+ let isFirstStep = true ;
469472
470473 return new Promise < void > ( ( resolve , reject ) => {
471474 Papa . parse < string [ ] > ( cache . url , {
@@ -503,7 +506,7 @@ function fetchRange({
503506 }
504507
505508 // add the row to the cache
506- if ( addParsedRowToCache ( { cache, parsedRow } ) ) {
509+ if ( addParsedRowToCache ( { cache, parsedRow, isFirstStep } ) ) {
507510 // send an event for the new row
508511 eventTarget . dispatchEvent ( new CustomEvent ( "resolve" ) ) ;
509512 }
@@ -513,6 +516,8 @@ function fetchRange({
513516 parser . abort ( ) ;
514517 return ;
515518 }
519+
520+ isFirstStep = false ;
516521 } ,
517522 complete : ( ) => {
518523 resolve ( ) ;
@@ -531,11 +536,16 @@ function isEmpty(data: string[]): boolean {
531536function addParsedRowToCache ( {
532537 cache,
533538 parsedRow,
539+ isFirstStep,
534540} : {
535541 cache : Cache ;
536542 parsedRow : ParsedRow ;
543+ isFirstStep : boolean ; // to handle the case where we start in the middle of a row
537544} ) : boolean {
538- console . debug ( { cache, parsedRow } ) ;
545+ if ( isFirstStep && parsedRow . data . length < cache . header . data . length ) {
546+ // the first parsed row is partial, we ignore it, it must be part of the previous row
547+ return false ;
548+ }
539549
540550 // TODO(SL): optimize
541551 const inserted = ! isEmpty ( parsedRow . data ) ;
0 commit comments