Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add something like @Observes from CDI #489

Closed
SentryMan opened this issue Jan 24, 2024 · 0 comments · Fixed by #491
Closed

Add something like @Observes from CDI #489

SentryMan opened this issue Jan 24, 2024 · 0 comments · Fixed by #491
Assignees
Labels
enhancement New feature or request

Comments

@SentryMan
Copy link
Collaborator

So there I am looking at the Jakarta CDI spec, and I find out about this useful technique available to them.

We could define an SPI for an event manager and an interface for sending events.

interface ObserverManager {
   <T> void registerEventListener(Consumer<T> observer, Type eventType);
   List<Consumer<T>> getObservers(Type eventType);
}

interface Event<T> {
  void fire(T);
  void fireAsync(T);
}

@interface EventType {
}

Here's how I'm thinking a user would go about it.

Publishing

Annotate an event class:

@EventType
class MyEvent  {
//contents...
}

And we'd generate the impl:

@Generated
@Component
class MyEventPublisher implements Event<MyEvent>  {
//inject the Observer Manager and implement publishing methods...
}

The user can then inject this class and publish an event.

Observing

The user can add @Observes to a bean method to signify this should be registered in the manager.

@Singleton
class MyObserver  {
//contents...
 void observeMyEvent(@Observes MyEvent) {
  //contents...
  }
}

We'd then generate the DI class to also register this method to the ObserverManager.

Thoughts?

@SentryMan SentryMan added the enhancement New feature or request label Jan 24, 2024
@SentryMan SentryMan self-assigned this Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant