-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventBus.java
118 lines (85 loc) · 3.4 KB
/
EventBus.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.ruoxu.eventbus;
import android.util.Log;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Created by wangli on 16/12/23.
*/
public class EventBus {
private static EventBus sDefaultBus;
//Map存储 EventType 为key,CopyOnWriteArrayList为 订阅者Subscriber集合
private final Map<EventType,CopyOnWriteArrayList<Subscriber>> mSubcriberMap = new ConcurrentHashMap<>();
EventDispatcher mDispatcher = new EventDispatcher(mSubcriberMap);
ThreadLocal<Queue<EventType>> mLocalEvents = new ThreadLocal<Queue<EventType>>() {
protected Queue<EventType> initialValue() {
return new ConcurrentLinkedQueue<EventType>();
};
};
public static EventBus getDefault() {
if (sDefaultBus == null) {
synchronized (EventBus.class) {
if (sDefaultBus == null) {
sDefaultBus = new EventBus();
}
}
}
return sDefaultBus;
}
public void notifyDataChange(Object event){ //等价于EventBus的 post发送事件
notifyDataChange(event, EventType.DEFAULT_TAG);
}
public void notifyDataChange(Object event, String tag) {
if (event == null) {
throw new RuntimeException("事件为空");
}
mLocalEvents.get().offer(new EventType(tag));
mDispatcher.dispatchEvents(mLocalEvents,event);
}
public Subscriber register(Subscriber subscriber){
register(subscriber,EventType.DEFAULT_TAG);
return subscriber;
}
public Subscriber register(Subscriber subscriber,String tag){
EventType eventType = new EventType(tag);
CopyOnWriteArrayList<Subscriber> subscribers = mSubcriberMap.get(eventType);
if (subscribers == null) {
subscribers = new CopyOnWriteArrayList<>();
}
if (subscribers.contains(subscriber)) {
Log.d("EventBus", "已经注册过了该观察者了");
} else {
subscribers.add(subscriber);
}
mSubcriberMap.put(eventType, subscribers);
return subscriber;
}
public void unregister(Subscriber subscriber) {
if (subscriber == null) {
return;
}
synchronized (this) {
Iterator<CopyOnWriteArrayList<Subscriber>> iterator = mSubcriberMap.values().iterator();
while (iterator.hasNext()) {
CopyOnWriteArrayList<Subscriber> subscriptions = iterator.next();
if (subscriptions != null) {
Iterator<Subscriber> subIterator = subscriptions.iterator();
while (subIterator.hasNext()) {
Subscriber subscription = subIterator.next();
if (subscription.equals(subscriber)) {
Log.d("", "### 移除订阅 " + subscriber.getClass().getName());
subscriptions.remove(subscription);
}
}
}
// 如果针对某个Event的订阅者数量为空了,那么需要从map中清除
if (subscriptions == null || subscriptions.size() == 0) {
iterator.remove();
}
}
}
}
}