Class Event<Parent, Args>

An event that handles subscription and fires its listeners in a synchronous fashion.

Each instance represents a single event:

const event = new Event(valueForThisInListeners);

The event can be emitted via the emit method:

event.emit('first-param', 'second-param');

Listeners can be added directly on the event, but it is recommended to use .subscribable for any API that is public:

// Adding a listener directly on the event
event.subscribe(() => ...);

// Subscribable provides a public API
event.subscribable(() => ..)
event.subscribable.subscribe(() => ...);

Listeners can be unsubscribed either via their handle or via the event:

// Use handle for easier unsubscription
const handle = event.addListener(() => ...);
handle.unsubscribe();

// Unsubscribe the actual listener
const listener = () => ...;
eventOrSubscribable.subscribe(listener);
eventOrSubscribable.unsubscribe(listener);

Types are fully supported and especially useful when events are used in classes:

import { Event, Subscribable } from 'atvik';

class Counter {
// Declaration of the event including the parameters it supports
private countUpdatedEvent: Event<this, [ count: number ]>;

public constructor() {
this.countUpdatedEvent = new Event(this);
this.count = 0;
}

public get onCountUpdated(): Subscribable<this, [ count: number ]> {
// Return `subscribable` of event - which only supports listening and not emitting
return this.countUpdatedEvent.subscribable;
}

public increment() {
// Increments the count and emits the value
this.count++;
this.countUpdatedEvent.emit(this.count);
}
}

// Create the counter and register the event
const counter = new Counter();
counter.onCountUpdated(count => console.log('Count is now', count));

// Request an increment triggering the listener
counter.increment();

Type Parameters

  • Parent

  • Args extends any[] = []

Hierarchy

  • Event

Implements

Constructors

  • Create a new event.

    Type Parameters

    • Parent

    • Args extends any[] = []

    Parameters

    • parent: Parent

      the parent that will be passed to listener as their this

    • Optional options: EventOptions

      options for creating this event

    Returns Event<Parent, Args>

Properties

subscribable: Subscribable<Parent, Args>

Public Subscribable that can safely be shared with consumers that should be able to listen for events.

Accessors

  • get hasListeners(): boolean
  • Get if there are any listeners available.

    Returns boolean

    true if listeners are present

  • get listeners(): Listener<Parent, Args>[]
  • Get a copy of the listeners as an array.

    Returns Listener<Parent, Args>[]

    listeners as array

Methods

  • Emit this event asynchronously. This will invoke all of the listeners with the passed arguments. Triggering of the listeners will be done in order, waiting for a previous listener to finish before invoking the next one.

    This method will not use the current ErrorStrategy and will instead reject if an error occurs.

    Parameters

    • Rest ...args: Args

      arguments that the listeners will receive

    Returns Promise<void>

    • promise that resolves when all listeners have handled the event
  • Clear all listeners for this event.

    Returns void

  • Emit this event. This will invoke all of the listeners with the passed arguments. If a listener is asynchronous this will method will use the current ErrorStrategy to handle errors, and will continue to trigger listeners while the listener is handling the event.

    Parameters

    • Rest ...args: Args

      arguments that the listeners will receive

    Returns void

  • Create a subscribable that will apply the specified filter to any listeners added.

    Parameters

    • filter: ((...args) => boolean | Promise<boolean>)

      function used to filter events

        • (...args): boolean | Promise<boolean>
        • Parameters

          • Rest ...args: Args

          Returns boolean | Promise<boolean>

    Returns Subscribable<Parent, Args>

    filtered Subscription

  • Monitor for changes to listeners. Only a single monitor is supported at a single time. This is intended to be used to react to if listeners are currently registered. This can be used for things such as only listening to events from other objects when this event is active.

    Parameters

    • monitor: ((event) => void)

      function used to monitor for changes to listeners

        • (event): void
        • Parameters

          • event: Event<Parent, Args>

          Returns void

    Returns void

  • Get a promise that will resolve the first time this event is fired after this call.

    Returns Promise<Args>

    promise that resolves the next time the event is fired

  • Emit this event in parallel. This will invoke all of the listeners with the passed arguments. Triggering of the listeners will done in parallel.

    This method will not use the current ErrorStrategy and will instead reject if an error occurs.

    Parameters

    • Rest ...args: Args

      arguments that the listeners will receive

    Returns Promise<void>

    • promise that resolves when all listeners have handled the event
  • Stop monitoring for listener changes.

    Returns void

  • Subscribe to this event using the given listener. The listener will be invoked any time the event is emitted.

    Parameters

    • listener: Listener<Parent, Args>

      listener to subscribe

    Returns void

  • Unsubscribe a listener from this handler. The specified listener will no longer be invoked when the event is emitted.

    Parameters

    • listener: Listener<Parent, Args>

      listener to unsubscribe

    Returns void

  • Unsubscribe a listener from this handler. The specified listener will no longer be invoked when the event is emitted.

    Parameters

    • listener: Listener<Parent, Args>

      listener to unsubscribe

    Returns void

  • Create a subscribable that changes the this argument used for listeners.

    Type Parameters

    • NewThis

    Parameters

    • newThis: NewThis

      what should be treated as this for event listeners

    Returns Subscribable<NewThis, Args>

    modified Subscribable

Generated using TypeDoc