got-events

Simple events for simple objects. Three methods to rule'em all: `on`, `off`, `trigger`.

Usage no npm install needed!

<script type="module">
  import gotEvents from 'https://cdn.skypack.dev/got-events';
</script>

README

got-events

Simple events for simple objects. Three methods to rule'em all: .on, .off, .trigger. Lots of fun.

Install (coming soon)

npm install --save got-events

Usage

Three options:

  1. Function:
var evHandler = require('got-events')();
  1. Extend object:
require('got-events').extend(simpleObject);

Optionally preserving old attributes:

require('got-events').extend(simpleObject, true);
  1. Singleton:
var evHandler = require('got-events').singleton;

Object methods

  • .on(eventName, callback)

    Adds a callback to an event.

    1. eventName: String or Array of strings.
    2. callback: Function.
  • .on(eventName, callbackId, callback)

    Adds a callback to an event, identified.

    1. eventName: String or Array of strings.
    2. callbackId: String.
    3. callback: Function.
  • .off(eventName)

    Clears the event's callback list.

    1. eventName: String or Array of strings.
  • .off(eventName, callback)

    Removes a callback from an event.

    1. eventName: String or Array of strings.
    2. callback: Function.
  • .off(eventName, callbackId)

    Removes an identified callback from an event.

    1. eventName: String or Array of strings.
    2. callbackId: String or Array of strings.
  • .trigger(eventName, data)

    Triggers an event with err = null.

    1. eventName: String or Array of strings.
    2. data: Anything.
  • .trigger(eventName, err, data...)

    Triggers an event.

    1. eventName: String or Array of strings.
    2. err: Error or Boolean.
    3. data...: Anything. Every argument from here will be passed to callback.

Other features

  • Wildcard event '*' fired on every trigger.