-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
feat(core): specify event listener API #14735
Conversation
type HasEventListeners interface { | ||
AppModule | ||
|
||
// RegisterEventListeners registers the module's events listeners. | ||
RegisterEventListeners(registrar *EventListenerRegistrar) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a question on why to expose EventListenerRegistrar
in core, shouldn't that be runtime?
The core interface could simply be:
type HasEventListeners interface { | |
AppModule | |
// RegisterEventListeners registers the module's events listeners. | |
RegisterEventListeners(registrar *EventListenerRegistrar) | |
} | |
type HasEventListeners interface { | |
AppModule | |
RegisterEventListeners() []func(context.Context, protoiface.MessageV1) | |
RegisterEventInterceptors() []func(context.Context, protoiface.MessageV1) error | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a more ergonomic API this way I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EventListenerRegistrar is just a dummy struct to make using generics easy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some questions about usage of any
instead of an interface
// RegisterEventListener registers an event listener for event type E. If a non-nil error is returned by the listener, | ||
// it will cause the process which emitted the event to fail. | ||
func RegisterEventListener[E protoiface.MessageV1](registrar *EventListenerRegistrar, listener func(context.Context, E) error) { | ||
registrar.listeners = append(registrar.listeners, listener) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not this instead of using []any
.
registrar.listeners = append(registrar.listeners, listener) | |
listenerF := func(ctx context.Context, iMsg protoiface.MessageV1) error { | |
concrete, ok := iMsg.(E) | |
if !ok { return fmt.Errorf("...") | |
return listener(ctx, concrete) | |
} | |
registrar.listeners = append(registrar.listeners, listenerF) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the caller will be doing a type check anyway. I think this would just add extra type checking. Also in core we don't want to add any implementation details. This should be defer all those details to runtime
|
||
// EventListenerRegistrar allows registering event listeners. | ||
type EventListenerRegistrar struct { | ||
listeners []any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
listeners []any | |
listeners []func(ctx context.Context, iMsg protoiface.MessageV1) error |
Are we okay to merge this? I think this should have a standalone ADR by the way. I can work on that after #12972. |
I'm taking the 👍 as a yes |
Description
Ref #14683
This adds an API for modules to register event listeners in core.
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!
to the type prefix if API or client breaking changeCHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
I have...
!
in the type prefix if API or client breaking change