@@ -662,35 +662,43 @@ function getGroupTsDeltas(
662662 } ,
663663 groupLeaderSlots : number [ ] ,
664664) {
665- const groupTsDeltas : {
665+ const tsDeltasByGroup : {
666666 [ leaderSlotNumber : number ] : TsDeltaRange ;
667667 } = { } ;
668668
669669 for ( const leaderSlot of groupLeaderSlots ) {
670- const slotNumbers = Array . from (
671- { length : slotsPerLeader } ,
672- ( _ , i ) => i + leaderSlot ,
670+ const fullGroupTsDeltas = Array . from ( { length : slotsPerLeader } , ( _ , i ) => {
671+ const slotNumber = i + leaderSlot ;
672+ return slotTsDeltas [ slotNumber ] ;
673+ } ) ;
674+
675+ const firstDefinedSlotIdx = fullGroupTsDeltas . findIndex (
676+ ( v ) => v !== undefined ,
673677 ) ;
674- const definedSlotTsDeltas = slotNumbers
675- . map ( ( slotNumber ) => slotTsDeltas [ slotNumber ] )
676- . filter ( ( v ) => v !== undefined ) ;
677678
678- if ( definedSlotTsDeltas . length === 0 ) {
679- continue ;
680- }
679+ // no slots, no group
680+ if ( firstDefinedSlotIdx === - 1 ) continue ;
681+
682+ // ignore missing slots at the start when positioning group.
683+ // handles the case where some we sometimes miss early slot completion events,
684+ // depending on the connection timing
685+ const groupTsDeltas = fullGroupTsDeltas . slice ( firstDefinedSlotIdx ) ;
686+
687+ const minStart = Math . min (
688+ ...groupTsDeltas . filter ( ( v ) => v !== undefined ) . map ( ( [ start ] ) => start ) ,
689+ ) ;
681690
682- // ignore missing slots at the start when positioning group
683- const minStart = Math . min ( ...definedSlotTsDeltas . map ( ( [ start ] ) => start ) ) ;
684- const ends = definedSlotTsDeltas . map ( ( [ , end ] ) => end ) ;
685- const definedEnds = ends . filter ( ( end ) => end !== undefined ) ;
691+ const definedEnds = groupTsDeltas
692+ . map ( ( pos ) => pos ?. [ 1 ] ?? undefined )
693+ . filter ( ( end ) => end !== undefined ) ;
686694
687695 // undefined slot end will extend to max x
688- const hasUndefinedEnd = ends . length > definedEnds . length ;
696+ const hasUndefinedEnd = definedEnds . length < groupTsDeltas . length ;
689697 const maxEnd = hasUndefinedEnd ? undefined : Math . max ( ...definedEnds ) ;
690698
691- groupTsDeltas [ leaderSlot ] = [ minStart , maxEnd ] ;
699+ tsDeltasByGroup [ leaderSlot ] = [ minStart , maxEnd ] ;
692700 }
693- return groupTsDeltas ;
701+ return tsDeltasByGroup ;
694702}
695703
696704/**
0 commit comments