-
Notifications
You must be signed in to change notification settings - Fork 13.3k
feat(angular): add DestroyRef support to subscribeWithPriority #31348
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||||||||||||||||||
| import { DOCUMENT } from '@angular/common'; | ||||||||||||||||||||||||||||||||||
| import { NgZone, Inject, Injectable } from '@angular/core'; | ||||||||||||||||||||||||||||||||||
| import type { DestroyRef } from '@angular/core'; | ||||||||||||||||||||||||||||||||||
| import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||||||||||||||||||||||||||||||||||
| import { getPlatforms, isPlatform } from '@ionic/core/components'; | ||||||||||||||||||||||||||||||||||
| import type { BackButtonEventDetail, KeyboardEventDetail, Platforms } from '@ionic/core/components'; | ||||||||||||||||||||||||||||||||||
| import { Subscription, Subject } from 'rxjs'; | ||||||||||||||||||||||||||||||||||
|
|
@@ -9,7 +11,13 @@ import { Subscription, Subject } from 'rxjs'; | |||||||||||||||||||||||||||||||||
| export interface BackButtonEmitter extends Subject<BackButtonEventDetail> { | ||||||||||||||||||||||||||||||||||
| subscribeWithPriority( | ||||||||||||||||||||||||||||||||||
| priority: number, | ||||||||||||||||||||||||||||||||||
| callback: (processNextHandler: () => void) => Promise<any> | void | ||||||||||||||||||||||||||||||||||
| callback: (processNextHandler: () => void) => Promise<any> | void, | ||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Pass a component's `DestroyRef` to have the subscription torn down with that | ||||||||||||||||||||||||||||||||||
| * component. Without it the subscription lives for the lifetime of the injector, | ||||||||||||||||||||||||||||||||||
| * which for a root-provided `Platform` means the lifetime of the application. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| destroyRef?: DestroyRef | ||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this move to I'd also trim the last two sentences. They explain why the change exists rather than what the parameter does, and they're already in the commit body and the PR description, so that's three copies to keep in sync. Up to you on that one. |
||||||||||||||||||||||||||||||||||
| ): Subscription; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -62,8 +70,10 @@ export class Platform { | |||||||||||||||||||||||||||||||||
| constructor(@Inject(DOCUMENT) private doc: any, zone: NgZone) { | ||||||||||||||||||||||||||||||||||
| zone.run(() => { | ||||||||||||||||||||||||||||||||||
| this.win = doc.defaultView; | ||||||||||||||||||||||||||||||||||
| this.backButton.subscribeWithPriority = function (priority, callback) { | ||||||||||||||||||||||||||||||||||
| return this.subscribe((ev) => { | ||||||||||||||||||||||||||||||||||
| this.backButton.subscribeWithPriority = function (priority, callback, destroyRef) { | ||||||||||||||||||||||||||||||||||
| const source$ = destroyRef ? this.pipe(takeUntilDestroyed(destroyRef)) : this; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return source$.subscribe((ev) => { | ||||||||||||||||||||||||||||||||||
| return ev.register(priority, (processNextHandler) => zone.run(() => callback(processNextHandler))); | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+73
to
79
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Using The operator is only registering an Worth a line in the docs either way: passing an already-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.
Passing a
DestroyRefties this tongOnDestroytiming, and withion-router-outletthat only fires when a page is popped. Pushing from A to B leaves A in the DOM, so A's handler stays subscribed while B is on screen. That's the case your description opens with, and I don't think this would fix it.What do you see when you push a few pages deep with this applied? I haven't run your app so I could be wrong about how it plays out in practice.
Our Angular lifecycle docs point people at
ionViewWillLeavefor unsubscribing for this reason. If that's the right hook, then "torn down with that component" is going to read as covering navigate-away when it doesn't.