What is @antv/event-emitter?
@antv/event-emitter is a lightweight and efficient event emitter library that allows you to create and manage custom events in JavaScript applications. It is part of the AntV visualization suite and is designed to be simple yet powerful, making it easy to integrate event-driven architecture into your projects.
What are @antv/event-emitter's main functionalities?
Event Emission
This feature allows you to emit events with a specific name and optional data payload. The emitted event can then be listened to by any registered listeners.
const EventEmitter = require('@antv/event-emitter');
const emitter = new EventEmitter();
emitter.emit('eventName', { key: 'value' });
Event Listening
This feature allows you to register listeners for specific events. When the event is emitted, the registered listeners are called with the event data.
const EventEmitter = require('@antv/event-emitter');
const emitter = new EventEmitter();
emitter.on('eventName', (data) => {
console.log('Event received:', data);
});
emitter.emit('eventName', { key: 'value' });
Removing Event Listeners
This feature allows you to remove specific listeners for an event. This is useful for cleaning up and preventing memory leaks in your application.
const EventEmitter = require('@antv/event-emitter');
const emitter = new EventEmitter();
const listener = (data) => {
console.log('Event received:', data);
};
emitter.on('eventName', listener);
emitter.off('eventName', listener);
emitter.emit('eventName', { key: 'value' });
Other packages similar to @antv/event-emitter
events
The 'events' package is the standard Node.js event emitter library. It provides a similar API for emitting and listening to events. It is widely used and well-documented, making it a reliable choice for event-driven programming in Node.js.
eventemitter3
The 'eventemitter3' package is a high-performance event emitter library for Node.js and the browser. It offers a similar API to @antv/event-emitter but is optimized for speed and memory efficiency. It is a popular choice for applications that require a fast and lightweight event emitter.
mitt
The 'mitt' package is a tiny, functional event emitter library. It provides a minimal API for emitting and listening to events, making it easy to use and integrate into projects. It is a good alternative for developers looking for a simple and lightweight event emitter.
@antv/event-emitter
event-emitter for @antvis.
Install
npm i --save @antv/event-emitter
Usage
import EE from '@antv/event-emitter';
const ee = new EE();
ee.on('mouseover', () => {});
ee.once('click', () => {});
ee.on('*', () => {});
ee.emit('click', 1, 'hello', true);
ee.off('click');
API
Simple and similar with event-emitter
.
on(evt: string, callback: Function)
: listen an event.once(evt: string, callback: Function)
: listen a event only once.emit(evt: string, ...parameters: any[])
: emit / trigger an event with parameters.off(evt?: string, callback?: Function)
: unsubscribe an event.getEvents()
: get all the exist events.