Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilliams720 committed Oct 30, 2024
2 parents 1d980ac + dde746e commit 9068d28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
11 changes: 4 additions & 7 deletions examples/adding-content/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
window.ctDebug = true;
// const observer = new ContainerPerformanceObserver((list) => {
// list.getEntries().forEach((entry) => {
// console.log(entry);
// });
// });
const queryString = window.location.search;
const urlParams = new URLSearchParams(window.location.search);
const nestedStrategy = urlParams.get("nestedStrategy") || "ignore"

const nativeObserver = new PerformanceObserver((v) => {
console.log(v.getEntries());
});

nativeObserver.observe({ entryTypes: ["container"] });
// observer.observe({ nestedStrategy: "transparent" });
nativeObserver.observe({ type: "container", nestedStrategy: nestedStrategy });

window.setTimeout(() => {
const innerContainer = document.querySelector(".container div");
Expand Down
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ <h1>Container Timing Examples</h1>
<ol>
<li><a href="./examples/table/table.html">Table</a></li>
<li>
<a href="./examples/adding-content/index.html">dynamic content</a>
Dynamic content with nested strategy
<a href="./examples/adding-content/index.html?nestedStrategy=ignore">ignore</a>,
<a href="./examples/adding-content/index.html?nestedStrategy=transparent">transparent</a>,
<a href="./examples/adding-content/index.html?nestedStrategy=shadowed">shadowed</a>
</li>
</ol>
</body>
Expand Down
34 changes: 22 additions & 12 deletions polyfill/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const native_implementation_available = ("PerformanceContainerTiming" in window);

// https://wicg.github.io/element-timing/#performanceelementtiming
interface PerformanceElementTiming extends PerformanceEntry {
name: string;
Expand Down Expand Up @@ -82,19 +84,25 @@ const mutationObserverCallback = (mutationList: MutationRecord[]) => {
}
};

// Wait until the DOM is ready then start collecting elements needed to be timed.
document.addEventListener("DOMContentLoaded", () => {
mutationObserver = new window.MutationObserver(mutationObserverCallback);

const config = { attributes: false, childList: true, subtree: true };
mutationObserver.observe(document, config);

const elms = document.querySelectorAll(containerTimingAttrSelector);
elms.forEach((elm) => {
containerRoots.add(elm);
ContainerPerformanceObserver.setDescendants(elm);
// Wait until the DOM is ready then start collecting elements needed to be timed.
if (!native_implementation_available) {
console.debug("Enabling polyfill")
document.addEventListener("DOMContentLoaded", () => {
mutationObserver = new window.MutationObserver(mutationObserverCallback);

const config = { attributes: false, childList: true, subtree: true };
mutationObserver.observe(document, config);

const elms = document.querySelectorAll(containerTimingAttrSelector);
elms.forEach((elm) => {
containerRoots.add(elm);
ContainerPerformanceObserver.setDescendants(elm);
});
});
});
} else {
console.debug("Native implementation of Container Timing available")
}

class PerformanceContainerTiming implements PerformanceEntry {
entryType = "container";
Expand Down Expand Up @@ -504,4 +512,6 @@ class ContainerPerformanceObserver implements PerformanceObserver {
}
}

window.PerformanceObserver = ContainerPerformanceObserver;
if (!native_implementation_available) {
window.PerformanceObserver = ContainerPerformanceObserver;
}

0 comments on commit 9068d28

Please sign in to comment.