Skip to content

Commit

Permalink
Adjust the tooltip performance (see #7687)
Browse files Browse the repository at this point in the history
Description
-----------

Fixes #7685

PR aims to change how the tooltip controller works

1. Only observing tooltip changes
2. Using Set instead of an array for faster access (Unsure if we could use WeakSet due to garbage collection)

/cc @m-vo can be tested on the current `5.x` with https://github.com/zoglo/contao/tree/fix/stimulus-tooltip

Commits
-------

e9b380b8 Adjust tooltip performance
f9ce10cc Rebuild the assets / trigger the actions
  • Loading branch information
zoglo authored Nov 21, 2024
1 parent 544d485 commit 037a951
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
21 changes: 9 additions & 12 deletions assets/controllers/tooltip-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
connect() {
this.initialized = [];
this.initialized = new Set();
this.createTooltip();
this.setup();
this.addObserver();
Expand All @@ -13,13 +13,11 @@ export default class extends Controller {
this.observer.disconnect();
}

Object.keys(this.selectors).forEach(selector => {
document.querySelectorAll(selector).forEach(el => {
if (this.initialized.includes(el)) {
document.removeEventListener('touchstart', el.globalTouchstartListener);
}
});
});
this.initialized.forEach(el => {
document.removeEventListener('touchstart', el.globalTouchstartListener);
})

this.initialized.clear();
}

createTooltip() {
Expand Down Expand Up @@ -51,8 +49,8 @@ export default class extends Controller {

Object.entries(this.selectors).forEach(([selector, options]) => {
document.querySelectorAll(selector).forEach(el => {
if (!this.initialized.includes(el)) {
this.initialized.push(el);
if (!this.initialized.has(el)) {
this.initialized.add(el);
this.init(el, options);
}
});
Expand Down Expand Up @@ -160,8 +158,7 @@ export default class extends Controller {
});
})

this.observer.observe(document, {
attributes: false,
this.observer.observe(this.element, {
childList: true,
subtree: true,
});
Expand Down
4 changes: 2 additions & 2 deletions public/backend.0f61cbcb.js → public/backend.3db086cc.js

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 1 addition & 1 deletion public/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"entrypoints": {
"backend": {
"js": [
"/bundles/contaocore/backend.0f61cbcb.js"
"/bundles/contaocore/backend.3db086cc.js"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"backend.js": "/bundles/contaocore/backend.0f61cbcb.js"
"backend.js": "/bundles/contaocore/backend.3db086cc.js"
}

0 comments on commit 037a951

Please sign in to comment.