I am writing a Firefox extension, which is doing two things (for the context of this question):
- Registering for certain DOM events, viz
DOMContentLoadedandDOMFrameContentLoaded. - In the call back for the events, access the DOM APIs and do certain operations.
The extension gets the first event (either DOMContentLoaded or DOMFrameContentLoaded), and the callback function invokes some DOM APIs. I am observing, before the call returning back to my extension from the DOM API call, another event firing and my call back function getting invoked (I haven’t been able to narrow down which specific DOM API, as my extension invokes bunch of DOM APIs).
Is this even possible? BTW, I am on Firefox 12 on Windows. I am printing the threadManager.isMainThread, and in both situations the event call back is being invoked on the main thread.
Any pointers will be highly appreciated.
JavaScript is generally single-threaded. However, this doesn’t mean that functions cannot be reentered (a function calling itself being the most obvious example). So an event handler could still be called while another event handler is executing. AFAICT this can happen under the following conditions:
element.setAttribute()will createDOMAttrModifiedevent and that event will be processed beforeelement.setAttribute()returns, including running event handlers.alert()) is opened – the current event handler will wait for this dialog to be closed while other event handlers can still be triggered. A less common case is the usage of theyieldkeyword in generators.alert()will callnsIThread.processNextEvent()internally to ensure that events are processed while the caller is blocked.