Skip to content

nabeel-khan/ngx-auto-unsubscribe

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Angular - Auto unsubscribe

Class decorator that will automatically unsubscribe from observables and events when the component destroyed

Installation

npm install ngx-auto-unsubscribe --save

Usage

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

@Component({
  selector: 'inbox'
})
@AutoUnsubscribe(blacklist = [])
export class InboxComponent {
  one: Subscription;
  two: Subscription;
  three;
  
  constructor( private store: Redux, private renderer: Renderer, private element : ElementRef ) {}
  
  ngOnInit() {
    this.one = store.select("data").subscribe(data => // do something);
    this.two = Observable.interval.subscribe(data => // do something);
    this.three = this.renderer.listen(this.element.nativeElement, this.event, e => // do something)
  }
}

Note:

Right now there is a problem with AOT mode. You need to define the ngOnDestroy() method explicitly. Otherwise, it will not work. I hope it will be fixed soon.

About

Class decorator that will automatically unsubscribe from observables and events

Resources

Stars

Watchers

Forks

Packages

No packages published