0.1.0 • Published 4 years ago

lemonade-events v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

lemonade-events

lemonade-events is a minimal event system. The implementation is based on mitt with a few changes:

  • It returns the unsuscribe function on subscribe to avoid keeping a reference of the listener function
  • It has a log mode for development
  • It exports an eventBus by default

Installation

npm install lemonade-events

Usage

// import the full instance
import EventBus from "lemonade-events";
// or import only needed methods
import { on, emit } from "lemonade-events";

EventBus.log = true;

let e = '@example / EVENT_NAME';

// subscribe to an event
function listener(data) {
    console.log(data); // { someData: true }
}

let eventListener = EventBus.on(e, listener);

// add subscription
EventBus.emit(e, { someData: true });

// remove subscription
eventListener();
// or
EventBus.off(e, listener);

You can also create an instance of EventBus by yourself if you need.

import { EventBus } from "lemonade-helpers";

let eventBus = EventBus();

// local instance
eventBus.on();
eventBus.emit();

Credits

License

MIT License, see LICENSE for details