Re: XMLHttpRequest and event loops

On Mon, 9 Feb 2009, Anne van Kesteren wrote:
> 
> I still haven't quite figured out how to do event loops for 
> XMLHttpRequest. Would it make sense to just emulate the media elements 
> and have two task sources here? One for the network stuff and one for 
> the events?

Are the events triggered by things occuring on the network? If so, use the 
networking task queue for them.


> Is it so that in case of the synchronous request you do not queue a task to do
> a request and for the asynchronous request you would?

Yes.


> Currently Gecko does trigger timeouts and several events during a 
> synchronous request, how is that represented in the event model? (I'm 
> not really sure whether we want to follow that model, but I'm just 
> wondering if it works.)

If you want the event loop to be pumped while in a synchronous method 
call, just have your algorithm run the event loop manually, e.g. add these 
steps in your algorithm (the numbers below are arbitrary):

   ...

   13. If the data is not yet available [or whatever condition you are 
       waiting for] then run the oldest task on one of the event loop's 
       task queues, ignoring tasks whose associated Documents are not 
       fully active. The user agent may pick any task queue. [Or, list the 
       task queues that should or should not be consisdered.]

   14. If a task was executed in the previous step, remove it from its 
       task queue.

   15. If the data is not yet available [or whatever condition you are
       waiting for] then return to step 13.

   ...

You would skip what is step 3 in the normal event loop algorithm, 
probably.


Basically the event loop model makes everything "synchronous", in that 
everything is executed at a given time. The browser just goes around the 
altogrithm for the event loop, and runs the tasks that are queued. While a 
task is running, you know no other script is running. Such a task might be 
something like running a script or parsing. (Rendering actually happens as 
part of the event loop, not as a separate task.)

When a method is invoked, you know you are running a task for that script. 
If you want to run things synchronously, you just do them, since you know 
that the scripts are waiting on you. If you want to do things async, you 
add them to a task queue which is then run later when the event loop gets 
to that task.

You can manually pump the event loop (as described above) when you want 
tasks to be run even though the event loop isn't running (because it is 
already dispatching a task).

HTH,
-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Monday, 9 February 2009 20:43:06 UTC