1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

Limit polling if not active on tab, use resize observer if supported (#1077)

This commit is contained in:
Fred Chasen 2020-06-05 10:34:57 -07:00 committed by GitHub
parent cb4facb6c2
commit 29e1e8fdd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -40,7 +40,8 @@ class Contents {
this.cfiBase = cfiBase || ""; this.cfiBase = cfiBase || "";
this.epubReadingSystem("epub.js", EPUBJS_VERSION); this.epubReadingSystem("epub.js", EPUBJS_VERSION);
this.called = 0;
this.active = true;
this.listeners(); this.listeners();
} }
@ -391,9 +392,14 @@ class Contents {
// this.transitionListeners(); // this.transitionListeners();
this.resizeListeners(); if (typeof ResizeObserver === "undefined") {
this.resizeListeners();
this.visibilityListeners();
} else {
this.resizeObservers();
}
// this.resizeObservers(); // this.mutationObservers();
this.linksHandler(); this.linksHandler();
} }
@ -408,6 +414,10 @@ class Contents {
this.removeSelectionListeners(); this.removeSelectionListeners();
if (this.observer) {
this.observer.disconnect();
}
clearTimeout(this.expanding); clearTimeout(this.expanding);
} }
@ -441,6 +451,23 @@ class Contents {
// Test size again // Test size again
clearTimeout(this.expanding); clearTimeout(this.expanding);
requestAnimationFrame(this.resizeCheck.bind(this)); requestAnimationFrame(this.resizeCheck.bind(this));
this.expanding = setTimeout(this.resizeListeners.bind(this), 350);
}
/**
* Listen for visibility of tab to change
* @private
*/
visibilityListeners() {
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible" && this.active === false) {
this.active = true;
this.resizeListeners();
} else {
this.active = false;
clearTimeout(this.expanding);
}
});
} }
/** /**
@ -493,10 +520,25 @@ class Contents {
} }
/** /**
* Use MutationObserver to listen for changes in the DOM and check for resize * Use ResizeObserver to listen for changes in the DOM and check for resize
* @private * @private
*/ */
resizeObservers() { resizeObservers() {
// create an observer instance
this.observer = new ResizeObserver((e) => {
console.log("ResizeObserver", e);
this.resizeCheck();
});
// pass in the target node
this.observer.observe(this.document.documentElement);
}
/**
* Use MutationObserver to listen for changes in the DOM and check for resize
* @private
*/
mutationObservers() {
// create an observer instance // create an observer instance
this.observer = new MutationObserver((mutations) => { this.observer = new MutationObserver((mutations) => {
this.resizeCheck(); this.resizeCheck();
@ -1211,12 +1253,7 @@ class Contents {
} }
destroy() { destroy() {
// Stop observing // this.document.removeEventListener('transitionend', this._resizeCheck);
if(this.observer) {
this.observer.disconnect();
}
this.document.removeEventListener('transitionend', this._resizeCheck);
this.removeListeners(); this.removeListeners();