Skip to content

Event Listeners created with BusGroup#register cannot easily be unregistered individually #74

Closed

Description

@Jonathing

Right now, classes and instances can be registered with a bus group, which will register all its applicable methods annotated with @SubscribeEvent to the bus group. This returns a collection of event listeners that can then later be used with BusGroup#unregister to unregister them. The thing is that these event listeners have descriptions given to them by the EventListener interface, which most notably include the event type of the listener. So, if I wanted to unregister a single listener from this collection, I would either have to:

  1. Use Collections#singleton or List#of on the listener and pass it into BusGroup#register
  2. Manually locate the event type and unregister it from its bus, making sure that the event bus is using the same bus group.

The thing about option 1 is that BusGroup#register will unconditionally run a for loop on this collection, even if it's a singleton. It would be far easier if a sister method exists, which only accepts a single EventListener, that can be used to unregister that one listener. This would remove the need to arbitrarily locate the relevant event bus.

Activity

  1. PaintNinja commented on Apr 24, 2025

    @PaintNinja
    Contributor

    Huh... from a technical perspective it's trivial to support this, but doing so opens a can of worms from an API design perspective.

    The BusGroup API currently only offers bulk operations. By adding an unregister operation for a single listener, it may lead people wondering why not also be able to register single listeners on the BusGroup, and then also why they have to specify a class instead of it being inferred through the TypeTools library.

    I think unregistering individual listeners after bulk registering them is likely quite a rare operation. Chances are you know the exact event type you're unregistering in that case and don't need a method in BusGroup to do it in a generic manner.

  2. Jonathing commented on Apr 24, 2025

    @Jonathing
    MemberAuthor

    Ok, then I will need to make that clear in the documentation. By returning a collection of event listeners, it's implied that it can be dismantled for each event listener to be interacted with on an individual level. For now, keeping this open. We can discuss this further later, but I assume we're leaning to not adding it.

  3. LexManos commented on May 7, 2025

    @LexManos
    Member

    I am of the same opinion, it is not common for someone to register in bulk, and then unregister partial.
    The old bus only supported unregistering a bulk in bulk. I'd rather keep the api small at the cost of a little design work on the modders side.
    I still plan on making a more Forge Modder interface on Forge's end but keeping EventBus itself simple is the goal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions