add EventBus

This commit is contained in:
2026-03-24 13:42:39 +00:00
parent 0c8c15754d
commit 4e27389df7
10 changed files with 284 additions and 0 deletions
@@ -1,15 +1,18 @@
package p.studio;
import p.studio.events.StudioEventBus;
import p.studio.utilities.ThemeService;
import p.studio.utilities.i18n.I18nService;
public class Container {
private static final I18nService i18nService;
private static final ThemeService themeService;
private static final StudioEventBus studioEventBus;
static {
i18nService = new I18nService();
themeService = new ThemeService();
studioEventBus = new StudioEventBus();
}
public static void init() {
@@ -18,4 +21,6 @@ public class Container {
public static I18nService i18n() { return i18nService; }
public static ThemeService theme() { return new ThemeService(); }
public static StudioEventBus events() { return studioEventBus; }
}
@@ -0,0 +1,6 @@
package p.studio.events;
import p.studio.utilities.events.SimpleEvent;
public interface StudioEvent extends SimpleEvent {
}
@@ -0,0 +1,27 @@
package p.studio.events;
import p.studio.utilities.events.EventSubscription;
import p.studio.utilities.events.TypedEventBus;
import java.util.Objects;
import java.util.function.Consumer;
public final class StudioEventBus {
private final TypedEventBus delegate;
public StudioEventBus() {
this(new TypedEventBus());
}
public StudioEventBus(TypedEventBus delegate) {
this.delegate = Objects.requireNonNull(delegate, "delegate");
}
public <T extends StudioEvent> EventSubscription subscribe(Class<T> eventType, Consumer<? super T> consumer) {
return delegate.subscribe(eventType, consumer);
}
public void publish(StudioEvent event) {
delegate.publish(event);
}
}
@@ -0,0 +1,33 @@
package p.studio.events;
import p.studio.utilities.events.EventSubscription;
import p.studio.utilities.events.TypedEventBus;
import p.studio.workspaces.WorkspaceId;
import java.util.Objects;
import java.util.function.Consumer;
public final class StudioWorkspaceEventBus {
private final WorkspaceId workspaceId;
private final StudioEventBus globalBus;
private final TypedEventBus localBus;
public StudioWorkspaceEventBus(WorkspaceId workspaceId, StudioEventBus globalBus) {
this.workspaceId = Objects.requireNonNull(workspaceId, "workspaceId");
this.globalBus = Objects.requireNonNull(globalBus, "globalBus");
this.localBus = new TypedEventBus();
}
public WorkspaceId workspaceId() {
return workspaceId;
}
public <T extends StudioEvent> EventSubscription subscribe(Class<T> eventType, Consumer<? super T> consumer) {
return localBus.subscribe(eventType, consumer);
}
public void publish(StudioEvent event) {
localBus.publish(event);
globalBus.publish(event);
}
}
@@ -0,0 +1,6 @@
package p.studio.events;
import p.studio.workspaces.WorkspaceId;
public record StudioWorkspaceSelectedEvent(WorkspaceId workspaceId) implements StudioEvent {
}
@@ -0,0 +1,46 @@
package p.studio.events;
import org.junit.jupiter.api.Test;
import p.studio.workspaces.WorkspaceId;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;
final class StudioWorkspaceEventBusTest {
@Test
void workspaceEventsAreObservedLocallyAndGlobally() {
final StudioEventBus globalBus = new StudioEventBus();
final StudioWorkspaceEventBus workspaceBus = new StudioWorkspaceEventBus(WorkspaceId.ASSETS, globalBus);
final List<String> localReceived = new CopyOnWriteArrayList<>();
final List<String> globalReceived = new CopyOnWriteArrayList<>();
workspaceBus.subscribe(TestStudioEvent.class, event -> localReceived.add(event.name()));
globalBus.subscribe(TestStudioEvent.class, event -> globalReceived.add(event.name()));
workspaceBus.publish(new TestStudioEvent("asset-discovered"));
assertEquals(List.of("asset-discovered"), localReceived);
assertEquals(List.of("asset-discovered"), globalReceived);
}
@Test
void globalEventsAreNotRebroadcastBackIntoWorkspaceBus() {
final StudioEventBus globalBus = new StudioEventBus();
final StudioWorkspaceEventBus workspaceBus = new StudioWorkspaceEventBus(WorkspaceId.ASSETS, globalBus);
final List<String> localReceived = new CopyOnWriteArrayList<>();
final List<String> globalReceived = new CopyOnWriteArrayList<>();
workspaceBus.subscribe(TestStudioEvent.class, event -> localReceived.add(event.name()));
globalBus.subscribe(TestStudioEvent.class, event -> globalReceived.add(event.name()));
globalBus.publish(new TestStudioEvent("global-only"));
assertEquals(List.of(), localReceived);
assertEquals(List.of("global-only"), globalReceived);
}
private record TestStudioEvent(String name) implements StudioEvent {
}
}