What is yaeti?
The 'yaeti' npm package is a lightweight library that provides a simple implementation of the EventTarget interface, which is a part of the DOM specification. This package is useful for creating custom event-driven architectures in JavaScript applications.
What are yaeti's main functionalities?
EventTarget Implementation
This feature allows you to create an instance of EventTarget, add event listeners, and dispatch events. It mimics the behavior of the EventTarget interface found in the DOM, making it useful for custom event handling in non-DOM environments.
const yaeti = require('yaeti');
// Create an instance of EventTarget
const eventTarget = new yaeti.EventTarget();
// Define an event listener
function eventListener(event) {
console.log('Event received:', event.type);
}
// Add the event listener
eventTarget.addEventListener('customEvent', eventListener);
// Dispatch an event
const event = new yaeti.Event('customEvent');
eventTarget.dispatchEvent(event);
Custom Event Creation
This feature allows you to create custom events with additional properties. The 'detail' property can be used to pass extra information with the event, similar to the CustomEvent interface in the DOM.
const yaeti = require('yaeti');
// Create a custom event with additional properties
const customEvent = new yaeti.Event('customEvent', {
detail: { key: 'value' }
});
// Access the custom properties
console.log(customEvent.detail); // Output: { key: 'value' }
Other packages similar to yaeti
eventemitter3
EventEmitter3 is a high-performance event emitter for Node.js and the browser. It provides a similar event-driven architecture but is more focused on performance and flexibility. Unlike yaeti, it does not aim to mimic the DOM EventTarget interface.
mitt
Mitt is a tiny (~200 bytes) functional event emitter. It provides a simple and minimalistic API for event handling. While it does not mimic the DOM EventTarget interface like yaeti, it is extremely lightweight and easy to use.
event-target-shim
Event-target-shim is a polyfill for the EventTarget interface. It provides a more complete and spec-compliant implementation of EventTarget, making it a closer alternative to yaeti for those who need full compliance with the DOM specification.
yaeti
Yet Another EventTarget Implementation.
The library exposes both the EventTarget interface and the Event interface.
Installation
$ npm install yaeti --save
Usage
var yaeti = require('yaeti');
function Foo() {
yaeti.EventTarget.call(this);
}
Foo.prototype = Object.create(yaeti.EventTarget.prototype);
Foo.prototype.constructor = Foo;
var foo = new Foo();
function listener1() {
console.log('listener1');
}
function listener2() {
console.log('listener2');
}
foo.addEventListener('bar', listener1);
foo.addEventListener('bar', listener2);
foo.removeEventListener('bar', listener1);
var event = new yaeti.Event('bar');
foo.dispatchEvent(event);
API
yaeti.EventTarget
interface
Implementation of the EventTarget interface.
ES5
function Foo() {
yaeti.EventTarget.call(this);
}
Foo.prototype = Object.create(yaeti.EventTarget.prototype);
Foo.prototype.constructor = Foo;
ES6
class Foo extends EventTarget () {
constructor () {
super();
}
}
The interface implements the addEventListener
, removeEventListener
and dispatchEvent
methods as defined by the W3C.
listeners
read-only property
Returns an object whose keys are configured event types (String) and whose values are an array of listeners (functions) for those event types.
yaeti.Event
interface
Implementation of the Event interface.
NOTE: Just useful in Node (the browser already exposes the native Event
interface).
var event = new yaeti.Event('bar');
Author
Iñaki Baz Castillo
License
MIT