-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
feat(auto-init): return initialized components #1333
feat(auto-init): return initialized components #1333
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
I signed it! |
CLAs look good, thanks! |
What exactly is the reason for wanting this change? Prevent memory leaks? Keep track of changes somehow? As far as I can tell, even in the issue example, there is no compelling use-case for this to be needed. When the nodes are removed, as long as they have no references left in the JS the related things will be cleaned up. We fell for a little trap of storing references internally in MDL's auto init system and it was only problematic in the end. I'd rather MCW avoids allowing for that pitfall without tangible gain on having it included. |
Keep a reference of the initialized MD components in an wrapper component so we can destroy them as advised in Framework Integration guide. It even cites the custom element example:
As a concrete example see this pen. It wraps the Getting Started demo in a custom element. The MD components are initialized with The alternative would be initialize and store a reference of each MD component to be destroyed in
So is not necessary to call |
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
Codecov Report
@@ Coverage Diff @@
## master #1333 +/- ##
==========================================
+ Coverage 98.66% 99.65% +0.99%
==========================================
Files 98 78 -20
Lines 4202 3522 -680
Branches 534 434 -100
==========================================
- Hits 4146 3510 -636
+ Misses 56 12 -44
Continue to review full report at Codecov.
|
I confirm |
@Garbee Can you update the thread on what the pitfalls in MDL were specifically? |
The original MDL component handler stored the initialized elements in an internal cache with no way to remove them. So if you deleted the node in the document, the garbage collector would never destroy it since the init system had a reference. While this directly isn't doing that, the change does open the gateway for that very easy mistake to make to be done by external parties. Destructing should be optional anyways. When your intent is to delete a node, just delete it and let the garbage collector do the rest. Each component should be engineered so that when it is deleted the garbage collector in browsers can clean up any unneeded things at that point. Also, if you need to destroy a component, you should be capable of calling the destruction on the node itself without needing to keep the references from auto initialization. |
That's fair enough, but MDC Web isn't storing any references to anything from auto init, so it would have to be a user retaining the reference and not destroying it when intended. You pointed so much out in your comment, but I think we can document and provide functionality in a way that would make it clear that if you store a reference to all of your MDC Web components as a result of using auto init, you'll need to manually destroy them like you would when initializing them and keeping references to them individually. As far as deleting a dom node, there are some components that listen for resize events on the |
It's user responsibility to handle such situations. This is the case for JavaScript in general, not MDC specific. Even without this feature is currently possible to have leaks with the MDC API as in the example below:
This contradicts the documentation: "When the wrapper component is destroyed (e.g. it is unbound and detached from the DOM), call mdcComponent.destroy() to clean up the MDC-Web component."
I can't see in a clean way to do that with autoInit It would need to call
|
Sorry this PR lapsed for so long. The changeset looks reasonable to me and I agree with the thoughts expressed by @amsheehan and @blikblum. The CLA was signed and it looks like this can still be merged without conflict, so I'm going to go ahead with it. |
Make
autoInit
return initialized components.Fixes #1329