raphaelameaume / lemonade-events

Minimal event system πŸ‘€

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Minimal event system πŸ‘€

License:MIT License


Languages

Language:JavaScript 100.0%