support buffered option in PerformanceObserver.observe()

When buffered is true, deliver existing performance entries that match
the observer's interest immediately, per the Performance Observer spec.
This commit is contained in:
egrs
2026-02-17 15:58:38 +01:00
parent db4a97743f
commit afbd927fc0
2 changed files with 40 additions and 0 deletions

View File

@@ -69,3 +69,31 @@
<script>
testing.expectEqual(['mark', 'measure'], PerformanceObserver.supportedEntryTypes);
</script>
<script id="buffered_option">
{
// Create marks BEFORE the observer
performance.mark("early1", { startTime: 1.0 });
performance.mark("early2", { startTime: 2.0 });
let receivedEntries = null;
const observer = new PerformanceObserver((list) => {
receivedEntries = list.getEntries();
});
// With buffered: true, existing marks should be delivered immediately
observer.observe({ type: "mark", buffered: true });
testing.eventually(() => {
testing.expectEqual(true, receivedEntries !== null);
// Should have received at least the 2 early marks
testing.expectEqual(true, receivedEntries.length >= 2);
const names = receivedEntries.map(e => e.name);
testing.expectEqual(true, names.indexOf("early1") >= 0);
testing.expectEqual(true, names.indexOf("early2") >= 0);
observer.disconnect();
});
}
</script>

View File

@@ -113,6 +113,18 @@ pub fn observe(
// Update interests.
self._interests = interests;
// Deliver existing entries if buffered option is set.
if (options.buffered) {
for (page.window._performance._entries.items) |entry| {
if (self.interested(entry)) {
try self._entries.append(page.arena, entry);
}
}
if (self.hasRecords()) {
try self.dispatch(page);
}
}
}
pub fn disconnect(self: *PerformanceObserver, page: *Page) void {