本文整理汇总了PHP中Rx\Observable类的典型用法代码示例。如果您正苦于以下问题:PHP Observable类的具体用法?PHP Observable怎么用?PHP Observable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Observable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: extractDateFrom
/**
* @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);
});
}
示例2: __construct
/**
* 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;
}
示例3: summarize
/**
* @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";
});
});
}
示例4: __invoke
/**
* @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]);
}
示例5: refCount_not_connected
/**
* @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);
}
示例6: range_dispose
/**
* @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());
}
示例7: testAsObservableNever
public function testAsObservableNever()
{
$results = $this->scheduler->startWithCreate(function () {
return Observable::never()->asObservable();
});
$this->assertMessages([], $results->getMessages());
}
示例8: toObservable
/**
* 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);
}
示例9: never_basic
/**
* @test
*/
public function never_basic()
{
$xs = Observable::never();
$results = new MockObserver($this->scheduler);
$xs->subscribe($results);
$this->assertMessages([], $results->getMessages());
}
示例10: annotations
/**
* @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);
});
}
示例11: fromArray_dispose
/**
* @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());
}
示例12: distinct_until_changed_never
/**
* @test
*/
public function distinct_until_changed_never()
{
$results = $this->scheduler->startWithCreate(function () {
$o = new EmptyObservable();
return Observable::never()->distinctUntilChanged();
});
$this->assertMessages([], $results->getMessages());
}
示例13: jobs
/**
* @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);
});
}
示例14: connect
/**
* @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;
}
示例15: commits
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);
});
}