Interface EventHost<EventMap>

An object that may dispatch events and have handlers for those events. Similar to the DOM's EventTarget, this is a basic event system based on event naming.

Some of the advantages of EventHost over EventTarget:

  • Not all supported platforms support EventTarget construction or extension.
  • EventTarget is designed for the DOM, with bubble and capture phases not necessary in Vinyl.
  • Event types are strict.
  • Events can be plain objects, no need to extend CustomEvent.
  • Easier unsubscription and disposal; reduces the risk of memory leaks.

EventHost supports concurrent modification, that is, handlers may be added or removed during the execution of another handler.

interface EventHost<EventMap> {
    logPrefix: string;
    dispatch<K>(type: K, event: EventMap[K]): void;
    hasAnyListeners(): boolean;
    hasListeners(type: keyof EventMap): boolean;
    on<K>(type: K, handler: EventHandler<EventMap[K]>, options?: SignalOptions): Unsubscribe;
}

Type Parameters

  • EventMap

Hierarchy (view full)

Implemented by

Properties

logPrefix: string

A string prefix for all log statements made by this target.

Methods

  • Dispatches an event.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K

      The key of the event within EventMap

    • event: EventMap[K]

      If the passed event is read-only, then it may be re-used/cached. Otherwise, it should be a new event object every dispatch. If no target property is set on the event, the target will be set to this host.

    Returns void

  • Returns true if the event host has any listeners.

    Returns boolean

  • Returns true if the event host has any listeners for the given type.

    Parameters

    Returns boolean

  • Adds an event handler for the given typed event.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K

      The key representing the event type to listen for.

    • handler: EventHandler<EventMap[K]>

      A callback to invoke when the event with the given type is dispatched.

    • Optionaloptions: SignalOptions

      Options for changing listening behavior.

    Returns Unsubscribe

    Returns a method, when invoked, removes the handler.