Skip to content

Latest commit

 

History

History

eventbus

@philip21/eventbus

  • universal and light(1kb) event bus module
  • no dependencies
  • typescript type inference supports.
  • simple and light implementation

Installation

npm install @philip21/eventbus --save

Getting Started

import EventBus from '@philip21/eventbus';

const eventBus = new EventBus();

eventBus.on('a', console.log);
eventBus.on({
  b: console.log,
  c: console.log,
});

eventBus.emit('a', 2);
eventBus.emit('b', 'p');
eventBus.emit('c', { n: 1 });

Type Inference Support

interface EventDefinition {
  a: 1 | 2 | 3;
  b: 'p';
}

const eventBus = new EventBus<EventDefinition>().on({
  a: console.log,
  b: console.log,
});

/**
 * or
 *
 * new EventBus<{
 *   a: 1 | 2 | 3;
 *   b: 'p';
 * }>();
 */

Specifications

  • spec code
  • .on(eventName, eventHandler, life?)
  • .on(eventSpecification)
  • .once(eventName, eventHandler)
  • .once(eventSpecification)
  • .off()
  • .off(eventName)
  • .off(eventName, eventHandler)
  • .has()
  • .has(eventName)
  • .has(eventName, eventHandler)
  • .emit(eventName, eventPayload?)