Skip to content

Commit 52f7b01

Browse files
martinsikdavidwdan
authored andcommitted
The finally() operator for 2.x branch (#132)
* added finally operator * finally() operator for 2.x
1 parent b59ebe5 commit 52f7b01

File tree

7 files changed

+414
-0
lines changed

7 files changed

+414
-0
lines changed

demo/finally/finally-error.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../bootstrap.php';
4+
5+
Rx\Observable::range(1, 3)
6+
->map(function($value) {
7+
if ($value == 2) {
8+
throw new \Exception('error');
9+
}
10+
return $value;
11+
})
12+
->finally(function() {
13+
echo "Finally\n";
14+
})
15+
->subscribe($stdoutObserver);

demo/finally/finally-error.php.expect

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Next value: 1
2+
Exception: error
3+
Finally

demo/finally/finally.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../bootstrap.php';
4+
5+
Rx\Observable::range(1, 3)
6+
->finally(function() {
7+
echo "Finally\n";
8+
})
9+
->subscribe($stdoutObserver);

demo/finally/finally.php.expect

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Next value: 1
2+
Next value: 2
3+
Next value: 3
4+
Complete!
5+
Finally

src/Observable.php

+19
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Rx\Operator\DistinctOperator;
3636
use Rx\Operator\DistinctUntilChangedOperator;
3737
use Rx\Operator\DoOnEachOperator;
38+
use Rx\Operator\FinallyOperator;
3839
use Rx\Operator\GroupByUntilOperator;
3940
use Rx\Operator\IsEmptyOperator;
4041
use Rx\Operator\MapOperator;
@@ -1940,6 +1941,24 @@ public function isEmpty(): Observable
19401941
});
19411942
}
19421943

1944+
/**
1945+
* Will call a specified function when the source terminates on complete or error.
1946+
*
1947+
* @param callable $callback
1948+
* @return Observable
1949+
*
1950+
* @demo finally/finally.php
1951+
* @demo finally/finally-error.php
1952+
* @operator
1953+
* @reactivex do
1954+
*/
1955+
public function finally(callable $callback): Observable
1956+
{
1957+
return $this->lift(function() use ($callback) {
1958+
return new FinallyOperator($callback);
1959+
});
1960+
}
1961+
19431962
/**
19441963
* @param Promise $promise
19451964
* @param SchedulerInterface|null $scheduler

src/Operator/FinallyOperator.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rx\Operator;
4+
5+
use Rx\DisposableInterface;
6+
use Rx\Disposable\CallbackDisposable;
7+
use Rx\Disposable\BinaryDisposable;
8+
use Rx\ObservableInterface;
9+
use Rx\ObserverInterface;
10+
11+
class FinallyOperator implements OperatorInterface
12+
{
13+
/** @var callable */
14+
private $callback;
15+
16+
/**
17+
* @param callable $callback
18+
*/
19+
public function __construct(callable $callback)
20+
{
21+
$this->callback = $callback;
22+
}
23+
24+
/**
25+
* @param \Rx\ObservableInterface $observable
26+
* @param \Rx\ObserverInterface $observer
27+
* @return \Rx\DisposableInterface
28+
*/
29+
public function __invoke(ObservableInterface $observable, ObserverInterface $observer): DisposableInterface
30+
{
31+
return new BinaryDisposable(
32+
$observable->subscribe($observer),
33+
new CallbackDisposable($this->callback)
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)