GWT - How to implement the GXT's DelayedTask in pure GWT ?

In the GXT framework there is a class called DelayedTask, which is a Timer that is cancelled if a new request is made. So, when we need such thing? Usually when we have multiple identical actions that are ocurring repetately (i.e. mouse event, or layout requests when modify content of the panels), and we'd like to ignore all calls except the last one.

To get the same functionality in pure GWT, which obviously will work well using different widget frameworks (GWT-Ext, pure GWT, GXT or SmartGWT), is presented in the class LastCallBurstEventsMgr, available for download HERE.

The class LastCallBurstEventsMgr takes care of the burst of the same event, by skipping the first ones and executing only the last one.
 
An example of a call for this class is presented below:

scrollPanel_.addScrollListener(new ScrollListener() {

    public void onScroll(final Widget widget, final int scrollLeft, final int scrollTop) {

        LastCallBurstEventsMgr.addEvent(
            new BurstEvent("scroll-me-baby-one-more-time",
                           new Function() {
                               public void execute() {
                                    System.out.println(" scrollLeft = " + scrollLeft + " scrollTop = " + scrollTop + " widget = " + widget.getClass());
                               }
                           },
                           200)
        );
    }
});

which means for each event with the same unique name 'scroll-me-baby-one-more-time' the manager will defer the execution if in the last 200 msec (like in the above exemple) another event has arrived, otherwise it launches the "Function" callback. So, in case of only one single event this code adds a latency of 200msec, which depends of your application if it acceptable of not.

Cheers!

No comments:

Post a Comment