forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split SubscribeOn into SubscribeOn/UnsubscribeOn
Working with @headinthebox based on discussions at ReactiveX#869 and ReactiveX#880 (comment) we determined that there are times when `unsubscribeOn` behavior is needed. The `subscribeOn` operator can not mix `subscribe` and `unsubscribe` scheduling behavior without breaking the `lift`/`Subscriber` behavior that allows unsubscribing synchronous sources. The newly added `unsubscribeOn` operator will not work with synchronous unsubscribes, but it will work for the targeted use cases such as UI event handlers.
- Loading branch information
1 parent
18f545a
commit 9be8468
Showing
7 changed files
with
812 additions
and
334 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
113 changes: 113 additions & 0 deletions
113
rxjava-core/src/main/java/rx/operators/OperatorSubscribeOnBounded.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,113 @@ | ||
/** | ||
* Copyright 2014 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 rx.operators; | ||
|
||
import rx.Observable; | ||
import rx.Observable.Operator; | ||
import rx.Scheduler; | ||
import rx.Scheduler.Inner; | ||
import rx.Subscriber; | ||
import rx.util.functions.Action1; | ||
|
||
/** | ||
* Subscribes and unsubscribes Observers on the specified Scheduler. | ||
* <p> | ||
* Will occur asynchronously except when subscribing to `GroupedObservable`, `PublishSubject` and possibly other "hot" Observables | ||
* in which case it will subscribe synchronously and buffer/block onNext calls until the subscribe has occurred. | ||
* <p> | ||
* See https://github.com/Netflix/RxJava/issues/844 for more information on the "time gap" issue that the synchronous | ||
* subscribe is solving. | ||
* | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/subscribeOn.png"> | ||
*/ | ||
public class OperatorSubscribeOnBounded<T> implements Operator<T, Observable<T>> { | ||
|
||
private final Scheduler scheduler; | ||
/** | ||
* Indicate that events fired between the original subscription time and | ||
* the actual subscription time should not get lost. | ||
*/ | ||
private final boolean dontLoseEvents; | ||
/** The buffer size to avoid flooding. Negative value indicates an unbounded buffer. */ | ||
private final int bufferSize; | ||
|
||
public OperatorSubscribeOnBounded(Scheduler scheduler) { | ||
this.scheduler = scheduler; | ||
this.dontLoseEvents = false; | ||
this.bufferSize = -1; | ||
} | ||
|
||
/** | ||
* Construct a SubscribeOn operator. | ||
* | ||
* @param scheduler | ||
* the target scheduler | ||
* @param bufferSize | ||
* if dontLoseEvents == true, this indicates the buffer size. Filling the buffer will | ||
* block the source. -1 indicates an unbounded buffer | ||
*/ | ||
public OperatorSubscribeOnBounded(Scheduler scheduler, int bufferSize) { | ||
this.scheduler = scheduler; | ||
this.dontLoseEvents = true; | ||
this.bufferSize = bufferSize; | ||
} | ||
|
||
@Override | ||
public Subscriber<? super Observable<T>> call(final Subscriber<? super T> subscriber) { | ||
return new Subscriber<Observable<T>>(subscriber) { | ||
|
||
@Override | ||
public void onCompleted() { | ||
// ignore | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
subscriber.onError(e); | ||
} | ||
|
||
boolean checkNeedBuffer(Observable<?> o) { | ||
return dontLoseEvents; | ||
} | ||
|
||
@Override | ||
public void onNext(final Observable<T> o) { | ||
if (checkNeedBuffer(o)) { | ||
// use buffering (possibly blocking) for a possibly synchronous subscribe | ||
final BufferUntilSubscriber<T> bus = new BufferUntilSubscriber<T>(bufferSize, subscriber); | ||
o.subscribe(bus); | ||
subscriber.add(scheduler.schedule(new Action1<Inner>() { | ||
@Override | ||
public void call(final Inner inner) { | ||
bus.enterPassthroughMode(); | ||
} | ||
})); | ||
return; | ||
} else { | ||
// no buffering (async subscribe) | ||
subscriber.add(scheduler.schedule(new Action1<Inner>() { | ||
|
||
@Override | ||
public void call(final Inner inner) { | ||
o.subscribe(subscriber); | ||
} | ||
})); | ||
} | ||
} | ||
|
||
}; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
rxjava-core/src/main/java/rx/operators/OperatorUnsubscribeOn.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,79 @@ | ||
/** | ||
* Copyright 2014 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 rx.operators; | ||
|
||
import rx.Observable.Operator; | ||
import rx.Scheduler; | ||
import rx.Scheduler.Inner; | ||
import rx.Subscriber; | ||
import rx.subscriptions.CompositeSubscription; | ||
import rx.subscriptions.MultipleAssignmentSubscription; | ||
import rx.subscriptions.Subscriptions; | ||
import rx.util.functions.Action0; | ||
import rx.util.functions.Action1; | ||
|
||
/** | ||
* Unsubscribes on the specified Scheduler. | ||
* <p> | ||
*/ | ||
public class OperatorUnsubscribeOn<T> implements Operator<T, T> { | ||
|
||
private final Scheduler scheduler; | ||
|
||
public OperatorUnsubscribeOn(Scheduler scheduler) { | ||
this.scheduler = scheduler; | ||
} | ||
|
||
@Override | ||
public Subscriber<? super T> call(final Subscriber<? super T> subscriber) { | ||
final CompositeSubscription parentSubscription = new CompositeSubscription(); | ||
subscriber.add(Subscriptions.create(new Action0() { | ||
|
||
@Override | ||
public void call() { | ||
final MultipleAssignmentSubscription mas = new MultipleAssignmentSubscription(); | ||
mas.set(scheduler.schedule(new Action1<Inner>() { | ||
|
||
@Override | ||
public void call(final Inner inner) { | ||
parentSubscription.unsubscribe(); | ||
mas.unsubscribe(); | ||
} | ||
})); | ||
} | ||
|
||
})); | ||
|
||
return new Subscriber<T>(parentSubscription) { | ||
|
||
@Override | ||
public void onCompleted() { | ||
subscriber.onCompleted(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
subscriber.onError(e); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
subscriber.onNext(t); | ||
} | ||
|
||
}; | ||
} | ||
} |
Oops, something went wrong.