Si tengo un elemento en mi plantilla que usa el enlace de propiedad, vinculado a un observable, usando una canalización async para asegurar que se da de baja automáticamente... ¿entonces sería innecesario usar takeUntil(this.destroy$) ?
Fragmento de plantilla:
<input name="SomeElement" [ngModel]="boundObservable$ | async"> </input>Componente:
export class MyComponent implements OnDestroy { destroy$: Subject<any> = new Subject<any>(); boundObservable$ = this.sourceObservable$.pipe( takeUntil(this.destroy$), map(this.transformData) ); ngOnDestroy() { this.destroy$.next(); this.destroy$.complete(); } }TL; DR: no es necesario porque la tubería async lo hace automáticamente cuando el componente se destruye.
Los detalles
Como se menciona en documentos angulares :
La tubería asíncrona se suscribe a un Observable o Promise y devuelve el último valor que ha emitido. Cuando se emite un nuevo valor, la canalización asíncrona marca el componente para verificar los cambios. Cuando el componente se destruye, la canalización asíncrona cancela la suscripción automáticamente para evitar posibles fugas de memoria.
Aquí está el código fuente de cómo lo maneja la tubería async :
// Create Subscription: _subscribe is called from the pipe's `transform` function: private _subscribe(obj: Subscribable<any>|Promise<any>|EventEmitter<any>): void { this._obj = obj; this._strategy = this._selectStrategy(obj); this._subscription = this._strategy.createSubscription( obj, (value: Object) => this._updateLatestValue(obj, value)); } // Dispose Subscription: ngOnDestroy(): void { if (this._subscription) { this._dispose(); } } private _dispose(): void { this._strategy.dispose(this._subscription!); this._latestValue = null; this._subscription = null; this._obj = null; }Donde la estrategia utilizada es una de las dos clases que implementan la interfaz SubscriptionStrategy :
interface SubscriptionStrategy { createSubscription(async: Subscribable<any>|Promise<any>, updateLatestValue: any): Unsubscribable |Promise<any>; dispose(subscription: Unsubscribable|Promise<any>): void; onDestroy(subscription: Unsubscribable|Promise<any>): void; } class SubscribableStrategy implements SubscriptionStrategy { createSubscription(async: Subscribable<any>, updateLatestValue: any): Unsubscribable { return async.subscribe({ next: updateLatestValue, error: (e: any) => { throw e; } }); } dispose(subscription: Unsubscribable): void { subscription.unsubscribe(); } onDestroy(subscription: Unsubscribable): void { subscription.unsubscribe(); } } class PromiseStrategy implements SubscriptionStrategy { createSubscription(async: Promise<any>, updateLatestValue: (v: any) => any): Promise<any> { return async.then(updateLatestValue, e => { throw e; }); } dispose(subscription: Promise<any>): void {} onDestroy(subscription: Promise<any>): void {} }