Skip to content

doOnRequest #1960

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

Merged
merged 1 commit into from
Dec 12, 2014
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4159,6 +4159,22 @@ public final void onNext(T args) {

return lift(new OperatorDoOnEach<T>(observer));
}

/**
* Modifies the source {@code Observable} so that it invokes the given action when it receives a request for more items.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code doOnRequest} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param onRequest
* the action that gets called when an observer requests items from this {@code Observable}
* @return the source {@code Observable} modified so as to call this Action when appropriate
*/
@Beta
public final Observable<T> doOnRequest(final Action1<Long> onRequest) {
return lift(new OperatorDoOnRequest<T>(onRequest));
}

/**
* Modifies the source {@code Observable} so that it invokes the given action when it is subscribed from
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/rx/internal/operators/OperatorDoOnRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* 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.internal.operators;

import rx.Observable.Operator;
import rx.Producer;
import rx.Subscriber;
import rx.functions.Action1;

/**
* This operator modifies an {@link rx.Observable} so a given action is invoked when the {@link rx.Observable.Producer} receives a request.
*
* @param <T>
* The type of the elements in the {@link rx.Observable} that this operator modifies
*/
public class OperatorDoOnRequest<T> implements Operator<T, T> {

private final Action1<Long> request;

public OperatorDoOnRequest(Action1<Long> request) {
this.request = request;
}

@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {

final ParentSubscriber<T> parent = new ParentSubscriber<T>(child);

child.setProducer(new Producer() {

@Override
public void request(long n) {
request.call(n);
parent.requestMore(n);
}

});

return parent;
}

private final class ParentSubscriber<T> extends Subscriber<T> {
private final Subscriber<? super T> child;

private ParentSubscriber(Subscriber<? super T> child) {
this.child = child;
}

private void requestMore(long n) {
request(n);
}

@Override
public void onCompleted() {
child.onCompleted();
}

@Override
public void onError(Throwable e) {
child.onError(e);
}

@Override
public void onNext(T t) {
child.onNext(t);
}
}
}