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

@libshaun/ts-event-dispatcher

v2.1.7

Published

A simple JS event dispatcher implementing the mediator & observer patterns

Downloads

6

Readme

ts-event-dispatcher

Version CI License Coverage

This is a simple library that allows your application components to communicate with each other by dispatching events and listening to them.

Installation

npm i @libshaun/ts-event-dispatcher

Usage

Events are identified by a unique name of your choosing. Any number of listeners might be listening for them. When an event is dispatched, data about that specific event is passed to listeners so that they have the information they need.

The EventDispatcher

Generally, you would create a single dispatcher and share it throughout your application. It maintains a registry of listeners. When an event is dispatched via the dispatcher, it notifies all listeners registered with that event.

import { EventDispatcher } from '@libshaun/ts-event-dispatcher'

const dispatcher = new EventDispatcher()

Dispatching an Event

Suppose you want to trigger an event when a user signs up on your website. Your event name might be user.created. When dispatching the event, you'll pass a User object to the listeners.

dispatcher.dispatch('user.created', { user, timestamp: 12345 })

Event Listeners

Event listeners are simple, callable functions that you register with the event dispatcher. The EventDispatcher passes your app-specific event data and a special context object (more on that later) to listeners when their event is dispatched.

dispatcher.addListener('user.created', (data) => {
  // ... do something ...
})

The addListener() method takes two or three arguments:

  1. An event name.
  2. A callable function to execute when the event is dispatched.
  3. An optional priority (defaults to 0), defined as a positive or negative integer. The higher the number, the earlier the listener is called. If two listeners have the same priority, they are executed in the order they were added to the dispatcher.

Async Event Listeners

Event listeners can be asynchronous. The dispatcher will call listeners in the intended order and await each one.

dispatcher.addListener('user.created', async ({ user }) => {
  await post('http://an-important-service', { user })
})

await dispatcher.dispatch('user.created', { user })

Stopping Event Propagation

Sometimes it may make sense for a listener to prevent the next listeners from being called. This can be accomplished with the special context object mentioned earlier. It's passed to event listeners as the second argument and can be used to stop event propagation.

const listener = (data, ev) => {
  // ... do something ...
  // Stop further propagation:
  ev.stopPropagation()
}

If you need to detect if propagation is stopped, the dispatcher returns the context object from dispatch().

const context = await dispatcher.dispatch('user.created', data)
console.log(context.isPropagationStopped)

Full Example

import { EventDispatcher } from '@libshaun/ts-event-dispatcher'

const dispatcher = new EventDispatcher()

// Set up an event listener
dispatcher.addListener('user.created', (data, ev) => {
  // ... do something ...
  sendWelcomeEmail(data.user)

  // Optionally, stop next listeners from being called.
  ev.stopPropagation()
})

// Dispatch the event
dispatcher.dispatch('user.created', { user })

TypeScript Example

import { EventDispatcher } from '@libshaun/ts-event-dispatcher'

interface User { name: string }
interface UserEvent { user: User }

// Provide a map of event names and data expectations.
const dispatcher = new EventDispatcher<{
  'user.created': UserEvent
}>()

// TypeScript will check both the event name and the listener signature.
dispatcher.addListener('user.created', (data, ev) => {
  // ... do something ...
  sendWelcomeEmail(data.user)

  // Optionally, stop next listeners from being called.
  ev.stopPropagation()
})

// TypeScript will check both the event name and the data signature.
dispatcher.dispatch('user.created', {
  user: 'John',           // causes TypeScript error
  user: { name: 'Sue' },  // good
  additionalProp: 123,    // causes TypeScript error
})

TSDX Bootstrap

This project was bootstrapped with TSDX.

Commands:

npm start
npm run build
npm test