Skip to content

Commit 887946d

Browse files
committed
Add event bus
1 parent d46321f commit 887946d

File tree

7 files changed

+19
-57
lines changed

7 files changed

+19
-57
lines changed

plugin/META-INF/MANIFEST.MF

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ Bundle-Classpath: target/classes/,
5252
target/dependency/jackson-databind-2.17.2.jar,
5353
target/dependency/jakarta.inject-api-2.0.1.jar,
5454
target/dependency/json-utils-2.28.26.jar,
55-
target/dependency/maven-artifact-3.9.9.jar,
5655
target/dependency/metrics-spi-2.28.26.jar,
5756
target/dependency/netty-nio-client-2.28.26.jar,
5857
target/dependency/nimbus-jose-jwt-9.41.2.jar,

plugin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
</dependencyManagement>
4343

4444
<dependencies>
45+
<dependency>
46+
<groupId>io.reactivex.rxjava3</groupId>
47+
<artifactId>rxjava</artifactId>
48+
<version>3.1.5</version>
49+
</dependency>
4550
<dependency>
4651
<groupId>jakarta.inject</groupId>
4752
<artifactId>jakarta.inject-api</artifactId>

plugin/src/software/aws/toolkits/eclipse/amazonq/broker/EventBroker.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.reactivex.rxjava3.subjects.PublishSubject;
1010
import io.reactivex.rxjava3.subjects.Subject;
1111
import software.aws.toolkits.eclipse.amazonq.broker.api.EventObserver;
12-
import software.aws.toolkits.eclipse.amazonq.broker.api.Subscription;
1312

1413
public final class EventBroker {
1514

@@ -22,25 +21,17 @@ public <T> void post(final T event) {
2221
eventBus.onNext(event);
2322
}
2423

25-
public <T> Subscription subscribe(final EventObserver<T> observer) {
24+
public <T> Disposable subscribe(final Class<T> eventType, final EventObserver<T> observer) {
2625
Consumer<T> consumer = new Consumer<>() {
2726
@Override
2827
public void accept(final T event) {
2928
observer.onEvent(event);
3029
}
3130
};
3231

33-
Disposable disposable = eventBus.ofType(observer.getEventType()).distinct()
32+
return eventBus.ofType(eventType).distinct()
3433
.observeOn(Schedulers.computation())
3534
.subscribe(consumer);
36-
37-
Subscription subscription = new Subscription() {
38-
@Override
39-
public void cancel() {
40-
disposable.dispose();
41-
}
42-
};
43-
return subscription;
4435
}
4536

4637
}

plugin/src/software/aws/toolkits/eclipse/amazonq/broker/api/EventObserver.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,7 @@
33

44
package software.aws.toolkits.eclipse.amazonq.broker.api;
55

6-
import java.lang.reflect.ParameterizedType;
7-
import java.lang.reflect.Type;
8-
6+
@FunctionalInterface
97
public interface EventObserver<T> {
10-
11-
// Reference:
12-
// https://stackoverflow.com/questions/3437897/how-do-i-get-a-class-instance-of-generic-type-t
13-
@SuppressWarnings("unchecked")
14-
default Class<T> getEventType() {
15-
Class<?> currentClass = getClass();
16-
while (currentClass != null) {
17-
for (Type type : currentClass.getGenericInterfaces()) {
18-
if (type instanceof ParameterizedType paramType && (paramType.getRawType() == EventObserver.class)) {
19-
Type typeArg = paramType.getActualTypeArguments()[0];
20-
if (typeArg instanceof Class<?>) {
21-
return (Class<T>) typeArg;
22-
}
23-
throw new IllegalStateException("Generic type parameter is not a Class");
24-
}
25-
}
26-
currentClass = currentClass.getSuperclass();
27-
}
28-
throw new IllegalStateException("Could not determine generic type");
29-
}
30-
318
void onEvent(T event);
32-
339
}

plugin/src/software/aws/toolkits/eclipse/amazonq/broker/api/Subscription.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQView.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import org.eclipse.swt.widgets.Display;
1010
import org.eclipse.ui.part.ViewPart;
1111

12+
import io.reactivex.rxjava3.disposables.Disposable;
1213
import software.aws.toolkits.eclipse.amazonq.broker.api.EventObserver;
13-
import software.aws.toolkits.eclipse.amazonq.broker.api.Subscription;
1414
import software.aws.toolkits.eclipse.amazonq.controllers.AmazonQViewController;
1515
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.AuthState;
1616
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
@@ -23,7 +23,7 @@ public abstract class AmazonQView extends ViewPart implements EventObserver<Auth
2323
private AmazonQCommonActions amazonQCommonActions;
2424
private static final ThemeDetector THEME_DETECTOR = new ThemeDetector();
2525

26-
private Subscription authStateSubscription;
26+
private Disposable authStateSubscription;
2727

2828
protected AmazonQView() {
2929
this.viewController = new AmazonQViewController();
@@ -83,10 +83,10 @@ private void setupActions(final AuthState authState) {
8383
}
8484

8585
private void setupAuthStatusListeners() {
86-
authStateSubscription = Activator.getEventBroker().subscribe(this);
87-
Activator.getEventBroker().subscribe(amazonQCommonActions.getSignoutAction());
88-
Activator.getEventBroker().subscribe(amazonQCommonActions.getFeedbackDialogContributionAction());
89-
Activator.getEventBroker().subscribe(amazonQCommonActions.getCustomizationDialogContributionAction());
86+
authStateSubscription = Activator.getEventBroker().subscribe(AuthState.class, this);
87+
Activator.getEventBroker().subscribe(AuthState.class, amazonQCommonActions.getSignoutAction());
88+
Activator.getEventBroker().subscribe(AuthState.class, amazonQCommonActions.getFeedbackDialogContributionAction());
89+
Activator.getEventBroker().subscribe(AuthState.class, amazonQCommonActions.getCustomizationDialogContributionAction());
9090
}
9191

9292
@Override
@@ -125,7 +125,7 @@ function waitForFunction(functionName, timeout = 30000) {
125125
*/
126126
@Override
127127
public void dispose() {
128-
authStateSubscription.cancel();
128+
authStateSubscription.dispose();
129129
super.dispose();
130130
}
131131

plugin/src/software/aws/toolkits/eclipse/amazonq/views/ReauthenticateView.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import org.eclipse.swt.widgets.Display;
1919
import org.eclipse.swt.widgets.Link;
2020

21+
import io.reactivex.rxjava3.disposables.Disposable;
2122
import software.aws.toolkits.eclipse.amazonq.broker.api.EventObserver;
22-
import software.aws.toolkits.eclipse.amazonq.broker.api.Subscription;
2323
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.AuthState;
2424
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
2525
import software.aws.toolkits.eclipse.amazonq.telemetry.UiTelemetryProvider;
@@ -38,12 +38,12 @@ public final class ReauthenticateView extends CallToActionView implements EventO
3838
private static final String BUTTON_LABEL = "Re-authenticate";
3939
private static final String LINK_LABEL = "Sign out";
4040

41-
private Subscription authStateSubscription;
41+
private Disposable authStateSubscription;
4242

4343
public ReauthenticateView() {
4444
// It is necessary for this view to be an `AuthStatusChangedListener` to switch the view back to Q Chat after the authentication
4545
// flow is successful. Without this listener, the re-authentication will succeed but the view will remain present.
46-
authStateSubscription = Activator.getEventBroker().subscribe(this);
46+
authStateSubscription = Activator.getEventBroker().subscribe(AuthState.class, this);
4747
}
4848

4949
@Override
@@ -140,7 +140,7 @@ protected void updateButtonStyle(final Button button) {
140140

141141
@Override
142142
public void dispose() {
143-
authStateSubscription.cancel();
143+
authStateSubscription.dispose();
144144
}
145145

146146
@Override

0 commit comments

Comments
 (0)