From eda498c65fdbb97bf194fac6542d70cad899f1b4 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Fri, 22 Apr 2016 11:56:12 -0400 Subject: [PATCH] Fixed bug where onComplete would throw an error if onError was called before --- .../acr/browser/lightning/activity/ReadingActivity.java | 7 +++++-- .../main/java/acr/browser/lightning/react/Observable.java | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java b/app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java index 7d30a16..088ae2f 100644 --- a/app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java +++ b/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.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -42,6 +43,8 @@ import butterknife.ButterKnife; public class ReadingActivity extends AppCompatActivity { + private static final String TAG = ReadingActivity.class.getSimpleName(); + @Bind(R.id.textViewTitle) TextView mTitle; @@ -197,11 +200,11 @@ public class ReadingActivity extends AppCompatActivity { subscriber.onNext(new ReaderInfo(result.getTitle(), result.getText())); } catch (Exception e) { subscriber.onError(new Throwable("Encountered exception")); - e.printStackTrace(); + Log.e(TAG, "Error parsing page", e); } catch (OutOfMemoryError e) { System.gc(); subscriber.onError(new Throwable("Out of memory")); - e.printStackTrace(); + Log.e(TAG, "Out of memory", e); } subscriber.onComplete(); } diff --git a/app/src/main/java/acr/browser/lightning/react/Observable.java b/app/src/main/java/acr/browser/lightning/react/Observable.java index 3605153..700a39e 100644 --- a/app/src/main/java/acr/browser/lightning/react/Observable.java +++ b/app/src/main/java/acr/browser/lightning/react/Observable.java @@ -146,6 +146,7 @@ public class Observable { @Nullable private volatile OnSubscribe mOnSubscribe; @NonNull private final Observable mObservable; private boolean mOnCompleteExecuted = false; + private boolean mOnError = false; public SubscriberImpl(@NonNull OnSubscribe onSubscribe, @NonNull Observable observable) { mOnSubscribe = onSubscribe; @@ -160,10 +161,10 @@ public class Observable { @Override public void onComplete() { OnSubscribe onSubscribe = mOnSubscribe; - if (!mOnCompleteExecuted && onSubscribe != null) { + if (!mOnCompleteExecuted && onSubscribe != null && !mOnError) { mOnCompleteExecuted = true; mObservable.executeOnObserverThread(new OnCompleteRunnable<>(onSubscribe)); - } else { + } else if (!mOnError) { Log.e(TAG, "onComplete called more than once"); throw new RuntimeException("onComplete called more than once"); } @@ -181,7 +182,7 @@ public class Observable { public void onError(@NonNull final Throwable throwable) { OnSubscribe onSubscribe = mOnSubscribe; if (onSubscribe != null) { - mOnCompleteExecuted = true; + mOnError = true; mObservable.executeOnObserverThread(new OnErrorRunnable<>(onSubscribe, throwable)); } }