1.6.0 • Published 1 year ago

@resreq/event-hub v1.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

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!

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

1.6.0

1 year ago

1.4.5

2 years ago

1.5.0

2 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.7

3 years ago

1.0.0

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago