After searching online, I see that people generally use one or the other and not both. Is it possible to use both? Is it good or bad practice?
Edit
I am not asking what take(1) or takeUntil is used for. Rather I am asking if it is possible to have both effects of take(1) and takeUntil. I understand that take(1) will take only the first value and end the subscription. However, it will not end the subscription if I never receive the event. I also understand that this use of takeUntil will always clean up the subscription when the component is no longer active as long as I trigger the unsubscribeSubject in the destroy. However, it will not free up that subscription after I receive the first value and will exist for the entire time the component is active.
What I want is a way to free the subscription after the first value as well as prevent memory leaks when the component is no longer active if not value is received. Specifically in a scenario where you have many subscriptions. This is because when you have many subscriptions, it is handy to have a single subject that can clean up all your subscriptions when the component is no longer needed.
ngAfterViewInit(){
//if first value comes, we process it and unsubscribe
this.myService.GetOneTimeObservable()
.pipe(take(1))
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(this.fooOT.bind(this))
//other subscriptions (assume many)
this.myService.GetLongLifeObservable1()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(this.foo1.bind(this))
this.myService.GetLongLifeObservable2()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(this.foo2.bind(this))
this.myService.GetLongLifeObservable3()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(this.foo3.bind(this))
}
ngOnDestroy(){
//Ideally cleans all subscriptions, including the OneTime if no value is received
this.unsubscribeSubject.next();
this.unsubscribeSubject.complete();
}