Skip to content

Commit

Permalink
fix: undefined event in canvas plugin (#26454)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Nov 27, 2024
1 parent c72ea8b commit f250d52
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,32 @@ function isCanvasMutation(e: eventWithTime): e is CanvasEventWithTime {
}

function quickFindClosestCanvasEventIndex(
events: CanvasEventWithTime[],
target: CanvasEventWithTime,
events: CanvasEventWithTime[] | undefined,
target: CanvasEventWithTime | undefined,
start: number,
end: number
): number {
if (!target) {
if (!target || !events || !events.length) {
return -1
}

if (start > end) {
return end
}

if (start < 0 || end > events.length - 1) {
return -1
}

const mid = Math.floor((start + end) / 2)

return target.timestamp <= events[mid].timestamp
// in production, we do sometimes see this be undefined
const middleEvent = events[mid]
if (!middleEvent) {
return -1
}

return target.timestamp <= middleEvent.timestamp
? quickFindClosestCanvasEventIndex(events, target, start, mid - 1)
: quickFindClosestCanvasEventIndex(events, target, mid + 1, end)
}
Expand Down

0 comments on commit f250d52

Please sign in to comment.