A controller that removes a group of DOM event listeners together.
An AbortController provides an AbortSignal that can be shared across multiple event listeners. Calling abort() removes every listener registered with that signal at once, without needing to keep individual EventListener handles.
Pass the controller's signal to each listener:
document.addEventListener(
"mousemove", OnDrag, { .signal = drag.
signal() });
document.addEventListener(
"mouseup", OnDrop, { .signal = drag.
signal() });
A controller that removes a group of DOM event listeners together.
Definition EventListener.h:452
AbortSignal signal() const
Get the signal to pass in AddEventListenerOptions (signal).
Definition EventListener.h:495
void abort()
Remove every listener added with this controller's signal (abort).
Definition EventListener.h:503
Tying Listeners to an Object
A callback that captures this lives as long as the page. Unlike on the web, destroying an AbortController aborts it automatically.
You should store a controller as a class member so its listeners stop firing when the owning object is destroyed:
class Hud {
public:
"click", [this] { CloseHud(); }, { .signal = listeners_.signal() });
}
private:
void CloseHud() {}
dom::AbortController listeners_;
};
The root of a page's DOM tree in a View or frame.
Definition Document.h:106
Element getElementById(std::string_view id) const
Find the element with a certain id (getElementById).
Definition Document.h:190
EventListener addEventListener(std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const
Listen for an event on this element (addEventListener).
Definition Element.h:1540
Replacing a Controller
An AbortController aborts only once. After aborting, its signal stays aborted and won't add new listeners.
To start over, assign a fresh controller to the variable (the old controller aborts first):
- Note
- Once its signal has added a listener, you must abort, destroy, or assign over the controller on the Renderer's thread.
- See also
- dom::AbortSignal, dom::AddEventListenerOptions, dom::EventListener