-
-
Notifications
You must be signed in to change notification settings - Fork 18
Add disconnectedCallback to prevent duplicate inputs on DOM reconnection #358
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
Conversation
Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## safari #358 +/- ##
=======================================
Coverage 98.20% 98.20%
=======================================
Files 8 8
Lines 223 223
=======================================
Hits 219 219
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
codingjoe
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements proper lifecycle management for the S3FileInput custom element to prevent duplicate hidden inputs and memory leaks when the element is removed and re-added to the DOM.
Key Changes:
- Added guard in
connectedCallbackto prevent re-initialization if already connected - Implemented
disconnectedCallbackto clean up event listeners and DOM elements - Refactored event handlers to use stored bound references for proper cleanup
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| s3file/static/s3file/js/s3file.js | Added lifecycle management with disconnectedCallback, guard logic in connectedCallback, and stored bound handler references for cleanup |
| tests/tests/s3file.test.js | Added tests for disconnectedCallback cleanup and prevention of duplicate inputs on reconnection |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| disconnectedCallback() { | ||
| // Clean up event listeners | ||
| if (this._fileInput) { | ||
| this._fileInput.removeEventListener("change", this._changeHandler) | ||
| } | ||
|
|
||
| if (this.form) { | ||
| this.form.removeEventListener("formdata", this._boundFormDataHandler) | ||
| this.form.removeEventListener("submit", this._boundSubmitHandler) | ||
| this.form.removeEventListener("upload", this._boundUploadHandler) | ||
| } | ||
|
|
||
| // Remove the file input from DOM | ||
| if (this._fileInput && this._fileInput.parentNode) { | ||
| this._fileInput.parentNode.removeChild(this._fileInput) | ||
| } | ||
|
|
||
| // Clear reference to allow reconnection | ||
| this._fileInput = null | ||
| } |
Copilot
AI
Dec 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bound handler references (_changeHandler, _boundFormDataHandler, _boundSubmitHandler, _boundUploadHandler) should be nulled after removing event listeners to prevent memory leaks. These references will persist in memory even after the element is disconnected and the event listeners are removed.
Consider adding the following after line 81:
- Set this._changeHandler = null
- Set this._boundFormDataHandler = null
- Set this._boundSubmitHandler = null
- Set this._boundUploadHandler = null
The
connectedCallbackmethod created new hidden inputs and event listeners on every invocation. When custom elements are removed and re-added to the DOM, this caused duplicate inputs and memory leaks.Changes
connectedCallbackif_fileInputalready existsdisconnectedCallback: Remove event listeners, clean up DOM, and null the_fileInputreference_changeHandler,_boundFormDataHandler,_boundSubmitHandler,_boundUploadHandler) to enable proper cleanupchangeHandler: Use stored reference when replacing submit handlerExample
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.