Inheritance: implements rx\ObservableInterface
 /**
  * @param Observable $observable
  *
  * @return Observable
  */
 public function extractDateFrom(Observable $observable)
 {
     return $observable->filter(function (SplFileInfo $file) {
         return in_array(strtolower($file->getExtension()), ['jpg', 'jpeg', 'mp4']);
     })->map(function (SplFileInfo $file) {
         $date = $this->imageDateRepository->extractDate($file);
         return new FileWithDate($file, $date);
     });
 }
Beispiel #2
0
 /**
  * ConnectionSubject constructor.
  * @param ObservableInterface $rawDataIn
  * @param ObserverInterface $rawDataOut
  * @param bool $mask
  * @param bool $useMessageObject
  * @param string $subProtocol
  * @param RequestInterface $request
  * @param ResponseInterface $response
  */
 public function __construct(ObservableInterface $rawDataIn, ObserverInterface $rawDataOut, $mask = false, $useMessageObject = false, $subProtocol = "", RequestInterface $request, ResponseInterface $response)
 {
     $this->request = $request;
     $this->response = $response;
     $this->rawDataIn = new AnonymousObservable(function ($observer) use($rawDataIn) {
         return $rawDataIn->subscribe($observer);
     });
     $this->rawDataOut = $rawDataOut;
     $this->mask = $mask;
     $this->subProtocol = $subProtocol;
     // This can be used instead of the subjecg when this issue is addressed:
     // https://github.com/asm89/Rx.PHP/issues/20
     // Actually - using the subject is better so that the framing doesn't get done for every
     // subscriber.
     //$frames = $this->rawDataIn
     //    ->lift(new WebsocketFrameOperator());
     $frames = new Subject();
     $this->rawDataIn->lift(function () {
         return new WebsocketFrameOperator();
     })->subscribe(new CallbackObserver([$frames, "onNext"], function ($error) use($frames) {
         $close = $this->createCloseFrame();
         if ($error instanceof WebsocketErrorException) {
             $close = $this->createCloseFrame($error->getCloseCode());
         }
         $this->sendFrame($close);
         $this->rawDataOut->onCompleted();
         // TODO: Should this error through to frame observers?
         $frames->onCompleted();
     }, function () use($frames) {
         $this->rawDataOut->onCompleted();
         $frames->onCompleted();
     }));
     $this->controlFrames = $frames->filter(function (Frame $frame) {
         return $frame->getOpcode() > 2;
     });
     // default ping handler (ping received from far end
     $this->controlFrames->filter(function (Frame $frame) {
         return $frame->getOpcode() === $frame::OP_PING;
     })->subscribe(new CallbackObserver(function (Frame $frame) {
         $pong = new Frame($frame->getPayload(), true, Frame::OP_PONG);
         $this->sendFrame($pong);
     }));
     $frames->filter(function (Frame $frame) {
         return $frame->getOpcode() < 3;
     })->lift(function () use($mask, $useMessageObject) {
         return new WebsocketMessageOperator($mask, $useMessageObject);
     })->subscribe(new CallbackObserver(function ($x) {
         parent::onNext($x);
     }, function ($x) {
         parent::onError($x);
     }, function () {
         parent::onCompleted();
     }));
     $this->subProtocol = $subProtocol;
 }
 /**
  * @param Observable $observable
  * @return Observable\AnonymousObservable
  */
 public function summarize(Observable $observable)
 {
     return $observable->groupBy(function (FileWithDate $file) {
         return $file->getDatePath();
     }, function (FileWithDate $file) {
         return $file->getDatePath();
     }, function ($key) {
         return $key;
     })->flatMap(function (GroupedObservable $g) {
         return $g->zip([$g->distinct(), $g->count()], function ($_, $b, $c) {
             return "Created {$b} with {$c} files";
         });
     });
 }
Beispiel #4
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     if ($this->scheduler !== null) {
         $scheduler = $this->scheduler;
     }
     $innerDisp = new SerialDisposable();
     $disp = $observable->subscribe(new CallbackObserver(function ($x) use($innerDisp, $observer, $scheduler) {
         $now = $scheduler->now();
         if ($this->nextSend <= $now) {
             $innerDisp->setDisposable(new EmptyDisposable());
             $observer->onNext($x);
             $this->nextSend = $now + $this->throttleTime - 1;
             return;
         }
         $newDisp = Observable::just($x)->delay($this->nextSend - $now)->subscribe(new CallbackObserver(function ($x) use($observer, $scheduler) {
             $observer->onNext($x);
             $this->nextSend = $scheduler->now() + $this->throttleTime - 1;
             if ($this->completed) {
                 $observer->onCompleted();
             }
         }, [$observer, 'onError']), $scheduler);
         $innerDisp->setDisposable($newDisp);
     }, function (\Exception $e) use($observer, $innerDisp) {
         $innerDisp->dispose();
         $observer->onError($e);
     }, function () use($observer) {
         $this->completed = true;
         if ($this->nextSend === 0) {
             $observer->onCompleted();
         }
     }), $scheduler);
     return new CompositeDisposable([$disp, $innerDisp]);
 }
Beispiel #5
0
 /**
  * @test
  */
 public function refCount_not_connected()
 {
     $disconnected = false;
     $count = 0;
     $xs = Observable::defer(function () use(&$count, &$disconnected) {
         $count++;
         return new AnonymousObservable(function () use(&$disconnected) {
             return new CallbackDisposable(function () use(&$disconnected) {
                 $disconnected = true;
             });
         });
     });
     $subject = new TestSubject();
     $conn = new ConnectableObservable($xs, $subject);
     $refd = $conn->refCount();
     $dis1 = $refd->subscribe(new CallbackObserver());
     $this->assertEquals(1, $count);
     $this->assertEquals(1, $subject->getSubscribeCount());
     $this->assertFalse($disconnected);
     $dis2 = $refd->subscribe(new CallbackObserver());
     $this->assertEquals(1, $count);
     $this->assertEquals(2, $subject->getSubscribeCount());
     $this->assertFalse($disconnected);
     $dis1->dispose();
     $this->assertFalse($disconnected);
     $dis2->dispose();
     $this->assertTrue($disconnected);
     $disconnected = false;
     $dis3 = $refd->subscribe(new CallbackObserver());
     $this->assertEquals(2, $count);
     $this->assertEquals(3, $subject->getSubscribeCount());
     $this->assertFalse($disconnected);
     $dis3->dispose();
     $this->assertTrue($disconnected);
 }
Beispiel #6
0
 /**
  * @test
  */
 public function range_dispose()
 {
     $results = $this->scheduler->startWithDispose(function () {
         return Observable::range(-10, 5, $this->scheduler);
     }, 204);
     $this->assertMessages([onNext(201, -10), onNext(202, -9), onNext(203, -8)], $results->getMessages());
 }
Beispiel #7
0
 public function testAsObservableNever()
 {
     $results = $this->scheduler->startWithCreate(function () {
         return Observable::never()->asObservable();
     });
     $this->assertMessages([], $results->getMessages());
 }
Beispiel #8
0
 /**
  * Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
  *
  * @param callable $factory
  * @return \Rx\Observable\AnonymousObservable
  */
 public static function toObservable(callable $factory)
 {
     $observableFactory = function () use($factory) {
         return Promise::toObservable($factory());
     };
     return Observable::defer($observableFactory);
 }
Beispiel #9
0
 /**
  * @test
  */
 public function never_basic()
 {
     $xs = Observable::never();
     $results = new MockObserver($this->scheduler);
     $xs->subscribe($results);
     $this->assertMessages([], $results->getMessages());
 }
Beispiel #10
0
 /**
  * @return ObservableInterface
  */
 public function annotations() : ObservableInterface
 {
     return Promise::toObservable($this->getTransport()->request('jobs/' . $this->id() . '/annotations'))->flatMap(function ($response) {
         return Observable::fromArray($response['annotations']);
     })->map(function ($annotation) {
         return $this->getTransport()->getHydrator()->hydrate('Annotation', $annotation);
     });
 }
Beispiel #11
0
 /**
  * @test
  */
 public function fromArray_dispose()
 {
     $xs = Observable::fromArray(['foo', 'bar', 'baz']);
     $results = $this->scheduler->startWithDispose(function () use($xs) {
         return $xs;
     }, 202);
     $this->assertMessages([onNext(201, 'foo')], $results->getMessages());
 }
 /**
  * @test
  */
 public function distinct_until_changed_never()
 {
     $results = $this->scheduler->startWithCreate(function () {
         $o = new EmptyObservable();
         return Observable::never()->distinctUntilChanged();
     });
     $this->assertMessages([], $results->getMessages());
 }
Beispiel #13
0
 /**
  * @return ObservableInterface
  */
 public function jobs() : ObservableInterface
 {
     return Promise::toObservable($this->getTransport()->request('builds/' . $this->id()))->flatMap(function ($response) {
         return Observable::fromArray($response['jobs']);
     })->map(function ($job) {
         return $this->getTransport()->getHydrator()->hydrate('Job', $job);
     });
 }
Beispiel #14
0
 /**
  * @return \Rx\Disposable\BinaryDisposable
  */
 public function connect()
 {
     if ($this->hasSubscription) {
         return $this->subscription;
     }
     $this->hasSubscription = true;
     $isDisposed = false;
     $connectableDisposable = new CallbackDisposable(function () use(&$isDisposed) {
         if ($isDisposed) {
             return;
         }
         $isDisposed = true;
         $this->hasSubscription = false;
     });
     $this->subscription = new BinaryDisposable($this->sourceObservable->subscribe($this->subject, $this->scheduler), $connectableDisposable);
     return $this->subscription;
 }
 public function commits() : ObservableInterface
 {
     return Promise::toObservable($this->getTransport()->request('repos/' . $this->slug() . '/builds'))->flatMap(function ($response) {
         return Observable::fromArray($response['commits']);
     })->map(function ($build) {
         return $this->getTransport()->getHydrator()->hydrate('Commit', $build);
     });
 }
Beispiel #16
0
 public function testSkipUntilNeverNever()
 {
     $l = Observable::never();
     $r = Observable::never();
     $results = $this->scheduler->startWithCreate(function () use($l, $r) {
         return $l->skipUntil($r);
     });
     $this->assertMessages([], $results->getMessages());
 }
Beispiel #17
0
 /**
  * @return Observable
  */
 protected function createSocketForAddress()
 {
     $socket = parent::createSocketForAddress();
     return Observable::create(function (ObserverInterface $observer) use($socket) {
         $stream = new Datagram($socket, $this->loop);
         $observer->onNext(new ConnectorEvent('/connector/connected', $stream));
         $observer->onCompleted();
     });
 }
Beispiel #18
0
 public function testScanNoSeedNever()
 {
     $results = $this->scheduler->startWithCreate(function () {
         return Observable::never()->scan(function ($acc, $x) {
             return $acc + $x;
         });
     });
     $this->assertMessages([], $results->getMessages());
 }
Beispiel #19
0
 public function observe(Observable $observable)
 {
     return $observable->subscribeCallback(function (Event $event) {
         //$name = basename(str_replace('\\', '/', (is_object($event) ? get_class($event) : $event)));
         //\Log::info("{$this->preposition} {$name} {$event->name}", $event->labels);
         if ($this->verbosity == 4) {
             $this->dump($event->data);
         }
     }, function ($e) {
         if ($this->verbosity == 4) {
             //\Log::error("{$this->preposition} {$e->getMessage()}");
         } else {
             //\Log::error("{$this->preposition} {$e->getMessage()}", [$e]);
         }
     }, function () {
         //\Log::warning("{$this->preposition} event stream is complete");
     });
 }
Beispiel #20
0
 /**
  * @test
  */
 public function it_passes_on_completed_from_sources()
 {
     $ys = $this->createHotObservable(array(onCompleted(250)));
     $results = $this->scheduler->startWithCreate(function () use($ys) {
         return Observable::mergeAll($ys);
     });
     $this->assertMessages(array(onCompleted(250)), $results->getMessages());
     $this->assertSubscriptions(array(subscribe(200, 250)), $ys->getSubscriptions());
 }
 /**
  * @test
  *
  */
 public function promise_success()
 {
     $source = Observable::just(42);
     $promise = Promise::fromObservable($source);
     $promise->then(function ($value) {
         $this->assertEquals(42, $value);
     }, function () {
         $this->assertTrue(false);
     });
 }
Beispiel #22
0
 /**
  * @test
  */
 public function concatAll_timer_missing_item()
 {
     $xs = $this->createHotObservable([onNext(201, 0), onNext(206, 1), onNext(211, 2), onCompleted(212)]);
     $results = $this->scheduler->startWithCreate(function () use($xs) {
         return $xs->map(function ($x) {
             return Observable::timer(5)->mapTo($x);
         })->concatAll();
     });
     $this->assertMessages([onNext(206, 0), onNext(211, 1), onNext(216, 2), onCompleted(216)], $results->getMessages());
 }
Beispiel #23
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $factory = $this->factory;
     try {
         $result = $factory();
     } catch (\Exception $e) {
         return Observable::error($e)->subscribe($observer, $scheduler);
     }
     return $result->subscribe($observer, $scheduler);
 }
Beispiel #24
0
 /**
  * @test
  */
 function cut_never()
 {
     $xs = Observable::never();
     $results = $this->scheduler->startWithCreate(function () use($xs) {
         return $xs->lift(function () {
             return new CutOperator();
         });
     });
     $this->assertMessages([], $results->getMessages());
 }
Beispiel #25
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());
 }
Beispiel #26
0
 public function consume($consumerId = null, $opts = [])
 {
     return Observable::create(function (ObserverInterface $observer) use($consumerId, $opts) {
         $params = ['callback' => [$observer, 'onNext'], 'queue' => $this->queue, 'consumerTag' => $consumerId, 'noLocal' => in_array(RabbitMq::CHANNEL_NO_LOCAL, $opts, true), 'noAck' => in_array(RabbitMq::CHANNEL_NO_ACK, $opts, true), 'exclusive' => in_array(RabbitMq::CHANNEL_EXCLUSIVE, $opts, true), 'noWait' => in_array(RabbitMq::CHANNEL_NO_WAIT, $opts, true)];
         $promise = call_user_func_array([$this->channel, 'consume'], $params);
         $promise->then(null, [$observer, 'onError']);
     })->map(function (Message $message) {
         return new RabbitMessage($this->channel, $message, $this->serializer);
     });
 }
Beispiel #27
0
 /**
  * @test
  */
 public function await_default_timeout()
 {
     $start = microtime(true);
     $observable = Observable::never()->takeUntil(Observable::timer(2000));
     $generator = \Rx\await($observable);
     foreach ($generator as $item) {
     }
     $totalTime = microtime(true) - $start;
     $this->assertEquals('2', round($totalTime));
 }
Beispiel #28
0
 public function __invoke(Observable $attempts)
 {
     return Observable::range(1, $this->max)->zip([$attempts], function ($i, $e) {
         if ($i >= $this->max) {
             throw $e;
         }
         return $i;
     })->flatMap(function ($i) {
         return Observable::timer(1000);
     });
 }
Beispiel #29
0
 public function sendRaw($msg, $to = null)
 {
     $msg = $this->serializer->serialize($msg);
     $msg = $to ? [$to, $msg] : [$msg];
     $this->buffer->send($msg);
     return Observable::create(function (ObserverInterface $observer) {
         // TODO if buffer > x raise error
         $observer->onNext(new ZmqEvent('/zmq/sent', ['socket' => $this]));
         $observer->onCompleted();
     });
 }
Beispiel #30
0
 /**
  * Converts a Promise to an Observable sequence
  *
  * @param \React\Promise\PromiseInterface $promise
  * @return \Rx\Observable\AnonymousObservable
  */
 public static function toObservable(PromiseInterface $promise)
 {
     return Observable::defer(function () use($promise) {
         $subject = new AsyncSubject();
         $promise->then(function ($value) use($subject) {
             $subject->onNext($value);
             $subject->onCompleted();
         }, [$subject, "onError"]);
         return $subject;
     });
 }