@@ -667,35 +667,43 @@ function getGroupTsDeltas(
667667 } ,
668668 groupLeaderSlots : number [ ] ,
669669) {
670- const groupTsDeltas : {
670+ const tsDeltasByGroup : {
671671 [ leaderSlotNumber : number ] : TsDeltaRange ;
672672 } = { } ;
673673
674674 for ( const leaderSlot of groupLeaderSlots ) {
675- const slotNumbers = Array . from (
676- { length : slotsPerLeader } ,
677- ( _ , i ) => i + leaderSlot ,
675+ const fullGroupTsDeltas = Array . from ( { length : slotsPerLeader } , ( _ , i ) => {
676+ const slotNumber = i + leaderSlot ;
677+ return slotTsDeltas [ slotNumber ] ;
678+ } ) ;
679+
680+ const firstDefinedSlotIdx = fullGroupTsDeltas . findIndex (
681+ ( v ) => v !== undefined ,
678682 ) ;
679- const definedSlotTsDeltas = slotNumbers
680- . map ( ( slotNumber ) => slotTsDeltas [ slotNumber ] )
681- . filter ( ( v ) => v !== undefined ) ;
682683
683- if ( definedSlotTsDeltas . length === 0 ) {
684- continue ;
685- }
684+ // no slots, no group
685+ if ( firstDefinedSlotIdx === - 1 ) continue ;
686+
687+ // ignore missing slots at the start when positioning group.
688+ // handles the case where some we sometimes miss early slot completion events,
689+ // depending on the connection timing
690+ const groupTsDeltas = fullGroupTsDeltas . slice ( firstDefinedSlotIdx ) ;
691+
692+ const minStart = Math . min (
693+ ...groupTsDeltas . filter ( ( v ) => v !== undefined ) . map ( ( [ start ] ) => start ) ,
694+ ) ;
686695
687- // ignore missing slots at the start when positioning group
688- const minStart = Math . min ( ...definedSlotTsDeltas . map ( ( [ start ] ) => start ) ) ;
689- const ends = definedSlotTsDeltas . map ( ( [ , end ] ) => end ) ;
690- const definedEnds = ends . filter ( ( end ) => end !== undefined ) ;
696+ const definedEnds = groupTsDeltas
697+ . map ( ( pos ) => pos ?. [ 1 ] ?? undefined )
698+ . filter ( ( end ) => end !== undefined ) ;
691699
692700 // undefined slot end will extend to max x
693- const hasUndefinedEnd = ends . length > definedEnds . length ;
701+ const hasUndefinedEnd = definedEnds . length < groupTsDeltas . length ;
694702 const maxEnd = hasUndefinedEnd ? undefined : Math . max ( ...definedEnds ) ;
695703
696- groupTsDeltas [ leaderSlot ] = [ minStart , maxEnd ] ;
704+ tsDeltasByGroup [ leaderSlot ] = [ minStart , maxEnd ] ;
697705 }
698- return groupTsDeltas ;
706+ return tsDeltasByGroup ;
699707}
700708
701709/**
0 commit comments