fix(angular): allow back button subscriptions to be tied to a DestroyRef - #31348
Open
MaximBelov wants to merge 1 commit into
Open
fix(angular): allow back button subscriptions to be tied to a DestroyRef#31348MaximBelov wants to merge 1 commit into
MaximBelov wants to merge 1 commit into
Conversation
subscribeWithPriority had no way to stop listening. The Platform it lives on is provided in root, so a subscription taken in a component outlives that component and keeps firing its callback after the component is gone -- for the lifetime of the application. Adds an optional destroyRef parameter. When passed, the stream is piped through takeUntilDestroyed so the subscription ends with the component that opened it. Omitted, behaviour is byte-for-byte what it was. takeUntilDestroyed is given an explicit DestroyRef rather than relying on an injection context, which is what makes it usable from the assignment inside the zone.run callback.
|
@MaximBelov is attempting to deploy a commit to the Ionic Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue number: resolves #
What is the current behavior?
BackButtonEmitter.subscribeWithPrioritygives you no way to stop listening.PlatformisprovidedIn: 'root', sobackButtonis a single application-lifetimeSubject. A component that registers a handler:keeps that handler registered after it is destroyed. The callback keeps running — against a destroyed component — for as long as the app is alive, and every navigation to that page adds another one. The only way out today is to hold the
Subscriptionand unsubscribe by hand inngOnDestroy, which is easy to forget and impossible to notice going wrong: nothing errors, the handler just quietly still runs.What is the new behavior?
subscribeWithPrioritytakes an optional third argument, aDestroyRef. When it is passed, the stream is piped throughtakeUntilDestroyed, so the subscription ends with the component that opened it:Omit it and behaviour is byte-for-byte what it is today, so nothing existing changes.
takeUntilDestroyedis handed an explicitDestroyRefrather than relying on an injection context — that is what makes it usable from the assignment inside thezone.runcallback, which is not one.Does this introduce a breaking change?
The parameter is optional and the no-argument path is unchanged.
takeUntilDestroyedneeds Angular 16, and this package already declares@angular/core: >=16.0.0, so it is within the supported range. It is the first use of@angular/core/rxjs-interopin the repository.Other information
On testing.
packages/angular'stestscript isecho 'angular no tests yet'and there are no unit specs in the package, so there is nothing here to extend —test/holds e2e apps. I did not add a unit-test harness to the package, since introducing one is a much larger change than this fix and not mine to decide. What I did instead:--strictagainst real@angular/coreandrxjstypings, including thethis.pipe(...) : thisunion thatsubscribeis called on.fesm2022output withpatch-package, which is what prompted submitting it properly.Happy to shape it differently — an overload instead of an optional parameter, or a separate method — if either fits the API better.