atvik
    Preparing search index...

    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.subscribe(() => ...);
    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 unknown[] = []

    Implements

    Index
    subscribable: Subscribable<Parent, Args>

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

    • get hasListeners(): boolean

      Get if there are any listeners available.

      Returns boolean

      true if listeners are present

    • get listenerCount(): number

      Get the number of listeners.

      Returns number

      number of listeners

    • 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

      • ...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 method will use the current ErrorStrategy to handle errors, and will continue to trigger listeners while the listener is handling the event.

      Parameters

      • ...args: Args

        arguments that the listeners will receive

      Returns void

    • Monitor for changes to listeners. The monitor is called after every subscribe, unsubscribe and clear. 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.

      More than one monitor can be added. The same monitor is only added one time.

      Parameters

      • monitor: (event: this) => void

        function used to monitor for changes to listeners

      Returns SubscriptionHandle

      handle that removes the monitor, also with using

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

      Parameters

      • Optionaloptions: OnceOptions

        options, such as a signal that stops the wait

      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 be done in parallel.

      This method will not use the current ErrorStrategy. All listeners are invoked, also if one of them fails. If one listener fails the promise rejects with its error. If more than one listener fails the promise rejects with an AggregateError that holds all of the errors.

      Parameters

      • ...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.

      Parameters

      • Optionalmonitor: (event: this) => void

        monitor to remove. If not given, all monitors are removed

      Returns void

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

      Parameters

      Returns void

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

      Parameters

      Returns void