1.1.0 โ€ข Published 2 years ago

ngx-auto-unsubscribe-decorator v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Angular - Auto unsubscribe decorator ๐Ÿฆ„

npm npm Build status

Installation

npm i ngx-auto-unsubscribe-decorator

Idea ๐Ÿ’ก

This library has been created for removing useless code with manually unsubscribes, for this one was created decorator that works with all types of subscriptions, you can wrap observable parameter, a method that returns observable or a method that returns subscription and doesn'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();
  }
}

Demo

See the Example 1 Example 2 on Stackblitz

1.1.0

2 years ago

0.1.0

2 years ago

1.0.0

2 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago