Friday, May 27, 2011

Aw: Re: SimpleEventBus: How to add a handler and fire an event for it when nested in a "parent" handler?

Well I think its not a design flaw in SimpleEventBus because as the name says its just a simple implementation. 

When you add a handler to the eventbus it has to be stored in a Map<EventType, List<Handler>>. Once an event is fired you will iterate over this list and that means you can not add or remove something to/from it. This will result in a ConcurrentModificationException. SimpleEventBus keeps track of these modifications and applies them after all events have been dispatched.

You can try and implement your own EventBus to support your example but I think its not that easy. The code that keeps track of handlers that were added/removed during an event dispatch won't be that easy.

I think its easier to change your app code to play nice with SimpleEventBus, e.g.:

thisMethodIsCalledBytheParentHandler(){ 


   myService.getMessage(

     id, new DefaultAsyncCallback<List<EventMessageDTO>>() { 

        public void onSuccess(Message myMessage){ 

            //do something 

            //do something (the code from your onMessageRetrieved())

            myEventBus.fireEvent(new MessageRetrievedEvent(id, myMessage)); 

         } 

   }); 




--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment