Skip to content

Instantly share code, notes, and snippets.

@mgp
Created March 3, 2016 19:15
Show Gist options
  • Save mgp/bfbd37e6262456d81603 to your computer and use it in GitHub Desktop.
Save mgp/bfbd37e6262456d81603 to your computer and use it in GitHub Desktop.
observable chain notes
We often talk about subscribing to observables, and we talk about observables filtering or transforming values.
But to be pedantic, and to better our understanding, you aren't really subscribing to an observable itself.
Observables create subscriber chains.
Each call to subscribe does something. Either execute some work, or create the next node in a subscriber chain.
structure:
The structure of the subscriber chain resembles that of the obervable chain that creates it.
Whereas each observable has a parent observable, subscriptions are a bidirectional concept.
Each node in the subscriber chain has a list of nodes that are subscribed to it.
Upon the first node subscribing to a node, it subscribes to its parent.
Upon the last node unsubscribing to a node, it unsubscribes from its parent.
Calling on an observable will call subscribe on its parent observable to create the next node in the chain.
This repeats until the root Observable is reached. A new chain is created.
If the client retains the Subscription instance, then the client can unsunscribe.
That subscription will now have no subscribers, and so it calls unsubscribe on the next node in the chain.
This repeats until the root Observable is unsubscribed from. The chain is destroyed.
event propagation:
Events flow down the subscriber chain.
When a node on the subscriber chain receives an event from its upsteram subscriber node, it forwards it to all of its own subscribers.
The behavior of each subscriber in that chain is prescribed by its correpsonding observable.
The root `Observable` instance creates the root of the subscriber chain.
It's this root of the subscriber chain that does work upon its first subscriber. Again, that work is prescribed by its corresponding Observable.
Events that it generates are then propagated down the chain.
It's the nodes down the subscriber chain that filter or transform values.
Again, the logic for filtering and transforming values is prescribed by its corresponding Observable instances.
Such events eventually reach the observer that originally subscribed at the end.
details:
Each node on the subscription chain implements the `Observer` interface, which is the interface that the subscriber implements:
```
public interface Observer<T> {
void onCompleted();
void onError(Throwable e);
void onNext(T t);
}
```
Hopefully this interface is not foreign to you.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment