Using the GWT Scheduler
Asked Answered
P

1

27

I'm having a tough time understanding the difference between various methods of the com.google.gwt.core.client.Scheduler interface, specifically, the scheduleDeferred, scheduleFinally, and scheduleIncremental methods.

I'm hampered in my understanding, I think, by my unfamiliarity with the browser event processing loop, to which the Scheduler documentation refers.

Would you please explain how these methods differ from each other, and how they work in relation to the browser event loop?

Piteous answered 27/2, 2011 at 2:40 Comment(0)
E
44

JavaScript (in a browser) is single threaded. The event loop model means, we're always in exactly one of two states:

  • in the event loop
  • executing an event handler

There are many kinds of events: Click events, onload events, XHR events, timer events, ... You'll have to declare some handlers (at least one during page load), otherwise none of your code will ever be executed. One of them is the handler you specify by implementing onModuleLoad.

It's important to keep all handlers short, because there's no parallelism and no interrupts (except for the last resort "unresponsive script" interrupt). This means, that users can't interact with the interface, until the browser returns to the event loop - and that doesn't happen before the current handler is finished.

So if you want to defer some code until after the other event handlers had a chance, then you can use Scheduler.scheduleDeferred.

Scheduler.scheduleIncremental helps you to split really long running tasks into multiple steps, giving the other event handlers a chance between each of the steps.

Scheduler.scheduleFinally just means: After handling our current handler (even if an exception occurs), but before returning to the event loop, execute my command.

See com.google.gwt.core.client.impl.Impl.entry0()

Edrick answered 27/2, 2011 at 9:22 Comment(4)
What if I want to make a change to the DOM, or perhaps CSS styling, before continuing with the remainder of my long-running event handler? Will using scheduleDeferred or scheduleIncremental on the remainder cause the page change to be visible immediately?Piteous
@David: Keep the code that changes the DOM/styling in the current handler. Move the remainder into scheduleDeferred. The changes will be re-rendered before your ScheduledCommand executes.Edrick
An example of scheduleDeferred should make things more clear. You can check this, specially answer by John Lablanca. groups.google.com/group/google-web-toolkit/browse_thread/thread/…Anjelicaanjou
@ChrisLercher I did not get it. What is the difference between scheduleDeferred and scheduleFinally. When do I use what?Ceratoid

© 2022 - 2024 — McMap. All rights reserved.