EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Support for one-to-one singular communication

Open mauriciogior opened this issue 9 years ago • 9 comments

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
            }
        });
}

mauriciogior avatar Apr 26 '15 08:04 mauriciogior

+1

vipinhelloindia avatar May 11 '15 11:05 vipinhelloindia

+10000 for one to one communication!

jmrbcu avatar May 16 '15 02:05 jmrbcu

:+1:

Chumper avatar May 16 '15 23:05 Chumper

Up. Maybe somebody wants to give it a try?

mauriciogior avatar Oct 17 '15 00:10 mauriciogior

Could be an architecture smell. If 1-to-1, shouldn't the caller know whom to call?

greenrobot avatar Oct 17 '15 06:10 greenrobot

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

Chumper avatar Oct 17 '15 10:10 Chumper

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);

gunhansancar avatar Dec 09 '15 11:12 gunhansancar

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()})

kemsky avatar Jan 12 '16 13:01 kemsky

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?

alienwings avatar Jun 28 '17 14:06 alienwings