Skip to content

Commit f58ccb1

Browse files
ctruedenclaude
andcommitted
scijava-events: a typed event bus, written clean
Replaces SciJava Common's event package without porting it, so the bushe fork and its attribution obligation are gone. subscribe(Class<E>, Consumer<? super E>) is type-safe, needs no reflection and therefore no `opens`, and returns a Subscription. The @eventhandler ergonomics are not lost: scijava-context already reflects over services, so it can scan annotated methods, register them as lambdas, and hold the subscriptions -- the convenience lives in the tier that already pays for reflection, and a service author still never writes cleanup code. Subscribers are held strongly. SciJava Common held them weakly, which produced handlers that silently stopped firing, and forced a WeakHashMap named keepEm whose only job was to stop the proxies being collected too early. Cleanup is structural instead: EventBus.close() drops every subscription, and a context calls it on dispose. There is deliberately no shared instance. A bus is nothing but shared mutable subscriber state, so an ambient singleton would let two application contexts hear each other's events; each context owns one bus. NB: this is the opposite call from Locations.get() and DataHandles.get(), which are safe to share precisely because they hold no such state. Also: - Class-hierarchy dispatch is kept, since it is what makes a bus more than a listener list, and any object may be an event -- no SciJavaEvent base type, so a class from another library can be one. - Consumption is opt-in through a Consumable interface on the event, modeled on AWT's InputEvent.consume(), so toolkit-style input events can stop propagation without every event paying for the concept. - Subscriber order is by scijava-priority, descending, ties broken by subscription order. It matters most for consumable events, where the first subscriber gets first refusal. - A failing subscriber does not truncate delivery: everyone still receives the event and the failures are thrown together afterward as an EventDeliveryException, first as cause and the rest suppressed. Cancellation is the exception -- an interrupted subscriber, or one throwing CancellationException, stops delivery at once rather than being buried among unrelated failures. EventHistory is dropped; a subscriber on Object.class reproduces it. scijava-listeners is not folded in after all: a listener list is the right primitive for "this object notifies its observers", and is a different thing from a bus. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013gJpzGd3ksbME4CN6Tu2ni
1 parent 92a93bf commit f58ccb1

11 files changed

Lines changed: 1005 additions & 5 deletions

File tree

‎docs/migration-plan.md‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,50 @@ No application context required by anything in this phase.
423423
reintroducing `FileChannel` later, since a handle may open a channel and
424424
a bank may be backed by a mapped buffer. This component is therefore
425425
complete.
426-
- **`scijava-events`** (new, `org.scijava.events`): a clean-room event bus,
427-
no context, designed for typed topics and weak-reference subscribers. Not a
428-
port of the bushe fork — rewriting removes the attribution obligation. The
429-
external `scijava-listeners` folds in here.
426+
- **`scijava-events`** (new, `org.scijava.events`): a clean-room typed event
427+
bus. **Done.** Not a port of the bushe fork, so the attribution obligation
428+
is gone. The external `scijava-listeners` stays a separate concern: a
429+
listener list is the right primitive for "this object notifies its
430+
observers", and is not a bus.
431+
- **Lambdas, not annotations.** `subscribe(Class<E>, Consumer<? super E>)`
432+
is type-safe, needs no reflection, and so needs no `opens`.
433+
`@EventHandler` ergonomics are not lost: `scijava-context` already
434+
reflects over services, so it can scan annotated methods, register them as
435+
lambdas, and hold the subscriptions. The convenience lives in the tier
436+
that already pays for reflection.
437+
- **Strong references plus an explicit `Subscription`.** SciJava Common held
438+
subscribers weakly, which produced handlers that silently stopped firing
439+
and forced a `WeakHashMap` named `keepEm` whose only job was stopping the
440+
proxies from being collected too early — the implementation fighting its
441+
own design. Cleanup is structural instead: `EventBus.close()` drops every
442+
subscription, and a context calls it on dispose, so a service that
443+
subscribes never arranges its own cleanup.
444+
- **No shared instance, deliberately.** A bus is nothing but shared mutable
445+
subscriber state, so an ambient singleton would let two application
446+
contexts hear each other's events. Each context owns one bus. NB: this is
447+
the opposite call from `Locations.get()` and `DataHandles.get()`, which
448+
are safe to share precisely because they hold no such state.
449+
- **Class-hierarchy dispatch** is retained — it is what makes a bus more
450+
than a listener list — and any object may be an event, with no marker
451+
interface, so a type from another library can be one.
452+
- **Consumption is opt-in**, via a `Consumable` interface on the event, on
453+
the model of AWT's `InputEvent.consume()`. Toolkit-style input events can
454+
then stop propagation without every event paying for the concept.
455+
- **Subscriber order** is by `scijava-priority`, descending, ties broken by
456+
subscription order. It matters most for consumable events, where an
457+
earlier subscriber gets first refusal.
458+
- **A failing subscriber does not truncate delivery.** Everyone still
459+
receives the event, and the failures are thrown together afterward as an
460+
`EventDeliveryException` — first as cause, rest suppressed, none lost.
461+
Cancellation is the exception: an interrupted subscriber, or one throwing
462+
`CancellationException`, stops delivery at once, since an interrupt means
463+
this thread is being cancelled and must not be buried among unrelated
464+
failures.
465+
- **`EventHistory` is dropped.** The real need behind it — a macro recorder
466+
that tracks module executions so a user can review what they did — is
467+
better served by an explicit layer later; note that SciJava Ops already
468+
tracks execution provenance per output object (`OpHistory.executionsUpon`),
469+
which answers "what produced this?" rather than "what happened?".
430470
- **Key/value parsing moves to Parsington**, not into a SJ3 module. The whole
431471
SJC `parse` package is 319 lines whose only non-Parsington dependency is
432472
`ObjectArray` (replaceable with `ArrayList`), and Parsington already carries

‎docs/migration.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ A **dropped** row is a successful outcome, not a gap.
103103
| `io.nio.ByteBufferByteBank` | `org.scijava.io3.ByteBufferByteBank`, with `direct()` factories and an `isDirect()` accessor. Off-heap direct buffers are its reason to exist alongside `ByteArrayByteBank`: bytes reach native code and shared memory without a copy, which matters increasingly for Appose's named shared memory blocks. **Bug fixed in the port:** growing a direct bank reallocated with `ByteBuffer.allocate`, quietly moving the bytes back onto the heap; it now grows through the bank's own provider | ported |
104104
| `io.nio.NIOService` | **not ported** — it wraps two JDK calls behind a system-property switch, was inherited from old Bio-Formats code, and SCIFIO only exposes it as a gateway accessor (`SCIFIO.nio()`) without using it. Nothing in the ported architecture precludes `FileChannel`-based handles later: a handle is free to open a channel, and a byte bank is free to be backed by a mapped buffer | dropped |
105105
| `io` (`IOPlugin`, `IOService`, `RecentFileService`) | `scijava-io3` (Phase 1), pending review of what is still used | open |
106-
| `event`, `event.bushe` | `org.scijava.events` — clean-room rewrite, not a port (Phase 1) | planned |
106+
| `event`, `event.bushe` | `org.scijava.events`, a clean-room rewrite. `EventService` becomes `EventBus`, one per context and with no shared instance. `subscribe` takes a lambda rather than scanning `@EventHandler` methods, and returns a `Subscription`; subscribers are held strongly and dropped by `EventBus.close()`. Events need no `SciJavaEvent` base type. Consumption is opt-in via `Consumable`; subscriber order is by `scijava-priority` | ported |
107+
| `event.EventHistory`, `DefaultEventHistory` | dropped — a subscriber on `Object.class` reproduces it. The macro-recorder need behind it deserves an explicit layer | dropped |
107108
| `parse` | Parsington (`org.scijava.parsington`) | planned |
108109
| `log` | SLF4J, used directly | dropped |
109110
| `cache` | `scijava-cache` moves in, keeping `org.scijava.cache` (Phase 2) | planned |

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<module>scijava-concurrent</module>
6464
<module>scijava-discovery</module>
6565
<module>scijava-discovery-test</module>
66+
<module>scijava-events</module>
6667
<module>scijava-function</module>
6768
<module>scijava-index</module>
6869
<module>scijava-io3</module>

‎scijava-events/pom.xml‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.scijava</groupId>
7+
<artifactId>pom-scijava</artifactId>
8+
<version>44.0.0</version>
9+
<relativePath />
10+
</parent>
11+
12+
<artifactId>scijava-events</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
15+
<name>SciJava Events</name>
16+
<description>A typed event bus for loosely coupled notification.</description>
17+
<url>https://github.com/scijava/scijava</url>
18+
<inceptionYear>2026</inceptionYear>
19+
<organization>
20+
<name>SciJava</name>
21+
<url>https://scijava.org/</url>
22+
</organization>
23+
<licenses>
24+
<license>
25+
<name>Simplified BSD License</name>
26+
<distribution>repo</distribution>
27+
</license>
28+
</licenses>
29+
30+
<developers>
31+
<developer>
32+
<id>ctrueden</id>
33+
<name>Curtis Rueden</name>
34+
<roles>
35+
<role>founder</role>
36+
<role>lead</role>
37+
<role>reviewer</role>
38+
<role>support</role>
39+
<role>maintainer</role>
40+
</roles>
41+
</developer>
42+
<developer>
43+
<id>gselzer</id>
44+
<name>Gabriel Selzer</name>
45+
<roles>
46+
<role>founder</role>
47+
<role>debugger</role>
48+
<role>reviewer</role>
49+
<role>support</role>
50+
</roles>
51+
</developer>
52+
</developers>
53+
<contributors>
54+
<contributor>
55+
<name>Johannes Schindelin</name>
56+
<properties><id>dscho</id></properties>
57+
</contributor>
58+
</contributors>
59+
60+
<mailingLists>
61+
<mailingList>
62+
<name>Image.sc Forum</name>
63+
<archive>https://forum.image.sc/tag/scijava</archive>
64+
</mailingList>
65+
</mailingLists>
66+
67+
<scm>
68+
<connection>scm:git:https://github.com/scijava/scijava</connection>
69+
<developerConnection>scm:git:git@github.com:scijava/scijava</developerConnection>
70+
<tag>HEAD</tag>
71+
<url>https://github.com/scijava/scijava</url>
72+
</scm>
73+
<issueManagement>
74+
<system>GitHub Issues</system>
75+
<url>https://github.com/scijava/scijava/issues</url>
76+
</issueManagement>
77+
<ciManagement>
78+
<system>GitHub Actions</system>
79+
<url>https://github.com/scijava/scijava/actions</url>
80+
</ciManagement>
81+
82+
<properties>
83+
<package-name>org.scijava.events</package-name>
84+
85+
<license.licenseName>bsd_2</license.licenseName>
86+
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
87+
88+
<!-- Note: These versions must match across all component POMs! -->
89+
<scijava-priority.version>1.0.1-SNAPSHOT</scijava-priority.version>
90+
</properties>
91+
92+
<dependencies>
93+
<dependency>
94+
<groupId>org.scijava</groupId>
95+
<artifactId>scijava-priority</artifactId>
96+
<version>${scijava-priority.version}</version>
97+
</dependency>
98+
99+
<!-- Test scope dependencies -->
100+
<dependency>
101+
<groupId>org.junit.jupiter</groupId>
102+
<artifactId>junit-jupiter-api</artifactId>
103+
<scope>test</scope>
104+
</dependency>
105+
</dependencies>
106+
107+
<build>
108+
<plugins>
109+
<plugin>
110+
<artifactId>maven-enforcer-plugin</artifactId>
111+
<executions>
112+
<execution>
113+
<id>enforce-package-rules</id>
114+
<goals>
115+
<goal>enforce</goal>
116+
</goals>
117+
<phase>test</phase>
118+
<configuration>
119+
<rules>
120+
<NoPackageCyclesRule implementation="org.scijava.maven.plugin.enforcer.NoPackageCyclesRule" />
121+
<NoSubpackageDependenceRule implementation="org.scijava.maven.plugin.enforcer.NoSubpackageDependenceRule" />
122+
</rules>
123+
</configuration>
124+
</execution>
125+
</executions>
126+
</plugin>
127+
<plugin>
128+
<artifactId>maven-javadoc-plugin</artifactId>
129+
<configuration>
130+
<links>
131+
<link>https://javadoc.scijava.org/Java11/</link>
132+
</links>
133+
</configuration>
134+
</plugin>
135+
</plugins>
136+
</build>
137+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* #%L
3+
* A typed event bus for loosely coupled notification.
4+
* %%
5+
* Copyright (C) 2026 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
module org.scijava.events {
31+
32+
exports org.scijava.events;
33+
34+
requires transitive org.scijava.priority;
35+
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* #%L
3+
* A typed event bus for loosely coupled notification.
4+
* %%
5+
* Copyright (C) 2026 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.events;
31+
32+
/**
33+
* An event that a handler can mark as handled, stopping its delivery.
34+
* <p>
35+
* This is opt-in: an event type implements it only if consumption makes sense
36+
* for it, and every other event is delivered to all subscribers regardless.
37+
* The model is {@code java.awt.event.InputEvent.consume()} - named in prose,
38+
* not linked, so that this component need not require {@code java.desktop} -
39+
* so that
40+
* toolkit-style input events - where one handler claims a keystroke and the
41+
* rest must not see it - can be expressed without every event paying for the
42+
* concept.
43+
* </p>
44+
* <p>
45+
* Delivery order is therefore meaningful for consumable events: subscribers
46+
* run in priority order, so a higher-priority handler gets first refusal.
47+
* </p>
48+
*
49+
* @author Curtis Rueden
50+
* @see EventBus#publish(Object)
51+
*/
52+
public interface Consumable {
53+
54+
/** Marks this event handled, so that remaining subscribers do not see it. */
55+
void consume();
56+
57+
/** Gets whether this event has been consumed. */
58+
boolean isConsumed();
59+
}

0 commit comments

Comments
 (0)