Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x: Flowable as a Publisher to be fully RS compliant #5112

Merged
merged 1 commit into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
65 changes: 58 additions & 7 deletions src/main/java/io/reactivex/Flowable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import io.reactivex.flowables.*;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.*;
import io.reactivex.internal.fuseable.ScalarCallable;
import io.reactivex.internal.fuseable.*;
import io.reactivex.internal.operators.flowable.*;
import io.reactivex.internal.operators.flowable.FlowableStrict.StrictSubscriber;
import io.reactivex.internal.operators.observable.ObservableFromPublisher;
import io.reactivex.internal.schedulers.ImmediateThinScheduler;
import io.reactivex.internal.subscribers.*;
Expand Down Expand Up @@ -1557,7 +1558,7 @@ public static <T> Flowable<T> concatEager(Publisher<? extends Publisher<? extend
@SchedulerSupport(SchedulerSupport.NONE)
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Flowable<T> concatEager(Publisher<? extends Publisher<? extends T>> sources, int maxConcurrency, int prefetch) {
return RxJavaPlugins.onAssembly(new FlowableConcatMapEager(sources, Functions.identity(), maxConcurrency, prefetch, ErrorMode.IMMEDIATE));
return RxJavaPlugins.onAssembly(new FlowableConcatMapEagerPublisher(sources, Functions.identity(), maxConcurrency, prefetch, ErrorMode.IMMEDIATE));
}

/**
Expand Down Expand Up @@ -11679,7 +11680,7 @@ public final Flowable<T> retryWhen(
public final void safeSubscribe(Subscriber<? super T> s) {
ObjectHelper.requireNonNull(s, "s is null");
if (s instanceof SafeSubscriber) {
subscribe(s);
subscribe((SafeSubscriber<? super T>)s);
} else {
subscribe(new SafeSubscriber<T>(s));
}
Expand Down Expand Up @@ -12713,13 +12714,15 @@ public final Flowable<T> startWithArray(T... items) {
* </dl>
* @return the new Flowable instance
* @since 2.0.5 - experimental
* @deprecated 2.0.7, will be removed in 2.1.0; by default, the Publisher interface is always strict
*/
@BackpressureSupport(BackpressureKind.PASS_THROUGH)
@SchedulerSupport(SchedulerSupport.NONE)
@Experimental
@CheckReturnValue
@Deprecated
public final Flowable<T> strict() {
return RxJavaPlugins.onAssembly(new FlowableStrict<T>(this));
return this;
}

/**
Expand Down Expand Up @@ -12892,13 +12895,61 @@ public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super T
@SchedulerSupport(SchedulerSupport.NONE)
@Override
public final void subscribe(Subscriber<? super T> s) {
if (s instanceof FlowableSubscriber) {
subscribe((FlowableSubscriber<? super T>)s);
} else {
ObjectHelper.requireNonNull(s, "s is null");
subscribe(new StrictSubscriber<T>(s));
}
}

/**
* Establish a connection between this Flowable and the given FlowableSubscriber and
* start streaming events based on the demand of the FlowableSubscriber.
* <p>
* This is a "factory method" and can be called multiple times, each time starting a new {@link Subscription}.
* <p>
* Each {@link Subscription} will work for only a single {@link FlowableSubscriber}.
* <p>
* If the same {@link FlowableSubscriber} instance is subscribed to multiple {@link Flowable}s and/or the
* same {@link Flowable} multiple times, it must ensure the serialization over its {@code onXXX}
* methods manually.
* <p>
* If the {@link Flowable} rejects the subscription attempt or otherwise fails it will signal
* the error via {@link FlowableSubscriber#onError(Throwable)}.
* <p>
* This subscribe method relaxes the following Reactive-Streams rules:
* <ul>
* <li>§1.3: onNext should not be called concurrently until onSubscribe returns.
* <b>FlowableSubscriber.onSubscribe should make sure a sync or async call triggered by request() is safe.</b></li>
* <li>§2.3: onError or onComplete must not call cancel.
* <b>Calling request() or cancel() is NOP at this point.</b></li>
* <li>§2.12: onSubscribe must be called at most once on the same instance.
* <b>FlowableSubscriber reuse is not checked and if happens, it is the responsibility of
* the FlowableSubscriber to ensure proper serialization of its onXXX methods.</b></li>
* <li>§3.9: negative requests should emit an onError(IllegalArgumentException).
* <b>Non-positive requests signal via RxJavaPlugins.onError and the stream is not affected.</b></li>
* </ul>
* <dl>
* <dt><b>Backpressure:</b></dt>
* <dd>The backpressure behavior/expectation is determined by the supplied {@code FlowableSubscriber}.</dd>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code subscribe} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
* @param s the FlowableSubscriber that will consume signals from this Flowable
* @since 2.0.7 - experimental
*/
@BackpressureSupport(BackpressureKind.SPECIAL)
@SchedulerSupport(SchedulerSupport.NONE)
@Experimental
public final void subscribe(FlowableSubscriber<? super T> s) {
ObjectHelper.requireNonNull(s, "s is null");
try {
s = RxJavaPlugins.onSubscribe(this, s);
Subscriber<? super T> z = RxJavaPlugins.onSubscribe(this, s);

ObjectHelper.requireNonNull(s, "Plugin returned null Subscriber");
ObjectHelper.requireNonNull(z, "Plugin returned null Subscriber");

subscribeActual(s);
subscribeActual(z);
} catch (NullPointerException e) { // NOPMD
throw e;
} catch (Throwable e) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/io/reactivex/FlowableSubscriber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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;

import org.reactivestreams.*;

import io.reactivex.annotations.Experimental;

/**
* Represents a Reactive-Streams inspired Subscriber that is RxJava 2 only
* and weakens rules §1.3 and §3.9 of the specification for gaining performance.
*
* @param <T> the value type
* @since 2.0.7 - experimental
*/
@Experimental
public interface FlowableSubscriber<T> extends Subscriber<T> {

/**
* Implementors of this method should make sure everything that needs
* to be visible in {@link #onNext(Object)} is established before
* calling {@link Subscription#request(long)}. In practice this means
* no initialization should happen after the {@code request()} call and
* additional behavior is thread safe in respect to {@code onNext}.
*
* {@inheritDoc}
*/
@Override
void onSubscribe(Subscription s);
}
4 changes: 2 additions & 2 deletions src/main/java/io/reactivex/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static <T> Flowable<T> concat(Publisher<? extends MaybeSource<? extends T
public static <T> Flowable<T> concat(Publisher<? extends MaybeSource<? extends T>> sources, int prefetch) {
ObjectHelper.requireNonNull(sources, "sources is null");
ObjectHelper.verifyPositive(prefetch, "prefetch");
return RxJavaPlugins.onAssembly(new FlowableConcatMap(sources, MaybeToPublisher.instance(), prefetch, ErrorMode.IMMEDIATE));
return RxJavaPlugins.onAssembly(new FlowableConcatMapPublisher(sources, MaybeToPublisher.instance(), prefetch, ErrorMode.IMMEDIATE));
}

/**
Expand Down Expand Up @@ -827,7 +827,7 @@ public static <T> Flowable<T> merge(Publisher<? extends MaybeSource<? extends T>
@SchedulerSupport(SchedulerSupport.NONE)
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Flowable<T> merge(Publisher<? extends MaybeSource<? extends T>> sources, int maxConcurrency) {
return RxJavaPlugins.onAssembly(new FlowableFlatMap(sources, MaybeToPublisher.instance(), false, maxConcurrency, Flowable.bufferSize()));
return RxJavaPlugins.onAssembly(new FlowableFlatMapPublisher(sources, MaybeToPublisher.instance(), false, maxConcurrency, Flowable.bufferSize()));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/reactivex/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ public static <T> Flowable<T> concat(Publisher<? extends SingleSource<? extends
@SchedulerSupport(SchedulerSupport.NONE)
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Flowable<T> concat(Publisher<? extends SingleSource<? extends T>> sources, int prefetch) {
ObjectHelper.requireNonNull(sources, "sources is null");
ObjectHelper.verifyPositive(prefetch, "prefetch");
return RxJavaPlugins.onAssembly(new FlowableConcatMap(sources, SingleInternalHelper.toFlowable(), prefetch, ErrorMode.IMMEDIATE));
return RxJavaPlugins.onAssembly(new FlowableConcatMapPublisher(sources, SingleInternalHelper.toFlowable(), prefetch, ErrorMode.IMMEDIATE));
}

/**
Expand Down Expand Up @@ -684,7 +685,7 @@ public static <T> Flowable<T> merge(Iterable<? extends SingleSource<? extends T>
@SchedulerSupport(SchedulerSupport.NONE)
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Flowable<T> merge(Publisher<? extends SingleSource<? extends T>> sources) {
return RxJavaPlugins.onAssembly(new FlowableFlatMap(sources, SingleInternalHelper.toFlowable(), false, Integer.MAX_VALUE, Flowable.bufferSize()));
return RxJavaPlugins.onAssembly(new FlowableFlatMapPublisher(sources, SingleInternalHelper.toFlowable(), false, Integer.MAX_VALUE, Flowable.bufferSize()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package io.reactivex.internal.fuseable;

import org.reactivestreams.Subscriber;
import io.reactivex.FlowableSubscriber;

/**
* A Subscriber with an additional onNextIf(T) method that
Expand All @@ -25,7 +25,7 @@
*
* @param <T> the value type
*/
public interface ConditionalSubscriber<T> extends Subscriber<T> {
public interface ConditionalSubscriber<T> extends FlowableSubscriber<T> {
/**
* Conditionally takes the value.
* @param t the value to deliver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void subscribeActual(CompletableObserver s) {

static final class CompletableConcatSubscriber
extends AtomicInteger
implements Subscriber<CompletableSource>, Disposable {
implements FlowableSubscriber<CompletableSource>, Disposable {
private static final long serialVersionUID = 9032184911934499404L;

final CompletableObserver actual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected void subscribeActual(final CompletableObserver cs) {
flowable.subscribe(new FromPublisherSubscriber<T>(cs));
}

static final class FromPublisherSubscriber<T> implements Subscriber<T>, Disposable {
static final class FromPublisherSubscriber<T> implements FlowableSubscriber<T>, Disposable {

final CompletableObserver cs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void subscribeActual(CompletableObserver s) {

static final class CompletableMergeSubscriber
extends AtomicInteger
implements Subscriber<CompletableSource>, Disposable {
implements FlowableSubscriber<CompletableSource>, Disposable {

private static final long serialVersionUID = -2108443387387077490L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ abstract class AbstractFlowableWithUpstream<T, R> extends Flowable<R> implements
/**
* The upstream source Publisher.
*/
protected final Publisher<T> source;
protected final Flowable<T> source;

/**
* Constructs a FlowableSource wrapping the given non-null (verified)
* source Publisher.
* @param source the source (upstream) Publisher instance, not null (verified)
*/
AbstractFlowableWithUpstream(Publisher<T> source) {
AbstractFlowableWithUpstream(Flowable<T> source) {
this.source = ObjectHelper.requireNonNull(source, "source is null");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.*;

import org.reactivestreams.*;
import org.reactivestreams.Subscription;

import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.MissingBackpressureException;
import io.reactivex.internal.queue.SpscArrayQueue;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.internal.util.*;

public final class BlockingFlowableIterable<T> implements Iterable<T> {
final Publisher<? extends T> source;
final Flowable<? extends T> source;

final int bufferSize;

public BlockingFlowableIterable(Publisher<? extends T> source, int bufferSize) {
public BlockingFlowableIterable(Flowable<? extends T> source, int bufferSize) {
this.source = source;
this.bufferSize = bufferSize;
}
Expand All @@ -44,7 +45,7 @@ public Iterator<T> iterator() {

static final class BlockingFlowableIterator<T>
extends AtomicReference<Subscription>
implements Subscriber<T>, Iterator<T>, Runnable, Disposable {
implements FlowableSubscriber<T>, Iterator<T>, Runnable, Disposable {

private static final long serialVersionUID = 6695226475494099826L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import java.util.*;

import org.reactivestreams.Publisher;

import io.reactivex.Flowable;
import io.reactivex.internal.util.*;
import io.reactivex.subscribers.DefaultSubscriber;

Expand All @@ -30,11 +29,11 @@
*/
public final class BlockingFlowableMostRecent<T> implements Iterable<T> {

final Publisher<? extends T> source;
final Flowable<? extends T> source;

final T initialValue;

public BlockingFlowableMostRecent(Publisher<? extends T> source, T initialValue) {
public BlockingFlowableMostRecent(Flowable<? extends T> source, T initialValue) {
this.source = source;
this.initialValue = initialValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.reactivestreams.*;

import io.reactivex.*;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Predicate;
import io.reactivex.internal.subscriptions.*;
Expand All @@ -23,7 +24,7 @@ public final class FlowableAll<T> extends AbstractFlowableWithUpstream<T, Boolea

final Predicate<? super T> predicate;

public FlowableAll(Publisher<T> source, Predicate<? super T> predicate) {
public FlowableAll(Flowable<T> source, Predicate<? super T> predicate) {
super(source);
this.predicate = predicate;
}
Expand All @@ -33,7 +34,7 @@ protected void subscribeActual(Subscriber<? super Boolean> s) {
source.subscribe(new AllSubscriber<T>(s, predicate));
}

static final class AllSubscriber<T> extends DeferredScalarSubscription<Boolean> implements Subscriber<T> {
static final class AllSubscriber<T> extends DeferredScalarSubscription<Boolean> implements FlowableSubscriber<T> {

private static final long serialVersionUID = -3521127104134758517L;
final Predicate<? super T> predicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

public final class FlowableAllSingle<T> extends Single<Boolean> implements FuseToFlowable<Boolean> {

final Publisher<T> source;
final Flowable<T> source;

final Predicate<? super T> predicate;

public FlowableAllSingle(Publisher<T> source, Predicate<? super T> predicate) {
public FlowableAllSingle(Flowable<T> source, Predicate<? super T> predicate) {
this.source = source;
this.predicate = predicate;
}
Expand All @@ -43,7 +43,7 @@ public Flowable<Boolean> fuseToFlowable() {
return RxJavaPlugins.onAssembly(new FlowableAll<T>(source, predicate));
}

static final class AllSubscriber<T> implements Subscriber<T>, Disposable {
static final class AllSubscriber<T> implements FlowableSubscriber<T>, Disposable {

final SingleObserver<? super Boolean> actual;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.reactivestreams.*;

import io.reactivex.Flowable;
import io.reactivex.*;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.plugins.RxJavaPlugins;
Expand Down Expand Up @@ -147,7 +147,7 @@ public void cancel() {
}
}

static final class AmbInnerSubscriber<T> extends AtomicReference<Subscription> implements Subscriber<T>, Subscription {
static final class AmbInnerSubscriber<T> extends AtomicReference<Subscription> implements FlowableSubscriber<T>, Subscription {

private static final long serialVersionUID = -1185974347409665484L;
final AmbCoordinator<T> parent;
Expand Down
Loading