zoukankan      html  css  js  c++  java
  • [EventBus源码解析] EventBus.post 方法详述

    前情概要

      上一篇blog我们了解了EventBus中register/unregister的过程,对EventBus如何实现观察者模式有了基本的认识。今天我们来看一下它是如何分发一个特定事件的,即post(Object event)方法。

    本篇概述

      EventBus中事件的分发与响应,post 方法。

    post 方法

    public void post(Object event) {
        PostingThreadState postingState = currentPostingThreadState.get();
        List<Object> 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;
            }
        }
    }

      postingState(分发状态)具有ThreadLocal属性,包含一个eventQueue,用来保存当前线程分发中的event,在while循环中,逐一调用 postSingleEvent(eventQueue.remove(0), postingState),清空发送队列。

    postSingleEvent 方法

    private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<?> eventClass = event.getClass();
        boolean subscriptionFound = false;
        if (eventInheritance) {
            List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
            int countTypes = eventTypes.size();
            for (int h = 0; h < countTypes; h++) {
                Class<?> clazz = eventTypes.get(h);
                subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
            }
        } else {
            subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
        }
        if (!subscriptionFound) {
            if (logNoSubscriberMessages) {
                Log.d(TAG, "No subscribers registered for event " + eventClass);
            }
            if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
                    eventClass != SubscriberExceptionEvent.class) {
                post(new NoSubscriberEvent(this, event));
            }
        }
    }

      这个方法用来分发单个事件,如果EventBus支持继承,则分发继承的所有基类事件;若没有找到订阅者,则根据logNoSubscribeMessages、sendNoSubscriberEvent这两个标志位处理相应的逻辑(打日志、发送NoSubscriberEvent事件)。

      再进一步,我们看看 postSingleEventForEventType 方法。

    postSingleEventForEventType 方法

      在仔细阅读源代码之前,我们应该大致就能够猜想得到,这一步,理应是遍历该事件类型的订阅者列表,找到对应的订阅者后把事件发送出去;结合之前了解过的EventBus响应事件的四种模式(PostThread, MainThread, BackgroundThread, Async),接下来关注的重点是分发事件时如何处理这一段逻辑。

    private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
        CopyOnWriteArrayList<Subscription> 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;
    }

      通过eventType找到对应的subscriptions后,逐一调用postToSubscription方法,其中对四种事件相应方式进行了处理。最终是通过反射进行了对应方法的调用。

    private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
            case PostThread:
                invokeSubscriber(subscription, event);
                break;
            case MainThread:
                if (isMainThread) {
                    invokeSubscriber(subscription, event);
                } else {
                    mainThreadPoster.enqueue(subscription, event);
                }
                break;
            case BackgroundThread:
                if (isMainThread) {
                    backgroundPoster.enqueue(subscription, event);
                } else {
                    invokeSubscriber(subscription, event);
                }
                break;
            case Async:
                asyncPoster.enqueue(subscription, event);
                break;
            default:
                throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
        }
    }

    总结

      分发事件的过程并不复杂,真正核心的一句话就可以概括:根据eventType找到对应的订阅者,通过反射进行具体方法调用。

    下期预告

      四种线程模式实现方式解析。

  • 相关阅读:
    python_异常处理
    python_类与对象
    函数
    字符串(查找,替换,分割)
    容器类型的数据
    条件语句
    关于WinSock编程的多线程控制
    利用Delphi编写Socket通信程序
    SQL Server数据库开发的二十一条军规
    SQL Server中的日期格式化
  • 原文地址:https://www.cnblogs.com/maozhige/p/4725095.html
Copyright © 2011-2022 走看看