Skip to content

Commit

Permalink
INP attribution: ensure LoAF order is maintained (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored Aug 7, 2024
1 parent 5b91b76 commit 8cdceaf
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/attribution/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let pendingLoAFs: PerformanceLongAnimationFrameTiming[] = [];
let pendingEntriesGroups: pendingEntriesGroup[] = [];

// The `processingEnd` time of most recently-processed event, chronologically.
let latestProcessingEnd: number;
let latestProcessingEnd: number = 0;

// A WeakMap to look up the event-timing-entries group of a given entry.
// Note that this only maps from "important" entries: either the first input or
Expand Down Expand Up @@ -188,7 +188,7 @@ const cleanupEntries = () => {

// Keep all pending LoAF entries that either:
// 1) intersect with entries in the newly cleaned up `pendingEntriesGroups`
// 2) occur after the most recently-processed event entry.
// 2) occur after the most recently-processed event entry (for up to MAX_PREVIOUS_FRAMES)
const loafsToKeep: Set<PerformanceLongAnimationFrameTiming> = new Set();
for (let i = 0; i < pendingEntriesGroups.length; i++) {
const group = pendingEntriesGroups[i];
Expand All @@ -198,18 +198,15 @@ const cleanupEntries = () => {
},
);
}
for (let i = 0; i < MAX_PREVIOUS_FRAMES; i++) {
// Look at pending LoAF in reverse order so the most recent are first.
const loaf = pendingLoAFs[pendingLoAFs.length - 1 - i];

// If we reach LoAFs that overlap with event processing,
// we can assume all previous ones have already been handled.
if (!loaf || loaf.startTime < latestProcessingEnd) break;

loafsToKeep.add(loaf);
}
const prevFrameIndexCutoff = pendingLoAFs.length - 1 - MAX_PREVIOUS_FRAMES;
// Filter `pendingLoAFs` to preserve LoAF order.
pendingLoAFs = pendingLoAFs.filter((loaf, index) => {
if (loaf.startTime > latestProcessingEnd && index > prevFrameIndexCutoff) {
return true;
}

pendingLoAFs = Array.from(loafsToKeep);
return loafsToKeep.has(loaf);
});

// Reset the idle callback handle so it can be queued again.
idleHandle = -1;
Expand Down

0 comments on commit 8cdceaf

Please sign in to comment.