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

ts-dispatcher

v0.1.1

Published

Type-safe Event Dispatcher implementation.

Downloads

2

Readme

Type-Safe Event Dispatcher

This is a simple event dispatcher which implements the mediator pattern, where a well-known centralised object is available to send and recieve messages.

Messages sent (with .dispatch()) have an assigned event type (or event key) which can be any type (Symbol, string, object, function etc.). Recievers can subscribe (with .addListener()) to these specifict event types.

WHY?

There are too many Event Dispatcher implementations on the net. Yet here I am making another one. To be honest a fully functional event dispatcher can be made with 3 lines of code. What this package additionally provides are a set of type declarations attached to the dispatcher class. With these type declarations your IDE will provide code completion for all possible events.

Usage

import { Dispatcher } from 'ts-dispatcher';

// make a type which lists all possible events
type DispatchedEvents =
  Dispatcher.Event<'event-key-1', string, number> |
  Dispatcher.Event<'event-key-2', string> |
  Dispatcher.Event<'event-key-3', number>;

// create a dispatcher
const dispatcher = new Dispatcher<DispatchedEvents>();

dispatcher.dispatch('event-key-1', 'abc', 123); // OK.
dispatcher.dispatch('event-key-1', 'abc'); // type error: missing third parameter (number).

dispatcher.dispatch('event-key-2', 'abc'); // OK.
dispatcher.dispatch('event-key-2', 'abc', 123); // type error: unknown third parameter (number).

dispatcher.addListener('event-key-3', (param1) => console.log(param1)); // OK.
dispatcher.addListener('event-key-3', (param1) => console.log(JSON.parse(param1))); // type error: param1 is not a string (JSON.parse expects a string).
dispatcher.addListener('event-key-3', (param1, param2) => console.log(param1)); // type error: unknown argument param2.

dispatcher.addListener('event-key-4', () => console.log(4)); // type error: unknown 'event-key-4' argument.

Usage with interfaces

// src/component.ts

import { dispatcher } from './dispatcher.ts';

declare global {
  interface DispatchedEvents {
    'event-key-1': [string, number];
  }
}

dispatcher.dispatch('event-key-1', 'abc', 123); // OK.
dispatcher.dispatch('event-key-1', 'abc'); // type error: missing third parameter (number).
// src/dispatcher.ts

import { Dispatcher } from 'ts-dispatcher';

export const dispatcher = new Dispatcher<Dispatcher.FromInterface<DispatchedEvents>>();

Note: With interfaces only string keys are possible as event keys, but allows you to separate the dispatched event types into modules.

Docs

new Dispatcher<Events>()

The Events generic parameter expects a touple type ([T1, T2]) where T1 is the event key and T2 is an array of the expected arguments of the dispatched message.

Example:

const dispatcher = new Dispatcher<['hello', [string, number]] | ['world', [string]] | ['no-params', []]>();

There is a helper type to make touples more readable: Dispatcher.Event<Key, P1, P2, ...>

Example:

const dispatcher = new Dispatcher<Dispatcher.Event<'hello', string, number> | Dispatcher.Event<'world', string> | Dispatcher.Event<'no-params'>>();

dispatcher.dispatch(eventKey, param1, param2, ...)

Notifies all registered event listeners passing the param1, param2, ... to the listener as arguments.

dispatcher.addListener(eventKey, listener)

Register a new event listener. Registering the same listener for the same event type multiple times is ignored.

dispatcher.removeListener(eventKey, listener)

Removes a registered event listener.