What is event-target-shim?
The event-target-shim npm package provides a cross-platform, shim implementation of the EventTarget interface, allowing developers to use a consistent API for event handling across different environments. It supports the standard addEventListener, removeEventListener, and dispatchEvent methods, along with custom event types. This makes it useful for projects that need to handle events in both browser and non-browser environments, such as Node.js applications or web components.
What are event-target-shim's main functionalities?
Basic Event Handling
This demonstrates how to create an instance of EventTarget, add an event listener for a custom event type, and then dispatch an event of that type. It's the basic usage for handling custom events.
const EventTargetShim = require('event-target-shim');
const myEventTarget = new EventTargetShim();
myEventTarget.addEventListener('customEvent', function(event) {
console.log(`Received: ${event.type}`);
});
myEventTarget.dispatchEvent(new EventTargetShim.Event('customEvent'));
Using Options for addEventListener
This example shows how to use the `once` option with `addEventListener` to automatically remove the event listener after it has been invoked once, demonstrating the package's support for event listener options.
const EventTargetShim = require('event-target-shim');
const myEventTarget = new EventTargetShim();
myEventTarget.addEventListener('customEvent', function(event) {
console.log(`Received: ${event.type}`);
}, { once: true });
myEventTarget.dispatchEvent(new EventTargetShim.Event('customEvent'));
myEventTarget.dispatchEvent(new EventTargetShim.Event('customEvent'));
Other packages similar to event-target-shim
eventemitter3
EventEmitter3 is a high-performance event emitter. While event-target-shim mimics the EventTarget interface specifically, EventEmitter3 provides a more general-purpose event handling mechanism. It doesn't follow the browser's EventTarget API as closely but offers a lightweight, fast solution for event management.
wolfy87-eventemitter
Wolfy87's EventEmitter is another alternative for event handling, offering a similar set of functionalities to event-target-shim but with a different API. It provides a more object-oriented approach to event handling, with features like namespaced events, which can be particularly useful in complex applications. Compared to event-target-shim, it might offer more flexibility in certain scenarios but doesn't aim to replicate the EventTarget interface directly.
event-target-shim
A polyfill for W3C EventTarget, plus few extensions.
See Also: https://dom.spec.whatwg.org/#interface-eventtarget
Overview
This module uses native implementation of EventTarget if it exists.
otherwise, defines shim.
Ummm, occured Illegal constructor
exception at native.
This module always defines shim.
And this provides an utility to define properties for attribute listeners (e.g. obj.onclick
).
declare class EventTarget {
addEventListener(type: string, listener: (event: Event) => void, capture?: boolean = false): void;
removeEventListener(type: string, listener: (event: Event) => void, capture?: boolean = false): void;
dispatchEvent(event: Event): void;
}
declare function EventTarget(...types: string[]): typeof(EventTarget);
Installation
npm install event-target-shim --save
Usage
This module has been desined that uses together Browserify.
import EventTarget from "event-target-shim";
class YourCoolType extends EventTarget {
}
class YourAwesomeType extends EventTarget("message", "error") {
}
{
let event = document.createEvent("Event");
event.initEvent("message", false, false);
event.data = "Hello!";
obj.dispatchEvent(event);
}
{
obj.dispatchEvent({type: "message", data: "Hello!"});
}