Android Open Source - rxloader Caching Weak Ref Subscriber






From Project

Back to project page rxloader.

License

The source code is released under:

Apache License

If you think the Android project rxloader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.tatarka.rxloader;
//w  ww. j  a  v a  2  s . c  om
import java.lang.ref.WeakReference;

import rx.Observer;
import rx.Subscription;

class CachingWeakRefSubscriber<T> implements Observer<T>, Subscription {
    private WeakReference<Observer<T>> subscriberRef;
    private Subscription subscription;
    private SaveCallback<T> saveCallback;
    private boolean isComplete;
    private boolean isError;
    private boolean hasValue;
    private Throwable error;
    private T lastValue;

    CachingWeakRefSubscriber() {
        set(null);
    }

    CachingWeakRefSubscriber(RxLoaderObserver<T> observer) {
        set(observer);
    }

    public void set(RxLoaderObserver<T> observer) {
        subscriberRef = new WeakReference<Observer<T>>(observer);
        if (observer == null) return;

        if (!(isComplete || isError)) {
            observer.onStarted();
        }

        if (hasValue) {
            observer.onNext(lastValue);
        }

        if (isComplete) {
            observer.onCompleted();
        } else if (isError) {
            observer.onError(error);
        }
    }

    public void setSubscription(Subscription subscription) {
        this.subscription = subscription;
    }

    public void setSave(SaveCallback<T> callback) {
        saveCallback = callback;
        if (callback == null) return;

        if (hasValue) {
            callback.onNext(lastValue);
        }
    }

    public Observer<T> get() {
        return subscriberRef.get();
    }

    @Override
    public void onCompleted() {
        isComplete = true;
        Observer<T> subscriber = subscriberRef.get();
        if (subscriber != null) subscriber.onCompleted();
    }

    @Override
    public void onError(Throwable e) {
        isError = true;
        error = e;
        Observer<T> subscriber = subscriberRef.get();
        if (subscriber != null) subscriber.onError(e);
    }

    @Override
    public void onNext(T value) {
        hasValue = true;
        lastValue = value;
        Observer<T> subscriber = subscriberRef.get();
        if (subscriber != null) subscriber.onNext(value);
        if (saveCallback != null) saveCallback.onNext(value);
    }

    @Override
    public void unsubscribe() {
        saveCallback = null;
        subscriberRef.clear();
        if (subscription != null) subscription.unsubscribe();
    }

    @Override
    public boolean isUnsubscribed() {
        Observer<T> subscriber = subscriberRef.get();
        return subscriber == null;
    }

    public void clear() {
        unsubscribe();
        isComplete = false;
        isError = false;
        hasValue = false;
        error = null;
        lastValue = null;
    }

    interface SaveCallback<T> {
        void onNext(T value);
    }
}




Java Source Code List

me.tatarka.rxloader.BaseRxLoader.java
me.tatarka.rxloader.CachingWeakRefSubscriber.java
me.tatarka.rxloader.ParcelableSaveCallback.java
me.tatarka.rxloader.RxLoader1.java
me.tatarka.rxloader.RxLoader2.java
me.tatarka.rxloader.RxLoaderBackendFragmentCompat.java
me.tatarka.rxloader.RxLoaderBackendFragmentHelper.java
me.tatarka.rxloader.RxLoaderBackendFragment.java
me.tatarka.rxloader.RxLoaderBackendNestedFragmentCompat.java
me.tatarka.rxloader.RxLoaderBackendNestedFragment.java
me.tatarka.rxloader.RxLoaderBackend.java
me.tatarka.rxloader.RxLoaderManagerCompat.java
me.tatarka.rxloader.RxLoaderManager.java
me.tatarka.rxloader.RxLoaderObserver.java
me.tatarka.rxloader.RxLoader.java
me.tatarka.rxloader.SaveCallback.java
me.tatarka.rxloader.sample.MainActivityWithFragment.java
me.tatarka.rxloader.sample.MainActivityWithViewPager.java
me.tatarka.rxloader.sample.MainActivity.java
me.tatarka.rxloader.sample.MainFragment.java
me.tatarka.rxloader.sample.SampleObservables.java