0

Assume that I have some subscriber implementation:

// SafeSubscriber extension doesn't matter here - the problem exists for simple Subscriber implementations too
class ParticularSubscriber<T> extends SafeSubscriber<T> {

    private Subscriber<T> actual;

    public ParticularSubscriber() {
        super(Subscribers.create(System.out::println));
    }
}

Then, I need to create an observer that performs some extra configurations depending on the subscriber passed. Naive approach first:

Observable<String> o = Observable.<String>create(subscriber -> {
    if(subscriber instanceof ParticularSubscriber) {
        // do some extra logic here.
    }
});

It is successfully works if we directly subscribe to the Observable o:

o.subscribe(new ParticularSubscriber<>());

But things changed if we apply some operations on it:

o.map(str -> str + 1)
        .filter(str -> str != "")
        .subscribe(new ParticularSubscriber<>());

In the latter case, some wrapper is passed to OnSubscribe. Deeply inside this wrapper there is my initial ParticularSubscriber, but I have no ability to get to it.

Is there any way to access initially passed subscriber in the observer's callbacks?

skapral
  • 1,128
  • 9
  • 26
  • That should not be possible otherwise one could call target subscriber's methods directly, skipping logic from downstream operators. Instead you can create different observables based on subscriber type: it could be method `getObservable(Subscriber targetSubs) { if(targetSubs instanceOf ParticularSubscriber) return observable1 else return observable2` – m.ostroverkhov Jul 16 '16 at 06:18
  • I thought the same way first - it is right that initial subscriber is encapsulated behind. Let me reformulate my question a bit: is there a way to pass some meta-information describing subscriber's part to the already created observable? – skapral Jul 17 '16 at 10:07

0 Answers0