What is a browser event loop?
Asked Answered
F

1

21

I have been doing some web application programming using GWT and have been confused by the term "browser event loop".

I have encountered situations where I need to execute deferred commands and "do something" after the browser event loop completes.

I would like to know as to what exactly it is and what happens during the event loop process and in which order?

Franchot answered 24/3, 2011 at 21:35 Comment(1)
Similar: https://mcmap.net/q/508621/-using-the-gwt-schedulerCockayne
S
20

A browser event loop is a thread started by the browser that is constantly scanning for and running different events, just like it sounds. As events occur they are put in the event queue and run in turn by the one event thread. Your javascript should not create its own loops waiting for it to complete or anything like that...it will block that one continuous event loop thread. Instead you would use something like setTimeout or setInterval and check for whatever conditions you are waiting for so the browser can do work while it 'waits'.

GWT is nice in that it can co-opt this process somewhat using the scheduler -- in your case where you want to run something after the event loop 'completes' you will probably want to use scheduleFinally or scheduleDeferred. It will inject a handler for a piece of code into the event queue so that it will run after all other code in the current execution context (current execution context == where ever you are in the current JavaScript object hierarchy with the window as the root object) is run but before the next event that is placed in the queue.

Supper answered 24/3, 2011 at 22:8 Comment(5)
Also, is the attachment of our widgets and UI elements to the DOM the last event in the loop, after which the browser event loop completes?Franchot
DOM manipulation events are handled in the exact same way as the other events. They are not necessarily the first or the last event to fire, just wherever they happen to land in the queue. They also bubble up the DOM tree so you can have listeners for those events anywhere on the tree 'above' where the event is triggered. The complicating factor, in vanilla js, is that each browser handles DOM mutation events differently.Supper
GWT 'captures' events with the GWTEventSystem to try and create a unified event handling scheme. Events follow the tree bubbling scheme outlined above, but for each event triggered it first passed through the GWT HandlerManager which looks for Handlers registered for that type of event. If it finds one it triggers the EventHandler code if not it passes it back to the browser for standard handling. What are you trying to do, exactly?Supper
Thanks for that reply. I was having a problem with doing some programmatic DOM manipulation using GWTP and was trying to figure out when and where exactly I would have to run the ScheduleDeferred() command. Here is the question I am talking about: #5425695Franchot
In general, switching tabs does not change the DOM. Both still exist and the tab switching is more or less a style trick. I am not 100% sure that is how GWT treats it. I will look into it and try to answer your other question.Supper

© 2022 - 2024 — McMap. All rights reserved.