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

@vecal/mitt

v0.0.2

Published

A library for the event publish-subscribe pattern

Downloads

1

Readme

mitt

mitt is a lightweight event handling system designed to manage events and callbacks. It provides a set of concise APIs for registering event listeners, triggering events, removing listeners, and clearing all events.

Features

  • Registering Listeners: Register multiple triggers or single triggers for specific events.
  • Triggering Events: Trigger registered events and pass parameters to listeners.
  • Removing Listeners: Remove specific event listeners as needed.
  • Clearing All Events: Remove all registered events and their listeners.

Installation

No separate installation is required. Simply import it directly into your project.

Usage Guide

Importing the Module

import mitt from '@vecal/mitt';

Interface Definition

on(eventName: Key, callback: Fn, time?: number): void

Registers an event listener, specifying the number of times it can trigger.

Parameters

  • eventName (Key): The event name.
  • callback (Fn): The callback function to register.
  • time (number): Optional parameter, specifies the maximum number of times the callback function can trigger. Default is infinite.

once(eventName: Key, callback: Fn): void

Registers a listener that triggers only once.

Parameters

  • eventName (Key): The event name.
  • callback (Fn): The callback function to register.

emit(eventName: Key, ...args: any[]): void

Triggers an event and passes parameters to the listeners.

Parameters

  • eventName (Key): The event name.
  • ...args (any[]): Additional arguments passed to the callback functions.

off(eventName: Key): void

Removes all listeners for a specific event.

Parameters

  • eventName (Key): The event name.

remove(eventName: Key, callback: Fn): void

Removes a specific listener for a specific event.

Parameters

  • eventName (Key): The event name.
  • callback (Fn): The callback function to remove.

clear(): void

Removes all registered events and their listeners.

Example

const mitt = new Mitt();

const callback = (data) => {
  console.log('Received update:', data);
};
// Register a listener
mitt.on('update', callback, 2); // Triggers only twice

// Register a one-time listener
mitt.once('finish', () => {
  console.log('Task finished');
});

// Trigger the event
mitt.emit('update', { message: 'New data available' });

// Remove the listener
mitt.remove('update', callback);

// This will not remove the one-time listener
mitt.remove('finish', () => {
  console.log('Task finished');
});

// Clear all events
mitt.clear();

API Documentation

Class: Mitt

Constructor

new Mitt();

Methods

on(eventName: Key, callback: Fn, time = Infinity): void

Registers an event listener, specifying the number of times it can trigger.

Parameters

  • eventName (Key): The event name.
  • callback (Fn): The callback function to register.
  • time (number): Optional parameter, specifies the maximum number of times the callback function can trigger. Default is infinite.
once(eventName: Key, callback: Fn): void

Registers a listener that triggers only once.

Parameters

  • eventName (Key): The event name.
  • callback (Fn): The callback function to register.
emit(eventName: Key, ...args: any[]): void

Triggers an event and passes parameters to the listeners.

Parameters

  • eventName (Key): The event name.
  • ...args (any[]): Additional arguments passed to the callback functions.
off(eventName: Key): void

Removes all listeners for a specific event.

Parameters

  • eventName (Key): The event name.
remove(eventName: Key, callback: Fn): void

Removes a specific listener for a specific event.

Parameters

  • eventName (Key): The event name.
  • callback (Fn): The callback function to remove.
clear(): void

Removes all registered events and their listeners.

License

This project is licensed under the MIT License. For details, please refer to the LICENSE file.