Skip to content

Commit

Permalink
Sort events at start, and keep ordered, as otherwise we risk misident…
Browse files Browse the repository at this point in the history
…ifying the last event (i.e. calling `finish()` prematurely)
  • Loading branch information
eoghanmurray committed Jan 29, 2021
1 parent 2578948 commit 1d4d538
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/replay/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,26 @@ export function createPlayerService(
if (machineEvent.type === 'ADD_EVENT') {
const { event } = machineEvent.payload;
addDelay(event, baselineTime);
events.push(event);

let insertion_index = -1;
let start = 0;
let end = events.length - 1;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (events[mid].timestamp < event.timestamp) {
start = mid + 1;
} else if (events[mid].timestamp > event.timestamp) {
end = mid - 1;
} else {
insertion_index = mid;
break;
}
}
if (insertion_index === -1) {
insertion_index = start;
}
events.splice(insertion_index, 0, event);

const isSync = event.timestamp < baselineTime;
const castFn = getCastFn(event, isSync);
if (isSync) {
Expand Down

0 comments on commit 1d4d538

Please sign in to comment.