EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Proper priority handling for events with inheritance

Open artkoenig opened this issue 9 years ago • 3 comments

Given two events class A {...} and class B extends A {...}

and the corresponding subscriptions

@Subscribe(priority = 2)
public void onEvent(A event)

and

@Subscribe(priority = 1)
public void onEvent(B event)

I would expect, that a B event would first be handled by onEvent(A event) and then onEvent(B event), but this is not the case. The priority is not handled properly for class hierarchies.

artkoenig avatar Jul 07 '16 07:07 artkoenig

I don't think it should work that way, as the object hierarchy has a higher quality of information than the priority given by the annotation. A more specific subscriber should be always called first, as it has the most specific behavior.

Maybe there could be two priorities :

  • GlobalPriority which works regardless of the object hierarchy
  • Priority which only works within the object hierarchy

Panthaaaa avatar Sep 19 '16 15:09 Panthaaaa

A more specific subscriber should be always called first, as it has the most specific behavior.

If I look at how overloaded methods should behave in object oriented programming languages - then event A should be called first and then B. The event handling should follow the class hierarchy in top -> bottom manner.

With the priority attribute you should be able to override this behaviour.

artkoenig avatar Nov 23 '16 11:11 artkoenig

I just stumbled across the same issue.

I work extensively with event hierarchies and look for a way to pre-process (with priority = Integer.MIN_VALUE) and post-process (with priority = Integer.MAX_VALUES) all events by subscribing to the parent. If a there is a subscriber for a specific event it should be processed between the generic subscribers (priority default(0)) .

Since changing this now is probably not possible (backward compatibility) I vote for the idea to introduce a globalPriority.

rvullriede avatar Oct 11 '19 14:10 rvullriede