开发者

GWT 2.1 : ResettableEventBus doesn't reset?

开发者 https://www.devze.com 2023-02-05 06:26 出处:网络
public class MyActivity extends AbstractActivity implements ContextChangedEvent.Handler { public MyActivity()
public class MyActivity extends AbstractActivity implements ContextChangedEvent.Handler
{
    public MyActivity()
    {
        ClientFactory.INSTANCE.getEventBus().addHandler(ContextChangedEvent.TYPE, this);
    }

    @override
    public void onContextChanged()
    {
        //do stuff
    }
}



//The getEventBus Implementation:
public EventBus getEventBus()
{
    if (eventBus == null)
        eventBus = new ResettableEventBus(new SimpleEventBus());
    return eventBus;
}

When I add a breakpoint in the onContextChange() method, I get the following behavior:

  • on the first Place, i break only once for each event fired
  • after a place changed, I break twice
  • after another place change, 3 times....

Since I'm using a new instance of MyActivity for each place, my guess is that I break in several instances of MyActivity. The ResettableEventBus 开发者_StackOverflow中文版should unregister all handler on each place change.

I am missing something?


With ResettableEventBus you still have to call removeHandlers (plural) to detach everything. ResettableEventBus only keeps track of your handlers and adds a function to remove all handlers that was attached to this instance.

If you are using ActivityManager and passing in your eventbus, ActivityManager will wrap your EventBus in ResettableEventBus and pass it to you in start.

ActivityManager(myActivityMap, ClientFactory.INSTANCE.getEventBus());

...

public class MyActivity extends AbstractActivity implements ContextChangedEvent.Handler
{
    public MyActivity()
    {
    }

    @override
    public void onContextChanged()
    {
        // do Stuff
    }

    @override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        eventBus.addHandler(ContextChangedEvent.TYPE, this);
    }
}

If you use the eventBus passed to you in "start", ActivityManager will automatically clean the handlers you attach to it automatically for you.

Also I would suggest constructing a SimpleEventBus in your factory instead of ResettableEventBus. There is a bug in the current version of ResettableEventBus that causes issue if you nest it (Memory Leak).

http://code.google.com/p/google-web-toolkit/issues/detail?id=5700

This is more of a FYI. Also don't remove any handlers manually from the passed in eventbus. This is caused by the same bug as above.

0

精彩评论

暂无评论...
验证码 换一张
取 消