-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
plugin/src/software/aws/toolkits/eclipse/amazonq/broker/EventBroker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
plugin/src/software/aws/toolkits/eclipse/amazonq/events/TestEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
plugin/src/software/aws/toolkits/eclipse/amazonq/publishers/TestPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
plugin/src/software/aws/toolkits/eclipse/amazonq/subscribers/TestSubscribers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters