• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Files spread between Dropbox, Google Drive, Gmail, Slack, and more? Dokkio, a new product from the PBworks team, integrates and organizes them for you. Try it for free today.

View
 

What does RunLoop do

Page history last edited by Tim Molendijk 10 years, 9 months ago

Run loops are a fundamental part of any application framework. A short step-by-step listing of how SC.RunLoop works and what it is for, as explained by Erich Ocean at irc.freenode.net#sproutcore:

 

  • the run loop starts whenever an event is received (a callback from the browser)
  • you can start a run loop manually by calling SC.RunLoop.begin()
  • the event is handled, which usually involves calling some code implemented by an app developer
  • that code could cause observers to fire, and those observers can themselves be bindings
  • bindings do not "fire" immediately
  • instead, they add themselves to a buffer on the run loop
  • when an app developer's code finishes running, the run loop will flush any pending bindings
  • those bindings could cause more app developer code to run, more observers to fire, and more bindings to buffer
  • but eventually, all of the bindings fire and there's no app developer code left to run
  • at that point, three other buffers come into play: the DOM buffer, the layout buffer, and the render buffer
  • SproutCore delays DOM modification, layout changes, and render updates until after all bindings have flushed
  • it then performs them in that order
  • (right before that, it invokes any methods that have been added to the run loop with "invokeOnce()"
  • once that is done, SproutCore calls any invokeLast() methods
  • finally, it checks to see if any SC.Timers should fire
  • if so, it fires them
  • otherwise, it does setTimeout() if there are any pending timers
  • if not, it simply returns control to the browser, waiting for the next event/callback
  • then it starts the run loop all over again

 

Comments (2)

sudara said

at 6:28 am on May 28, 2009

Awesome page, thanks for compiling this!

sudara said

at 4:58 am on Sep 7, 2009

Though, to be totally honest, I still have some significant confusion surrounding Run loops, including, but not limited to:

1. What benefit do they provide? Why were they created? The list above provides a walkthrough of "how" but not a single drop of "why"
2. Are they only fired when called manually or by the browser? What in sproutcore depends on RunLoops and what is independent?
3. A real world example walkthrough would be useful - at this point as a n00b sproutcore dev, it'd be pretty much impossible to understand when to use invokeOnce or invokeLast, or SC.Timer, etc.
4. How frequently should app devs be manually calling RunLoops. Are there performance hits?
5. I imagine there are times that people are calling RunLoops when really they want to rerender their views, or force just one specific thing to update. Are there best practices here?

You don't have permission to comment on this page.