Topic: make reentrant
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
3d57eb633e
commit
65292aaa79
@ -24,7 +24,9 @@ public class Topic {
|
|||||||
if (subscribers == null) {
|
if (subscribers == null) {
|
||||||
subscribers = new LinkedList<>();
|
subscribers = new LinkedList<>();
|
||||||
}
|
}
|
||||||
subscribers.add(new WeakReference<>(sub));
|
synchronized (subscribers) {
|
||||||
|
subscribers.add(new WeakReference<>(sub));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unsubscribe() {
|
public void unsubscribe() {
|
||||||
@ -32,10 +34,12 @@ public class Topic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void unsubscribe(Subscriber sub) {
|
public void unsubscribe(Subscriber sub) {
|
||||||
for (Iterator<WeakReference<Subscriber>> i = subscribers.iterator(); i.hasNext();) {
|
synchronized (subscribers) {
|
||||||
WeakReference<Subscriber> subscriber = i.next();
|
for (Iterator<WeakReference<Subscriber>> i = subscribers.iterator(); i.hasNext(); ) {
|
||||||
if (subscriber.get() == null || subscriber.get() == sub) {
|
WeakReference<Subscriber> subscriber = i.next();
|
||||||
i.remove();
|
if (subscriber.get() == null || subscriber.get() == sub) {
|
||||||
|
i.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,7 +62,11 @@ public class Topic {
|
|||||||
state = PUBLISHED;
|
state = PUBLISHED;
|
||||||
this.results = results;
|
this.results = results;
|
||||||
if (subscribers != null) {
|
if (subscribers != null) {
|
||||||
for (WeakReference<Subscriber> subscriber : subscribers) {
|
List<WeakReference<Subscriber>> subscribersCopy = new LinkedList<>();
|
||||||
|
synchronized (subscribers) {
|
||||||
|
subscribersCopy.addAll(subscribers);
|
||||||
|
}
|
||||||
|
for (WeakReference<Subscriber> subscriber : subscribersCopy) {
|
||||||
if (subscriber.get() != null)
|
if (subscriber.get() != null)
|
||||||
subscriber.get().onTopicPublished(this);
|
subscriber.get().onTopicPublished(this);
|
||||||
}
|
}
|
||||||
@ -86,12 +94,15 @@ public class Topic {
|
|||||||
topic.subscribe(this);
|
topic.subscribe(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default void unsubscribeTopics() {
|
default void unsubscribeTopics() {
|
||||||
for (Topic event : getSubscription()) {
|
for (Topic event : getSubscription()) {
|
||||||
event.unsubscribe(this);
|
event.unsubscribe(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onTopicPublished(Topic topic);
|
void onTopicPublished(Topic topic);
|
||||||
|
|
||||||
Topic[] getSubscription();
|
Topic[] getSubscription();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user