Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit polling if not active on tab and use resize observer if supported #1077

Merged
merged 1 commit into from Jun 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 47 additions & 10 deletions src/contents.js
Expand Up @@ -40,7 +40,8 @@ class Contents {
this.cfiBase = cfiBase || "";

this.epubReadingSystem("epub.js", EPUBJS_VERSION);

this.called = 0;
this.active = true;
this.listeners();
}

Expand Down Expand Up @@ -391,9 +392,14 @@ class Contents {

// this.transitionListeners();

this.resizeListeners();
if (typeof ResizeObserver === "undefined") {
this.resizeListeners();
this.visibilityListeners();
} else {
this.resizeObservers();
}

// this.resizeObservers();
// this.mutationObservers();

this.linksHandler();
}
Expand All @@ -408,6 +414,10 @@ class Contents {

this.removeSelectionListeners();

if (this.observer) {
this.observer.disconnect();
}

clearTimeout(this.expanding);
}

Expand Down Expand Up @@ -441,6 +451,23 @@ class Contents {
// Test size again
clearTimeout(this.expanding);
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);
}
});
}

/**
Expand Down Expand Up @@ -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
*/
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
this.observer = new MutationObserver((mutations) => {
this.resizeCheck();
Expand Down Expand Up @@ -1211,12 +1253,7 @@ class Contents {
}

destroy() {
// Stop observing
if(this.observer) {
this.observer.disconnect();
}

this.document.removeEventListener('transitionend', this._resizeCheck);
// this.document.removeEventListener('transitionend', this._resizeCheck);

this.removeListeners();

Expand Down