EventTarget

Node API: Event Target

The EventTarget and Event objects are a Node.js-specific implementation of the EventTarget Web API that are exposed by some Node.js core APIs.

The EventTarget interface is implemented by objects that can receive events and may have listeners for them; events are dispatched via dispatchEvent, and listeners are managed via addEventListener and removeEventListener.

Event targets are implementable from host code or guest code.

Event targets in Node.js-style dispatch form differ slightly from web-standard event targets; see below for more information.

Node.js EventTarget vs. DOM EventTarget

There are two key differences between the Node.js EventTarget and the EventTarget Web API:

  • Whereas DOM EventTarget instances may be hierarchical, there is no concept of hierarchy and event propagation in Node.js. That is, an event dispatched to an EventTarget does not propagate through a hierarchy of nested target objects that may each have their own set of handlers for the event.

  • In the Node.js EventTarget, if an event listener is an async function or returns a Promise, and the returned Promise rejects, the rejection is automatically captured and handled the same way as a listener that throws synchronously (see EventTarget error handling docs for details).

Inheritors

Functions

Link copied to clipboard
abstract fun addEventListener(type: String, listener: EventListener)
abstract fun addEventListener(type: String, options: AddEventListenerOptions, listener: EventListener)
abstract fun addEventListener(type: String, listener: Value, options: Value?)

Adds a new handler for the type event. Any given listener is added only once per type and per capture option value.

Link copied to clipboard
abstract fun dispatchEvent(event: Event): Boolean

Dispatches the event to the list of handlers for Event.type.

Link copied to clipboard
abstract fun removeEventListener(type: String, listener: EventListener)
abstract fun removeEventListener(type: String, listener: Value)
abstract fun removeEventListener(type: String, listener: EventListener, options: RemoveEventListenerOptions)
abstract fun removeEventListener(type: String, listener: EventListener, options: Value)
abstract fun removeEventListener(type: String, listener: Value, options: Value)

Removes the listener from the list of handlers for event type.