Skip to content

Files

Latest commit

 

History

History
55 lines (44 loc) · 1.23 KB

prefer-takeuntil.md

File metadata and controls

55 lines (44 loc) · 1.23 KB

Pattern: Missing use of takeUntil

Issue: -

Description

This rule effects failures if subscribe is called within a component and the takeUntil-destroyed pattern is not used.

Examples of incorrect code for this rule:

import { Component, OnInit } from "@angular/core";
import { switchMap } from "rxjs/operators";

@Component({
  selector: "some-component"
})
class SomeComponent implements OnInit {
  // ...
  ngOnInit() {
    this.values.pipe(
      switchMap((value) => something(value))
    ).subscribe();
  }
}

Examples of correct code for this rule:

import { Component, OnDestroy, OnInit } from "@angular/core";
import { switchMap } from "rxjs/operators";

@Component({
  selector: "some-component"
})
class SomeComponent implements OnDestroy, OnInit {
  private destroy = new Subject<void>();
  // ...
  ngOnInit() {
    this.values.pipe(
      switchMap((value) => something(value)),
      takeUntil(this.destroy)
    ).subscribe();
  }
  ngOnDestroy() {
    this.destroy.next();
    this.destroy.complete();
  }
}

Further Reading