Allow detached autosize DOM elements to be garbage collected #410
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.
The proper way is to use
autosize.destroy
when an autosize instance is no longer needed.However, sometimes it may be hard to explicitly destroy existing widget instances. See for this issue from another widget where the DOM element or DOM container element is simply removed. For example, try creating a simple demo page with a div container and some textareas with autosize, then remove the div container from the DOM. The
assignedElements
still contains all entries, and the memory tab of the dev tools shows many detached DOM elements that weren't garbage collected.This happens because of 2 reasons: (a) The
assignedElements
map keeps a hard reference to all DOM textareas and the autosize instances; (b) each instances adds a globalresize
event listener to the window, and the event listener captures the textarea DOM element with a closure.There is a simple change that solves these issues: