@airb/events

Events for JavaScript

Usage no npm install needed!

<script type="module">
  import airbEvents from 'https://cdn.skypack.dev/@airb/events';
</script>

README

events

Events for JavaScript

Install

$ yarn add @airb/events

Usage

import createEmitter, { EventEmitter } from '@airb/events';

// create
const emitter = createEmitter();

// or extends
class MyEmitter extends EventEmitter {
    // todo something;
}

// create listener
let listener = (...args) => {
    // todo something with args;
    // or `throw error` stop follow listeners
}

// add or remove listener
emitter.on('data', listener);
emitter.once('data', listener);
emitter.off('data', listener);

// emit data
emitter.emit('data', 1, 2, 3);

Class: EventEmitter

on(name, listener)

add listener, return this;

once(name, listener)

add once listener, return this;

next(name, listener)

add once listener, return a promise as the listener resolved;

off(name, listener)

remove listener, return this;

emit(name, ...args)

emit listener, return false as task stoped by throw error or true;

Event: $emit

Emitted when emit some listener by emit function

Event: $error

Emitted when some listener throw error;