-
Notifications
You must be signed in to change notification settings - Fork 26.9k
feat(router): add controls for route cleanup #65991
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
base: main
Are you sure you want to change the base?
Conversation
454e237 to
d522cf7
Compare
d522cf7 to
14415ed
Compare
This commit introduces a new feature to automatically destroy `EnvironmentInjector`s associated with routes that are no longer active or stored. This helps in managing memory by releasing resources held by unused injectors.
Key changes:
- Added `shouldDestroyInjector` and `retrieveStoredRouteHandles` to `RouteReuseStrategy`.
- Introduced `withExperimentalAutoCleanupInjectors` provider function to opt-in to this feature.
- Implemented a "mark and sweep" algorithm to identify and destroy unused injectors.
- Added a dev mode warning if the strategy implements destruction methods but the feature is not enabled.
- Added integration tests to verify the behavior.
This commit also `destroyDetachedRouteHandle`, a standalone function that allows developers to explicitly destroy the component associated with a `DetachedRouteHandle`.
We chose to implement this as a standalone function rather than adding a `destroy()` method to the `DetachedRouteHandle` type to avoid breaking changes. Currently, `DetachedRouteHandle` is defined as `{}`, and many applications mock it as such. Adding a required method to the interface would break these existing tests and usages.
By using a standalone function, we can safely cast the handle to its internal type and invoke destruction without altering the public contract of the handle itself.
This feature is particularly useful for `RouteReuseStrategy` implementations that need to drop stored handles and ensure the associated components are properly cleaned up to prevent memory leaks.
resolves angular#27290
resolves angular#37095
14415ed to
09a537c
Compare
|
Deployed adev-preview for 726fc25 to: https://ng-dev-previews-fw--pr-angular-angular-65991-adev-prev-tzo73rw0.web.app Note: As new commits are pushed to this pull request, this link is updated after the preview is rebuilt. |
| * This function should be used when a `RouteReuseStrategy` decides to drop a stored handle | ||
| * and wants to ensure that the component is destroyed. |
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.
I'm wondering if we can enforce that somehow (avoid calling this function in other places in the code).
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.
You can call this function from anywhere at any time. This is not called automatically by the router but ad-hoc if/when the user decides to they don't need the handle anymore.
This commit introduces a new feature to automatically destroy
EnvironmentInjectors associated with routes that are no longer active or stored. This helps in managing memory by releasing resources held by unused injectors.Key changes:
shouldDestroyInjectorandretrieveStoredRouteHandlestoRouteReuseStrategy.withExperimentalAutoCleanupInjectorsprovider function to opt-in to this feature.This commit also
destroyDetachedRouteHandle, a standalone function that allows developers to explicitly destroy the component associated with aDetachedRouteHandle.We chose to implement this as a standalone function rather than adding a
destroy()method to theDetachedRouteHandletype to avoid breaking changes. Currently,DetachedRouteHandleis defined as{}, and many applications mock it as such. Adding a required method to the interface would break these existing tests and usages.By using a standalone function, we can safely cast the handle to its internal type and invoke destruction without altering the public contract of the handle itself.
This feature is particularly useful for
RouteReuseStrategyimplementations that need to drop stored handles and ensure the associated components are properly cleaned up to prevent memory leaks.resolves #27290
resolves #37095
resolves #15873