micro-event-manager
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

micro-event-manager

npm version

A tiny event manager library based on publish-subscribe pattern.

Installation

yarn add micro-event-manager

Usage

import { EventManager } from 'micro-event-manager';

// first create a manager instance
const eventManager = new EventManager();

// then subscribe to an event, it will return an unique key
const key = eventManager.subscribe('log', () => {
  console.log('executed');
});

// trigger the event, the second argument is optional
eventManager.publish('log', 'data');

// you can use the generated key to unsubscribe the event
eventManager.unSubscribe('log', key);

// when you are not sure if the subscription happens before the publishment
// and you want to make sure any published events should be triggered, you can use this function to subscribe
eventManager.ensureTriggeredAndSubscribe('log', () => {
  console.log('executed');
});

Package Sidebar

Install

npm i micro-event-manager

Weekly Downloads

1,514

Version

1.0.0

License

MIT

Unpacked Size

13.6 kB

Total Files

11

Last publish

Collaborators

  • xinkule