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

evtmanager

v1.0.0

Published

Simple and easy to use eventemitter to manage your events synchronously and asynchronously too

Downloads

11

Readme

EvtManager

Simple and easy to use eventemitter to manage your events synchronously and asynchronously too for Deno, node and for the browser with a typesafe environment!

Installation

For deno

import { EventEmitter } from "https://raw.githubusercontent.com/Scientific-Guy/evtmanager/master/mod.ts";

For node and browser

> npm i evtmanager

Example

Evtmanager is same as the basic events package but with some additional utilities and typesafe environment!

import { EventEmitter } from "evtmanager";

interface EventMap{
    ready: () => void;
    start: () => void;
    destory: (reason?: string) => void;
    error: (reason?: string) => void;
}

const system = new EventEmitter<EventMap>();

system.on("ready", () => {
    console.log('System is ready!');
});

system.emit('ready');

Now here is an example of a typesafe environment

system.on("ready", () => {
    console.log('System is ready!');
}); // Will compile

system.on("ready", (ctx) => {
    console.log('System is ready!');
}); // Will not compile as it has a parameter ctx additional which is not supplied in the event map!

Here are some methods which you already might know!

const listener = () => console.log("System is ready");

system.addListener("ready", listener); // Adds an listener
system.on("ready", () => console.log("System is ready")); // Same as addListener method
system.once("ready", () => console.log("System is ready")); // Adds an listener which listens only one event

system.removeListener("ready", listener); // Removes a listener
system.removeListeners("ready"); // Removes all listeners registered on event "ready"
system.clear(); // Clears all event listeners

const listeners = system.getListeners("ready"); // Returns an array of listeners listening to the event
const count = system.listenerCount("ready"); // Returns number of listeners listening to the "ready" event

const responses = await system.emit("ready"); // Dispatches a event and runs all the listeners one by one awaiting it
const syncResponses = system.emitSync("ready"); // Dispatches a event and does not waits for listeners to return a value

system.maxListeners.set("ready", 5); // Set maximum listeners to 5 so only about 5 listeners can listen to the event!
system.maxListeners.delete("ready"); // Remove the limit
system.maxListeners.get("ready"); // Get limit

Awaiting responses

Waiting events means it waits for the emitter for the event to be emitted and returns the args received!

const response = await system.wait("destroy");
console.log(`System finally destoryed with the reason as: ${response}`);

This is used for the once method. You can even set a timeout for it!

try{
    const response = await system.wait("destroy", 2000);
    console.log(`System finally destoryed with the reason as: ${response}`);
} catch {
    console.log(`Could not catch the event within 2 seconds!`);
}

Iterating

You can iterate asynchronously over an event like this

This method is made as for some utility!

for await (const [reason] of system.iterator("error")) {
    console.log(`Found an error: ${reason}`);
}

MonoEmitter

MonoEmitter is nothing but the same eventemitter but does not needs any event name to register! For example, view the code block given below:

import { MonoEmitter } from "evtmanager";

const system = new MonoEmitter();

system.on(() => {
    console.log('System is ready!');
});

system.emit();

It has almost the same methods

const listener = () => console.log("I am emitted");

event.addListener(listener); // Adds an listener
event.on(() => console.log("I am emitted")); // Same as addListener method
event.once(() => console.log("I am emitted")); // Adds an listener which listens only one event

event.removeListener(listener); // Removes a listener
event.clear(); // Clears all event listeners

event.listeners; // Returns an array of listeners listening to the event
event.listenerCount; // Returns number of listeners listening to the "ready" event

const responses = await event.emit(); // Dispatches a event and runs all the listeners one by one awaiting it
const syncResponses = event.emitSync(); // Dispatches a event and does not waits for listeners to return a value

event.maxListenersCount = 5; // Set maximum listeners to 5 so only about 5 listeners can listen to the event!
delete event.maxListenersCount; // Remove the limit

And you can use the same utility methods to the MonoEmitter too!

try{
    const args = await event.wait(2000);
    console.log(args);
} catch {
    console.log(`Could not catch the event within 2 seconds!`);
}
for await (const args of event.iterator("error")) {
    console.log(`Caught a new event: `, args);
}