take-until-destroy
Version:
A simple way to unsubscribe from an RxJs stream in Angular (2+) when the component is destroyed
37 lines (36 loc) • 974 B
TypeScript
import { MonoTypeOperatorFunction } from 'rxjs';
/**
* An RxJs operator which takes an Angular class instance as a parameter. When the component is destroyed, the stream will be
* unsubscribed from.
*
* <b>Important:</b> Make sure you have either {@link Destroyable @Destroyable} decorating your class like so:
* <pre><code>
* @Destroyable
* @Component({
* ...
* })
* export class ExampleComponent {}
* </code></pre>
*
* or that your class implements OnDestroy like so:
* <pre><code>
* @Component({
* ...
* })
* export class ExampleComponent implements OnDestroy {
* ngOnDestroy () {}
* }
* </code></pre>
*
* @example
* <pre><code>
* ngOnInit() {
* this.randomObservable
* .pipe(takeUntilDestroy(this))
* .subscribe((val) => console.log(val))
* }
* </code></pre>
* @param {Object} target (normally `this`)
* @returns {Observable<T>}
*/
export declare const takeUntilDestroy: <T>(target: any) => MonoTypeOperatorFunction<T>;