Quantum/DOM: Difference between revisions

Jump to navigation Jump to search
Event Targets section tweaks
(Runnable naming convention tweak)
(Event Targets section tweaks)
Line 72: Line 72:
== Event Targets ==
== Event Targets ==


A lot of existing Gecko code uses an nsIEventTarget to decide where to dispatch runnables. The DocGroup and TabGroup classes expose EventTargetFor(category) methods that return an event target. Using this event target is equivalent to calling Dispatch on the DocGroup or TabGroup. (The one difference is that no name is provided for the runnable.) {TabGroup,DocGroup}::EventTargetFor can be called on any thread. As a convenience, you can also use nsIDocument::EventTargetFor (also callable from any thread) or nsIGlobalObject::EventTargetFor (main thread only).
A lot of existing Gecko code currently uses an nsIEventTarget to decide where to dispatch runnables. The DocGroup and TabGroup classes expose EventTargetFor(category) methods that return an nsIEventTarget. Using this event target is equivalent to calling Dispatch on the DocGroup or TabGroup (except that unfortunately no name is provided for the runnable). {TabGroup,DocGroup}::EventTargetFor can be called on any thread. As a convenience, you can also use nsIDocument::EventTargetFor (also callable from any thread) or nsIGlobalObject::EventTargetFor (main thread only).


=== Runnable names ===
=== Runnable names ===


One disadvantage of using EventTargetFor is that any runnables dispatched this way are not given a name. However, there are other options for assigning a name to runnables. To have a name, a runnable needs to implement the GetName method of the nsINamed interface. mozilla::Runnable already does this. You can use the SetName method on an existing mozilla::Runnable to change its name.
One disadvantage of using EventTargetFor is that any runnables dispatched this way are not given a name. However, there are other options for assigning a name to runnables. To have a name, a runnable needs to inherit the nsINamed interface and implement its GetName method. mozilla::Runnable already does this, so you can use the SetName method on an existing mozilla::Runnable to set its name.


Usually, though, you'll be using EventTargetFor in cases where you don't have direct access to the runnable. Typically you'll be giving the event target to a sub-system that will dispatch multiple runnables. Timers, the IPC code, and workers are examples of this. In these cases it's best to modify the sub-system to set pass down an appropriate name for the runnable. The IPC code, for example, can set the runnable name to the name of the message being dispatched.
Usually, though, you'll be using EventTargetFor in cases where you don't have direct access to the runnable. Typically you'll be giving the event target to a sub-system that will dispatch multiple runnables. Timers, the IPC code, and workers are examples of this. In these cases it's best to modify the sub-system to set pass down an appropriate name for the runnable. The IPC code, for example, can set the runnable name to the name of the message being dispatched.
Confirmed users, Bureaucrats and Sysops emeriti
969

edits

Navigation menu