I have an API which exposes an IObservable
Status. But this status depends on an underlying observable source which has to be initialised via Init
.
What I'd like to do is protect the users from having to do things in the right order: as it currently stands, if they try to subscribe to the Status
before performing an Init
, they get an exception because they source is not initialised.
So I had the genius idea of using a Subject
to decouple the two: the external user subscribing to my Status
is just subscribing to the Subject, then when they call Init
, I subscribe to the underlying service using my Subject.
The idea in code
private ISubject<bool> _StatusSubject = new Subject<bool>();
public IObservable<bool> Status { get { return _StatusSubject; } }
public void Init()
{
_Connection = new Connection();
Underlying.GetDeferredObservable(_Connection).Subscribe(_StatusSubject);
}
However, from tests on a dummy project, the problem is that the initialisation 'wakes up' my underlying Observable by subscribing the Subject to it, even if nobody has yet subscribed to the subject. That's something I'd like to avoid if possible, but I'm not sure how...
(I'm also mindful of the received wisdom that "the general rule is that if you're using a subject then you're doing something wrong")