-
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 #3242 from akarnokd/OperatorUsing2x
Operator using, some internal refactorings.
- Loading branch information
Showing
20 changed files
with
205 additions
and
56 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
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
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
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
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
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
143 changes: 143 additions & 0 deletions
143
src/main/java/io/reactivex/internal/operators/PublisherUsing.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,143 @@ | ||
/** | ||
* Copyright 2015 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; | ||
|
||
import java.util.function.*; | ||
|
||
import org.reactivestreams.*; | ||
|
||
import io.reactivex.internal.subscriptions.*; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
public final class PublisherUsing<T, D> implements Publisher<T> { | ||
final Supplier<? extends D> resourceSupplier; | ||
final Function<? super D, ? extends Publisher<? extends T>> sourceSupplier; | ||
final Consumer<? super D> disposer; | ||
final boolean eager; | ||
|
||
public PublisherUsing(Supplier<? extends D> resourceSupplier, | ||
Function<? super D, ? extends Publisher<? extends T>> sourceSupplier, | ||
Consumer<? super D> disposer, | ||
boolean eager) { | ||
this.resourceSupplier = resourceSupplier; | ||
this.sourceSupplier = sourceSupplier; | ||
this.disposer = disposer; | ||
this.eager = eager; | ||
} | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super T> s) { | ||
D resource; | ||
|
||
try { | ||
resource = resourceSupplier.get(); | ||
} catch (Throwable e) { | ||
EmptySubscription.error(e, s); | ||
return; | ||
} | ||
|
||
Publisher<? extends T> source; | ||
try { | ||
source = sourceSupplier.apply(resource); | ||
} catch (Throwable e) { | ||
EmptySubscription.error(e, s); | ||
return; | ||
} | ||
|
||
UsingSubscriber<T, D> us = new UsingSubscriber<>(s, resource, disposer, eager); | ||
|
||
source.subscribe(us); | ||
} | ||
|
||
static final class UsingSubscriber<T, D> implements Subscriber<T>, Subscription { | ||
final Subscriber<? super T> actual; | ||
final D resource; | ||
final Consumer<? super D> disposer; | ||
final boolean eager; | ||
|
||
Subscription s; | ||
|
||
public UsingSubscriber(Subscriber<? super T> actual, D resource, Consumer<? super D> disposer, boolean eager) { | ||
this.actual = actual; | ||
this.resource = resource; | ||
this.disposer = disposer; | ||
this.eager = eager; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
if (SubscriptionHelper.validateSubscription(this.s, s)) { | ||
return; | ||
} | ||
this.s = s; | ||
actual.onSubscribe(this); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
actual.onNext(t); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
if (eager) { | ||
try { | ||
disposer.accept(resource); | ||
} catch (Throwable e) { | ||
t.addSuppressed(e); | ||
} | ||
actual.onError(t); | ||
} else { | ||
actual.onError(t); | ||
disposeAfter(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
if (eager) { | ||
try { | ||
disposer.accept(resource); | ||
} catch (Throwable e) { | ||
actual.onError(e); | ||
return; | ||
} | ||
actual.onComplete(); | ||
} else { | ||
actual.onComplete(); | ||
disposeAfter(); | ||
} | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
s.request(n); | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
s.cancel(); | ||
disposeAfter(); | ||
} | ||
|
||
void disposeAfter() { | ||
try { | ||
disposer.accept(resource); | ||
} catch (Throwable e) { | ||
// can't call actual.onError unless it is serialized, which is expensive | ||
RxJavaPlugins.onError(e); | ||
} | ||
} | ||
} | ||
} |
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.