The Observable::create() method versus the Subject class
Apart from creating custom Observables, we know that we can use the Observable::create()
static method or an instance of the Subject
class to emit items by ourselves, but so far we haven't talked about which one we should choose over the other and why.
As a rule of thumb it's usually better to use Observable::create()
. It's not always possible, but it has its advantages.
For the next couple of examples, let's consider that we want to work with an API that implements the following interface. This could be any Facebook/Twitter/WebSocket or system API:
interface RemoteAPI { public function connect($connectionDetails); public function fetch($path, $callback); public function close(); }
Hot/cold Observables and Observable::create()
In the most general sense an Observable is just a function that connects an observer with the producer of values. By producer we understand any source of values that is unrelated...