Firing events from multiple instances of the same class on the EventBus

352 views
Skip to first unread message

Matt Hallman

unread,
Jan 17, 2014, 2:03:34 PM1/17/14
to gwt-pl...@googlegroups.com
I have a presenter widget which is registered for an event on the EventBus. This is event can be fired by multiple instances of the same class. I was wondering if there was a best practice to only respond to the events from a specific instance of this class. 

For example, I fire an event from a Reciever when it receives a result from the server. This event is handled by a presenter widget to update the UI. My issue is that I have a modal which uses a different instance of this same Reciever, and fires the same event. When this happens, the UI behind the modal is updated because the PresenterWidget is receiving the event. 

I was wondering if there was a way to only register the handler for a specific instance of a source.

Clay Harris

unread,
Jan 17, 2014, 2:42:33 PM1/17/14
to gwt-pl...@googlegroups.com
I see a few possible solutions, if I understand your problem correctly.

1. Instead of firing events on the eventbus, instead pass in a handler to the receiver to handle onReceive, etc for a particular call.  This assumes a direct or near direct call to the Receiver etc, which may not fit your situation.

2. If the events fired are used in different situations - perhaps you should be using different events.  This doesn't have to be complicated - you could extend your current event, and create "ModalxxxEvent", "UIxxxEvent", etc.  This isn't ideal, as you still duplicate code to a degree and multiple the number of classes, but it could be a solution.

3. Modify your event to also contain a source or target field or perhaps UI or Modal boolean or something of the sort, that can be checked when the event is handled to determine whether a particular presenter or class should handle the event.

Is that helpful at all?


--
You received this message because you are subscribed to the Google Groups "GWTP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gwt-platform...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Casey Jordan

unread,
Jan 19, 2014, 9:44:54 PM1/19/14
to gwt-pl...@googlegroups.com
What is the general opinion on having multiple event buses? In our application (Matt works with me) our system is split up into separate 'apps' which are just major modules that have sub-modules.

As I was thinking about this, I considered an approach where we could have a global event bus (a singleton like is used now) for communication across all apps, and then each app could also inject their own non-singleton bus for communication internally with presenter widgets and sub-modules. This bus would have to be passed to sub-modules and presenter widgets via assisted injection, so that might be tedius, but worth it.

Initially I was concerned about doing this because I thought it may cause confusion, however our application is only in it's infancy and it already has a large number of 'top level' applications that can have any number of sub-modules. This problem is only going to grow as things get bigger.

I am wondering if anyone can think of inherent problems using this approach?
--
--
Casey Jordan
easyDITA a product of Jorsek LLC
"CaseyDJordan" on LinkedIn, Twitter & Facebook
(585) 348 7399
easydita.com


This message is intended only for the use of the Addressee(s) and may
contain information that is privileged, confidential, and/or exempt from
disclosure under applicable law.  If you are not the intended recipient,
please be advised that any disclosure  copying, distribution, or use of
the information contained herein is prohibited.  If you have received
this communication in error, please destroy all copies of the message,
whether in electronic or hard copy format, as well as attachments, and
immediately contact the sender by replying to this e-mail or by phone.
Thank you.

Clay Harris

unread,
Jan 20, 2014, 8:54:51 AM1/20/14
to gwt-pl...@googlegroups.com
Personally, I think that sounds like a workable solution.  From my understanding, the EventBus gets slower and less efficient as it's use grows, so sub-eventbuses could be a good solution.  Instead of using non-singletons, etc, however, perhaps you could have singleton subclasses of the EventBus, say AppXEventBus, AppYEventBus, which are injected in your apps' presenters using the regular dependency injection, just like the current eventbus is.  

Matt Hallman

unread,
Jan 20, 2014, 1:21:42 PM1/20/14
to gwt-pl...@googlegroups.com
If I have two of AppX on the screen, and I don't want their event bus (AppXEventBus) to communicate with each other, is the only way to achieve this through assisted injection?

For example, if I subclassed EventBus to create AppXEventBus, and used regular dependency injection, both of AppX would hear each other's events passed on AppXEventBus.
If I want each instance of AppX (and it's sub-modules) to have a separate AppXEventBus, the only way to to this would be with assisted injection, correct?

Clay Harris

unread,
Jan 20, 2014, 1:24:55 PM1/20/14
to gwt-pl...@googlegroups.com
Ahh, I didn't realize it was multiple instances of the same app, as opposed to different apps running together.  In that case, your assisted injection idea is better than anything else that occurs to me offhand.

Christian Goudreau

unread,
Jan 20, 2014, 1:44:45 PM1/20/14
to gwt-pl...@googlegroups.com
You don't need assisted injection, only a Provider of EventBus annotated with a custom annotation or Name. That way you will be able to inject your won instance of the eventbus that you can share wherever you want.

I often use that approach using the Observer pattern where my observer has its own eventbus that collaborators register to.

For you case, you need to use a local eventbus. That approach is also used by GWT widgets that has their own internal eventbus that you can register to with the addClickHandler pattern (using a HasClickHandlers interface).

Basically, users of your presenterWidget would use something like a addReceiverHandler(ReceiverEvent event); that will publicly be available in your PresenterWidget.
Christian Goudreau | CEO - Président
M: 1.877.635.1585 | S: christian.goudreau

Matt Hallman

unread,
Jan 20, 2014, 3:02:08 PM1/20/14
to gwt-pl...@googlegroups.com
Christian,
I'm not sure what you mean by EventBus Provider with custom annotation or Name, could you provide an example?

Christian Goudreau

unread,
Jan 20, 2014, 3:09:05 PM1/20/14
to gwt-pl...@googlegroups.com
bind(EventBus.class).annotatedWith(YourAnnotation.class).to(SimpleEventBus.class);

Not that I'm not binding it as singleton. If you need more than one instance in the same class you can inject: @YourAnnotation Provider<EventBus> myEventBusProvider

The use myEventBusProvider.get() to have a new instance each time.

But in your case, I don't event think that a Provider is useful, just inject @YourAnnotation EventBus myEventBus

Matt Hallman

unread,
Jan 20, 2014, 5:21:16 PM1/20/14
to gwt-pl...@googlegroups.com
When I use this bind(EventBus.class).annotatedWith(YourAnnotation.class).to(SimpleEventBus.class);   each of my PresenterWidgets get a different @YourAnnotation EventBus, but when I bind it as singleton, they each have the same @YourAnnotation EventBus.
 If I display this app twice on a page, will this make the same @YourAnnotation EventBus in each separate app, since it is bound as singleton?

Christian Goudreau

unread,
Jan 20, 2014, 5:56:16 PM1/20/14
to gwt-pl...@googlegroups.com
What do you mean by twice on a page?

Casey Jordan

unread,
Jan 20, 2014, 10:39:03 PM1/20/14
to gwt-pl...@googlegroups.com
Hi Christian, thanks for the guidance.

In our application we have a file browser, just like in a desktop environment sometimes you will have multiple file browsers open at once. The file browser has presenter widgets that are responsible for displaying things like the current number of files displayed etc. These are updated via events sent to the event bus. If more than one of these file browsers are open at the same time (See the screenshots attached) then we get interference between the two interfaces because they are listening to the same events on the bus.

Hopefully the screenshots will make things clearer. I tried to create a video to show exactly what is happening, but my computer is not cooperating. 

Thanks!
main-interface.png
Screenshot_1_20_14_10_34_PM.png

Christian Goudreau

unread,
Jan 21, 2014, 7:48:42 AM1/21/14
to gwt-pl...@googlegroups.com
In those cases, you need an internal eventbus for each instance so that the don't interfere with each other. Using a singleton won't work.

How you achieve that depends on how you created your file explorer. If it's a nested hierarchy of presenter widget, the parent presenter widget of all children should be the one to have that eventbus, then children can reuse it once created using assistedinjection. At that point, assistedinjection may be needed to create all children presenterwidget with the right eventbus that the parent is using.

It really depends on how you created all that at this point.
Reply all
Reply to author
Forward
0 new messages