Browse Source

Fixed bug where onComplete would throw an error if onError was called before

master
Anthony Restaino 9 years ago
parent
commit
eda498c65f
  1. 7
      app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java
  2. 7
      app/src/main/java/acr/browser/lightning/react/Observable.java

7
app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java

@ -13,6 +13,7 @@ import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -42,6 +43,8 @@ import butterknife.ButterKnife;
public class ReadingActivity extends AppCompatActivity { public class ReadingActivity extends AppCompatActivity {
private static final String TAG = ReadingActivity.class.getSimpleName();
@Bind(R.id.textViewTitle) @Bind(R.id.textViewTitle)
TextView mTitle; TextView mTitle;
@ -197,11 +200,11 @@ public class ReadingActivity extends AppCompatActivity {
subscriber.onNext(new ReaderInfo(result.getTitle(), result.getText())); subscriber.onNext(new ReaderInfo(result.getTitle(), result.getText()));
} catch (Exception e) { } catch (Exception e) {
subscriber.onError(new Throwable("Encountered exception")); subscriber.onError(new Throwable("Encountered exception"));
e.printStackTrace(); Log.e(TAG, "Error parsing page", e);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
System.gc(); System.gc();
subscriber.onError(new Throwable("Out of memory")); subscriber.onError(new Throwable("Out of memory"));
e.printStackTrace(); Log.e(TAG, "Out of memory", e);
} }
subscriber.onComplete(); subscriber.onComplete();
} }

7
app/src/main/java/acr/browser/lightning/react/Observable.java

@ -146,6 +146,7 @@ public class Observable<T> {
@Nullable private volatile OnSubscribe<T> mOnSubscribe; @Nullable private volatile OnSubscribe<T> mOnSubscribe;
@NonNull private final Observable<T> mObservable; @NonNull private final Observable<T> mObservable;
private boolean mOnCompleteExecuted = false; private boolean mOnCompleteExecuted = false;
private boolean mOnError = false;
public SubscriberImpl(@NonNull OnSubscribe<T> onSubscribe, @NonNull Observable<T> observable) { public SubscriberImpl(@NonNull OnSubscribe<T> onSubscribe, @NonNull Observable<T> observable) {
mOnSubscribe = onSubscribe; mOnSubscribe = onSubscribe;
@ -160,10 +161,10 @@ public class Observable<T> {
@Override @Override
public void onComplete() { public void onComplete() {
OnSubscribe<T> onSubscribe = mOnSubscribe; OnSubscribe<T> onSubscribe = mOnSubscribe;
if (!mOnCompleteExecuted && onSubscribe != null) { if (!mOnCompleteExecuted && onSubscribe != null && !mOnError) {
mOnCompleteExecuted = true; mOnCompleteExecuted = true;
mObservable.executeOnObserverThread(new OnCompleteRunnable<>(onSubscribe)); mObservable.executeOnObserverThread(new OnCompleteRunnable<>(onSubscribe));
} else { } else if (!mOnError) {
Log.e(TAG, "onComplete called more than once"); Log.e(TAG, "onComplete called more than once");
throw new RuntimeException("onComplete called more than once"); throw new RuntimeException("onComplete called more than once");
} }
@ -181,7 +182,7 @@ public class Observable<T> {
public void onError(@NonNull final Throwable throwable) { public void onError(@NonNull final Throwable throwable) {
OnSubscribe<T> onSubscribe = mOnSubscribe; OnSubscribe<T> onSubscribe = mOnSubscribe;
if (onSubscribe != null) { if (onSubscribe != null) {
mOnCompleteExecuted = true; mOnError = true;
mObservable.executeOnObserverThread(new OnErrorRunnable<>(onSubscribe, throwable)); mObservable.executeOnObserverThread(new OnErrorRunnable<>(onSubscribe, throwable));
} }
} }

Loading…
Cancel
Save