Writing the DebugSubject class
One common use case for Subject
class is proxying all values and notifications from its source Observable.
In one of the preceding paragraphs, we wrote the PrintObserver
class, which prints all values it receives. However, a more common situation is where we want to output values from an Observable while being able to chain it with another operator or observer. The Subject
class exactly fits this use case, so we'll rewrite the preceding PrintObserver
class and inherit Subject
instead of AbstractObserver
:
class DebugSubject extends Rx\Subject\Subject { public function __construct($identifier=null, $maxLen=64){ $this->identifier = $identifier; $this->maxLen = $maxLen; } public function onCompleted() { printf("%s%s onCompleted\n", $this->getTime(), $this->id()); parent::onCompleted(); } public function onNext($val) { $type = is_object($val) ? get_class($val) : gettype($val); ...