Skip to content

Commit 8cdceaf

Browse files
authored
INP attribution: ensure LoAF order is maintained (#512)
1 parent 5b91b76 commit 8cdceaf

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/attribution/onINP.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let pendingLoAFs: PerformanceLongAnimationFrameTiming[] = [];
6363
let pendingEntriesGroups: pendingEntriesGroup[] = [];
6464

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

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

189189
// Keep all pending LoAF entries that either:
190190
// 1) intersect with entries in the newly cleaned up `pendingEntriesGroups`
191-
// 2) occur after the most recently-processed event entry.
191+
// 2) occur after the most recently-processed event entry (for up to MAX_PREVIOUS_FRAMES)
192192
const loafsToKeep: Set<PerformanceLongAnimationFrameTiming> = new Set();
193193
for (let i = 0; i < pendingEntriesGroups.length; i++) {
194194
const group = pendingEntriesGroups[i];
@@ -198,18 +198,15 @@ const cleanupEntries = () => {
198198
},
199199
);
200200
}
201-
for (let i = 0; i < MAX_PREVIOUS_FRAMES; i++) {
202-
// Look at pending LoAF in reverse order so the most recent are first.
203-
const loaf = pendingLoAFs[pendingLoAFs.length - 1 - i];
204-
205-
// If we reach LoAFs that overlap with event processing,
206-
// we can assume all previous ones have already been handled.
207-
if (!loaf || loaf.startTime < latestProcessingEnd) break;
208-
209-
loafsToKeep.add(loaf);
210-
}
201+
const prevFrameIndexCutoff = pendingLoAFs.length - 1 - MAX_PREVIOUS_FRAMES;
202+
// Filter `pendingLoAFs` to preserve LoAF order.
203+
pendingLoAFs = pendingLoAFs.filter((loaf, index) => {
204+
if (loaf.startTime > latestProcessingEnd && index > prevFrameIndexCutoff) {
205+
return true;
206+
}
211207

212-
pendingLoAFs = Array.from(loafsToKeep);
208+
return loafsToKeep.has(loaf);
209+
});
213210

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

0 commit comments

Comments
 (0)