文章目录
  1. 1. 概述:EventBus主要的流程包括以下三个方面:
    1. 1.1. 1.EventBus的初始化过程
    2. 1.2. 2.EventBus的注册过程
    3. 1.3. 3.EventBus的事件发送过程
  2. 2. EventBus的初始化过程:
    1. 2.1. 1.单利初始化
    2. 2.2. 2.使用Builder初始化
      1. 2.2.1. 通过EventBus的静态方法builder()创建EventBusBuilder对象
  3. 3. 第一部分总结:简易vs定制
    1. 3.1. 通过EventBus提供的两种初始化方式,我们可以通过单利简易的使用EventBus为我们提供的默认配置。
    2. 3.2. 而通过第二种Builder模式创建对象开发者可以定制EventBus对象。
  4. 4. EventBus的注册过程:
    1. 4.1. 真正的订阅逻辑:

概述:EventBus主要的流程包括以下三个方面:

1.EventBus的初始化过程

2.EventBus的注册过程

3.EventBus的事件发送过程

EventBus的初始化过程:

1.单利初始化

1
2
3
4
5
6
7
8
9
10
11
12
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
//这里其实使用了默认的Builder
//public EventBus() { this(DEFAULT_BUILDER); }
}
}
}
return defaultInstance;
}

2.使用Builder初始化

通过EventBus的静态方法builder()创建EventBusBuilder对象
1
2
3
public static EventBusBuilder builder() {
return new EventBusBuilder();
}
1
EventBus eventBus = EventBus.builder().xxx.build();

第一部分总结:简易vs定制

通过EventBus提供的两种初始化方式,我们可以通过单利简易的使用EventBus为我们提供的默认配置。

而通过第二种Builder模式创建对象开发者可以定制EventBus对象。


EventBus的注册过程:

1
2
3
4
5
6
7
8
9
10
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();//被注册类的类对象
//通过被注册的类对象找到所有响应方法
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);//遍历所有响应方法逐个订阅
}
}
}

真正的订阅逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/***
*
* @param subscriber register的类
* @param subscriberMethod 类里的subscriber方法
*/

private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;//事件类的类对象
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//subscriptionsByEventType存储了<事件类的Class对象,所有的订阅者>
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}

int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}

List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
//黏性和继承性的处理
if (subscriberMethod.sticky) {//是黏性事件
if (eventInheritance) {//有继承
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
//当存在很多黏性事件的时候迭代所有事件是很低效的,应该换用高效点的数据结构去查找
//例如,用一个额外的Map来存<父类,List<子类>>
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//根据线程类型调用对应的事件方法
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {//判断threadMode
case POSTING:
invokeSubscriber(subscription, event);
break;
case MAIN:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case MAIN_ORDERED:
if (mainThreadPoster != null) {
mainThreadPoster.enqueue(subscription, event);
} else {
// temporary: technically not correct as poster not decoupled from subscriber
invokeSubscriber(subscription, event);
}
break;
case BACKGROUND:
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);
}
}
文章目录
  1. 1. 概述:EventBus主要的流程包括以下三个方面:
    1. 1.1. 1.EventBus的初始化过程
    2. 1.2. 2.EventBus的注册过程
    3. 1.3. 3.EventBus的事件发送过程
  2. 2. EventBus的初始化过程:
    1. 2.1. 1.单利初始化
    2. 2.2. 2.使用Builder初始化
      1. 2.2.1. 通过EventBus的静态方法builder()创建EventBusBuilder对象
  3. 3. 第一部分总结:简易vs定制
    1. 3.1. 通过EventBus提供的两种初始化方式,我们可以通过单利简易的使用EventBus为我们提供的默认配置。
    2. 3.2. 而通过第二种Builder模式创建对象开发者可以定制EventBus对象。
  4. 4. EventBus的注册过程:
    1. 4.1. 真正的订阅逻辑: