-
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.
Merge pull request #3752 from akarnokd/SingleUsing1x
1.x: Single.using()
- Loading branch information
Showing
4 changed files
with
746 additions
and
10 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
116 changes: 116 additions & 0 deletions
116
src/main/java/rx/internal/operators/SingleOnSubscribeUsing.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,116 @@ | ||
package rx.internal.operators; | ||
|
||
import java.util.Arrays; | ||
|
||
import rx.*; | ||
import rx.exceptions.*; | ||
import rx.functions.*; | ||
import rx.plugins.RxJavaPlugins; | ||
|
||
/** | ||
* Generates a resource, derives a Single from it and disposes that resource once the | ||
* Single terminates. | ||
* @param <T> the value type of the Single | ||
* @param <Resource> the resource type | ||
*/ | ||
public final class SingleOnSubscribeUsing<T, Resource> implements Single.OnSubscribe<T> { | ||
final Func0<Resource> resourceFactory; | ||
final Func1<? super Resource, ? extends Single<? extends T>> singleFactory; | ||
final Action1<? super Resource> disposeAction; | ||
final boolean disposeEagerly; | ||
|
||
public SingleOnSubscribeUsing(Func0<Resource> resourceFactory, | ||
Func1<? super Resource, ? extends Single<? extends T>> observableFactory, | ||
Action1<? super Resource> disposeAction, boolean disposeEagerly) { | ||
this.resourceFactory = resourceFactory; | ||
this.singleFactory = observableFactory; | ||
this.disposeAction = disposeAction; | ||
this.disposeEagerly = disposeEagerly; | ||
} | ||
|
||
@Override | ||
public void call(final SingleSubscriber<? super T> child) { | ||
final Resource resource; | ||
|
||
try { | ||
resource = resourceFactory.call(); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
child.onError(ex); | ||
return; | ||
} | ||
|
||
Single<? extends T> single; | ||
|
||
try { | ||
single = singleFactory.call(resource); | ||
} catch (Throwable ex) { | ||
handleSubscriptionTimeError(child, resource, ex); | ||
return; | ||
} | ||
|
||
if (single == null) { | ||
handleSubscriptionTimeError(child, resource, new NullPointerException("The single")); | ||
return; | ||
} | ||
|
||
SingleSubscriber<T> parent = new SingleSubscriber<T>() { | ||
@Override | ||
public void onSuccess(T value) { | ||
if (disposeEagerly) { | ||
try { | ||
disposeAction.call(resource); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
|
||
child.onError(ex); | ||
return; | ||
} | ||
} | ||
|
||
child.onSuccess(value); | ||
|
||
if (!disposeEagerly) { | ||
try { | ||
disposeAction.call(resource); | ||
} catch (Throwable ex2) { | ||
Exceptions.throwIfFatal(ex2); | ||
RxJavaPlugins.getInstance().getErrorHandler().handleError(ex2); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
handleSubscriptionTimeError(child, resource, error); | ||
} | ||
}; | ||
child.add(parent); | ||
|
||
single.subscribe(parent); | ||
} | ||
|
||
void handleSubscriptionTimeError(SingleSubscriber<? super T> t, Resource resource, Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
|
||
if (disposeEagerly) { | ||
try { | ||
disposeAction.call(resource); | ||
} catch (Throwable ex2) { | ||
Exceptions.throwIfFatal(ex2); | ||
ex = new CompositeException(Arrays.asList(ex, ex2)); | ||
} | ||
} | ||
|
||
t.onError(ex); | ||
|
||
if (!disposeEagerly) { | ||
try { | ||
disposeAction.call(resource); | ||
} catch (Throwable ex2) { | ||
Exceptions.throwIfFatal(ex2); | ||
RxJavaPlugins.getInstance().getErrorHandler().handleError(ex2); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.