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: Add Disposable Observer for Maybe, Completable & Single #4504

Merged
merged 1 commit into from
Sep 8, 2016
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2016 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.observers;

import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.CompletableObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.DisposableHelper;

/**
* An abstract {@link CompletableObserver} that allows asynchronous cancellation by implementing Disposable.
*/
public abstract class DisposableCompletableObserver implements CompletableObserver, Disposable {
final AtomicReference<Disposable> s = new AtomicReference<Disposable>();

@Override
public final void onSubscribe(Disposable s) {
if (DisposableHelper.setOnce(this.s, s)) {
onStart();
}
}

/**
* Called once the single upstream Disposable is set via onSubscribe.
*/
protected void onStart() {
}

@Override
public final boolean isDisposed() {
return s.get() == DisposableHelper.DISPOSED;
}

@Override
public final void dispose() {
DisposableHelper.dispose(s);
}
}
52 changes: 52 additions & 0 deletions src/main/java/io/reactivex/observers/DisposableMaybeObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2016 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.observers;

import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.MaybeObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.DisposableHelper;

/**
* An abstract {@link MaybeObserver} that allows asynchronous cancellation by implementing Disposable.
*
* @param <T> the received value type
*/
public abstract class DisposableMaybeObserver<T> implements MaybeObserver<T>, Disposable {
final AtomicReference<Disposable> s = new AtomicReference<Disposable>();

@Override
public final void onSubscribe(Disposable s) {
if (DisposableHelper.setOnce(this.s, s)) {
onStart();
}
}

/**
* Called once the single upstream Disposable is set via onSubscribe.
*/
protected void onStart() {
}

@Override
public final boolean isDisposed() {
return s.get() == DisposableHelper.DISPOSED;
}

@Override
public final void dispose() {
DisposableHelper.dispose(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.reactivex.internal.disposables.*;

/**
* An abstract Observer that allows asynchronous cancellation by implementing Disposable.
* An abstract {@link Observer} that allows asynchronous cancellation by implementing Disposable.
*
* @param <T> the received value type
*/
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/io/reactivex/observers/DisposableSingleObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2016 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.observers;

import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.DisposableHelper;

/**
* An abstract {@link SingleObserver} that allows asynchronous cancellation by implementing Disposable.
*
* @param <T> the received value type
*/
public abstract class DisposableSingleObserver<T> implements SingleObserver<T>, Disposable {
final AtomicReference<Disposable> s = new AtomicReference<Disposable>();

@Override
public final void onSubscribe(Disposable s) {
if (DisposableHelper.setOnce(this.s, s)) {
onStart();
}
}

/**
* Called once the single upstream Disposable is set via onSubscribe.
*/
protected void onStart() {
}

@Override
public final boolean isDisposed() {
return s.get() == DisposableHelper.DISPOSED;
}

@Override
public final void dispose() {
DisposableHelper.dispose(s);
}
}