angularrxjsobservablesubject

foo.subscribe with bar not always working


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.


Solution

  • 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(),
    })