NetanelBasal / ngx-auto-unsubscribe

Class decorator that will automatically unsubscribe from observables

Home Page:https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Angular - Auto Unsubscribe For Pros

npm Build Status Build Status npm Awesome

For Angular 9+, use until-destroy

Class decorator that will automatically unsubscribe from observable subscriptions when the component is destroyed

Installation

npm install ngx-auto-unsubscribe --save

Usage

import { AutoUnsubscribe } from "ngx-auto-unsubscribe";

@AutoUnsubscribe()
@Component({
  selector: 'inbox'
})
export class InboxComponent {
  one: Subscription;
  two: Subscription;

  constructor( private store: Store<any>, private element : ElementRef ) {}

  ngOnInit() {
    this.one = store.select("data").subscribe(data => // do something);
    this.two = Observable.interval.subscribe(data => // do something);
  }

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

Options

Option Description Default Value
arrayName unsubscribe from subscriptions only in specified array ''
blackList an array of properties to exclude []
event a name of event callback to execute on ngOnDestroy

Note: blackList is ignored if arrayName is specified.

Similar projects

You can also use https://github.com/NetanelBasal/ngx-take-until-destroy.

About

Class decorator that will automatically unsubscribe from observables

https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88

License:MIT License


Languages

Language:TypeScript 100.0%