Rx\Observable::lift PHP Method

lift() public method

Lifts a function to the current Observable and returns a new Observable that when subscribed to will pass the values of the current Observable through the Operator function.
public lift ( callable $operatorFactory ) : Rx\Observable\AnonymousObservable
$operatorFactory callable
return Rx\Observable\AnonymousObservable
    public function lift(callable $operatorFactory)
    {
        return new AnonymousObservable(function (ObserverInterface $observer, SchedulerInterface $schedule) use($operatorFactory) {
            $operator = $operatorFactory();
            return $operator($this, $observer, $schedule);
        });
    }

Usage Example

Beispiel #1
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;
 }