1.0.4 • Published 7 years ago

emit-kit v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

emit-kit

Build Status Coverage Status Inline Docs dependencies Status npm Version

A wrapper on top of eventemitter3 with the convenient abstractions of event-kit.

Usage

import {Emitter} from 'emit-kit';

// The event emitter
class Superhero {
  constructor() {
    this._emitter = new Emitter();
  }

  onFlight(callback) {
    return this._emitter.on('flight', callback);
  }

  fly(time) {
    this._emitter.emit('flight', time);
  }
}

// The subscriber
class Supervillain {
  plotToKill(superhero) {
    this.surveillance = superhero.onFlight(time => console.log(time));
  }

  giveUp() {
    this.surveillance.cancel();
  }
}

API

Emitter

  • .on(event, callback, context)

    • Subscribes a callback to an event.
    • event - String. The name of the event.
    • callback - Function. Invoked with arguments
    • context (optional) - Object. The this object to bind to the callback.
  • .once(event, callback, context)

    • Same as .on() but with a one-time callback.
  • .emit(event, ...args)

    • Invokes all callbacks subscribed to event.
    • event - String. The name of the event.
    • args - Arguments to be passed to the callbacks.
  • .clear(event)

    • If event is given, removes only the callbacks subscribed to event. Else removes all callbacks.
    • event (optional) - String. The name of the event.
  • .count(event)

    • If event is given, returns the number of callbacks registered to event. Else returns the total number of callbacks.

Subscription

  • .cancel()
    • Unsubscribes from the event emitter.

Subscriptions

  • .add(...subs)

    • Groups subs together so that they can later be cancelled as a group.
  • .cancel()

    • Calls cancel asynchronously on all associated subs. Returns a promise that fulfills upon completion.
  • .cancelSync()

    • Calls cancel synchronously on all associated subs.
1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago