docs
Loading...
Searching...
No Matches
AbortController

#include <Ultralight/dom/EventListener.h>

Overview

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() });
// Later:
drag.abort();
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:
explicit Hud(dom::Document document) {
document.getElementById("close").addEventListener(
"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

Public Member Functions

 AbortController ()
 Create a controller that hasn't aborted.
 AbortController (const AbortController &)=delete
AbortController & operator= (const AbortController &)=delete
 AbortController (AbortController &&other) noexcept=default
 Move constructor (other becomes empty, and its signal() then returns a signal that never aborts).
AbortController & operator= (AbortController &&other) noexcept
 Move assignment (aborts this controller, then takes over other's signal).
 ~AbortController ()
 Destroy this controller and abort it.
AbortSignal signal () const
 Get the signal to pass in AddEventListenerOptions (signal).
void abort ()
 Remove every listener added with this controller's signal (abort).

Constructor & Destructor Documentation

◆ AbortController() [1/3]

AbortController ( )
inline

Create a controller that hasn't aborted.

◆ AbortController() [2/3]

AbortController ( const AbortController & )
delete

◆ AbortController() [3/3]

AbortController ( AbortController && other)
defaultnoexcept

Move constructor (other becomes empty, and its signal() then returns a signal that never aborts).

Parameters
otherThe AbortController to move from.

◆ ~AbortController()

~AbortController ( )
inline

Destroy this controller and abort it.

Member Function Documentation

◆ abort()

void abort ( )
inline

Remove every listener added with this controller's signal (abort).

After this, signal().aborted() returns true and listeners added with the signal are never added. Calling it again does nothing. Safe to call from inside a listener's callback.

◆ operator=() [1/2]

AbortController & operator= ( AbortController && other)
inlinenoexcept

Move assignment (aborts this controller, then takes over other's signal).

Parameters
otherThe AbortController to move from.
Returns
Returns this AbortController.

◆ operator=() [2/2]

AbortController & operator= ( const AbortController & )
delete

◆ signal()

AbortSignal signal ( ) const
inline

Get the signal to pass in AddEventListenerOptions (signal).

Returns
Returns this controller's signal. Every call returns the same signal.

The documentation for this class was generated from the following file: