What is event-lite?
event-lite is a lightweight event emitter library for JavaScript. It provides a simple and efficient way to handle events in your application, similar to Node.js's EventEmitter but with a smaller footprint.
What are event-lite's main functionalities?
Adding Event Listeners
This feature allows you to add event listeners to an event emitter instance. In the code sample, an event listener for the 'foo' event is added, and the event is emitted, triggering the listener.
const EventLite = require('event-lite');
const emitter = new EventLite();
function onFoo() {
console.log('foo event triggered');
}
emitter.on('foo', onFoo);
emitter.emit('foo');
Removing Event Listeners
This feature allows you to remove event listeners from an event emitter instance. In the code sample, the event listener for the 'foo' event is added and then removed, so emitting the event does not trigger the listener.
const EventLite = require('event-lite');
const emitter = new EventLite();
function onFoo() {
console.log('foo event triggered');
}
emitter.on('foo', onFoo);
emitter.off('foo', onFoo);
emitter.emit('foo');
Once Event Listeners
This feature allows you to add an event listener that will be triggered only once. In the code sample, the event listener for the 'foo' event is added with the 'once' method, so it is triggered only the first time the event is emitted.
const EventLite = require('event-lite');
const emitter = new EventLite();
function onFoo() {
console.log('foo event triggered');
}
emitter.once('foo', onFoo);
emitter.emit('foo');
emitter.emit('foo');
Other packages similar to event-lite
events
The 'events' package is the standard event emitter library used in Node.js. It provides a robust and feature-rich API for handling events. Compared to event-lite, it has more features but also a larger footprint.
eventemitter3
eventemitter3 is a high-performance event emitter for Node.js and the browser. It offers a similar API to event-lite but is optimized for performance and has additional features like wildcard event listeners.
mitt
mitt is a tiny (~200 bytes) functional event emitter. It provides a minimal API for handling events, making it even lighter than event-lite. However, it lacks some of the more advanced features found in other event emitter libraries.
event-lite.js
Light-weight EventEmitter (less than 1KB when gzipped)
Usage
const EventLite = require("event-lite");
function MyClass() {...}
EventLite.mixin(MyClass.prototype);
const obj = new MyClass();
obj.on("foo", function(v) {...});
obj.once("bar", function(v) {...});
obj.emit("foo", v);
obj.emit("bar", v);
obj.off("foo");
Node.js
npm install event-lite --save
Browsers
<script src="https://cdn.jsdelivr.net/npm/event-lite/dist/event-lite.min.js"></script>
TypeScript
import EventLite = require("event-lite");
class MyClass extends EventLite {
}
const obj = new MyClass();
obj.on("foo", v => {...});
obj.once("bar", v => {...});
obj.emit("foo", v);
obj.emit("bar", v);
obj.off("foo");
Repository
Documentation
See Also
License
The MIT License (MIT)
Copyright (c) 2015-2023 Yusuke Kawasaki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.