Examining RxPHP's internals
In the previous chapter, we briefly mentioned disposables as a means for releasing resources used by observers, Observables, Subjects, and so on. In practice, a disposable is returned, for example, when subscribing to an Observable. Consider the following code from the default Rx\Observable::subscribe()
method:
function subscribe(ObserverI $observer, $scheduler = null) { $this->observers[] = $observer; $this->started = true; return new CallbackDisposable(function () use ($observer) { $this->removeObserver($observer); }); }
This method first adds the observer to the array of all subscribed observers. It then marks this Observable as started (remember the difference between "cold" and "hot" Observables from Chapter 2, Reactive Programming with RxPHP) and, at the end, it returns a new instance of the CallbackDisposable
class. This class takes a Closure as an argument and invokes it when...