How do you use GWT's SimpleEventBus or EventBu

2019-04-09 08:56发布

问题:

I'm working on developing some simple graphing software which needs to be implemented in both swing and gwt. On the gwt side, I'm going to use gwt-g2d for the canvas. I was hoping to use an eventbus across both implementations to simplify some of the software.

My understanding is that it should be something like this:

  1. Instantiate EventBus
  2. Instantiate Parent Widget, sink the events you want for the EventBus (mouseover or rpc callback, for example)
  3. Set EventBus to listen for events from the Parent Widget.
  4. Instantiate child widgets, who should register with the EventBus the listeners it has (Do they need to register events they might fire?)
  5. When the EventBus receives an event, it should make a decision about the event and then act on that decision, whether that means ignoring it, changing the event type, or just relaying it to all applicable child widgets.

Is this generally how it should work? SimpleEventBus is still sort of new, and I can't find much on web about how to really use it.

回答1:

A SimpleEventBus provides the very basic functionality of storing a collection of handlers, and calling event.dispatch() with all of the relevant handlers when appropriate. I wouldn't say that you "set EventBus to listen for events" from the parent widget. The SimpleEventBus doesn't have a concept of a "parent." Instead, you pass around a reference to your SimpleEventBus. Then, any object can "fire an event" with the SimpleEventBus.fireEvent method. There's no hierarchy.

Similarly, any object can listen for those events with SimpleEventBus.addHandler(). This is closest to your #4 - addHandler takes a type of event and a handler, so whenever that type of event is passed to fireEvent (by something in your app), that handler will be passed to the event. Note that the Event is what calls the handler, NOT the EventBus! The EventBus just calls event.dispatch(theHandler), and the programmer of the event is responsible for calling the proper function in theHandler. Your #5 is inaccurate, at least for a SimpleEventBus - SimpleEventBus does not inspect the events at all, except to see which handlers to call. It doesn't know about any child widgets or any app logic, and never changes the type of an Event.

Your logic about seeing which region was picked would not be appropriate in an EventBus - instead, you would make a handler that does the logic and listens for ClickEvents. Then, that handler could tell the selected region that it was selected directly, or you could create your own RegionSelectionEvent that you could fire along an EventBus, and then all of the regions could be informed that a RegionSelection has occurred, and your logger could get a notification, or your server monitor could get the notification and notify the boss that someone selected a region, or whatever. If you just need to deselect one region and select another, an eventbus is overkill.

On the other hand, something like a "resize" event makes a lot of sense, since all of your widgets might need to know about it.



回答2:

As far as I understand the GWT EventBus, it's meant for events that are application wide (think "systemwide object x chanded property y"; anything that listens to that particular event can act upon it). This helps you to separate your application logic.

You probably do not want to fire any UI events into it: your parent widgets can implement the handlers or you can just use anonymous instances.



标签: gwt gwt-mvp