File tree Expand file tree Collapse file tree 7 files changed +414
-0
lines changed
test/Rx/Functional/Operator Expand file tree Collapse file tree 7 files changed +414
-0
lines changed Original file line number Diff line number Diff line change 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 );
Original file line number Diff line number Diff line change 1+ Next value: 1
2+ Exception: error
3+ Finally
Original file line number Diff line number Diff line change 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 );
Original file line number Diff line number Diff line change 1+ Next value: 1
2+ Next value: 2
3+ Next value: 3
4+ Complete!
5+ Finally
Original file line number Diff line number Diff line change 3535use Rx \Operator \DistinctOperator ;
3636use Rx \Operator \DistinctUntilChangedOperator ;
3737use Rx \Operator \DoOnEachOperator ;
38+ use Rx \Operator \FinallyOperator ;
3839use Rx \Operator \GroupByUntilOperator ;
3940use Rx \Operator \IsEmptyOperator ;
4041use 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments