Skip to content

Commit

Permalink
Add event bus POC
Browse files Browse the repository at this point in the history
  • Loading branch information
taldekar committed Jan 9, 2025
1 parent 7d0945c commit a2396f2
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.broker;

import java.lang.reflect.ParameterizedType;

import rx.Observable;
import rx.Observer;
import rx.subjects.PublishSubject;

public final class EventBroker {

private static final EventBroker INSTANCE;
private final PublishSubject<Object> eventBus = PublishSubject.create();

static {
INSTANCE = new EventBroker();
}

public static EventBroker getInstance() {
return INSTANCE;
}

public void postEvent(final Object event) {
eventBus.onNext(event);
}

@SuppressWarnings("unchecked")
public <T> void subscribe(final Observer<T> observer) {
eventBus.ofType((Class<T>) ((ParameterizedType) observer.getClass()
.getGenericInterfaces()[0]).getActualTypeArguments()[0]).subscribe(observer);
}

public <T> Observable<T> ofObservable(final Class<T> eventType) {
return eventBus.ofType(eventType);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.events;

public final class TestEvent {

private final String message;

public TestEvent(final String message) {
this.message = message;
}

public String getMessage() {
return message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.publishers;

import software.aws.toolkits.eclipse.amazonq.broker.EventBroker;
import software.aws.toolkits.eclipse.amazonq.events.TestEvent;

public final class TestPublisher {

private final EventBroker eventBroker;

public TestPublisher() {
eventBroker = EventBroker.getInstance();

for (int i = 0; i < 10; i++) {
String message = "Test message " + i;
eventBroker.postEvent(new TestEvent(message));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.subscribers;

import rx.Observer;
import software.aws.toolkits.eclipse.amazonq.broker.EventBroker;
import software.aws.toolkits.eclipse.amazonq.events.TestEvent;

public final class TestSubscribers {

public TestSubscribers() {
fromObserver();
fromObservable();
}

public void fromObserver() {
Observer<TestEvent> observer = new Observer<>() {
@Override
public void onNext(final TestEvent event) {
System.out.println(event.getMessage());
}

@Override
public void onError(final Throwable error) {
System.out.println(error.getMessage());
}

@Override
public void onCompleted() {
System.out.println("Completed");
}
};

// Direct subscription
EventBroker.getInstance().subscribe(observer);

// Subscribe using observable
EventBroker.getInstance().ofObservable(TestEvent.class).subscribe(observer);
}

public void fromObservable() {
EventBroker.getInstance().ofObservable(TestEvent.class).subscribe(
event -> System.out.println(event.getMessage()),
throwable -> System.out.println(throwable.getMessage()), () -> System.out.println("Complete"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

import rx.Observer;
import software.aws.toolkits.eclipse.amazonq.broker.EventBroker;
import software.aws.toolkits.eclipse.amazonq.controllers.AmazonQViewController;
import software.aws.toolkits.eclipse.amazonq.events.TestEvent;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.AuthStatusChangedListener;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.AuthStatusProvider;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.AuthState;
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
import software.aws.toolkits.eclipse.amazonq.util.ThemeDetector;
import software.aws.toolkits.eclipse.amazonq.views.actions.AmazonQCommonActions;

public abstract class AmazonQView extends ViewPart implements AuthStatusChangedListener {
public abstract class AmazonQView extends ViewPart implements AuthStatusChangedListener,
Observer<TestEvent> {

private AmazonQViewController viewController;
private AmazonQCommonActions amazonQCommonActions;
private static final ThemeDetector THEME_DETECTOR = new ThemeDetector();

protected AmazonQView() {
this.viewController = new AmazonQViewController();
EventBroker.getInstance().subscribe(this);
}

public final Browser getBrowser() {
Expand Down Expand Up @@ -127,4 +132,19 @@ public void dispose() {
super.dispose();
}

@Override
public final void onNext(final TestEvent event) {
System.out.println(event.getMessage());
}

@Override
public final void onError(final Throwable throwable) {
System.out.println(throwable.getMessage());
}

@Override
public final void onCompleted() {
System.out.println("Completed");
}

}

0 comments on commit a2396f2

Please sign in to comment.