Skip to content

Commit

Permalink
docs(OfflineLocalView): defer data loading when not visible
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Nov 1, 2023
1 parent d61d2e4 commit 8b90828
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion Examples/Applications/OfflineLocalView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ function preventDefaults(e) {
e.stopPropagation();
}

function onVisible(element, callback) {
new window.parent.IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.intersectionRatio > 0) {
callback();
observer.disconnect();
}
});
}).observe(element);
}

function runOnVisible(callback) {
if (!window.frameElement) {
callback();
return true;
}
const visible = window.frameElement.getClientRects().length > 0;
if (!visible) {
onVisible(window.frameElement, callback);
} else {
callback();
}
return visible;
}

export function load(container, options) {
autoInit = false;
emptyContainer(container);
Expand Down Expand Up @@ -161,7 +186,9 @@ if (userParams.url || userParams.fileURL) {
rootBody.style.margin = '0';
rootBody.style.padding = '0';
}
load(myContainer, userParams);

autoInit = false;
runOnVisible(() => load(myContainer, userParams));
}

// Auto setup if no method get called within 100ms
Expand Down

0 comments on commit 8b90828

Please sign in to comment.