@resreq/event-hub

This is a minimalist event hub.

Usage no npm install needed!

<script type="module">
  import resreqEventHub from 'https://cdn.skypack.dev/@resreq/event-hub';
</script>

README

EventHub

version deno land workflow coverage 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.

{
  "imports": {
    "EventHub": "https://deno.land/x/event_hub/src/index.ts"
  }
}

Documentation

Get Started

It is recommended to use the kebab-case event name.

import EventHub from '@resreq/event-hub'

const hub = new EventHub()

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

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

Event

Custom event name type.

type Event = string | symbol

Instance Methods

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