npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vrls-simple-event-target

v0.1.2

Published

A lightweight TypeScript library for creating type-safe event-driven classes with an API mirroring the browser's EventListener. Offers streamlined event management with no dependencies.

Downloads

10

Readme

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
# or
yarn add vrls-simple-event-target

Usage

To get started with Simple Event Target, follow these steps:

  1. 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.

  2. 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";

// Define possible states
type CustomState = "loading" | "ready";

// Define event map
type CustomEventMap = {
  statechange: (newState: CustomState) => void;
};

// Extend SimpleEventTarget to create a custom class
class MyClass extends SimpleEventTarget<CustomEventMap> {
  changeState(newState: CustomState) {
    // Dispatch the 'statechange' event with the new state
    this.dispatchEvent("statechange", newState);
  }
}

// Usage
const myClassInstance = new MyClass();
myClassInstance.addEventListener("statechange", (newState) => {
  console.log(`State changed to: ${newState}`);
});

// Trigger the state change
myClassInstance.changeState("loading"); // Logs: "State changed to: loading"
myClassInstance.changeState("ready"); // Logs: "State changed to: 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!