本文整理汇总了PHP中Rx\Observable::create方法的典型用法代码示例。如果您正苦于以下问题:PHP Observable::create方法的具体用法?PHP Observable::create怎么用?PHP Observable::create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rx\Observable
的用法示例。
在下文中一共展示了Observable::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createSocketForAddress
/**
* @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: consume
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: sendRaw
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: submit
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: createSocketForAddress
/**
* @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: __invoke
/**
* @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: handle
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: defer_error_while_subscribe_with_immediate_scheduler_passes_through
/**
* @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: sendRaw
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: __invoke
public function __invoke($res)
{
return Observable::create(function (ObserverInterface $observer) use($res) {
return $this->pollForOutput($observer, $res);
});
}
示例11: use
<?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: delay_completes_during_subscribe_without_throwing
/**
* @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: create_observer_does_not_catch
/**
* @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: fromQueue
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: requestRaw
/**
* @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);
});
}