0

Imagine we have foo$ and bar$ and both of them are BehaviorSubjects.

I have a bug in angular that sometimes in some cases foo$.subscribe(bar$) works fine, in most casses actually.

But sometimes I have a bug and its not working until I do foo$.subscribe(foo => bar$.next(foo)), and even worse it seems like it works the first time and then the second time it dose not work.

I experienced this bug sometimes but still rearly. Not sure even how to make a proper minimal reproduction. I would like to understand what makes this causing the bug? And is the angular team aware of it? I didn't see anywhere so far anyone posting about it.


Have checked if it solves anything by removing zonejs and it dose not.

4
  • 3
    Hi, without a repro this is very hard to investigate. Commented Mar 20 at 12:37
  • I tougth maybe its a very very general issue which I'm just not aware of. I'll try to make a repo. Commented Mar 20 at 12:38
  • @MatthieuRiegler here is a minimal repo stackblitz.com/edit/… You can see in the console hello... is logged once when doing it the first way, and doing it the second way its logged as many times as you click the button. Commented Mar 20 at 13:10
  • After looking it however myself I see the issue is actully in the this.onDestroy$.next(); call. but still strange that it works fine when changing the line of code. Commented Mar 20 at 13:12

1 Answer 1

2

When you do this.foo$.pipe(takeUntil(this.onDestroy$)).subscribe(this.bar$);

The bar$ subject actually completes when the foo$ subscription completes (because if the takeUntil).

This is become subscribe(this.bar$) implies

foo$.subscribe({
  next: value => bar$.next(value),
  error: error => bar$.error(error),
  complete: () => bar$.complete(),
})
Sign up to request clarification or add additional context in comments.

2 Comments

So to clarify, the issue is that foo$.subscribe(foo => bar$.next(foo)) and foo$.subscribe(bar$) are in fact different. I tougth its just the shortform. So it makes sense that it will complete bar if foo completes. I think that answers all my questions. Thank you Matthieu.
One more question, if foo$ throws an error and I used foo$.subscribe(foo => bar$.next(foo)) then if I catch the error when piping bar$ I won't see the error? Is there any shorthand to forward the next as well as the error but not complete it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.