Skip to content

Commit f49e711

Browse files
committed
Add EventService#subscribe(EventSubscriber) method
Otherwise, there is no way to register an EventSubscriber, because the DefaultEventBus is not publicly accessible.
1 parent 0988a1d commit f49e711

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.93.2-SNAPSHOT</version>
13+
<version>2.94.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.</description>

src/main/java/org/scijava/event/DefaultEventService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ public List<EventSubscriber<?>> subscribe(final Object o) {
138138
return subscribers;
139139
}
140140

141+
@Override
142+
public void subscribe(final EventSubscriber<?> subscriber) {
143+
eventBus.subscribe(subscriber.getClass(), subscriber);
144+
}
145+
141146
@Override
142147
public void unsubscribe(final Collection<EventSubscriber<?>> subscribers) {
143148
for (final EventSubscriber<?> subscriber : subscribers) {

src/main/java/org/scijava/event/EventService.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ public interface EventService extends SciJavaService {
125125
*/
126126
List<EventSubscriber<?>> subscribe(Object o);
127127

128+
/**
129+
* Subscribes the given {@link EventSubscriber} to its associated event class.
130+
* Its {@link EventSubscriber#onEvent} method will be called whenever an event
131+
* of the matching type is published.
132+
* <p>
133+
* <strong>Important note:</strong> The event service does <em>not</em> keep a
134+
* strong reference to the subscriber! If you use this method, you are also
135+
* responsible for keeping a reference to the subscriber, or else it is likely
136+
* to be garbage collected, and thus no longer respond to events as intended.
137+
* One simple way to force a strong reference to exist is to add it to
138+
* SciJava's {@link org.scijava.object.ObjectService} via
139+
* {@link org.scijava.object.ObjectService#addObject}.
140+
* </p>
141+
*
142+
* @param subscriber the event subscriber to register
143+
*/
144+
void subscribe(EventSubscriber<?> subscriber);
145+
128146
/**
129147
* Removes all the given subscribers; they will no longer be notified when
130148
* events are published.

0 commit comments

Comments
 (0)