EventBus 是人們在日常開發(fā)中經(jīng)常會用到的開源庫,即使是不直接用的人,也多少借鑒過事件總線的用法。而且EventBus的代碼其實(shí)是非常簡單的,可以試著閱讀一下。
源碼閱讀系列不采用對功能進(jìn)行歸類的方法進(jìn)行閱讀,而是采用一個剛開始閱讀源碼的視角,從我們平時的API調(diào)用,一步步的去理解設(shè)計(jì)意圖和實(shí)現(xiàn)原理。
從這里開始
從這里開始吧,我們最常用的地方就是給一個函數(shù)添加上注解,我們先拋開apt生成的table,只看這個運(yùn)行時版本的訂閱設(shè)定。
// eventbus/Subscribe@Documented@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD})public @interface Subscribe { ThreadMode threadMode() default ThreadMode.POSTING; /** * If true, delivers the most recent sticky event (posted with * {@link EventBus#postSticky(Object)}) to this subscriber (if event available). */ boolean sticky() default false; /** Subscriber priority to influence the order of event delivery. * Within the same delivery thread ({@link ThreadMode}), higher priority subscribers will receive events before * others with a lower priority. The default priority is 0. Note: the priority does *NOT* affec