Add support for onStart and onError
This commit is contained in:
parent
c05cc7c9be
commit
d861a9a502
@ -84,6 +84,9 @@ public class Observable<T> {
|
|||||||
@Override
|
@Override
|
||||||
public void onComplete() {}
|
public void onComplete() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(@NonNull Throwable throwable) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNext(T item) {}
|
public void onNext(T item) {}
|
||||||
});
|
});
|
||||||
@ -106,6 +109,7 @@ public class Observable<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
executeOnObserverThread(new OnStartRunnable<>(subscription));
|
||||||
mAction.onSubscribe(new Subscriber<T>() {
|
mAction.onSubscribe(new Subscriber<T>() {
|
||||||
@Override
|
@Override
|
||||||
public void onComplete() {
|
public void onComplete() {
|
||||||
@ -118,6 +122,17 @@ public class Observable<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(@NonNull final Throwable throwable) {
|
||||||
|
if (!mOnCompleteExecuted) {
|
||||||
|
mOnCompleteExecuted = true;
|
||||||
|
executeOnObserverThread(new OnErrorRunnable<>(subscription, throwable));
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "onComplete already called");
|
||||||
|
throw new RuntimeException("onComplete already called");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNext(final T item) {
|
public void onNext(final T item) {
|
||||||
if (!mOnCompleteExecuted) {
|
if (!mOnCompleteExecuted) {
|
||||||
@ -174,5 +189,31 @@ public class Observable<T> {
|
|||||||
subscription.onNext(item);
|
subscription.onNext(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class OnErrorRunnable<T> implements Runnable {
|
||||||
|
private final Subscription<T> subscription;
|
||||||
|
private final Throwable throwable;
|
||||||
|
|
||||||
|
public OnErrorRunnable(Subscription<T> subscription, Throwable throwable) {
|
||||||
|
this.subscription = subscription;
|
||||||
|
this.throwable = throwable;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
subscription.onError(throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class OnStartRunnable<T> implements Runnable {
|
||||||
|
private final Subscription<T> subscription;
|
||||||
|
|
||||||
|
public OnStartRunnable(Subscription<T> subscription) {this.subscription = subscription;}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
subscription.onStart();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,24 @@
|
|||||||
package acr.browser.lightning.react;
|
package acr.browser.lightning.react;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
public interface Subscriber<T> {
|
public interface Subscriber<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the observable
|
||||||
|
* runs into an error that will
|
||||||
|
* cause it to abort and not finish.
|
||||||
|
* Receiving this callback means that
|
||||||
|
* the observable is dead and no
|
||||||
|
* {@link #onComplete()} or {@link #onNext(Object)}
|
||||||
|
* callbacks will be called.
|
||||||
|
*
|
||||||
|
* @param throwable an optional throwable that could
|
||||||
|
* be sent.
|
||||||
|
*/
|
||||||
|
void onError(@NonNull Throwable throwable);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the Observer emits an
|
* Called when the Observer emits an
|
||||||
* item. It can be called multiple times.
|
* item. It can be called multiple times.
|
||||||
|
@ -1,8 +1,29 @@
|
|||||||
package acr.browser.lightning.react;
|
package acr.browser.lightning.react;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
public interface Subscription<T> {
|
public abstract class Subscription<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the observable
|
||||||
|
* runs into an error that will
|
||||||
|
* cause it to abort and not finish.
|
||||||
|
* Receiving this callback means that
|
||||||
|
* the observable is dead and no
|
||||||
|
* {@link #onComplete()} or {@link #onNext(Object)}
|
||||||
|
* callbacks will be called.
|
||||||
|
*
|
||||||
|
* @param throwable an optional throwable that could
|
||||||
|
* be sent.
|
||||||
|
*/
|
||||||
|
public void onError(@NonNull Throwable throwable) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before the observer begins
|
||||||
|
* to process and emit items or complete.
|
||||||
|
*/
|
||||||
|
public void onStart() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the Observer emits an
|
* Called when the Observer emits an
|
||||||
@ -13,7 +34,7 @@ public interface Subscription<T> {
|
|||||||
* @param item the item that has been emitted,
|
* @param item the item that has been emitted,
|
||||||
* can be null.
|
* can be null.
|
||||||
*/
|
*/
|
||||||
void onNext(@Nullable T item);
|
public void onNext(@Nullable T item) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called when the observer is
|
* This method is called when the observer is
|
||||||
@ -22,5 +43,5 @@ public interface Subscription<T> {
|
|||||||
* called on the Subscription after this method
|
* called on the Subscription after this method
|
||||||
* has been called.
|
* has been called.
|
||||||
*/
|
*/
|
||||||
void onComplete();
|
public void onComplete() {}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user