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

feat(core): specify event listener API #14735

Merged
merged 6 commits into from
Jan 24, 2023
Merged

Conversation

aaronc
Copy link
Member

@aaronc aaronc commented Jan 23, 2023

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...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

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...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@aaronc aaronc marked this pull request as ready for review January 23, 2023 16:41
@aaronc aaronc requested a review from a team as a code owner January 23, 2023 16:41
Comment on lines +11 to +16
type HasEventListeners interface {
AppModule

// RegisterEventListeners registers the module's events listeners.
RegisterEventListeners(registrar *EventListenerRegistrar)
}
Copy link
Contributor

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:

Suggested change
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
}

Copy link
Member Author

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

Copy link
Member Author

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

core/appmodule/event.go Outdated Show resolved Hide resolved
@aaronc aaronc mentioned this pull request Jan 23, 2023
14 tasks
@aaronc aaronc changed the title feat(core): add event listener API feat(core): specify event listener API Jan 23, 2023
Copy link
Contributor

@testinginprod testinginprod left a 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)
Copy link
Contributor

@testinginprod testinginprod Jan 23, 2023

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.

Suggested change
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)

Copy link
Member Author

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
Copy link
Contributor

@testinginprod testinginprod Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
listeners []any
listeners []func(ctx context.Context, iMsg protoiface.MessageV1) error

@aaronc
Copy link
Member Author

aaronc commented Jan 24, 2023

Are we okay to merge this? I think this should have a standalone ADR by the way. I can work on that after #12972.

@aaronc
Copy link
Member Author

aaronc commented Jan 24, 2023

I'm taking the 👍 as a yes

@aaronc aaronc enabled auto-merge (squash) January 24, 2023 17:22
@aaronc aaronc merged commit e822585 into main Jan 24, 2023
@aaronc aaronc deleted the aaronc/core-api-event-listeners branch January 24, 2023 17:41
@coderabbitai coderabbitai bot mentioned this pull request May 3, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants