Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2264
Description
This pull request fixes an issue where
sl-popup
would create multiple instances ofautoUpdate
, leading to event listeners accumulating over time when components usingsl-popup
are repeatedly added to and removed from the DOM. This accumulation could cause performance issues and potential memory leaks.Problem
The issue occurs due to the sequence of lifecycle methods and property updates in
sl-popup
. When the component is added to the DOM, the following sequence happens:active: false
.connectedCallback()
is called, which callsstart()
regardless of theactive
state.active
property changes totrue
.updated()
method detects the change inactive
and callsstart()
again.This results in
start()
being called twice without properly cleaning up the previousautoUpdate
, causing multipleautoUpdate
instances to run concurrently.Changes
To fix this issue, the following changes were made:
Modified
connectedCallback()
to only callstart()
ifactive
istrue
.This solution prevents
start()
from being called unnecessarily inconnectedCallback()
when the component is not active, and ensures thatstart()
andstop()
are only called in response to changes in theactive
property.Testing
Writing an automated test to verify this behavior was challenging due to the nature of the event listener accumulation and limitations in accessing internal properties or intercepting event listeners in a testing environment.
Despite these challenges, I performed manual testing in the browser:
sl-popup
(e.g.,sl-select
) from the DOM.resize
andscroll
events.Additionally, I wrote the following test to ensure that the component handles positioning correctly when the active property changes:
This test verifies that:
The component can be activated and deactivated without errors.
Event listeners do not cause issues when active changes.
The component remains stable during property changes and event dispatches.
I welcome any suggestions on how to create an effective automated test for this scenario, but given the constraints, manual testing was used to confirm the fix.
Before fix
After fix