-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.x: add doAfterNext & doAfterSuccess to the other types (#4835)
- Loading branch information
Showing
9 changed files
with
887 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
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
99 changes: 99 additions & 0 deletions
99
src/main/java/io/reactivex/internal/operators/maybe/MaybeDoAfterSuccess.java
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,99 @@ | ||
/** | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.maybe; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.exceptions.Exceptions; | ||
import io.reactivex.functions.Consumer; | ||
import io.reactivex.internal.disposables.DisposableHelper; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
/** | ||
* Calls a consumer after pushing the current item to the downstream. | ||
* @param <T> the value type | ||
* @since 2.0.1 - experimental | ||
*/ | ||
@Experimental | ||
public final class MaybeDoAfterSuccess<T> extends AbstractMaybeWithUpstream<T, T> { | ||
|
||
final Consumer<? super T> onAfterSuccess; | ||
|
||
public MaybeDoAfterSuccess(MaybeSource<T> source, Consumer<? super T> onAfterSuccess) { | ||
super(source); | ||
this.onAfterSuccess = onAfterSuccess; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(MaybeObserver<? super T> s) { | ||
source.subscribe(new DoAfterObserver<T>(s, onAfterSuccess)); | ||
} | ||
|
||
static final class DoAfterObserver<T> implements MaybeObserver<T>, Disposable { | ||
|
||
final MaybeObserver<? super T> actual; | ||
|
||
final Consumer<? super T> onAfterSuccess; | ||
|
||
Disposable d; | ||
|
||
DoAfterObserver(MaybeObserver<? super T> actual, Consumer<? super T> onAfterSuccess) { | ||
this.actual = actual; | ||
this.onAfterSuccess = onAfterSuccess; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
if (DisposableHelper.validate(this.d, d)) { | ||
this.d = d; | ||
|
||
actual.onSubscribe(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSuccess(T t) { | ||
actual.onSuccess(t); | ||
|
||
try { | ||
onAfterSuccess.accept(t); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
// remember, onSuccess is a terminal event and we can't call onError | ||
RxJavaPlugins.onError(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
actual.onError(e); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
actual.onComplete(); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
d.dispose(); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return d.isDisposed(); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/io/reactivex/internal/operators/observable/ObservableDoAfterNext.java
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,77 @@ | ||
/** | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.observable; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.functions.Consumer; | ||
import io.reactivex.internal.observers.BasicFuseableObserver; | ||
|
||
/** | ||
* Calls a consumer after pushing the current item to the downstream. | ||
* @param <T> the value type | ||
* @since 2.0.1 - experimental | ||
*/ | ||
@Experimental | ||
public final class ObservableDoAfterNext<T> extends AbstractObservableWithUpstream<T, T> { | ||
|
||
final Consumer<? super T> onAfterNext; | ||
|
||
public ObservableDoAfterNext(ObservableSource<T> source, Consumer<? super T> onAfterNext) { | ||
super(source); | ||
this.onAfterNext = onAfterNext; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(Observer<? super T> s) { | ||
source.subscribe(new DoAfterObserver<T>(s, onAfterNext)); | ||
} | ||
|
||
static final class DoAfterObserver<T> extends BasicFuseableObserver<T, T> { | ||
|
||
final Consumer<? super T> onAfterNext; | ||
|
||
DoAfterObserver(Observer<? super T> actual, Consumer<? super T> onAfterNext) { | ||
super(actual); | ||
this.onAfterNext = onAfterNext; | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
actual.onNext(t); | ||
|
||
if (sourceMode == NONE) { | ||
try { | ||
onAfterNext.accept(t); | ||
} catch (Throwable ex) { | ||
fail(ex); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public int requestFusion(int mode) { | ||
return transitiveBoundaryFusion(mode); | ||
} | ||
|
||
@Override | ||
public T poll() throws Exception { | ||
T v = qs.poll(); | ||
if (v != null) { | ||
onAfterNext.accept(v); | ||
} | ||
return v; | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/io/reactivex/internal/operators/single/SingleDoAfterSuccess.java
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,96 @@ | ||
/** | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.single; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.exceptions.Exceptions; | ||
import io.reactivex.functions.Consumer; | ||
import io.reactivex.internal.disposables.DisposableHelper; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
/** | ||
* Calls a consumer after pushing the current item to the downstream. | ||
* @param <T> the value type | ||
* @since 2.0.1 - experimental | ||
*/ | ||
@Experimental | ||
public final class SingleDoAfterSuccess<T> extends Single<T> { | ||
|
||
final SingleSource<T> source; | ||
|
||
final Consumer<? super T> onAfterSuccess; | ||
|
||
public SingleDoAfterSuccess(SingleSource<T> source, Consumer<? super T> onAfterSuccess) { | ||
this.source = source; | ||
this.onAfterSuccess = onAfterSuccess; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(SingleObserver<? super T> s) { | ||
source.subscribe(new DoAfterObserver<T>(s, onAfterSuccess)); | ||
} | ||
|
||
static final class DoAfterObserver<T> implements SingleObserver<T>, Disposable { | ||
|
||
final SingleObserver<? super T> actual; | ||
|
||
final Consumer<? super T> onAfterSuccess; | ||
|
||
Disposable d; | ||
|
||
DoAfterObserver(SingleObserver<? super T> actual, Consumer<? super T> onAfterSuccess) { | ||
this.actual = actual; | ||
this.onAfterSuccess = onAfterSuccess; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
if (DisposableHelper.validate(this.d, d)) { | ||
this.d = d; | ||
|
||
actual.onSubscribe(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSuccess(T t) { | ||
actual.onSuccess(t); | ||
|
||
try { | ||
onAfterSuccess.accept(t); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
// remember, onSuccess is a terminal event and we can't call onError | ||
RxJavaPlugins.onError(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
actual.onError(e); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
d.dispose(); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return d.isDisposed(); | ||
} | ||
} | ||
} |
Oops, something went wrong.