Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

347
Views
Angular RxJS: ¿Necesito hacer .takeUntil(this.destroy$) si estoy usando una canalización asíncrona?

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(); } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 {} }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!