Eventbus3.0源码解析(下)

我们来分析Eventbus3.0 的 post()方法

Eventbus3.0源码解析(下)_第1张图片
class-relation.png

subscriptionsByEventType : key 为订阅事件event Class , value 为 所有订阅者的数据 , 可以根据 EventType 查找订阅事件。
typesBySubscriber : key 为当前项目有哪些Java类 注册了事件 , value 为这些类都订阅了哪些接收方法 , 根据我们的订阅对象找到 EventType。

   /** Posts the given event to the event bus. */
    public void post(Object event) {
        PostingThreadState postingState = currentPostingThreadState.get();
        List eventQueue = postingState.eventQueue;
        eventQueue.add(event);

        if (!postingState.isPosting) {
            postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
            postingState.isPosting = true;
            if (postingState.canceled) {
                throw new EventBusException("Internal error. Abort state was not reset");
            }
            try {
                while (!eventQueue.isEmpty()) {
                    postSingleEvent(eventQueue.remove(0), postingState);
                }
            } finally {
                postingState.isPosting = false;
                postingState.isMainThread = false;
            }
        }
    }
 
 

currentPostingThreadState 是一个 ThreadLocal 对象 他会在每个线程有自己的 PostingThreadState 没有存过就创建一个默认的对象;

 
final static class PostingThreadState {
    //通过post方法参数传入的事件集合
    final List eventQueue = new ArrayList(); 
    boolean isPosting; //是否正在执行postSingleEvent()方法
    boolean isMainThread;
    Subscription subscription;
    Object event;
    boolean canceled;
    }
 
 

PostingThreadState 封装了发送需要的一些数据

postSingleEvent()里会调用postSingleEventForEventType()

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class eventClass) {
        CopyOnWriteArrayList subscriptions;
        synchronized (this) {
            subscriptions = subscriptionsByEventType.get(eventClass);
        }
        if (subscriptions != null && !subscriptions.isEmpty()) {
            for (Subscription subscription : subscriptions) {
                postingState.event = event;
                postingState.subscription = subscription;
                boolean aborted = false;
                try {
                    postToSubscription(subscription, event, postingState.isMainThread);
                    aborted = postingState.canceled;
                } finally {
                    postingState.event = null;
                    postingState.subscription = null;
                    postingState.canceled = false;
                }
                if (aborted) {
                    break;
                }
            }
            return true;
        }
        return false;
    }

subscriptionsByEventType
key 事件类型
value 接收的事件的方法List

postToSubscription()
会根据不同的线程来执行不同的方法向订阅者发送事件其中invokeSubscriber() 是根据方法名来反射操作的;

unregister

 /** Unregisters the given subscriber from all event classes. */
    public synchronized void unregister(Object subscriber) {
        List> subscribedTypes = typesBySubscriber.get(subscriber);
        if (subscribedTypes != null) {
            for (Class eventType : subscribedTypes) {
                unsubscribeByEventType(subscriber, eventType);
            }
            typesBySubscriber.remove(subscriber);
        } else {
            Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
        }
    }

typesBySubscriber
是个map key 为我们传入的this 也就是当前类
value是当前类所有接收事件方法class类型

 /** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
    private void unsubscribeByEventType(Object subscriber, Class eventType) {
        List subscriptions = subscriptionsByEventType.get(eventType);
        if (subscriptions != null) {
            int size = subscriptions.size();
            for (int i = 0; i < size; i++) {
                Subscription subscription = subscriptions.get(i);
                if (subscription.subscriber == subscriber) {
                    subscription.active = false;
                    subscriptions.remove(i);
                    i--;
                    size--;
                }
            }
        }
    }

subscriptionsByEventType map对象 , 根据我们的eventType 来取出订阅这个事件所有方法, 判断是当前要取消注册这个事件的元素就在集合remove掉;

总结 :

发送过程

  1. 利用localThread来实现取出当前线程的封装收
  2. 循环一个map取出当前事件的所有订阅者
  3. 利用反射发送事件

取消注册逻辑

  1. 利用2个map判断其事件类型,来移除要解注册元素

注册逻辑

  1. 利用SubscriberMethodFinder 对象来取出 List 集合,他是查看当前类有哪些接收事件
  2. 然后遍历他,把接收事件的方法 存入2个总的map中;
注意

1.eventBus 不要多次注册,否则会导致方法接收被多次调用

你可能感兴趣的:(Eventbus3.0源码解析(下))