I'm trying to return an Observable
that is created asynchronously in a callback:
const mkAsync = (observer, delay) =>
setTimeout(() => Observable.of('some result').subscribe(observer), delay)
const create = arg => {
const ret = new Subject()
mkAsync(ret, arg)
return ret
}
Therefore I use a Subject
as a unicast proxy which is subscribed to the underlying Observable
in the callback.
The problem I have with this solution is that when I unsubscribe from the Subject
's subsrciption the unsubscribe isn't forwarded to the underlying Observable
. Looks like I need some type of refcounting to make the Subject
unsubscribe when there are no more subscribers, but I wasn't able to figure it out when using it in this kind of imperative callback style.
I have to keep the mkAsync
a void and am looking for an alternative implementation.
Is that the right way to do it? Is there an alternative solution to using a Subject
?
How do I make sure that the created Observable
is cancelled (unsubscribe
is called on the Subscription
) when the Subject is unsubscribed from?