Pacote
    Preparing search index...

    Interface Emitter<E>

    interface Emitter<E extends Events> {
        emit<K extends string | number | symbol>(
            name: K,
            ...args: Parameters<E[K]>,
        ): void;
        on<K extends string | number | symbol>(
            name: K,
            fn: E[K],
            options?: SubscriberOptions,
        ): () => void;
    }

    Type Parameters

    • E extends Events
    Index

    Methods

    Methods

    • Fires the named event, triggering any subscribers previously bound to it using the .on() method. Arguments provided after the name are passed to the subscriber function.

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • name: K

        Event name.

      • ...args: Parameters<E[K]>

        Optional subscriber parameters.

      Returns void

      const emitter = createEmitter()
      emitter.on('greet', (name) => console.log(`Hello, ${name}!`))

      emitter.emit('greet', 'Pacote') // => 'Hello, Pacote!'
    • Binds a subscriber function to an event with the supplied name.

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • name: K

        Event name.

      • fn: E[K]

        Subscriber to invoke when the event is emitted.

      • Optionaloptions: SubscriberOptions

        Optional subscription options. If options.signal is provided, the subscriber is automatically removed when the signal is aborted.

      Returns () => void

      Function to unbind the event.

      const emitter = createEmitter()

      const unbind = emitter.on('greet', () => console.log('Hello, world!'))

      emitter.emit('greet') // triggers the callback
      unbind()
      emitter.emit('greet') // no longer triggers the callback
      const emitter = createEmitter()
      const controller = new AbortController()

      emitter.on(
      'greet',
      () => console.log('Hello, world!'),
      { signal: controller.signal }
      )

      emitter.emit('greet') // triggers the callback
      controller.abort()
      emitter.emit('greet') // no longer triggers the callback