Exemplo n.º 1
0
 /**
  * @param int $count
  * @return void
  */
 public function produceNext($count = 1)
 {
     if ($this->completed) {
         return;
     }
     for ($i = 0; $i < $count; $i++) {
         if (!$this->isIteratorValid()) {
             $this->completed = true;
             $this->obs->onCompleted();
             return;
         }
         $this->obs->onNext($this->iterator->current());
         $this->iterator->next();
     }
 }
Exemplo n.º 2
0
 /**
  * @param $sourceDir
  * @param $targetDir
  * @param $isDryRun
  * @return Observable\ConnectableObservable
  */
 public function execute($sourceDir, $targetDir, $isDryRun)
 {
     $files = Observable::fromIterator($this->filesystem->ls($sourceDir));
     $filesWithDate = $this->fileWithDateRepository->extractDateFrom($files)->publish();
     $symlinkCommands = $this->symlinkCommandRepository->createSymlinkCommands($filesWithDate, new Path($targetDir));
     $summary = $this->summaryRepository->summarize($filesWithDate);
     $symlinkCommands->merge($summary)->subscribe($this->output);
     if (!$isDryRun) {
         $symlinkCommands->subscribeCallback(function (SymlinkCommand $cmd) {
             $this->output->onNext("write file");
             $this->linkRepository->createLink($cmd->getSource(), $cmd->getTarget());
         });
     }
     $filesWithDate->connect();
 }
Exemplo n.º 3
0
 /**
  *
  */
 public function onCompleted()
 {
     if ($this->valueSet) {
         parent::onNext($this->value);
     }
     parent::onCompleted();
 }
Exemplo n.º 4
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $outerDisposable = new SerialDisposable();
     $this->disposable->add($outerDisposable);
     $subscribe = function () use($outerDisposable, $observable, $observer, $scheduler, &$subscribe) {
         $this->sourceComplete = false;
         $outerSubscription = $observable->subscribe(new CallbackObserver([$observer, "onNext"], [$observer, "onError"], function () use($observer, &$subscribe, $outerDisposable) {
             $this->sourceComplete = true;
             if (!$this->repeat) {
                 $observer->onCompleted();
                 return;
             }
             $outerDisposable->setDisposable(new EmptyDisposable());
             $this->completions->onNext(++$this->count);
         }), $scheduler);
         $outerDisposable->setDisposable($outerSubscription);
     };
     $notifierDisposable = $this->notifier->subscribe(new CallbackObserver(function () use(&$subscribe, $scheduler) {
         $subscribe();
     }, function ($ex) use($observer) {
         $this->repeat = false;
         $observer->onError($ex);
     }, function () use($observer) {
         $this->repeat = false;
         if ($this->sourceComplete) {
             $observer->onCompleted();
         }
     }), $scheduler);
     $this->disposable->add($notifierDisposable);
     try {
         $handled = call_user_func($this->notificationHandler, $this->completions->asObservable());
         $handledDisposable = $handled->subscribe($this->notifier, $scheduler);
         $this->disposable->add($handledDisposable);
     } catch (\Exception $e) {
         $observer->onError($e);
         return new EmptyDisposable();
     }
     $subscribe();
     return $this->disposable;
 }
Exemplo n.º 5
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $errors = new Subject();
     $disposable = new CompositeDisposable();
     $sourceDisposable = new EmptyDisposable();
     $innerCompleted = false;
     $sourceError = false;
     try {
         $when = call_user_func($this->notificationHandler, $errors->asObservable());
     } catch (\Exception $e) {
         $observer->onError($e);
         return;
     }
     $subscribeToSource = function () use($observer, $disposable, $observable, &$sourceError, $errors, &$sourceDisposable, $scheduler, &$innerCompleted) {
         $sourceError = false;
         $sourceDisposable = $observable->subscribe(new CallbackObserver([$observer, 'onNext'], function ($err) use(&$sourceError, $errors, $disposable, &$sourceDisposable, &$innerCompleted, $observer) {
             $sourceError = true;
             $disposable->remove($sourceDisposable);
             $sourceDisposable->dispose();
             if ($innerCompleted) {
                 $observer->onCompleted();
                 return;
             }
             $errors->onNext($err);
         }, [$observer, 'onCompleted']), $scheduler);
         $disposable->add($sourceDisposable);
     };
     $whenDisposable = $when->subscribe(new CallbackObserver(function ($x) use($subscribeToSource, &$sourceError) {
         if ($sourceError) {
             $sourceError = false;
             $subscribeToSource();
         }
     }, [$observer, 'onError'], function () use(&$innerCompleted, &$sourceError, $observer) {
         $innerCompleted = true;
         if ($sourceError) {
             $observer->onCompleted();
         }
     }), $scheduler);
     $disposable->add($whenDisposable);
     $subscribeToSource();
     return $disposable;
 }
Exemplo n.º 6
0
 public function onNext($value)
 {
     $this->value = $value;
     return parent::onNext($value);
 }
Exemplo n.º 7
0
 /**
  * @test
  * @expectedException RuntimeException
  */
 public function it_passes_on_next_if_disposed()
 {
     $subject = new Subject();
     $value = 42;
     $subject->dispose();
     $subject->onNext($value);
 }
Exemplo n.º 8
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;
 }
Exemplo n.º 9
0
 private function startConnection()
 {
     $loop = \EventLoop\getLoop();
     $dnsResolverFactory = new Factory();
     $dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
     $factory = new \React\HttpClient\Factory();
     $client = $factory->create($loop, $dnsResolver);
     $cNegotiator = new ClientNegotiator($this->url, $this->subProtocols);
     $headers = $cNegotiator->getRequest()->getHeaders();
     $flatHeaders = [];
     foreach ($headers as $k => $v) {
         $flatHeaders[$k] = $v[0];
     }
     $request = $client->request("GET", $this->url, $flatHeaders, '1.1');
     $request->on('response', function (Response $response, Request $request) use($cNegotiator) {
         if ($response->getCode() !== 101) {
             throw new \Exception("Unexpected response code " . $response->getCode());
         }
         // TODO: Should validate response
         //$cNegotiator->validateResponse($response);
         $subprotoHeader = "";
         $psr7Response = new \GuzzleHttp\Psr7\Response($response->getCode(), $response->getHeaders(), null, $response->getVersion());
         if (count($psr7Response->getHeader('Sec-WebSocket-Protocol')) == 1) {
             $subprotoHeader = $psr7Response->getHeader('Sec-WebSocket-Protocol')[0];
         }
         parent::onNext(new MessageSubject(new AnonymousObservable(function (ObserverInterface $observer) use($response) {
             $response->on('data', function ($data) use($observer) {
                 $observer->onNext($data);
             });
             $response->on('error', function ($e) use($observer) {
                 $observer->onError($e);
             });
             $response->on('close', function () use($observer) {
                 $observer->onCompleted();
             });
             $response->on('end', function () use($observer) {
                 $observer->onCompleted();
                 // complete the parent observer - we only do 1 connection
                 parent::onCompleted();
             });
             return new CallbackDisposable(function () use($response) {
                 // commented this out because disposal was causing the other
                 // end (the request) to close also - which causes the pending messages
                 // to get tossed
                 //$response->close();
             });
         }), new CallbackObserver(function ($x) use($request) {
             $request->write($x);
         }, function ($e) use($request) {
             $request->close();
         }, function () use($request) {
             $request->end();
         }), true, $this->useMessageObject, $subprotoHeader, $cNegotiator->getRequest(), $psr7Response));
     });
     $request->writeHead();
 }
Exemplo n.º 10
0
 public function onNext($event)
 {
     parent::onNext($event);
     $this->notifyCompleted();
 }