Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Emitter

An internal class to construct implementations of {@link events.Stream} with a privately scoped emit function.

This allows safe construction of event-based interfaces without exposing the means of emitting events.

Hierarchy

  • Emitter

Index

Constructors

constructor

  • Create an Emitter

    If a Stream is provided in the options, the Emitter will use it as the stream to emit events to. In this case, the listeners must also be supplied.

    If no Stream is provided, one will be created with the optional allowedEvents option.

    If the event option is specified, listeners will be registered by calling Stream.on passing the event option and the callback option as parameters.

    Parameters

    Returns Emitter

Properties

Private closed

closed: boolean = false

A flag indicating whether the Emitter is closed

Private listeners

listeners: Listeners

The listeners object mapping event names to arrays of callback functions

Private stream

stream: StreamImpl

The stream that is used to register listeners

Methods

close

  • close(reason?: any): void
  • Close the emitter. This will emit a close event. No further events will be emitted.

    Parameters

    • Optional reason: any

      the reason for closing the emitter. This can be of any type. The type of the close reason should be documented in the concrete implementation of the Stream that the emitter is linked to.

    Returns void

emit

  • emit(event?: undefined | string, ...args: any[]): void
  • Emit an event.

    When an event is emitted, all listeners registered with the Stream for this event will be called.

    If the emitter is closed, or no event is specified, nothing will be emitted.

    Any additional arguments will be passed to the listeners.

    Parameters

    • Optional event: undefined | string

      the event name

    • Rest ...args: any[]

      arguments to pass to the listeners

    Returns void

Private emit_unsafe

  • emit_unsafe(event: string, args?: any[]): void
  • Emit an event without checking if the emitter is closed.

    see

    Emitter.emit

    Parameters

    • event: string

      the event name

    • Optional args: any[]

      arguments to pass to the listeners

    Returns void

error

  • error(reason?: any): void
  • Signal a error and close the emitter. This will emit an error event and subsequently a close event. No further events will be emitted.

    Parameters

    • Optional reason: any

      the error causing the emitter to close

    Returns void

get

  • Get the stream that is used to register the callbacks

    Returns StreamImpl

    the internal stream object

immediate

  • immediate(event?: undefined | string, ...args: any[]): void
  • Process an event immediately.

    Experimental.

    Parameters

    • Optional event: undefined | string
    • Rest ...args: any[]

    Returns void

listen

  • Add a listener to all events

    Parameters

    • fn: StreamCallback

      a callback function that will be called whenever an event is emitted. The event name will be passed as the first argument.

    Returns void

Private listener

  • listener(): void
  • A single listener that will be called whenever an event is emitted. The event name will be passed as the first argument.

    Returns void

Static create

  • Create an emitter factory that can create an Emitter.

    Parameters

    • Optional allowedEvents: string[]

      a list of allowed event names

    Returns EmitterFactory

    a factory that creates an Emitter