Skip to content

The decorator that help to unsubscribe from observables automatically

License

Notifications You must be signed in to change notification settings

Nillcon248/auto-unsubscribe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Angular - Auto unsubscribe decorator 🦄

npm npm Build status

Installation

npm i ngx-auto-unsubscribe-decorator

Idea 💡

This library has created for removing useless code with manually unsubsribes, for this one was create decorator that work with all types of subscriptions, you can wrap observable parameter, method that return observable or method that return subscription and don't think about memory leak.

Examples 🧪

Work with parameters

export class UserComponent implements OnInit {
  @AutoUnsubscribe() // <-- Should be on the target parameter
  private userData$ = new BehaviorSubject(<-Some data->);

  public ngOnInit(): void {
    // After ngOnDestroy this subscription will unsubscribe
    this.userData$.subscribe();

    // You can override parameter, it will unsubscribe too
    this.userData$ = new Subject();
    this.userData$.subscribe();
  }
}

Work with methods

export class UserComponent implements OnInit {

  public ngOnInit(): void {
    this.getUserData$.subscribe();

    this.initUserDataSubscription();
  }

  @AutoUnsubscribe() // <-- Should be on the target method
  public getUserData$(): BehaviorSubject {
    return new BehaviorSubject(<-Some data->);
  }

  @AutoUnsubscribe() // <-- Should be on the target method
  public initUserDataSubscription(): BehaviorSubject {
    return new BehaviorSubject(<-Some data->).subscribe();
  }
}

About

The decorator that help to unsubscribe from observables automatically

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •