Function createEventAdapter

  • Adapt an event emitted by a Node EventEmitter or by a DOM event target. Adapters are useful when wanting to bridge events from a third-party library or from the DOM.

    This adapter will register every listener with the event emitter, and let you use the Subscribable API to subscribe, unsubscribe, filter and iterate over events.

    Three different types of event emitters are supported:

    • Emitters with on and off methods, such as EventEmitter in NodeJS
    • Emitters with addEventListener and removeEventListener methods such as DOM event targets like elements
    • Emitters with addListener and removeListener methods

    Limitations: If the adapted event emitter has a method to clear all listeners the returned Subscribable may falsely report true when unsubscribe is called.

    Node EventEmitter

    const events = new EventEmitter();

    const onEcho = createEventAdapter(events, 'echo');
    onEcho.subscribe(value => console.log('echo', value));

    events.emit('echo, 'argument');

    DOM events

    DOM events can be adapted:

    // Adapt DOM events
    await createEventAdapter(document, 'DOMContentLoaded').once();

    const onFocus = createEventAdapter(htmlElement, 'focus');
    onFocus(event => console.log('focused', event));

    Type Parameters

    Parameters

    • emitter: Emitter

      emitter to adapt an event from

    • event: Event

      event to adapt

    • Optional options: EventAdapterOptions

      options for adapter

    Returns Subscribable<Emitter, Args>

    subscribable

Generated using TypeDoc