Skip to content

Commit

Permalink
Avoid undelivered notifications error
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusMorton committed Jul 23, 2024
1 parent 36e1d63 commit d7e21a7
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions libraries/browser-tracker-core/src/helpers/browser_props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,44 @@ function useResizeObserver(): boolean {
}

let resizeObserverInitialized = false;
let readBrowserPropertiesTask: number | null = null;
function initializeResizeObserver() {
if (resizeObserverInitialized) {
return;
}
if(!document || !document.body || !document.documentElement) {
if (!document || !document.body || !document.documentElement) {
return;
}
resizeObserverInitialized = true;

const resizeObserver = new ResizeObserver((entries) => {
for (let entry of entries) {
if (entry.target === document.body || entry.target === document.documentElement) {
cachedProperties = readBrowserProperties();
}
if (readBrowserPropertiesTask === null) {
// The browser property lookup causes a forced synchronous layout when offsets/sizes are
// queried. It's possible that the forced synchronous layout causes the ResizeObserver
// to be fired again, leading to an infinite loop and the "ResizeObserver loop completed
// with undelivered notifications" error.
readBrowserPropertiesTask = scheduleBrowserPropertiesCallback(() => {
readBrowserPropertiesTask = null;
for (let entry of entries) {
if (entry.target === document.body || entry.target === document.documentElement) {
cachedProperties = readBrowserProperties();
}
}
});
}
});
resizeObserver.observe(document.body);
resizeObserver.observe(document.documentElement);
}

function scheduleBrowserPropertiesCallback(func: () => void) {
if (requestAnimationFrame) {
return requestAnimationFrame(func);
} else {
return window.setTimeout(func, 0);
}
}

let cachedProperties: BrowserProperties;

/* Separator used for dimension values e.g. widthxheight */
Expand Down

0 comments on commit d7e21a7

Please sign in to comment.