Simple Event Target
Simple Event Target is a TypeScript library designed to simplify the creation of classes that emit events. By leveraging TypeScript's powerful type system, it offers a streamlined, type-safe interface for event handling that closely mimics the familiar browser's EventListener API. The primary motivation behind Simple Event Target is to provide a lightweight, easy-to-use solution for event management without the overhead of additional options found in similar libraries.
Features
- Full TypeScript support for type-safe event handling.
- Mimics the browser's EventListener API for ease of use.
- Lightweight and straightforward, with no unnecessary options.
- No dependencies.
Installation
Install Simple Event Target using your favorite package manager:
pnpm add vrls-simple-event-target
Or, if you prefer npm
or yarn
, you can use:
npm install vrls-simple-event-target
yarn add vrls-simple-event-target
Usage
To get started with Simple Event Target, follow these steps:
-
Define a Custom Event Map: Start by creating a type that represents your custom event map. This map will define the events your class can emit and the type of the event handler functions.
-
Extend Your Class: Extend SimpleEventTarget
with your custom event map to create a new class capable of emitting events.
Example
Below is an example demonstrating how to create a custom class that emits a statechange
event:
import { SimpleEventTarget } from "vrls-simple-event-target";
type CustomState = "loading" | "ready";
type CustomEventMap = {
statechange: (newState: CustomState) => void;
};
class MyClass extends SimpleEventTarget<CustomEventMap> {
changeState(newState: CustomState) {
this.dispatchEvent("statechange", newState);
}
}
const myClassInstance = new MyClass();
myClassInstance.addEventListener("statechange", (newState) => {
console.log(`State changed to: ${newState}`);
});
myClassInstance.changeState("loading");
myClassInstance.changeState("ready");
Contributing
We welcome contributions from the community! Whether it's adding new features, fixing bugs, or improving documentation, please feel free to make a pull request.
Support
If you encounter any issues or have questions, please file an issue on the GitHub repository. We're here to help!
By using Simple Event Target, you can easily add robust event-handling capabilities to your TypeScript projects with minimal overhead and maximum type safety. Give it a try today!