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
An implementation of WHATWG EventTarget interface, plus few extensions.
- This provides
EventTarget
constructor that can inherit for your custom object. - This provides an utility that defines properties of attribute listeners (e.g.
obj.onclick
).
class Foo extends EventTarget("message", "error") {
}
Installation
npm install --save event-target-shim
Or download from dist
directory.
Usage
Basic
const EventTarget = require("event-target-shim");
class Foo extends EventTarget {
}
let foo = new Foo();
foo.addEventListener("foo", event => {
console.log(event.hello);
});
foo.addEventListener("foo", event => {
if (event.hello !== "hello") {
event.preventDefault();
}
});
let event = document.createEvent("CustomEvent");
event.initCustomEvent("foo", false, false, null);
event.hello = "hello";
foo.dispatchEvent(event);
foo.dispatchEvent({type: "foo", hello: "hello"});
if (!foo.dispatchEvent({type: "foo", cancelable: true, hello: "hey"})) {
console.log("defaultPrevented");
}
if (foo instanceof window.EventTarget) {
console.log("yay!");
}
The Extension for Attribute Listeners
const EventTarget = require("event-target-shim");
class Foo extends EventTarget("message", "error") {
}
class Foo extends EventTarget(["message", "error"]) {
}
let foo = new Foo();
foo.onmessage = event => {
console.log(event.data);
};
foo.onerror = event => {
console.log(event.message);
};
foo.addEventListener("message", event => {
console.log(event.data);
});
foo.dispatchEvent({type: "message", data: "hello"});
foo.dispatchEvent({type: "error", message: "an error"});
Use in ES5
-
Basic.
function Foo() {
EventTarget.call(this);
}
Foo.prototype = Object.create(EventTarget.prototype, {
constructor: {
value: Foo,
configurable: true,
writable: true
},
});
-
With attribute listeners.
function Foo() {
EventTarget.call(this);
}
Foo.prototype = Object.create(EventTarget("message", "error").prototype, {
constructor: {
value: Foo,
configurable: true,
writable: true
},
});
Use with RequireJS
require(["https://cdn.rawgit.com/mysticatea/event-target-shim/v2.0.0/dist/event-target-shim.min.js"], function(EventTarget) {
});
API
declare class EventTarget {
constructor();
addEventListener(type: string, listener?: (event: Event) => void, options?: boolean | AddEventListenerOptions): void;
removeEventListener(type: string, listener?: (event: Event) => void, options?: boolean | EventListenerOptions): void;
dispatchEvent(event: Event | EventLike): boolean;
}
declare function EventTarget(...types: string[]): EventTarget;
declare function EventTarget(types: string[]): EventTarget;
interface EventListenerOptions {
capture?: boolean;
}
interface AddEventListenerOptions extends EventListenerOptions {
once?: boolean;
passive?: boolean;
}
interface EventLike {
type: string;
cancelable?: boolean;
}