Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions demo/do/doFinally-error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require_once __DIR__ . '/../bootstrap.php';

Rx\Observable::range(1, 3)
->map(function($value) {
if ($value == 2) {
throw new \Exception('error');
}
return $value;
})
->doFinally(function() {
echo "Finally\n";
})
->subscribe($stdoutObserver);
3 changes: 3 additions & 0 deletions demo/do/doFinally-error.php.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Next value: 1
Exception: error
Finally
9 changes: 9 additions & 0 deletions demo/do/doFinally.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

require_once __DIR__ . '/../bootstrap.php';

Rx\Observable::range(1, 3)
->doFinally(function() {
echo "Finally\n";
})
->subscribe($stdoutObserver);
5 changes: 5 additions & 0 deletions demo/do/doFinally.php.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Next value: 1
Next value: 2
Next value: 3
Complete!
Finally
20 changes: 20 additions & 0 deletions lib/Rx/Observable.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Rx\Operator\DistinctOperator;
use Rx\Operator\DistinctUntilChangedOperator;
use Rx\Operator\DoOnEachOperator;
use Rx\Operator\DoFinallyOperator;
use Rx\Operator\GroupByUntilOperator;
use Rx\Operator\IsEmptyOperator;
use Rx\Operator\MapOperator;
Expand Down Expand Up @@ -1867,4 +1868,23 @@ public function isEmpty()
return new IsEmptyOperator();
});
}


/**
* Will call a specified function when the source terminates on complete or error.
*
* @param callable $callback
* @return AnonymousObservable
*
* @demo do/doFinally.php
* @demo do/doFinally-error.php
* @operator
* @reactivex do
*/
public function doFinally(callable $callback)
{
return $this->lift(function() use ($callback) {
return new DoFinallyOperator($callback);
});
}
}
37 changes: 37 additions & 0 deletions lib/Rx/Operator/DoFinallyOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rx\Operator;

use Rx\Disposable\CallbackDisposable;
use Rx\Disposable\BinaryDisposable;
use Rx\ObservableInterface;
use Rx\ObserverInterface;
use Rx\SchedulerInterface;

class DoFinallyOperator implements OperatorInterface
{
/** @var callable */
private $callback;

/**
* @param callable $callback
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}

/**
* @param \Rx\ObservableInterface $observable
* @param \Rx\ObserverInterface $observer
* @param \Rx\SchedulerInterface $scheduler
* @return \Rx\DisposableInterface
*/
public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
{
return new BinaryDisposable(
$observable->subscribe($observer, $scheduler),
new CallbackDisposable($this->callback)
);
}
}
Loading