Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EventDispatcher

EventDispatcher is something like a map between the EventType and CallbackList.

EventDispatcher holds a map of <EventType, CallbackList> pairs. On dispatching, EventDispatcher finds the CallbackList of the event type, then invoke the callback list. The invocation is always synchronous. The listeners are triggered when EventDispatcher.dispatch is called.

Nested listener safety

  1. If a listener adds another listener of the same event to the dispatcher during a dispatching, the new listener is guaranteed not to be triggered within the same dispatching. This is guaranteed by an unsigned 64 bits integer counter. This rule will be broken is the counter is overflowed to zero in a dispatching, but this rule will continue working on the subsequence dispatching.
  2. Any listeners that are removed during a dispatching are guaranteed not triggered.
export

Hierarchy

Index

Constructors

constructor

Properties

Protected argumentsAsArray

argumentsAsArray: boolean

Readonly Static argumentPassingExcludeEvent

argumentPassingExcludeEvent: ArgumentPassingMode = ...

ArgumentPassingMode: ExcludeEvent

static
readonly
memberof

EventDispatcher

Readonly Static argumentPassingIncludeEvent

argumentPassingIncludeEvent: ArgumentPassingMode = ...

ArgumentPassingMode: IncludeEvent

static
readonly
memberof

EventDispatcher

Readonly Static defaultArgumentPassingMode

defaultArgumentPassingMode: ArgumentPassingMode = ...

ArgumentPassingMode: ExcludeEvent

static
readonly
memberof

EventDispatcher

Methods

appendListener

  • Add the callback to the dispatcher to listen to event.
    The listener is added to the end of the listener list.
    Return a handle object which represents the listener. The handle can be used to remove this listener or insert other listener before this listener.
    If appendListener is called in another listener during a dispatching, the new listener is guaranteed not triggered during the same dispatching.
    If the same callback is added twice, it results duplicated listeners.
    The time complexity is O(1).

    memberof

    EventDispatcher

    Parameters

    • event: string | number
    • callback: Callback

    Returns CallbackNode

applyDispatch

  • applyDispatch(args: any[]): void
  • Dispatch an event.
    The listeners are called with arguments arg1, arg2, etc.
    Note the arguments are passed in an array, similar to Function.prototype.apply.

    memberof

    EventDispatcher

    Parameters

    • args: any[]

    Returns void

dispatch

  • dispatch(...args: any[]): void
  • Dispatch an event.
    The listeners are called with arguments arg1, arg2, etc.

    memberof

    EventDispatcher

    Parameters

    • Rest ...args: any[]

    Returns void

forEach

  • forEach(event: string | number, func: (callback: Callback) => any): void
  • Apply func to all listeners of event.
    The func receives one parameter which is the callback.
    Note: the func can remove any listeners, or add other listeners, safely.

    memberof

    EventDispatcher

    Parameters

    • event: string | number
    • func: (callback: Callback) => any

    Returns void

forEachIf

  • forEachIf(event: string | number, func: (callback: Callback) => any): boolean
  • Apply func to all listeners of event. func must return a boolean value, and if the return value is false, forEachIf stops the looping immediately.
    Return true if all listeners are invoked, or event is not found, false if func returns false.

    memberof

    EventDispatcher

    Parameters

    • event: string | number
    • func: (callback: Callback) => any

    Returns boolean

hasAnyListener

  • hasAnyListener(event: string | number): boolean
  • Return true if the dispatcher contains any callback.

    memberof

    EventDispatcher

    Parameters

    • event: string | number

    Returns boolean

hasListener

  • Return true if the dispatcher contains callback.
    callback can be a callback function, or a handle object.

    memberof

    EventDispatcher

    Parameters

    Returns boolean

insertListener

  • Insert the callback to the dispatcher to listen to event before the listener handle before. If before is not found, callback is added at the end of the listener list.
    before can be a callback function, or a handle object.
    Return a handle object which represents the listener. The handle can be used to remove this listener or insert other listener before this listener.
    If insertListener is called in another listener during a dispatching, the new listener is guaranteed not triggered during the same dispatching.
    The time complexity is O(1).

    memberof

    EventDispatcher

    Parameters

    Returns CallbackNode

prependListener

  • Add the callback to the dispatcher to listen to event.
    The listener is added to the beginning of the listener list.
    Return a handle object which represents the listener. The handle can be used to remove this listener or insert other listener before this listener.
    If prependListener is called in another listener during a dispatching, the new listener is guaranteed not triggered during the same dispatching.
    The time complexity is O(1).

    memberof

    EventDispatcher

    Parameters

    • event: string | number
    • callback: Callback

    Returns CallbackNode

removeListener

  • removeListener(event: string | number, handle: undefined | null | CallbackNode | Callback): boolean
  • Remove the listener callback which listens to event from the dispatcher.
    callback can be a callback function, or a handle object.
    Return true if the listener is removed successfully, false if the listener is not found.
    The time complexity is O(1).

    memberof

    EventDispatcher

    Parameters

    Returns boolean

Generated using TypeDoc