create() public static method

Creates an observable sequence from a specified subscribeAction callable implementation.
public static create ( callable $subscribeAction ) : Rx\Observable\AnonymousObservable
$subscribeAction callable Implementation of the resulting observable sequence's subscribe method.
return Rx\Observable\AnonymousObservable The observable sequence with the specified implementation for the subscribe method.
コード例 #1
0
ファイル: Udp.php プロジェクト: domraider/rxnet
 /**
  * @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();
     });
 }
コード例 #2
0
ファイル: RabbitQueue.php プロジェクト: domraider/rxnet
 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);
     });
 }
コード例 #3
0
ファイル: SocketWithBuffer.php プロジェクト: domraider/rxnet
 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();
     });
 }
コード例 #4
0
ファイル: RxPool.php プロジェクト: domraider/rxnet
 public function submit(\Threaded $thread, $loop)
 {
     parent::submit($thread);
     return Observable::create(function (ObserverInterface $observer) use($thread, $loop) {
         while ($thread->isRunning()) {
             $loop->tick();
             //var_dump($thread->isRunning());
             //usleep(100);
         }
         $observer->onNext(new Event('/thread/ok', $thread));
         $observer->onCompleted();
     });
 }
コード例 #5
0
ファイル: Tcp.php プロジェクト: domraider/rxnet
 /**
  * @return Observable\AnonymousObservable
  * @throws \Exception
  */
 protected function createSocketForAddress()
 {
     $socket = parent::createSocketForAddress();
     // Wait TCP handshake
     return Observable::create(function (ObserverInterface $observer) use($socket) {
         $this->loop->addWriteStream($socket, function ($socket) use($observer) {
             $this->onConnected($socket, $observer);
         });
         return new CallbackDisposable(function () use($socket, $observer) {
             $this->loop->removeStream($socket);
             if (is_resource($socket)) {
                 fclose($socket);
             }
         });
     });
 }
コード例 #6
0
ファイル: WaitForAnswer.php プロジェクト: domraider/rxnet
 /**
  * @param ZmqEvent $event
  * @return mixed
  */
 public function __invoke($event)
 {
     return Observable::create(function (ObserverInterface $observer) use($event) {
         $id = $event->getLabel('id');
         $req = new ZmqRequest();
         $req->subscribe($observer);
         $disposable = $this->source->filter(function (Event $event) use($id) {
             return $event->hasLabel('id', $id);
         })->take(1)->subscribe($req);
         $req->subscribeCallback(null, null, function () use($disposable) {
             $disposable->dispose();
         });
         return new CallbackDisposable(function () use($disposable) {
             $disposable->dispose();
         });
     });
 }
コード例 #7
0
ファイル: RxThread.php プロジェクト: domraider/rxnet
 public function handle(\Thread $thread)
 {
     $thread->start();
     echo "Start thread with ID {$thread->getCurrentThreadId()}\n";
     return Observable::create(function (ObserverInterface $observer) use($thread) {
         while ($thread->isRunning()) {
             $this->loop->tick();
         }
         try {
             echo "Thread finished\n";
             $thread->join();
             $observer->onNext(new Event('/thread/ok', $thread));
             $observer->onCompleted();
         } catch (\Exception $e) {
             echo "Thread error\n";
             $observer->onError($e);
         }
         unset($thread);
     });
 }
コード例 #8
0
ファイル: DeferTest.php プロジェクト: ReactiveX/RxPHP
 /**
  * @test
  */
 public function defer_error_while_subscribe_with_immediate_scheduler_passes_through()
 {
     $onErrorCalled = false;
     Observable::defer(function () {
         return Observable::create(function ($observer, $scheduler = null) {
             $observer->onError(new \Exception('I take exception'));
         });
     })->subscribeCallback(null, function (\Exception $e) use(&$onErrorCalled) {
         $onErrorCalled = true;
         $this->assertEquals('I take exception', $e->getMessage());
     }, null, new ImmediateScheduler());
     $this->assertTrue($onErrorCalled);
 }
コード例 #9
0
ファイル: Socket.php プロジェクト: domraider/rxnet
 public function sendRaw($msg, $to = null)
 {
     try {
         $msg = $this->serializer->serialize($msg);
         $msg = $to ? [$to, $msg] : [$msg];
         $res = $this->socket->sendmulti($msg, \ZMQ::MODE_DONTWAIT);
     } catch (\Exception $e) {
         $res = $e;
     }
     return Observable::create(function (ObserverInterface $observer) use($res) {
         if (!$res) {
             return $observer->onError(new ConnectException("Socket is not answering"));
         }
         if ($res instanceof \Exception) {
             return $observer->onError($res);
         }
         $observer->onNext(new ZmqEvent('/zmq/sent', ['socket' => $res]));
         $observer->onCompleted();
     });
 }
コード例 #10
0
 public function __invoke($res)
 {
     return Observable::create(function (ObserverInterface $observer) use($res) {
         return $this->pollForOutput($observer, $res);
     });
 }
コード例 #11
0
ファイル: takeUntil.php プロジェクト: voryx/Rx.PHP
<?php

require_once __DIR__ . '/../bootstrap.php';
$loop = \React\EventLoop\Factory::create();
$timeout = \Rx\Observable::create(function (\Rx\ObserverInterface $o) use($loop) {
    $loop->addTimer(6, function () use($o) {
        $o->onNext(0);
    });
    return new \Rx\Disposable\EmptyDisposable();
});
$source = (new \Rx\React\Interval(1000, $loop))->takeUntil($timeout);
$subscription = $source->subscribe($stdoutObserver);
$loop->run();
コード例 #12
0
ファイル: DelayTest.php プロジェクト: ihor/RxPHP
 /**
  * @test
  */
 public function delay_completes_during_subscribe_without_throwing()
 {
     $completes = false;
     Observable::create(function ($observer, $scheduler) {
         $observer->onCompleted();
     })->delay(1, $this->scheduler)->subscribeCallback(null, null, function () use(&$completes) {
         $completes = true;
     });
     $this->scheduler->start();
     $this->assertTrue($completes);
 }
コード例 #13
0
ファイル: CreateTest.php プロジェクト: voryx/Rx.PHP
 /**
  * @test
  *
  */
 public function create_observer_does_not_catch()
 {
     $this->assertException(function () {
         Observable::create(function (ObserverInterface $o) {
             $o->onNext(1);
             return new EmptyDisposable();
         })->subscribe(new CallbackObserver(function () {
             throw new \Exception();
         }));
     });
     $this->assertException(function () {
         Observable::create(function (ObserverInterface $o) {
             $o->onError(new \Exception());
             return new EmptyDisposable();
         })->subscribe(new CallbackObserver(null, function () {
             throw new \Exception();
         }));
     });
     $this->assertException(function () {
         Observable::create(function (ObserverInterface $o) {
             $o->onCompleted();
             return new EmptyDisposable();
         })->subscribe(new CallbackObserver(null, null, function () {
             throw new \Exception();
         }));
     });
 }
コード例 #14
0
ファイル: functions.php プロジェクト: domraider/rxnet
function fromQueue(\SplQueue $queue)
{
    return Observable::create(function (ObserverInterface $observer) use($queue) {
        while ($value = $queue->count()) {
            //echo "dequeue";
            $observer->onNext($queue->pop());
        }
        $observer->onCompleted();
    });
}
コード例 #15
0
ファイル: Http.php プロジェクト: domraider/rxnet
 /**
  * @param Request $request
  * @param array $opts
  * @return Observable\AnonymousObservable
  */
 public function requestRaw(Request $request, array $opts = [])
 {
     // To retry properly this observable will be retried
     return Observable::create(function (ObserverInterface $observer) use($request, $opts) {
         $scheme = $request->getUri()->getScheme();
         if (!($port = $request->getUri()->getPort())) {
             $port = $request->getUri()->getScheme() === 'http' ? 80 : 443;
         }
         $streamed = Arrays::get($opts, 'stream', false);
         $req = new HttpRequest($request, $streamed);
         $this->dns->resolve($request->getUri()->getHost())->flatMap(function ($ip) use($scheme, $opts, $port) {
             return $this->getConnector($scheme, $opts)->connect($ip, $port)->map(function (Event $e) {
                 if ($e instanceof ConnectorEvent) {
                     $stream = $e->data;
                     $bufferedStream = new BufferedStream($stream->getSocket(), $stream->getLoop());
                     return new ConnectorEvent($e->name, $bufferedStream, $e->labels, $e->getPriority());
                 }
                 return $e;
             });
         })->subscribe($req);
         $req->subscribe($observer);
     });
 }
コード例 #16
0
ファイル: RabbitMq.php プロジェクト: domraider/rxnet
 /**
  * One time produce on dedicated channel and close after
  * @param $data
  * @param array $headers
  * @param string $exchange
  * @param $routingKey
  * @return Observable\AnonymousObservable
  */
 public function produce($data, $headers = [], $exchange = '', $routingKey)
 {
     return Observable::create(function (ObserverInterface $observer) use($exchange, $data, $headers, $routingKey) {
         return $this->channel()->flatMap(function (Channel $channel) use($exchange, $data, $headers, $routingKey) {
             return $this->exchange($exchange, [], $channel)->produce($data, $routingKey, $headers)->doOnNext(function () use($channel) {
                 $channel->close();
             });
         })->subscribe($observer);
     });
 }
コード例 #17
0
ファイル: CatchErrorTest.php プロジェクト: ReactiveX/RxPHP
 /**
  * does not lose subscription to underlying observable
  * @test
  */
 public function catchError_does_not_lose_subscription()
 {
     $subscribes = 0;
     $unsubscribes = 0;
     $tracer = Observable::create(function () use(&$subscribes, &$unsubscribes) {
         ++$subscribes;
         return new CallbackDisposable(function () use(&$unsubscribes) {
             ++$unsubscribes;
         });
     });
     // Try it without catchError()
     $s = $tracer->subscribe(new CallbackObserver());
     $this->assertEquals($subscribes, 1, '1 subscribes');
     $this->assertEquals($unsubscribes, 0, '0 unsubscribes');
     $s->dispose();
     $this->assertEquals($subscribes, 1, 'After dispose: 1 subscribes');
     $this->assertEquals($unsubscribes, 1, 'After dispose: 1 unsubscribes');
     // And now try again with catchError(function()):
     $subscribes = 0;
     $unsubscribes = 0;
     $s = $tracer->catchError(function () {
         return Observable::never();
     })->subscribe(new CallbackObserver());
     $this->assertEquals($subscribes, 1, 'catchError(Observable): 1 subscribes');
     $this->assertEquals($unsubscribes, 0, 'catchError(Observable): 0 unsubscribes');
     $s->dispose();
     $this->assertEquals($subscribes, 1, 'catchError(Observable): After dispose: 1 subscribes');
     $this->assertEquals($unsubscribes, 1, 'catchError(Observable): After dispose: 1 unsubscribes');
 }
コード例 #18
0
ファイル: create.php プロジェクト: voryx/Rx.PHP
<?php

use Rx\Disposable\CallbackDisposable;
require_once __DIR__ . '/../bootstrap.php';
//With static method
$source = \Rx\Observable::create(function (\Rx\ObserverInterface $observer) {
    $observer->onNext(42);
    $observer->onCompleted();
    return new CallbackDisposable(function () {
        echo "Disposed \n";
    });
});
$subscription = $source->subscribe($createStdoutObserver());
//Next value: 42
//Complete!
//Disposed
//With Class
$source = new \Rx\Observable\AnonymousObservable(function (\Rx\ObserverInterface $observer) {
    $observer->onNext(42);
    $observer->onCompleted();
    return new CallbackDisposable(function () {
        echo "Disposed \n";
    });
});
$subscription = $source->subscribe($createStdoutObserver());
//Next value: 42
//Complete!
//Disposed