Skip to content
🦊 RxJS operator that unsubscribe from observables on destroy
TypeScript JavaScript HTML
Branch: master
Clone or download
Latest commit fbfcb04 Jan 1, 2020

README.md

🦁 Unsubscribe For Pros

A neat way to unsubscribe from observables when the component destroyed

Use with Ivy

npm install @ngneat/until-destroy
# Or if you use yarn
yarn add @ngneat/until-destroy
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';

@UntilDestroy()
@Component({})
export class InboxComponent {
  ngOnInit() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe();
  }
}

You can set the checkProperties option to true if you want to unsubscribe from subscriptions properties automatically:

@UntilDestroy({ checkProperties: true })
@Component({})
export class HomeComponent {
  // We'll dispose it on destroy
  subscription = fromEvent(document, 'mousemove').subscribe();
}

You can set the arrayName property if you want to unsubscribe from each subscription in the specified array.

@UntilDestroy({ arrayName: 'subscriptions' })
@Component({})
export class HomeComponent {
  subscriptions = [
    fromEvent(document, 'click').subscribe(),
    fromEvent(document, 'mousemove').subscribe()
  ];

  // You can still use the opertator
  ngOnInit() {
    interval(1000).pipe(untilDestroyed(this));
  }
}

Use with Non-Singleton Services

@UntilDestroy()
@Injectable()
export class InboxService {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe();
  }
}

@Component({
  providers: [InboxService]
})
export class InboxComponent {
  constructor(inboxService: InboxService) {}
}

All options, described above, are also applicable to providers.

Use with View Engine

npm install ngx-take-until-destroy
# Or if you use yarn
yarn add ngx-take-until-destroy
import { untilDestroyed } from 'ngx-take-until-destroy';

@Component({})
export class InboxComponent implements OnDestroy {
  ngOnInit() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe(val => console.log(val));
  }

  // This method must be present, even if empty.
  ngOnDestroy() {
    // To protect you, we'll throw an error if it doesn't exist.
  }
}

Use with Any Class

import { untilDestroyed } from 'ngx-take-until-destroy';

export class Widget {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this, 'destroy'))
      .subscribe(console.log);
  }

  // The name needs to be the same as the second parameter
  destroy() {}
}

Migration from View Engine to Ivy

To make it easier for you to migrate, we've built a script that will update the imports path, and add the decorator for you. You need to run it manually on your project.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Netanel Basal
Netanel Basal

πŸ’» πŸ“– πŸ€”
Artur Androsovych
Artur Androsovych

πŸ’» πŸ’‘ πŸ€” πŸš‡
Krzysztof Karol
Krzysztof Karol

πŸ–‹
Alex Malkevich
Alex Malkevich

πŸ’»
Khaled Shaaban
Khaled Shaaban

πŸ’»
kmathy
kmathy

πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

You can’t perform that action at this time.