-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Core: Allow a teardown function to be returned from renderToDOM
#18457
Core: Allow a teardown function to be returned from renderToDOM
#18457
Conversation
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 661011f. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this branch ✅ Successfully ran 1 targetSent with 💌 from NxCloud. |
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.
Code LGTM, but we definitely need some tests for this! 👍
@tmeasday can I already create a second draft pr with a fix for angular? |
@kroeder You should be able to telescope off this PR and create a second PR on top of it |
Already working on it and it looks promising! |
@kroeder -- discussed with @shilman and a plan to get this into 6.5 with a minimum of disruptive changes would be as follows:
React (and likely others) should also return a teardown function, at least for docs mode, but we are concerned about making breaking changes especially to the way components/stories are unmounted when changing stories. I would suggest we should be careful for angular in doing that also, which is why I suggest only returning a teardown function in docs mode, at least for now. As part of the 7.0 cycle we plan to look closely at various existing and closed issues around remounting and get some test in place that'll give us confidence to make this change everywhere. |
I added some integration tests. We don't currently have a mechanism for full integration tests with the docs stack (i.e. |
Co-authored-by: Michael Shilman <shilman@users.noreply.github.com>
@tmeasday please update snapshots |
also move getStorybookMetadata method to the bottom so it's used after the definition of computeStorybookMetadata
`title: 'DocumentScreen',` appears twice in the component meta.
Make code snippet consistent with the others on the page. Typically yarn is shown before npm, so this change will make the code snippet consistent with the rest.
Co-authored-by: Michael Shilman <shilman@users.noreply.github.com>
…anup-function-from-rendertodom
renderToDOM
renderToDOM
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. Let's get this working in 7.0 Angular before deciding whether to patch to 6.5
Awesome! Workin on that starting today :) @shilman |
@@ -147,4 +147,6 @@ export async function renderToDOM( | |||
} | |||
|
|||
await renderElement(element, domElement); | |||
|
|||
return () => unmountElement(domElement); |
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.
If we patch this back we should make sure we don't include this change (in fact should we take it out until we've considered this properly?)
Issue: Stories (both in Canvas and when rendered in
modernInlineRender
) had no way to be properly torn down, leading to memory/event listener leaks when browsing around (as noticed in Angular by @kroeder).What I did
Added some stories to highlight the problem to
react-ts
(not sure this is where they should go, and if we should have a way to properly test this @shilman).Notice the following (without the changes to
react/../render.tsx
):The Canvas event listeners eventually do get torn down because we are ultimately rendering a different story to the same DOM element, and React handles this for us.
But
a) it should happen earlier I think and
b) that doesn't happen automatically in Angular as I understand it.
Solution
I allowed
renderToDOM
to return ateardown
function that will be called whenever aStoryRender
is taken off the screen.How to test
See reproduction above. I will also add a bunch of
PreviewWeb
integration tests once we are satisfied this is the right approach.