Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../bootstrap.php'; | ||
|
||
$loop = \React\EventLoop\Factory::create(); | ||
$scheduler = new \Rx\Scheduler\EventLoopScheduler($loop); | ||
|
||
$source = Rx\Observable::fromArray([ | ||
\Rx\Observable::interval(100)->mapTo('a'), | ||
\Rx\Observable::interval(200)->mapTo('b'), | ||
\Rx\Observable::interval(300)->mapTo('c'), | ||
]) | ||
->switchFirst() | ||
->take(3); | ||
|
||
$subscription = $source->subscribe($stdoutObserver, $scheduler); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Next value: a | ||
Next value: a | ||
Next value: a | ||
Complete! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Rx\Operator; | ||
|
||
use Rx\Disposable\CompositeDisposable; | ||
use Rx\Disposable\SingleAssignmentDisposable; | ||
use Rx\Observable; | ||
use Rx\ObservableInterface; | ||
use Rx\Observer\CallbackObserver; | ||
use Rx\ObserverInterface; | ||
use Rx\Operator\OperatorInterface; | ||
use Rx\SchedulerInterface; | ||
|
||
final class SwitchFirstOperator implements OperatorInterface | ||
{ | ||
private $isStopped = false; | ||
private $hasCurrent = false; | ||
|
||
public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null) | ||
{ | ||
$singleDisposable = new SingleAssignmentDisposable(); | ||
$disposable = new CompositeDisposable(); | ||
|
||
$disposable->add($singleDisposable); | ||
|
||
$callbackObserver = new CallbackObserver( | ||
function (Observable $x) use ($disposable, $scheduler, $observer) { | ||
if ($this->hasCurrent) { | ||
return; | ||
} | ||
$this->hasCurrent = true; | ||
|
||
$inner = new SingleAssignmentDisposable(); | ||
$disposable->add($inner); | ||
|
||
$innerSub = $x->subscribe(new CallbackObserver( | ||
[$observer, "onNext"], | ||
[$observer, "onError"], | ||
function () use ($disposable, $inner, $observer) { | ||
$disposable->remove($inner); | ||
$this->hasCurrent = false; | ||
|
||
if ($this->isStopped && $disposable->count() === 1) { | ||
$observer->onCompleted(); | ||
} | ||
} | ||
), $scheduler); | ||
|
||
$inner->setDisposable($innerSub); | ||
}, | ||
[$observer, 'onError'], | ||
function () use ($disposable, $observer) { | ||
$this->isStopped = true; | ||
if (!$this->hasCurrent && $disposable->count() === 1) { | ||
$observer->onCompleted(); | ||
} | ||
} | ||
); | ||
|
||
$singleDisposable->setDisposable($observable->subscribe($callbackObserver, $scheduler)); | ||
|
||
return $disposable; | ||
|
||
} | ||
} |
Oops, something went wrong.