Rx\Observable::start PHP Method

start() public static method

Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an observable sequence.
public static start ( callable $action, rx\SchedulerInterface $scheduler = null ) : Rx\Observable\AnonymousObservable
$action callable
$scheduler rx\SchedulerInterface
return Rx\Observable\AnonymousObservable
    public static function start(callable $action, SchedulerInterface $scheduler = null)
    {
        $scheduler = $scheduler ?: new ImmediateScheduler();
        $subject = new AsyncSubject();
        $scheduler->schedule(function () use($subject, $action) {
            $result = null;
            try {
                $result = call_user_func($action);
            } catch (\Exception $e) {
                $subject->onError($e);
                return;
            }
            $subject->onNext($result);
            $subject->onCompleted();
        });
        return $subject->asObservable();
    }

Usage Example

Example #1
0
 /**
  * @test
  */
 public function start_with_error()
 {
     $error = new \Exception();
     $results = $this->scheduler->startWithCreate(function () use($error) {
         return Observable::start(function () use($error) {
             throw $error;
         }, $this->scheduler);
     });
     $this->assertMessages([onError(200, $error)], $results->getMessages());
 }