Skip to content

[Improvement] Refactor EventBus#dispatchEvent #6880

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

Open
Abyss-lord opened this issue Apr 10, 2025 · 0 comments · May be fixed by #6881
Open

[Improvement] Refactor EventBus#dispatchEvent #6880

Abyss-lord opened this issue Apr 10, 2025 · 0 comments · May be fixed by #6881
Assignees
Labels
improvement Improvements on everything

Comments

@Abyss-lord
Copy link
Collaborator

What would you like to be improved?

Current implementation lacks elegance.

  1. The current implementation hardcodes the supported event types. Whenever a new event type (e.g., ErrorEvent, AuditEvent) is added, the dispatchEvent method must be modified.
  2. If the number of event types increases or becomes more granular in the future, it's easy to miss handling certain branches.
public void dispatchEvent(BaseEvent event) {
  Consumer<BaseEvent> handler = dispatcherMap.get(event.getClass());
  if (handler != null) {
    handler.accept(event);
  } else {
    throw new IllegalArgumentException("Unsupported event type: " + event.getClass().getName());
  }
}

How should we improve?

A more elegant approach should be used—one that allows each event to determine its own handling logic, eliminating the need for hardcoded type checks and avoiding missing branches. This way, potential issues can be caught at compile time.

@Abyss-lord Abyss-lord added the improvement Improvements on everything label Apr 10, 2025
@Abyss-lord Abyss-lord self-assigned this Apr 10, 2025
Abyss-lord added a commit to Abyss-lord/gravitino that referenced this issue Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvements on everything
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant