Skip to content

molvqingtai/event-hub

Repository files navigation

EventHub

version deno land download JavaScript Style Guide

🚌 This is a minimalist event hub.

Install

npm i @resreq/event-hub

or

yarn add @resreq/event-hub

If you use the Deno

Please add to import_map.json.

import EventHub from 'https://esm.sh/@resreq/event-hub'

Documentation

Get Started

import EventHub from '@resreq/event-hub'

const hub = new EventHub()

hub.on('custom-send', (message) => {
  console.log(message) // => Hello, EventHub!
})

hub.emit('custom-send', 'Hello, EventHub!')

Instance Methods

Event

Event name type.

type Event = string | symbol

on(event,handler)

  • Arguments:
    • {Event | Event[]} event
    • {Function} handler
  • Usage:

    Listen for a custom event on the current Instance. Events can be triggered by instance.emit. The handler will receive all the additional arguments passed into these event-triggering methods.

once(event,handler)

  • Arguments:
    • {Event} event
    • {Function} handler
  • Usage:

    Listen for a custom event, but only once. The listener will be removed once it triggers for the first time.

off([event,handler])

  • Arguments:
    • {Event | Event[]} event
    • {Function} [handler]
  • Usage:

    Remove custom event listener(s).

    • If no arguments are provided, remove all event listeners;
    • If only the event is provided, remove all listeners for that event;
    • If both event and handler are given, remove the listener for that specific handler only.

emit(event,...args)

  • Arguments:

    • {Event} event

    • {any} ...args

  • Usage:

    Trigger an event on the current instance. Any additional arguments will be passed into the listener’s handler function.

License

This project is licensed under the MIT License - see the LICENSE file for details