I'm implementing a byte message deserializer, which would dispatch deserialized messages on a dispatcher interface and return an observable of all Throwables thrown, so that client code can handle the errors.
A sketch of the prototype of the method doing that:
Observable<Throwable> dispatchDeserializedMessages(Observable<byte[]>, Dispatcher)
Now as of recent I am familiar with Subject<T, R>, which would fit here perfectly, e.g.
Subject<byte[], Throwable> dispatchDeserializedMessages(Dispatcher)
But there are no convenience methods like create() which could easily delegate to an observer and an observable. All concrete implementations unify T with R, so there's no way I could use one of those.
So my concrete question: Is there a way I can instantiate a suitable Subject<byte[], Throwable> which delegates to an Observer and Observable? Is there any other way I can create such a Subject without having to implement (in the sense of having to delegate each implemented method by hand) the whole of Subject, Observable and Observer?
Switching to Subject-based API might not be the best idea because you change a potentially cold API into a mandatory hot API. In your original design, the consumer of the
Throwablesequence would assume when it subscribes, theObservable<byte[]>gets subscribed to too.Otherwise, I have a blog series about creating
Subjects but you can't avoid the heavy lifting with them.