10章 RxJava原始碼分析

Hensen發表於2019-05-13

本篇文章已授權微信公眾號 YYGeeker 獨家釋出轉載請標明出處

CSDN學院課程地址

10. RxJava原始碼分析

RxJava原始碼分析最主要的點在於

  • RxJava是如何從事件流的傳送方傳送到事件流的接收方的
  • RxJava是如何對操作符進行封裝和操作的
  • RxJava是如何隨意切換執行緒的

在分析的過程中,部分原始碼分析我們會通過手寫RxJava的部分程式碼進行分析,當然也會結合實際RxJava的程式碼進行分析。其中,手寫RxJava的原因是為了簡化原始碼,讓讀者方便閱讀到主要程式碼,更快的看懂RxJava的實現思路。在閱讀原始碼之前,我們需要對RxJava的大體概念進行簡單的梳理

  • 發射器:Emitter,發射資料的物件
  • 被觀察者:Observable,被觀察的物件
  • 觀察者:Observer,觀察的物件
  • 被觀察者被訂閱時:ObservableOnSubscribe,被訂閱時的回撥,同時建立出發射器
  • 釋放者:Disposable,釋放RxJava的物件

RxJava的分析三步驟

  • 建立:被觀察者建立的過程
  • 訂閱:被觀察者訂閱觀察者的過程
  • 發射:發射器發射的過程

RxJava原理圖解

  • 第一排表示各個物件的建立關係,A->B->C->D
  • 第二排表示各個物件的訂閱關係,D->C->B->A
  • 第三排表示各個物件的發射關係,A->B->C->D

在這裡插入圖片描述

10.1 RxJava的事件發射原理

知識點:

  • 理解發射資料的過程
  • 理解接收資料的過程

以下是手寫RxJava的程式碼

Observable.create(new ObservableOnSubscribe<String>() {
    @Override
    public void subscribe(Emitter<String> emitter) {
        emitter.onNext("Hello RxJava");
        emitter.onError();
        emitter.onNext("Hello RxJava");
    }
}).subscribe(new Observabler<String>() {
    @Override
    public void onSubscribe() {
        
    }

    @Override
    public void onNext(String string) {
        System.out.println("onNext=" + string);
    }

    @Override
    public void onError() {
        System.out.println("onError");
    }

    @Override
    public void onComplete() {

    }
});
複製程式碼

輸出結果:在輸出onError後,就不會繼續收到新的事件流,表示事件已經被釋放了

onNext=Hello RxJava
onError
複製程式碼

1、定義介面

發射器

public interface Emitter<T> {
    void onNext(T t);
    void onError();
}
複製程式碼

觀察者

public interface Observer<T> {
    void onSubscribe();
    void onNext(T t);
    void onError();
    void onComplete();
}
複製程式碼

被觀察者被訂閱時

public interface ObservableOnSubscribe<T> {
    void subscribe(Emitter<T> emitter);
}
複製程式碼

2、實現被觀察者

被觀察者Observable負責建立、訂閱,發射由發射器負責

  • 建立:建立的過程只是將傳遞進來的引數交給新的ObservableCreate進行管理
  • 訂閱:訂閱的過程只是實現建立出來的ObservableCreate的subscribeActual方法
public abstract class Observable<T> {

    public static <T> ObservableCreate create(ObservableOnSubscribe<T> observableOnSubscribe) {
        return new ObservableCreate<T>(observableOnSubscribe);
    }

    public void subscribe(Observer<T> observer) {
        subscribeActual(observer);
    }

    public abstract void subscribeActual(Observer<T> observer);
}
複製程式碼

3、ObservableCreate

ObservableCreate繼承自Observable,由於Observable.create返回當前ObservableCreate,所以在subscribe的時候,走的是這裡的subscribeActual,subscribeActual中會去建立發射器,並給發射器傳遞進去observer

public class ObservableCreate<T> extends Observable{

    private ObservableOnSubscribe source;

    public ObservableCreate(ObservableOnSubscribe observableOnSubscribe) {
        this.source = observableOnSubscribe;
    }

    @Override
    public void subscribeActual(Observer observer) {
        //固定的三步曲分析法(個人建立,基本都是這個步驟)
        
        //1、建立發射器
        EmitterCreate<T> emitterCreate = new EmitterCreate<>(observer);
        //2、回撥observer的onSubscribe
        observer.onSubscribe();
        //3、回撥上一個的subscribe
        source.subscribe(emitterCreate);
    }
}
複製程式碼

4、EmitterCreate

傳遞進來的observer即是我們最開始訂閱時候new出來的,此時發射資料,就會去呼叫Observer的onNext方法,這樣資料就從發射器中傳遞到觀察者中了。DisposableHelper在後面會講到,這裡只是用作判斷是否被釋放的一個工具類

public class EmitterCreate<T>
        extends AtomicReference<Disposable>
        implements Emitter<T>, Disposable {

    private Observer<T> observer;

    public EmitterCreate(Observer<T> observer) {
        this.observer = observer;
    }

    @Override
    public void onNext(T t) {
        if (!isDisposed()) {
            observer.onNext(t);
        }
    }

    @Override
    public void onError() {
        if (!isDisposed()) {
            try {
                observer.onError();
            } finally {
                dispose();
            }
        }
    }

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

    @Override
    public boolean isDisposed() {
        return DisposableHelper.isDisposed(get());
    }
}
複製程式碼

以下是RxJava原始碼

1、Observable.create

public static <T> Observable<T> create(ObservableOnSubscribe<T> source) {
    ObjectHelper.requireNonNull(source, "source is null");//判空
    return RxJavaPlugins.onAssembly(new ObservableCreate<T>(source));//返回自身
}
複製程式碼

RxJavaPlugins.onAssembly只是對傳遞進來的引數做判斷處理,最終還是返回ObservableCreate,有關RxJavaPlugins的東西最終都是返回自身,RxJavaPlugins後面分析會說到,這裡只需要知道他是返回引數本身即可

2、Observable.subscribe

public final void subscribe(Observer<? super T> observer) {
    ObjectHelper.requireNonNull(observer, "observer is null");//判空
    try {
        observer = RxJavaPlugins.onSubscribe(this, observer);//返回自身

        ObjectHelper.requireNonNull(observer, "Plugin returned null Observer");

        subscribeActual(observer);//回撥ObservableCreate的subscribeActual
    } catch (NullPointerException e) { // NOPMD
        throw e;
    } catch (Throwable e) {
        ......
        throw npe;
    }
}
複製程式碼

observable.subscribe和我們手寫程式碼一樣,最終呼叫的是ObservableCreate的subscribeActual方法

3、ObservableCreate

public final class ObservableCreate<T> extends Observable<T> {
    final ObservableOnSubscribe<T> source;

    public ObservableCreate(ObservableOnSubscribe<T> source) {
        this.source = source;
    }

    @Override
    protected void subscribeActual(Observer<? super T> observer) {
        //1、建立發射器
        CreateEmitter<T> parent = new CreateEmitter<T>(observer);
        //2、回撥observer的onSubscribe
        observer.onSubscribe(parent);

        try {
            //3、回撥ObservableOnSubscribe的subscribe
            source.subscribe(parent);
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            parent.onError(ex);
        }
    }

    static final class CreateEmitter<T>
    extends AtomicReference<Disposable>
    implements ObservableEmitter<T>, Disposable {


        private static final long serialVersionUID = -3434801548987643227L;

        final Observer<? super T> observer;

        CreateEmitter(Observer<? super T> observer) {
            this.observer = observer;
        }

        @Override
        public void onNext(T t) {
            if (t == null) {
                onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
                return;
            }
            if (!isDisposed()) {
                observer.onNext(t);
            }
        }

        @Override
        public void onError(Throwable t) {
            if (!tryOnError(t)) {
                RxJavaPlugins.onError(t);
            }
        }

        @Override
        public boolean tryOnError(Throwable t) {
            if (t == null) {
                t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
            }
            if (!isDisposed()) {
                try {
                    observer.onError(t);
                } finally {
                    dispose();
                }
                return true;
            }
            return false;
        }

        @Override
        public void onComplete() {
            if (!isDisposed()) {
                try {
                    observer.onComplete();
                } finally {
                    dispose();
                }
            }
        }

        @Override
        public void setDisposable(Disposable d) {
            DisposableHelper.set(this, d);
        }

        @Override
        public void setCancellable(Cancellable c) {
            setDisposable(new CancellableDisposable(c));
        }

        @Override
        public ObservableEmitter<T> serialize() {
            return new SerializedEmitter<T>(this);
        }

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

        @Override
        public boolean isDisposed() {
            return DisposableHelper.isDisposed(get());
        }
    }
}
複製程式碼

ObservableCreate和我們手寫程式碼一樣,建立發射器,並在發射器中做發射資料等操作

小結

如圖所示

在這裡插入圖片描述

10.2 RxJava的事件釋放原理

知識點:

  • 理解釋放事件的原理

有關RxJava的釋放原理是基於Observable可以返回Disposable物件,只有呼叫dispose()才能釋放事件,通過上面的例子,我們知道在發射器裡面有isDisposed()dispose()操作,在發射完onError事件的情況下,我們會將事件釋放,所以在finally會做釋放操作,防止後面的事件再次發射

以下是手寫RxJava的程式碼

public class EmitterCreate<T>
        extends AtomicReference<Disposable>
        implements Emitter<T>, Disposable {

    private Observer<T> observer;

    public EmitterCreate(Observer<T> observer) {
        this.observer = observer;
    }

    @Override
    public void onNext(T t) {
        if (!isDisposed()) {
            observer.onNext(t);
        }
    }

    @Override
    public void onError() {
        if (!isDisposed()) {
            try {
                observer.onError();
            } finally {
                dispose();
            }
        }
    }

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

    @Override
    public boolean isDisposed() {
        return DisposableHelper.isDisposed(get());
    }
}
複製程式碼

以下是RxJava原始碼

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

@Override
public boolean isDisposed() {
    return DisposableHelper.isDisposed(get());
}
複製程式碼

可以發現事件的釋放都是通過DisposableHelper去觸發的,不管是手寫RxJava還是原始碼,釋放RxJava都是通過DisposableHelper進行釋放,具體看DisposableHelper。在我們的演示程式中,我們通過發射onNext->onError->onNext的過程,去挖掘事件是怎麼被釋放掉的

public enum DisposableHelper implements Disposable {
    
    DISPOSED
    ;

    public static boolean isDisposed(Disposable d) {
        return d == DISPOSED;
    }

    public static boolean dispose(AtomicReference<Disposable> field) {
        Disposable current = field.get();//獲取引數的Disposable物件
        Disposable d = DISPOSED;//宣告一個已經釋放的Disposable物件
        if (current != d) {//如果當前未被釋放
            current = field.getAndSet(d);//則將當前的Disposable賦值成已經釋放過的Disposable物件
            if (current != d) {//如果當前還未被釋放
                if (current != null) {//且不為空
                    current.dispose();//則釋放當前Disposable物件
                }
                return true;
            }
        }
        return false;
    }
}
複製程式碼

在事件釋放的過程中,EmitterCreate本身是個AtomicReference<Disposable>,程式碼通過get()去獲取Disposable物件,其中程式碼會通過雙層判斷去做釋放,防止在多執行緒的時候出現搶奪的情況

  • onNext:第一次發射資料時,get()會獲取一個null物件,所以不符合d == DISPOSED
  • onEroor:這時候會呼叫dispose()去比較當前和釋放過的物件,如果不等於,則將當前的物件設定為釋放過的值
  • onNext:第二次發射資料時,get()會獲取一個已經釋放過的物件,這個時候符合d == DISPOSED

其實這裡的操作如同設定一個Flag,但由於Disposable是物件的形式,且需要保證原子性,AtomicReference型別是個最佳選擇,能保證物件的原子性

10.3 RxJava的背壓原理

知識點:

  • 理解背壓實現的本質
  • 理解背壓資料項丟棄的本質

背壓原理有一部分和RxJava事件發射原理相似,其背壓的過程就是在不同策略的發射器去處理當前的資料項而已。在分析背壓策略的時候,我們都知道背壓是需要手動進行請求才可以將資料發射到觀察者中,所以我們會呼叫s.request(Long.MAX_VALUE)讓觀察者能接收到資料。有些人就會有疑問,為什麼有些人平時用背壓的時候,不需要去呼叫request()就能接收到資料,原因是有些背壓已經在內部預設呼叫了s.request(Long.MAX_VALUE),所以這裡是不用多想的,是一定要呼叫s.request(Long.MAX_VALUE)才能收到資料的。由於不同背壓的策略的原理大同小異,主要以Drop策略去分析背壓的原理

public static void drop(View view) {
    Flowable.create(new FlowableOnSubscribe<Integer>() {
        @Override
        public void subscribe(FlowableEmitter<Integer> emitter) throws Exception {
            for (int i = 0; i < 1000; i++) {
                emitter.onNext(i);
            }
        }
    }, BackpressureStrategy.DROP)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new FlowableSubscriber<Integer>() {
                @Override
                public void onSubscribe(Subscription s) {
                    s.request(Long.MAX_VALUE);
                }

                @Override
                public void onNext(Integer integer) {
                    Log.e("TAG", "onNext=" + integer);
                }

                @Override
                public void onError(Throwable t) {
                    t.printStackTrace();
                }

                @Override
                public void onComplete() {

                }
            });
}
複製程式碼

以下是RxJava原始碼

1、Flowable.create

public static <T> Flowable<T> create(FlowableOnSubscribe<T> source, BackpressureStrategy mode) {
    ObjectHelper.requireNonNull(source, "source is null");//判空
    ObjectHelper.requireNonNull(mode, "mode is null");//判空
    return RxJavaPlugins.onAssembly(new FlowableCreate<T>(source, mode));//返回自身
}
複製程式碼

Flowable.create跟我們前面是一樣的,最後還是會交給新物件FlowableCreate去處理

2、Flowable.subscribe

public final void subscribe(FlowableSubscriber<? super T> s) {
    ObjectHelper.requireNonNull(s, "s is null");
    try {
        Subscriber<? super T> z = RxJavaPlugins.onSubscribe(this, s);

        ObjectHelper.requireNonNull(z, "Plugin returned null Subscriber");

        subscribeActual(z);
    } catch (NullPointerException e) { // NOPMD
        throw e;
    } catch (Throwable e) {
        ......
        throw npe;
    }
}
複製程式碼

Flowable.subscribe跟我們前面是一樣的,最終呼叫的是FlowableCreate的subscribeActual方法

3、FlowableCreate.subscribeActual

public final class FlowableCreate<T> extends Flowable<T> {

    final FlowableOnSubscribe<T> source;

    final BackpressureStrategy backpressure;

    public FlowableCreate(FlowableOnSubscribe<T> source, BackpressureStrategy backpressure) {
        this.source = source;
        this.backpressure = backpressure;
    }

    @Override
    public void subscribeActual(Subscriber<? super T> t) {
        //使用三步曲分析法
        
        BaseEmitter<T> emitter;

        switch (backpressure) {
        case MISSING: {
            emitter = new MissingEmitter<T>(t);
            break;
        }
        case ERROR: {
            emitter = new ErrorAsyncEmitter<T>(t);
            break;
        }
        case DROP: {
            emitter = new DropAsyncEmitter<T>(t);
            break;
        }
        case LATEST: {
            emitter = new LatestAsyncEmitter<T>(t);
            break;
        }
        default: {
            emitter = new BufferAsyncEmitter<T>(t, bufferSize());
            break;
        }
        }

        t.onSubscribe(emitter);
        try {
            source.subscribe(emitter);
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            emitter.onError(ex);
        }
    }
}
複製程式碼

subscribeActual會根據不同的策略生成不同的發射器,具體的所有策略邏輯都在發射器中體現的

4、DropAsyncEmitter

static final class DropAsyncEmitter<T> extends NoOverflowBaseAsyncEmitter<T> {

    private static final long serialVersionUID = 8360058422307496563L;

    DropAsyncEmitter(Subscriber<? super T> actual) {
        super(actual);
    }

    @Override
    void onOverflow() {
        // nothing to do
    }

}
複製程式碼

DropAsyncEmitter其實沒做什麼事情,主要都在其父類中實現了,onOverflow的回撥錶示事件流溢位的時候的處理,很明顯Drop策略就把溢位的資料項直接不做處理,意思就是拋棄掉這個資料項了

static final class ErrorAsyncEmitter<T> extends NoOverflowBaseAsyncEmitter<T> {

    private static final long serialVersionUID = 338953216916120960L;

    ErrorAsyncEmitter(Subscriber<? super T> actual) {
        super(actual);
    }

    @Override
    void onOverflow() {
        onError(new MissingBackpressureException("create: could not emit value due to lack of requests"));
    }

}
複製程式碼

再看看Error策略,溢位之後就會丟擲溢位的異常,其他策略也類似分析,具體父類是如何處理溢位函式的呢

5、NoOverflowBaseAsyncEmitter

abstract static class NoOverflowBaseAsyncEmitter<T> extends BaseEmitter<T> {

    private static final long serialVersionUID = 4127754106204442833L;

    NoOverflowBaseAsyncEmitter(Subscriber<? super T> actual) {
        super(actual);
    }

    @Override
    public final void onNext(T t) {
        if (isCancelled()) {
            return;
        }

        if (t == null) {
            onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
            return;
        }
        
        //這裡暫時將get()函式當作是類似於List這種的容器,儲存的是當前需要處理的資料項
        if (get() != 0) { //從資料項容器中取值,如果當前有資料項需要處理
            actual.onNext(t); //發射資料項
            BackpressureHelper.produced(this, 1); //對當前存在需要處理的資料項進行-1操作
        } else {
            onOverflow(); //從資料項容器中取值,如果當前沒有資料項需要處理,則回撥溢位函式
        }
    }

    abstract void onOverflow();
}
複製程式碼

NoOverflowBaseAsyncEmitter在發射資料項的時候,會去BaseEmitter中的資料項容器去取出資料項,如果存在則處理,不存在則表示溢位,回撥溢位函式,那麼具體的資料項容器時候怎麼儲存需要處理的資料項的呢

6、BaseEmitter

abstract static class BaseEmitter<T>
    extends AtomicLong
    implements FlowableEmitter<T>, Subscription {
    
    private static final long serialVersionUID = 7326289992464377023L;

    final Subscriber<? super T> actual;

    final SequentialDisposable serial;

    BaseEmitter(Subscriber<? super T> actual) {
        this.actual = actual;
        this.serial = new SequentialDisposable();
    }

    @Override
    public void onComplete() {
        complete();
    }

    protected void complete() {
        if (isCancelled()) {
            return;
        }
        try {
            actual.onComplete();
        } finally {
            serial.dispose();
        }
    }

    @Override
    public final void onError(Throwable e) {
        if (!tryOnError(e)) {
            RxJavaPlugins.onError(e);
        }
    }

    @Override
    public final void request(long n) {
        if (SubscriptionHelper.validate(n)) {
            BackpressureHelper.add(this, n);
        }
    }
}
複製程式碼

BaseEmitter就是一個AtomicLong,如果沒學過AtomicLong的話,可以簡單理解為一個計數器,get()就是獲取當前的Long值,只要不等於0就表示有值。主要還是在request()request()表示此時需要處理的資料項。結合上面NoOverflowBaseAsyncEmitter的中的BackpressureHelper.produced(this, 1)和當前BaseEmitter中的BackpressureHelper.add(this, n),可得資料項的容器完全都是由BackpressureHelper去控制,我們只需要對BackpressureHelper的儲存和獲取做分析,就可以知道當前是否有資料項需要處理

7、BackpressureHelper

public final class BackpressureHelper {

    public static long add(AtomicLong requested, long n) {
        for (;;) {
            long r = requested.get(); //獲取當前資料項
            if (r == Long.MAX_VALUE) {
                return Long.MAX_VALUE;
            }
            long u = addCap(r, n);//當前資料項 + 新增的資料項
            if (requested.compareAndSet(r, u)) { //設定最新的資料項
                return r;
            }
        }
    }
    
    public static long addCap(long a, long b) {
        long u = a + b;
        if (u < 0L) {
            return Long.MAX_VALUE;
        }
        return u;
    }
    
    public static long produced(AtomicLong requested, long n) {
        for (;;) {
            long current = requested.get(); //獲取當前資料項
            if (current == Long.MAX_VALUE) {
                return Long.MAX_VALUE;
            }
            long update = current - n; //當前資料項 - 需要發射的資料項(從原始碼上,n為1)
            if (update < 0L) { //不能為負數
                RxJavaPlugins.onError(new IllegalStateException("More produced than requested: " + update));
                update = 0L;
            }
            if (requested.compareAndSet(current, update)) { //設定最新的資料項
                return update;
            }
        }
    }
}
複製程式碼

BackpressureHelper就是利用AtomicLong的原子性就行簡單的計數器操作而已,並沒有什麼複雜的操作。至此,我們就知道背壓的原理原來就是利用AtomicLong計數器和生產消費的模式去決定是否發射當前的資料項而已

10.4 RxJava的常用操作符原理

知識點:

  • 理解map操作符的原理

RxJava常用操作符的代表就是map,分析map原始碼後,其他的操作符的思想是一樣的,只不過是實現邏輯不一致而已。下面我們通過分析map的主要流程去分析map是如何轉換字串的,從上面我們知道Observable的建立、訂閱、發射的過程,這次對於重複的內容就不再繼續分析,主要是分析中間map是如何回撥apply()去將資料項轉換成字串的

public void map() {
    //建立被觀察者
    Observable
            .create(new ObservableOnSubscribe<String>() {
                @Override
                //預設在主執行緒裡執行該方法
                public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                    e.onNext("俊俊俊很帥");
                    e.onNext("你值得擁有");
                    e.onNext("取消關注");
                    e.onNext("但還是要保持微笑");
                    e.onComplete();
                }
            })
            .map(new Function<String, String>() {
                @Override
                public String apply(String s) throws Exception {
                    return "Hello";
                }
            })
            //建立觀察者並訂閱
            .subscribe(new Observer<String>() {
                @Override
                public void onSubscribe(Disposable d) {
                    if (!d.isDisposed()) {
                        d.dispose();
                    }
                }

                @Override
                public void onNext(String s) {
                    System.out.println("onNext=" + s);
                }

                @Override
                public void onError(Throwable e) {
                    System.out.println("onNext=" + e.getMessage());
                }

                @Override
                public void onComplete() {

                }
            });
}
複製程式碼

以下是RxJava原始碼

1、Observable.map

public static <T> Observable<T> create(ObservableOnSubscribe<T> source) {
    ObjectHelper.requireNonNull(source, "source is null");
    return RxJavaPlugins.onAssembly(new ObservableCreate<T>(source));
}

public final <R> Observable<R> map(Function<? super T, ? extends R> mapper) {
    ObjectHelper.requireNonNull(mapper, "mapper is null");
    return RxJavaPlugins.onAssembly(new ObservableMap<T, R>(this, mapper));
}
複製程式碼

從create到map的過程中,create的時候,當前的Observable已經被轉換成ObservableCreate,再次map的時候, 當前的Observable已經被轉換成ObservableMap,而且在ObservableMap中傳遞的引數包含this,所以當前ObservableMap中是巢狀著ObservableCreate

2、Observable.subscribe

由於當前的Observable是ObservableMap,所以Observable.subscribe會回撥ObservableMap中的subscribeActual

public final class ObservableMap<T, U> extends AbstractObservableWithUpstream<T, U> {
    final Function<? super T, ? extends U> function;

    public ObservableMap(ObservableSource<T> source, Function<? super T, ? extends U> function) {
        super(source);
        this.function = function;
    }

    @Override
    public void subscribeActual(Observer<? super U> t) {
        source.subscribe(new MapObserver<T, U>(t, function));//source是傳遞進來的ObservableCreate
    }
}
複製程式碼

ObservableMap中的subscribeActual,會去呼叫ObservableCreatesubscribe方法,最後還是會去回撥 ObservableCreatesubscribeActual,不過這裡在回撥的過程中增加了一個引數MapObserver,這個引數只有在ObservableCreate發射器發射的時候才會被呼叫

3、ObservableCreate.subscribeActual

@Override
protected void subscribeActual(Observer<? super T> observer) {
    CreateEmitter<T> parent = new CreateEmitter<T>(observer);
    observer.onSubscribe(parent);

    try {
        source.subscribe(parent);
    } catch (Throwable ex) {
        Exceptions.throwIfFatal(ex);
        parent.onError(ex);
    }
}
複製程式碼

ObservableCreate.subscribeActual中,會接收一個Observer的引數,這個時候的Observer的引數是從ObservableMap中傳遞過來的MapObserver,當CreateEmitter發射onNext的時候,就會在當前的MapObserver物件onNext進行處理

4、MapObserver.onNext

static final class MapObserver<T, U> extends BasicFuseableObserver<T, U> {
    final Function<? super T, ? extends U> mapper;

    MapObserver(Observer<? super U> actual, Function<? super T, ? extends U> mapper) {
        super(actual);
        this.mapper = mapper;
    }

    @Override
    public void onNext(T t) {
        if (done) {
            return;
        }

        if (sourceMode != NONE) {
            actual.onNext(null);
            return;
        }

        U v;

        try {
            v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
        } catch (Throwable ex) {
            fail(ex);
            return;
        }
        actual.onNext(v);
    }
    
    ......
}
複製程式碼

onNext主要做了兩個事情,一個是mapper.apply(t),這個就是map操作符所實現的方法,這裡就將原來的值轉換成新的值,一個是actual.onNext(v),將轉換出來的新值v繼續onNext出去,這裡的actual就是在建構函式中傳遞進來的ObservableCreate,這裡就已經將資料項經過map的操作符後繼續執行後面正常的發射流程

小結

如圖所示

在這裡插入圖片描述

10.5 RxJava的執行緒切換原理

知識點:

  • 理解在工作執行緒上為什麼能執行耗時操作
  • 理解在UI執行緒為什麼能執行更新UI的操作

沿用上面的例子,線上程切換的過程中,無非就是相當於不同的操作符繼續運算元據項而已,根本的實現思路和map等操作符是一樣的,也是通過巢狀Observable的過程來執行的,只不過是執行緒切換的操作符內部實現的邏輯有區別而已。通過我們以往的思路去想,這兩個知識點無非就是啟動執行緒池去執行耗時任務,而UI執行緒則是交給Handler去處理,RxJava執行緒切換的原理就是這樣的

Observable
        .create(new ObservableOnSubscribe<String>() {
            @Override
            //預設在主執行緒裡執行該方法
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                e.onNext("俊俊俊很帥");
                e.onNext("你值得擁有");
                e.onNext("取消關注");
                e.onNext("但還是要保持微笑");
                e.onComplete();
            }
        })
        .map(new Function<String, String>() {
            @Override
            public String apply(String s) throws Exception {
                return "Hello";
            }
        })
        //將被觀察者切換到子執行緒
        .subscribeOn(Schedulers.io())
        //將觀察者切換到主執行緒  需要在Android環境下執行
        .observeOn(AndroidSchedulers.mainThread())
        //建立觀察者並訂閱
        .subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                if (!d.isDisposed()) {
                    d.dispose();
                }
            }

            @Override
            public void onNext(String s) {
                System.out.println("onNext=" + s);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("onNext=" + e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
複製程式碼

基礎概念:

  • Schedulers:排程器的管理者。管理著多種不同種類的Scheduler
  • Scheduler:排程器。負責執行緒Worker的建立createWorker(),排程Worker的執行schedule()
  • Worker:抽象的工作執行緒。被執行緒排程器管理,負責執行緒的建立和執行

在原始碼中,我們需要先熟悉這三者之間的關係到底是如何運作的

以下是RxJava原始碼

1、observeOn()

@CheckReturnValue
@SchedulerSupport("custom")
public final Observable<T> observeOn(Scheduler scheduler) {
    return this.observeOn(scheduler, false, bufferSize());
}

@CheckReturnValue
@SchedulerSupport("custom")
public final Observable<T> observeOn(Scheduler scheduler, boolean delayError, int bufferSize) {
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");
    ObjectHelper.verifyPositive(bufferSize, "bufferSize");
    return RxJavaPlugins.onAssembly(new ObservableObserveOn(this, scheduler, delayError, bufferSize));
}
複製程式碼

當前的Observable已經被轉換成ObservableObserveOn

public final class ObservableObserveOn<T> extends AbstractObservableWithUpstream<T, T> {
    final Scheduler scheduler;
    final boolean delayError;
    final int bufferSize;

    public ObservableObserveOn(ObservableSource<T> source, Scheduler scheduler, boolean delayError, int bufferSize) {
        super(source);
        this.scheduler = scheduler;
        this.delayError = delayError;
        this.bufferSize = bufferSize;
    }

    protected void subscribeActual(Observer<? super T> observer) {
        if (this.scheduler instanceof TrampolineScheduler) {
            this.source.subscribe(observer);
        } else {
            //1、建立工作執行緒
            Worker w = this.scheduler.createWorker();
            //2、訂閱之後,在發射的過程中
            this.source.subscribe(new ObservableObserveOn.ObserveOnObserver(observer, w, this.delayError, this.bufferSize));
        }

    }

    static final class ObserveOnObserver<T> extends BasicIntQueueDisposable<T> implements Observer<T>, Runnable {
        private static final long serialVersionUID = 6576896619930983584L;
        final Observer<? super T> actual;
        final Worker worker;
        final boolean delayError;
        final int bufferSize;
        SimpleQueue<T> queue;
        
        public void onNext(T t) {
            if (!this.done) {
                if (this.sourceMode != 2) {
                    this.queue.offer(t);
                }
                //3、在OnNext中執行
                this.schedule();
            }
        }

        void schedule() {
            if (this.getAndIncrement() == 0) {
                //4、執行工作執行緒
                this.worker.schedule(this);
            }
        }
    }
}
複製程式碼

其三者的關係簡單的說就是在每次訂閱的時候,都會去建立出對應的工作執行緒,這個工作執行緒取決於你傳遞的引數是哪個Worker,在發射器發射的過程中,這個工作執行緒總會去執行它的回撥schedule,其實大部分的操作就是在schedule裡面執行執行緒。搞懂了三者的關係之後,分析執行緒切換就簡單多了,就相當於工廠一樣,給個具體的任務給到具體的工人去執行,很像工廠的流水線,我們已經確定下來了流水線的流程了,這個時候我們就需要去關心引數具體是什麼東西了。在閱讀subscribeOn、observeOn前,我們先看看這兩個方法中的引數都是什麼

1、Schedulers.io()

public final class Schedulers {

    @NonNull
    static final Scheduler IO;
    
    static {
        
        SINGLE = RxJavaPlugins.initSingleScheduler(new SingleTask());

        COMPUTATION = RxJavaPlugins.initComputationScheduler(new ComputationTask());
        
        //1、在初始化的時候就構建出了IOTask,initIoScheduler會去執行IOTask的call方法
        IO = RxJavaPlugins.initIoScheduler(new IOTask());

        TRAMPOLINE = TrampolineScheduler.instance();

        NEW_THREAD = RxJavaPlugins.initNewThreadScheduler(new NewThreadTask());
    }
    
    static final class IOTask implements Callable<Scheduler> {
        @Override
        public Scheduler call() throws Exception {
            //2、IOTask的call方法會去獲取IoHolder的值
            return IoHolder.DEFAULT;
        }
    }
    
    static final class IoHolder {
        //3、建立IoScheduler
        static final Scheduler DEFAULT = new IoScheduler();
    }
    
    public static Scheduler io() {
        //Schedulers.io():它會去獲取前面3步建立出來的IoScheduler物件
        return RxJavaPlugins.onIoScheduler(IO); //返回IO自身
    }
}
複製程式碼

其正在實現在IoScheduler,其表示管理Io執行緒的管理者

public final class IoScheduler extends Scheduler {
    
    final AtomicReference<CachedWorkerPool> pool;
    
    static final CachedWorkerPool NONE;
    static {
        ......
        //1、建立CachedWorkerPool
        NONE = new CachedWorkerPool(0, null, WORKER_THREAD_FACTORY);
    }
    
    static final class CachedWorkerPool implements Runnable {
        
        ......
        private final ScheduledExecutorService evictorService;
        private final Future<?> evictorTask;
        
        CachedWorkerPool(long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory) {
            ......
            Future<?> task = null;
            if (unit != null) {
                evictor = Executors.newScheduledThreadPool(1, EVICTOR_THREAD_FACTORY);
                task = evictor.scheduleWithFixedDelay(this, this.keepAliveTime, this.keepAliveTime, TimeUnit.NANOSECONDS);
            }
            evictorService = evictor;
            evictorTask = task;
        }
    }
    
    @NonNull
    @Override
    public Worker createWorker() {
        //2、建立具體的執行緒
        return new EventLoopWorker(pool.get());
    }
    
    static final class EventLoopWorker extends Scheduler.Worker {
        private final CompositeDisposable tasks;
        private final CachedWorkerPool pool;
        private final ThreadWorker threadWorker;

        final AtomicBoolean once = new AtomicBoolean();

        EventLoopWorker(CachedWorkerPool pool) {
            this.pool = pool;
            this.tasks = new CompositeDisposable();
            this.threadWorker = pool.get();
        }

        @NonNull
        @Override
        public Disposable schedule(@NonNull Runnable action, long delayTime, @NonNull TimeUnit unit) {
            if (tasks.isDisposed()) {
                // don't schedule, we are unsubscribed
                return EmptyDisposable.INSTANCE;
            }
            //3、最終會去呼叫ThreadWorker的scheduleActual
            return threadWorker.scheduleActual(action, delayTime, unit, tasks);
        }
    }
    
    //4、由於ThreadWorker沒有scheduleActual,在父類中找NewThreadWorker
    static final class ThreadWorker extends NewThreadWorker {
        private long expirationTime;

        ThreadWorker(ThreadFactory threadFactory) {
            super(threadFactory);
            this.expirationTime = 0L;
        }

        public long getExpirationTime() {
            return expirationTime;
        }

        public void setExpirationTime(long expirationTime) {
            this.expirationTime = expirationTime;
        }
    }
}
複製程式碼

NewThreadWorker,最終還是呼叫executor.submit()executor.schedule()

@NonNull
public ScheduledRunnable scheduleActual(final Runnable run, long delayTime, @NonNull TimeUnit unit, @Nullable DisposableContainer parent) {
    Runnable decoratedRun = RxJavaPlugins.onSchedule(run);

    ScheduledRunnable sr = new ScheduledRunnable(decoratedRun, parent);

    if (parent != null) {
        if (!parent.add(sr)) {
            return sr;
        }
    }

    Future<?> f;
    try {
        if (delayTime <= 0) {
            f = executor.submit((Callable<Object>)sr);
        } else {
            f = executor.schedule((Callable<Object>)sr, delayTime, unit);
        }
        sr.setFuture(f);
    } catch (RejectedExecutionException ex) {
        if (parent != null) {
            parent.remove(sr);
        }
        RxJavaPlugins.onError(ex);
    }

    return sr;
}
複製程式碼

2、AndroidSchedulers.mainThread()

public final class AndroidSchedulers {

    private static final class MainHolder {

        static final Scheduler DEFAULT = new HandlerScheduler(new Handler(Looper.getMainLooper()));
    }

    private static final Scheduler MAIN_THREAD = RxAndroidPlugins.initMainThreadScheduler(
            new Callable<Scheduler>() {
                @Override public Scheduler call() throws Exception {
                    return MainHolder.DEFAULT;
                }
            });

    /** A {@link Scheduler} which executes actions on the Android main thread. */
    public static Scheduler mainThread() {
        return RxAndroidPlugins.onMainThreadScheduler(MAIN_THREAD);
    }
}
複製程式碼

返回一個HandlerScheduler,建立單例模式的主執行緒Handler

final class HandlerScheduler extends Scheduler {
    private final Handler handler;

    HandlerScheduler(Handler handler) {
        this.handler = handler;
    }

    public Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit) {
        if (run == null) {
            throw new NullPointerException("run == null");
        } else if (unit == null) {
            throw new NullPointerException("unit == null");
        } else {
            run = RxJavaPlugins.onSchedule(run);
            HandlerScheduler.ScheduledRunnable scheduled = new HandlerScheduler.ScheduledRunnable(this.handler, run);
            this.handler.postDelayed(scheduled, unit.toMillis(delay));
            return scheduled;
        }
    }

    public Worker createWorker() {
        //建立具體工作執行緒
        return new HandlerScheduler.HandlerWorker(this.handler);
    }

    ......
}
複製程式碼

就好像我們上面分析的三者關係一樣,Schedule最終還是會管理著具體的工作執行緒

private static final class HandlerWorker extends Worker {
    private final Handler handler;

    private volatile boolean disposed;

    HandlerWorker(Handler handler) {
        this.handler = handler;
    }

    @Override
    public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
        if (run == null) throw new NullPointerException("run == null");
        if (unit == null) throw new NullPointerException("unit == null");

        if (disposed) {
            return Disposables.disposed();
        }

        run = RxJavaPlugins.onSchedule(run);
        //包裝新的Runnable交給Handler
        ScheduledRunnable scheduled = new ScheduledRunnable(handler, run);

        Message message = Message.obtain(handler, scheduled);
        message.obj = this; // Used as token for batch disposal of this worker's runnables.

        handler.sendMessageDelayed(message, unit.toMillis(delay));

        // Re-check disposed state for removing in case we were racing a call to dispose().
        if (disposed) {
            handler.removeCallbacks(scheduled);
            return Disposables.disposed();
        }

        return scheduled;
    }

    @Override
    public void dispose() {
        disposed = true;
        handler.removeCallbacksAndMessages(this /* token */);
    }

    @Override
    public boolean isDisposed() {
        return disposed;
    }
}
複製程式碼

3、subscribeOn()

理解完引數後,回到我們的分析重點

public final Observable<T> subscribeOn(Scheduler scheduler) {
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");
    return RxJavaPlugins.onAssembly(new ObservableSubscribeOn<T>(this, scheduler));
}
複製程式碼

subscribeOn就如同普通操作符一樣,包裝一層ObservableSubscribeOn,在subscribe的時候真正走的還是subscribeActual

public final class ObservableSubscribeOn<T> extends AbstractObservableWithUpstream<T, T> {
    @Override
    public void subscribeActual(final Observer<? super T> s) {
        //使用三步曲分析法
        final SubscribeOnObserver<T> parent = new SubscribeOnObserver<T>(s);
    
        s.onSubscribe(parent);
        //3、將第三步的內容放到執行緒中去執行
        parent.setDisposable(scheduler.scheduleDirect(new SubscribeTask(parent)));
    }
    
    final class SubscribeTask implements Runnable {
        private final SubscribeOnObserver<T> parent;
    
        SubscribeTask(SubscribeOnObserver<T> parent) {
            this.parent = parent;
        }
    
        @Override
        public void run() {
            //3、回撥ObservableOnSubscribe的subscribe
            source.subscribe(parent);
        }
    }
}
複製程式碼

scheduler.scheduleDirect中會去執行Scheduler裡的方法,這裡的scheduler就是IoScheduler

@NonNull
public Disposable scheduleDirect(@NonNull Runnable run) {
    return scheduleDirect(run, 0L, TimeUnit.NANOSECONDS);
}

@NonNull
public Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
    final Worker w = createWorker();

    final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);

    DisposeTask task = new DisposeTask(decoratedRun, w);

    w.schedule(task, delay, unit);

    return task;
}
複製程式碼

回撥IoScheduler的createWorker()並執行w.schedule()

小結

如圖所示

在這裡插入圖片描述

10.6 RxJava的自定義Operator原理

知識點:

  • 自定義Operator是如何實現的

在講解之前,讓我們先回味下自定義Operator

public class CustomOperator implements ObservableOperator<String, List<String>> {

    @Override
    public Observer<? super List<String>> apply(final Observer<? super String> observer) throws Exception {
        return new Observer<List<String>>() {
            @Override
            public void onSubscribe(Disposable d) {
                observer.onSubscribe(d);
            }

            @Override
            public void onNext(List<String> strings) {
                observer.onNext(strings.toString());
            }

            @Override
            public void onError(Throwable e) {
                observer.onError(e);
            }

            @Override
            public void onComplete() {
                observer.onComplete();
            }
        };
    }
}
複製程式碼
Observable.create(new ObservableOnSubscribe<List<String>>() {
    @Override
    public void subscribe(@NonNull ObservableEmitter<List<String>> e) throws Exception {
      
    }
}).lift(new CustomOperator())
複製程式碼

自定義Operator如同普通的操作符原理差不多,用的是lift的操作符,只不過在lift裡面將邏輯的執行回撥到自定義的Operator的apply()

以下是RxJava原始碼

1、Observable.lift

public final <R> Observable<R> lift(ObservableOperator<? extends R, ? super T> lifter) {
    ObjectHelper.requireNonNull(lifter, "onLift is null");
    return RxJavaPlugins.onAssembly(new ObservableLift<R, T>(this, lifter));
}
複製程式碼

2、ObservableLift.subscribeActual

public final class ObservableLift<R, T> extends AbstractObservableWithUpstream<T, R> {
    /** The actual operator. */
    final ObservableOperator<? extends R, ? super T> operator;

    public ObservableLift(ObservableSource<T> source, ObservableOperator<? extends R, ? super T> operator) {
        super(source);
        this.operator = operator;
    }

    @Override
    public void subscribeActual(Observer<? super R> s) {
        Observer<? super T> observer;
        try {
            observer = ObjectHelper.requireNonNull(operator.apply(s), "Operator " + operator + " returned a null Observer");
        } catch (NullPointerException e) { // NOPMD
            throw e;
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            // can't call onError because no way to know if a Disposable has been set or not
            // can't call onSubscribe because the call might have set a Disposable already
            RxJavaPlugins.onError(e);

            NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS");
            npe.initCause(e);
            throw npe;
        }

        source.subscribe(observer);
    }
}
複製程式碼

可以看到程式碼非常快的就將傳遞進來的引數operator執行apply()

10.7 RxJava的自定義Transformer原理

知識點:

  • 自定義Transformer是如何實現的

在講解之前,讓我們先回味下自定義Transformer

public class NetWorkTransformer implements ObservableTransformer {

    @Override
    public ObservableSource apply(Observable upstream) {
        return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }
}
複製程式碼
Observable.create(new ObservableOnSubscribe<Integer>() {
    @Override
    public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
        
    }
}).compose(new CustomTransformer())
複製程式碼

自定義Transformer如同普通的操作符原理差不多,用的是compose的操作符,只不過在compose裡面將邏輯的執行回撥到自定義的Transformer的apply()

以下是RxJava原始碼

1、Observable.compose

public final <R> Observable<R> compose(ObservableTransformer<? super T, ? extends R> composer) {
    return wrap(((ObservableTransformer<T, R>) ObjectHelper.requireNonNull(composer, "composer is null")).apply(this));
}
複製程式碼

可以看到程式碼非常快的就將傳遞進來的引數composer執行apply(),這裡的wrap()只是將程式碼裹了一層,如果你想簡單的理解的話,可以理解為作者的強迫症犯了,只是為了讓所有程式碼看起來都比較規範,不然這裡實在和其他操作符的實現不一樣,我們可以追進去wrap()

public static <T> Observable<T> wrap(ObservableSource<T> source) {
    ObjectHelper.requireNonNull(source, "source is null");
    if (source instanceof Observable) {
        return RxJavaPlugins.onAssembly((Observable<T>)source);
    }
    return RxJavaPlugins.onAssembly(new ObservableFromUnsafeSource<T>(source));
}
複製程式碼

wrap()其實是對composer操作符做了Hook,因為所有操作符都會被RxJava去Hook住,這裡會在下面講到自定義Plugin原理的時候就明白了

10.8 RxJava的自定義Plugin原理

知識點:

  • 自定義Plugin是如何實現AOP的

在講解之前,讓我們先回味下自定義Plugin

public class CustomObservableAssembly implements Function<Observable, Observable> {
    @Override
    public Observable apply(Observable observable) throws Exception {
        System.out.println("CustomObservableAssembly observable.toString:" + observable.toString());
        return observable;
    }
}
複製程式碼
RxJavaPlugins.setOnObservableAssembly(new CustomObservableAssembly());
複製程式碼

在自定義Plugin中,類似於Android的術語Hook,但在這裡並不是真正的Hook,而是作者在寫RxJava的時候去限定一套規範,讓後面的所有操作符或其他操作等,都可以實現Hook的原理

以下是RxJava原始碼

1、RxJavaPlugins.setOnObservableAssembly

public static void setOnObservableAssembly(@Nullable Function<? super Observable, ? extends Observable> onObservableAssembly) {
    if (lockdown) {
        throw new IllegalStateException("Plugins can't be changed anymore");
    }
    RxJavaPlugins.onObservableAssembly = onObservableAssembly;
}
複製程式碼

RxJavaPlugins.setOnObservableAssembly只是對成員變數設定了自定義的值,這個時候onObservableAssembly就有了值,預設是為null的。設定完值就表示已經Hook成功了,當操作符執行的時候,是如何回撥我們Hook的函式的

2、Observable.create

public static <T> Observable<T> create(ObservableOnSubscribe<T> source) {
    ObjectHelper.requireNonNull(source, "source is null");
    return RxJavaPlugins.onAssembly(new ObservableCreate<T>(source));
}
複製程式碼

create相當於一個操作符,在每個操作符的裡面都會去執行一段RxJavaPlugins.onAssembly,這裡就是RxJava規定的規範,一開始我們只是說返回自身,但是有了Hook之後,就會回撥Hook函式,返回已經經過二次加工的自身

3、RxJavaPlugins.onAssembly

public static <T> Observable<T> onAssembly(@NonNull Observable<T> source) {
    Function<? super Observable, ? extends Observable> f = onObservableAssembly;
    if (f != null) {
        return apply(f, source);
    }
    return source;
}
複製程式碼

由於我們已經設定了新值,這裡的onObservableAssembly就不為null,不為null則執行apply()apply()就是我們Hook傳進去引數的回撥方法

10.9 美團WhiteBoard

美團的WhiteBoard其實是取自美團的開源框架Shield——開源的移動端頁面模組化開發框架中的程式碼,其主要作用是應用RxJava的Subject搭起元件間通訊的橋樑。實質上在WhiteBoard中,是將所有的元件的資料和Subject通訊的橋樑儲存起來,通過key作為元件的唯一標誌。不過比較可惜的是WhiteBoard使用的是RxJava1,不過關係不大,只要讀懂裡面的原始碼即可

1、WhiteBoard的初始化

初始化放在Activity/Fragment介面中,相當於通訊的橋樑,每個介面中僅有一個WhiteBoard的例項,並由所有元件共用

public abstract class ShieldFragment extends Fragment implements AgentCellBridgeInterface, DriverInterface {
    
    static final String TAG = ShieldFragment.class.getSimpleName();
    ......
    protected WhiteBoard whiteBoard;

    public ShieldFragment() {
        this.whiteBoard = new WhiteBoard();//初始化
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ......
        whiteBoard.onCreate(savedInstanceState);//對應生命週期
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ......
        whiteBoard.onDestory();//對應生命週期
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ......
        whiteBoard.onSaveInstanceState(outState);//對應生命週期
    }

    @Override
    public WhiteBoard getWhiteBoard() {
        return whiteBoard;//獲取例項
    }
}
複製程式碼

2、WhiteBoard監聽通知

元件只監聽某個key的事件,有通知的時候就能收到

public class MixCellAgent extends LightAgent {
    private MixCell mixCell;
    private Subscription loadingSubscription;
    private Subscription emptySubscription;

    public MixCellAgent(Fragment fragment, DriverInterface bridge, PageContainerInterface pageContainer) {
        super(fragment, bridge, pageContainer);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mixCell = new MixCell(getContext(), this);
        loadingSubscription = getWhiteBoard().getObservable(MixLoadingAgent.KEY_LOADING).filter(new Func1() {
            @Override
            public Object call(Object o) {
                return o instanceof Boolean && ((Boolean) o);
            }
        }).subscribe(new Action1() {
            @Override
            public void call(Object o) {
                loading();
            }
        });

        emptySubscription = getWhiteBoard().getObservable(MixLoadingAgent.KEY_EMPTY).filter(new Func1<Object, Boolean>() {
            @Override
            public Boolean call(Object o) {
                return o instanceof Boolean && ((Boolean) o);
            }
        }).subscribe(new Action1() {
            @Override
            public void call(Object o) {
                mixCell.onEmpty();
            }
        });
        
        ......
    }

    @Override
    public void onDestroy() {
        if (loadingSubscription != null) {
            loadingSubscription.unsubscribe();
            loadingSubscription = null;
        }

        if (emptySubscription != null) {
            emptySubscription.unsubscribe();
        }
        
        ......
    }
}
複製程式碼

3、WhiteBoard的傳送通知

WhiteBoard傳送通知就是呼叫WhiteBoard提供的所有put方法,具體是如何收到訊息的,還需要通過WhiteBoard的原始碼看下

public class MixLoadingAgent extends LightAgent implements MixLoadingCell.MixLoadingListener {
    public static final String KEY_LOADING = "loading";
    public static final String KEY_EMPTY = "empty";
    public static final String KEY_FAILED = "failed";
    public static final String KEY_MORE = "more";
    public static final String KEY_DONE = "done";

    private MixLoadingCell mixLoadingCell;

    public MixLoadingAgent(Fragment fragment, DriverInterface bridge, PageContainerInterface pageContainer) {
        super(fragment, bridge, pageContainer);
        mixLoadingCell = new MixLoadingCell(getContext());
        mixLoadingCell.setOnMixLoadingListener(this);
    }

    @Override
    public SectionCellInterface getSectionCellInterface() {
        return mixLoadingCell;
    }

    @Override
    public void onLoading() {
        getWhiteBoard().putBoolean(KEY_LOADING, true);
    }

    @Override
    public void onEmpty() {
        getWhiteBoard().putBoolean(KEY_EMPTY, true);
    }

    @Override
    public void onFailed() {
        getWhiteBoard().putBoolean(KEY_FAILED, true);
    }

    @Override
    public void onMore() {
        getWhiteBoard().putBoolean(KEY_MORE, true);
    }

    @Override
    public void onDone() {
        getWhiteBoard().putBoolean(KEY_DONE, true);
    }
}
複製程式碼

4、WhiteBoard的原理

最後只需要獲取例項後傳送通知即可,getWhiteBoard().putBoolean(key)。WhiteBoard原理是隻要還是Subject的橋樑的作用

public class WhiteBoard {

    public static final String WHITE_BOARD_DATA_KEY = "White_Board_Data";
    protected Bundle mData;//儲存所有元件的資料
    protected HashMap<String, Subject> subjectMap;//儲存所有元件的通訊橋樑

    public WhiteBoard() {
        this(null);
    }

    public WhiteBoard(Bundle data) {
        mData = data;
        if (mData == null) {
            mData = new Bundle();//初始化
        }

        subjectMap = new HashMap<>();//初始化
    }

    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mData = savedInstanceState.getBundle(WHITE_BOARD_DATA_KEY);
        }

        if (mData == null) {
            mData = new Bundle();
        }
    }

    public void onSaveInstanceState(Bundle outState) {
        if (outState != null) {

            // here we must save a new copy of the mData into the outState
            outState.putBundle(WHITE_BOARD_DATA_KEY, new Bundle(mData));
        }
    }

    public void onDestory() {
        subjectMap.clear();
        mData.clear();
    }

    //通過key獲取某元件的橋樑
    public Observable getObservable(final String key) {

        Subject res = null;
        if (subjectMap.containsKey(key)) {
            res = subjectMap.get(key);
        } else {
            res = PublishSubject.create();
            subjectMap.put(key, res);
        }
        if (getData(key) != null) {
            return res.startWith(getData(key));//帶上已經儲存過的資料
        } else {
            return res;
        }
    }

    //通過key通知某元件
    protected void notifyDataChanged(String key) {
        if (subjectMap.containsKey(key)) {
            subjectMap.get(key).onNext(mData.get(key));
        }
    }

    //移除元件中的資料
    public void removeData(String key) {
        mData.remove(key);
        notifyDataChanged(key);
    }
    
    //每次put值的時候,就會去通知對應的元件
    public void putBoolean(@Nullable String key, boolean value) {
        mData.putBoolean(key, value);
        notifyDataChanged(key);
    }
    public void putInt(@Nullable String key, int value) {
        mData.putInt(key, value);
        notifyDataChanged(key);
    }
    public void putString(@Nullable String key, @Nullable String value) {
        mData.putString(key, value);
        notifyDataChanged(key);
    }
    ......

    public double getDouble(String key) {
        return mData.getDouble(key);
    }
    public String getString(String key, String defaultValue) {
        return mData.getString(key, defaultValue);
    }
    ......
}
複製程式碼

相關文章