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

houk

v3.2.1

Published

[![GitHub CI Status](https://img.shields.io/github/workflow/status/krmax44/houk/build/main)](https://github.com/krmax44/houk/actions?query=workflow%3Abuild) [![Code Coverage](https://img.shields.io/codecov/c/github/krmax44/houk)](https://codecov.io/gh/krm

Downloads

138

Readme

Houk

GitHub CI Status Code Coverage bundle size npm version

A safely typed, super simple, universal event bus with awesome IDE auto completions. Requires TypeScript 4+.

Installation

yarn add houk
# or using npm
npm i houk

Usage

See the complete API below.

import Houk from 'houk';

class CoffeeShop extends Houk<{
	order: [name: string, type: string];
}> {
	makeMeACoffee() {
		const name = 'Max';
		const type = 'Cappuccino with oat milk';

		this.emit('order', name, type);
	}
}

const barista = new CoffeeShop();
barista.on('order', (name, type) => {
	console.log(`${name} would like a ${type}, please.`);
});

barista.makeMeACoffee();

// --> Max would like a Cappuccino with oat milk, please.

See the complete API below.

import { HoukBus } from 'houk';

const coffeeShop = new HoukBus<{
	order: [name: string, type: string];
}>();

coffeeShop.on('order', (name, type) => {
	console.log(`${name} would like a ${type}, please.`);
});

const name = 'Max';
const type = 'Cappuccino with oat milk';
coffeeShop.emit('order', name, type);

// --> Max would like a Cappuccino with oat milk, please.

API

Constructor

The Houk constructor requires one type argument:

class MyClass extends Houk<{
	eventName: [arg1: string, arg2: number];
}> {}

The object links the event names to the arguments the listener functions will receive. As labeled tuple types were introduced with TypeScript 4, make sure to be on the latest version. In the example above, the listener function for the event eventName must be called with arg1 of type string and arg2 with type number. Rest parameters and optional types are possible.

Houk.on

Listen to a particular event. Takes the event name, a listener function and whether the listener should only be called once (false by default).

on(eventName, listener, once?): void

Houk.off

Unregister an event listener. Returns true on success and false when the event listener didn't exist before.

off(eventName, listener) => boolean

Houk.awaitEvent

Returns a promise that will resolve, once the given event was triggered.

await houk.awaitEvent('myEvent');
console.log('myEvent was emitted');

Houk.emit

Only available to deriving classes. Trigger all listeners of a particular event. ...args will be passed along to the listeners.

The listeners will all be called at once. It returns a Promise, which will resolve once all listeners have reached completion.

emit(eventName, ...args) => Promise

Houk.emitSync

Only available to deriving classes. Trigger all listeners of a particular event. ...args will be passed along to the listeners.

The listeners will be called once at a time, in order of registration. It returns a Promise, which will resolve once all listeners have reached completion.

emit(eventName, ...args) => Promise

Houk.getListeners

Only available to deriving classes, returns a set of listener functions.

getListeners(eventName) => Set<() => {})>

HoukBus

You can also create an open bus, using HoukBus:

import { HoukBus } from 'houk';

const bus = new HoukBus<EventTypes>();

The same API from Houk applies to HoukBus, except that all methods are public.

v3 Breaking Changes

Houk used to be both an event and hook chain bus. The concept of combining those two into one is neat, but definitely janky at times, which paired together with type safety doesn't feel right. Therefore, I decided to drop hook chains and make Houk a super-simple, type-safe event bus only.