1.0.1 • Published 5 years ago

subscription-collector v1.0.1

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

SubscriptionCollector

How to use it in Angular

Look at typical component

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})
export class MyComponent implements OnInit, OnDestroy {
  private sc = new SubscriptionCollector(this);
  
  constructor(
    private userService: UserService,
    private postsService: PostsService,
    private route: ActivatedRoute,
  ) { }
  
  ngOnInit() {
    this.sc.collect = this.userSubscription();
  }
  
  ngOnDestroy() { }
  
  private userSubscription(): Subscription {
    return this.route.params.pipe(
      switchMap(params => this.userService.getUser(params.userId),
      switchMap(user => this.postsService.getPosts(user.userId),
    )
      .subscribe()
  }

When you assing new subscription to this.sc.collect SubscriptionCollector take this subscription and add to accumulating subscription under the hood.

When ngOnDestroy calls accumulating subscription unsubscribes.

Implementation of OnDestroy is required.

Why use it? No more constructions like

ngOnDestroy() {
  if (this.myPrettySubscription1) {
    this.myPrettySubscription1.unsubscribe();
  }
  if (this.myPrettySubscription2) {
    this.myPrettySubscription2.unsubscribe();
  }
}
1.0.1

5 years ago

1.0.0

5 years ago