Skip to content

Defer intersection monitoring until needed to prevent race conditions #7278

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

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,8 @@ The `renderWhenVisible` function is passed to the show function as part of the `

Additionally, `renderWhenVisible` returns a boolean value indicating whether the provided function was executed immediately (`true`) or deferred (`false`).

Monitoring of visibility begins after the first call to `renderWhenVisible` is made.

Here’s the signature for the show function:

`show(element, isEditing, viewOptions)`
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/plot/chart/MctChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export default {
mounted() {
this.chartVisible = true;
this.chartContainer = this.$refs.chart;
this.drawnOnce = false;
this.visibilityObserver = new IntersectionObserver(this.visibilityChanged);
this.visibilityObserver.observe(this.chartContainer);
eventHelpers.extend(this);
this.seriesModels = [];
this.config = this.getConfig();
Expand Down Expand Up @@ -687,6 +687,10 @@ export default {
if (!this.drawScheduled) {
const called = this.renderWhenVisible(this.draw);
this.drawScheduled = called;
if (!this.drawnOnce && called) {
this.drawnOnce = true;
this.visibilityObserver.observe(this.chartContainer);
}
}
},
draw() {
Expand Down
9 changes: 7 additions & 2 deletions src/utils/visibility/VisibilityObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export default class VisibilityObserver {
}
this.#element = element;
this.isIntersecting = true;
this.calledOnce = false;

this.#observer = new IntersectionObserver(this.#observerCallback);
this.#observer.observe(this.#element);
this.lastUnfiredFunc = null;
this.renderWhenVisible = this.renderWhenVisible.bind(this);
}
Expand All @@ -66,7 +66,12 @@ export default class VisibilityObserver {
* @returns {boolean} True if the function was executed immediately, false otherwise.
*/
renderWhenVisible(func) {
if (this.isIntersecting) {
if (!this.calledOnce) {
this.calledOnce = true;
this.#observer.observe(this.#element);
window.requestAnimationFrame(func);
return true;
} else if (this.isIntersecting) {
window.requestAnimationFrame(func);
return true;
} else {
Expand Down