Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added singleInstance operator (#185)
- Loading branch information
Showing
4 changed files
with
197 additions
and
1 deletion.
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,24 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../bootstrap.php'; | ||
|
||
$interval = Rx\Observable::interval(1000); | ||
|
||
$source = $interval | ||
->take(2) | ||
->do(function () { | ||
echo 'Side effect', PHP_EOL; | ||
}); | ||
|
||
$single = $source->singleInstance(); | ||
|
||
// two simultaneous subscriptions, lasting 2 seconds | ||
$single->subscribe($createStdoutObserver('SourceA ')); | ||
$single->subscribe($createStdoutObserver('SourceB ')); | ||
|
||
\Rx\Observable::timer(5000)->subscribe(function () use ($single, &$createStdoutObserver) { | ||
// resubscribe two times again, more than 5 seconds later, | ||
// long after the original two subscriptions have ended | ||
$single->subscribe($createStdoutObserver('SourceC ')); | ||
$single->subscribe($createStdoutObserver('SourceD ')); | ||
}); |
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,16 @@ | ||
Side effect | ||
SourceA Next value: 0 | ||
SourceB Next value: 0 | ||
Side effect | ||
SourceA Next value: 1 | ||
SourceB Next value: 1 | ||
SourceA Complete! | ||
SourceB Complete! | ||
Side effect | ||
SourceC Next value: 0 | ||
SourceD Next value: 0 | ||
Side effect | ||
SourceC Next value: 1 | ||
SourceD Next value: 1 | ||
SourceC Complete! | ||
SourceD 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,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rx\Functional\Operator; | ||
|
||
use Rx\Disposable\CompositeDisposable; | ||
use Rx\Disposable\SerialDisposable; | ||
use Rx\Functional\FunctionalTestCase; | ||
|
||
class SingleInstanceTest extends FunctionalTestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function singleInstance_basic() | ||
{ | ||
$xs = $this->createColdObservable([ | ||
onNext(100, 1), | ||
onNext(150, 2), | ||
onNext(200, 3), | ||
onCompleted(250) | ||
]); | ||
|
||
$ys = null; | ||
$results1 = $this->scheduler->createObserver(); | ||
$results2 = $this->scheduler->createObserver(); | ||
$disposable = null; | ||
|
||
$this->scheduler->scheduleAbsolute($this->scheduler::CREATED, function () use (&$ys, $xs) { | ||
$ys = $xs->singleInstance(); | ||
}); | ||
|
||
$this->scheduler->scheduleAbsolute($this->scheduler::SUBSCRIBED, function () use (&$ys, &$disposable, $results1, $results2) { | ||
$disposable = new CompositeDisposable([ | ||
$ys->subscribe($results1), | ||
$ys->subscribe($results2) | ||
]); | ||
}); | ||
|
||
$this->scheduler->scheduleAbsolute($this->scheduler::DISPOSED, function () use (&$disposable) { | ||
$disposable->dispose(); | ||
}); | ||
|
||
$this->scheduler->start(); | ||
|
||
$this->assertMessages([ | ||
onNext(300, 1), | ||
onNext(350, 2), | ||
onNext(400, 3), | ||
onCompleted(450) | ||
], $results1->getMessages()); | ||
|
||
$this->assertMessages([ | ||
onNext(300, 1), | ||
onNext(350, 2), | ||
onNext(400, 3), | ||
onCompleted(450) | ||
], $results2->getMessages()); | ||
|
||
$this->assertSubscriptions([ | ||
subscribe(200, 450) | ||
], $xs->getSubscriptions()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function singleInstance_subscribe_after_stopped() | ||
{ | ||
$xs = $this->createColdObservable([ | ||
onNext(100, 1), | ||
onNext(150, 2), | ||
onNext(200, 3), | ||
onCompleted(250) | ||
]); | ||
|
||
$ys = null; | ||
$results1 = $this->scheduler->createObserver(); | ||
$results2 = $this->scheduler->createObserver(); | ||
$disposable = new SerialDisposable(); | ||
|
||
$this->scheduler->scheduleAbsolute(100, function () use (&$ys, $xs) { | ||
$ys = $xs->singleInstance(); | ||
}); | ||
|
||
$this->scheduler->scheduleAbsolute(200, function () use (&$ys, $disposable, $results1) { | ||
$disposable->setDisposable($ys->subscribe($results1)); | ||
}); | ||
|
||
$this->scheduler->scheduleAbsolute(600, function () use (&$ys, $disposable, $results2) { | ||
$disposable->setDisposable($ys->subscribe($results2)); | ||
}); | ||
|
||
$this->scheduler->scheduleAbsolute(900, function () use (&$disposable) { | ||
$disposable->dispose(); | ||
}); | ||
|
||
$this->scheduler->start(); | ||
|
||
$this->assertMessages([ | ||
onNext(300, 1), | ||
onNext(350, 2), | ||
onNext(400, 3), | ||
onCompleted(450) | ||
], $results1->getMessages()); | ||
|
||
$this->assertMessages([ | ||
onNext(700, 1), | ||
onNext(750, 2), | ||
onNext(800, 3), | ||
onCompleted(850) | ||
], $results2->getMessages()); | ||
|
||
$this->assertSubscriptions([ | ||
subscribe(200, 450), | ||
subscribe(600, 850) | ||
], $xs->getSubscriptions()); | ||
} | ||
} |