I have a function that returns observable that automatically unsubscribe when some internal thing happens using a Subject. Here is a simple version that shows the issue:
const subject = new Subject();
const source = fromEvent(document.querySelector("h1"), "click").pipe(
takeUntil(subject)
);
fromEvent(document.querySelector("p"), "click").subscribe(() => {
subject.next();
});
The issue is that if someone subscribes source
and uses higher-order observable, it will not subscribe from the inner observable when the Subject
emits:
source.pipe(mergeMap(() => interval(1000))).subscribe(x => console.log(x));
There is a way to solve this issue without forcing the consumer to call unsubscribe
?