EventBus
EventBus copied to clipboard
Support for one-to-one singular communication
I don't know if this makes sense to you, but it might be useful in some cases (avoids sending events from one to another). I don't know the best way to run this on scenarios with more than one class listening to SomeEvent
.
// ... some class listening to SomeEvent
public int onEvent(SomeEvent event) {
if (event.message.equals("something")) {
return 1;
}
return 0;
}
// ... some class that triggers a SomeEvent
public void whichValue() {
EventBus.getDefault()
.post(new SomeEvent("something"), new OnResult() {
public void onResult(int result) {
// do something with result
}
});
}
+1
+10000 for one to one communication!
:+1:
Up. Maybe somebody wants to give it a try?
Could be an architecture smell. If 1-to-1, shouldn't the caller know whom to call?
Depends, it does not need to be 1-1, in my case it could also be 1-m where the first events that answers wins.
So like this: "Listen guys, i got messageA for you and i would like to have a response from anybody in 300 miliseconds, otherwise I assume it is resultA."
I think you have different needs for this issue but: For something like this you have 2 options: 1- You can have a wrapper event class which is only registered to some specific class. Then which leads boilerplate classes. 2-You can assign priorities when you register a class. And then you can call cancelEventDelivery() which stops further propagation of that event to other subscribers. EventBus.getDefault().cancelEventDelivery(event);
It makes sense sometimes to get notified if event was not delivered/cancelled.
In this particular case you could put callback into event: new Event(data, ()-> {alert()})
Well, I think if solved 1-1 by 1-m, it may cause siginificantly performance loss, while two many subscribers registered. For example, how about the posted object is a base object like SuccessMessageEvent or ErrorMesssageEvent defined in network framework? Am I right?