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

ueve

v1.69.1

Published

🔥 Hellishly fast and 🤏 tiny async/sync event emitter

Downloads

10

Readme

yarn add ueve # npm install ueve

Concept

μEve is designed to be as low-level and unopinionated as possible so you can build your own abstractions on top of it.

It supports both sync and async modes. That means you can use sync to solve problems where sync fits the job best and async where async fits the job best.


Performance

There are two aspects of performance that μEve is trying to solve.

1. Runtime performance

This one is obvious. It's what we usually think of when someone says performance.

μEve is just a small wrapper around Set. Thanks to that the performance is very close to the native performance of Set and its methods.

You can't really go any faster (see benchmark results).

2. Library size

What people often overlook is the size of a library. There are two major things that make bigger (in kb) libraries slower.

  1. When used in a browser the library needs to be downloaded before it can be parsed
  2. No matter where the library is used it needs to get parsed by the JS engine before it can be executed

μEve is trying to solve these issues by opting out of OOP approach (which many other similar libraries use). In OOP the code for all class methods has to be downloaded and parsed first before it can be executed.

Instead we create a simple object with private properties (with the help of symbols). The object is then passed into all of the methods as the first argument. This makes the methods importable and tree-shakeable.

All of this means that you download and parse only the code that ends up being executed.


Examples

  1. Simple counter
  2. Input transformation
  3. Input filtering
  4. Sync vs Async

API

eve

It creates an event with an optional Transform function.

The Transform function is used to transform I value from pub into a O value.

You can skip the value propagation by returning undefined inside the Transform function.

There are two versions of this function:

  1. Sync imported from ueve/sync
  2. Async imported from ueve/async

In the sync version the Transform function is a simple (sync) function. In the async version the Transform function is an async function which returns Promise.

This event just passes the input value to subscribers:

const message$ = eve<{
	to: string;
	date: Date;
	content: string;
}>();

This one transforms the input value:

const click$<[x: number, y: number]>(
	async ([x, y]) => ({ x, y })
);

And this one filters the input value:

const login$ = eve<User>(
	async (v) => !(v%2) ? v: undefined
);

sub

It adds the Subscriber to the event.

It returns uns function which deletes the Subscriber from the event.

const unsub = sub(
	message$, 
	(v) => console.log(`${v.to} -> ${v.content}`)
);

// later in the code
unsub();

uns

It deletes the Subscriber from the event.

In order to delete the same Subscriber you have to keep the reference to the function saved somewere yourself and use it both in sub and uns.

pub

It uses the Transform function to transform the input value and notify all subscribers using this transformed value.

If the Transform function returns undefined then no subscribers are notified.

There are two versions of this function:

  1. Sync imported from ueve/sync
  2. Async imported from ueve/async

In the sync version the pub function is a simple sync function. In the async version the pub function is an async function which returns Promise and has to be awaited.

has

It checks whether the event has the Subscriber.

clr

It deletes all subcribers from the event.