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

event-emitters

v1.0.6

Published

event emitters

Downloads

174

Readme

event-emitters

build status Known Vulnerabilities Renovate

Typesafe event emitters.

EventEmitter<T>

Similar to node's EventEmitter but:

  • Only emits a single event of type T.
  • Throw an exception when a client registers the same listener more than once.
  • Throw an exception when a client tries to remove a listener that is not listening.
  • Support async iteration.
const emitter = new EventEmitter<number>()
const listener = (n: number): void => {
  console.log(n)
}

emitter.subscribe(listener)
emitter.emit(42)

try {
  emitter.subscribe(listener)
} catch (e) {
  console.log('same listener')
}

emitter.unsubscribe(listener)
try {
  emitter.unsubscribe(() => {})
} catch (e) {
  console.log('listener not found')
}

The above will log:

42
same listener
listener not found
const emitter = new EventEmitter<number>()

async function logEmitterValues(emitter: EventEmitter<number>) {
  for await (const val of emitter) {
    console.log(val)
  }
}

logEmitterValues(emitter)

emitter.emit(42)
emitter.emit(43)

The above will log:

42
43
const emitter = new EventEmitter<number>()
const listener = emitter.subscribe((val: number) => {
  console.log(val)
})
emitter.unsubscribe(listener)

subscribe returns the callback passed to it which can be useful for unsubscribing later.

EventEmitterWithCurrent<T>

Provides the same API as EventEmitter but:

  • Is initialized with the current message.
  • Emits the current message to each listener as soon as it subscribes.
const emitter = new EventEmitterWithCurrent<number>(42)
emitter.subscribe((n: number): void => {
  console.log(n)
})
emitter.emit(43)

The above will log:

42
43

EventEmitterWithOptionalCurrent<T>

Provides the same API as EventEmitterWithCurrent but:

  • Can optionally be initialized with the current message.
  • Emits the current message to each listener as soon as it subscribes only when the current message is available.
const emitter = new EventEmitterWithCurrent<number>()
emitter.subscribe((n: number): void => {
  console.log(n)
})
emitter.emit(43)

The above will log:

43

QueueingEventEmitter<T>

Provides the same API as EventEmitter but:

  • Queues messages when there are no subscribers.
  • Delivers queued message to the first subscriber, then drains the queue.
const emitter = new EventEmitterWithCurrent<number>(42)
emitter.emit(44)
emitter.emit(45)
emitter.subscribe((n: number): void => {
  console.log(n)
})
emitter.emit(46)

The above will log:

44
45
46