-
Notifications
You must be signed in to change notification settings - Fork 283
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
[Advanced Select, Dropdown, ...] Memory leak when calling autoInit() repeatedly (e.g. due to ajax) #429
Comments
The code responsible is (Advanced Select as an example, comments by me) // src/plugins/select/index.ts
static autoInit() {
if (!window.$hsSelectCollection) window.$hsSelectCollection = [];
document
.querySelectorAll('[data-hs-select]:not(.--prevent-on-load-init)')
.forEach((el: HTMLElement) => {
if (
!window.$hsSelectCollection.find(
(elC) => (elC?.element?.el as HTMLElement) === el,
)
) {
const data = el.getAttribute('data-hs-select');
const options: ISelectOptions = data ? JSON.parse(data) : {};
new HSSelect(el, options);
}
});
// this code block is basically always run after the first autoInit(), and registers the same event listeners over and over again.
if (window.$hsSelectCollection) {
window.addEventListener('click', (evt) => {
const evtTarget = evt.target;
HSSelect.closeCurrentlyOpened(evtTarget as HTMLElement);
});
document.addEventListener('keydown', (evt) =>
HSSelect.accessibility(evt),
);
}
} From what I can tell, a quick fix would look like this static autoInit() {
if (!window.$hsSelectCollection) {
window.$hsSelectCollection = [];
window.addEventListener('click', (evt) => {
const evtTarget = evt.target;
HSSelect.closeCurrentlyOpened(evtTarget as HTMLElement);
});
document.addEventListener('keydown', (evt) =>
HSSelect.accessibility(evt),
);
}
document
.querySelectorAll('[data-hs-select]:not(.--prevent-on-load-init)')
.forEach((el: HTMLElement) => {
if (
!window.$hsSelectCollection.find(
(elC) => (elC?.element?.el as HTMLElement) === el,
)
) {
const data = el.getAttribute('data-hs-select');
const options: ISelectOptions = data ? JSON.parse(data) : {};
new HSSelect(el, options);
}
});
} Sorry, I can't provide more right now. I hope it's enough for now to get this out of the door. |
Here a PR which addresses this issue. |
@oliverhaas Hi, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Summary
Calling autoInit() repeatedly will lead to a memory leak
Steps to Reproduce
Demo Link
None
Expected Behavior
No response
Actual Behavior
No response
Screenshots
No response
The text was updated successfully, but these errors were encountered: