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

@sapphirejs/event

v0.0.14

Published

Event System for Sapphire Framework

Downloads

1

Readme

Event

A thin wrapper for Node's native events. Mostly to offer a few abstractions, a base class to be extended by event classes, and generally a tighter integration with Sapphire Framework. Other than that, it doesn't have any logic of its own and can be completely ignored if you don't like even such thin abstractions.

Usage

$ npm install --save @sapphirejs/event

As you'd expect, events can be listened with on and emmited with emit.

const { Event } = require('@sapphirejs/event')

const event = new Event()
event.on('some.event', () => {
  console.log('Yay, I was called')
})
event.emit('some.event')

More interesting though, are event classes as a way to organize and encapsulate event handlers.

class SendWelcomeEmail {
  listen(userId) {
    // send email to user with id=userId
  }
}

event.on('user.registered', new SendWelcomeEmail().listen)
event.emit('user.registered', 10)

An event can have multiple listeners:

event.on('user.registered', new SendWelcomeEmail().listen)
event.on('user.registered', new SendConfirmationSms().listen)

// or

event.on('user.registered', [
  new SendWelcomeEmail().listen,
  new SendConfirmationSms().listen
])


event.emit('user.registered')

BaseEvent

The BaseEvent class enforces the usage of the listen method and has the capabilities to be extended by the framework, like injecting a context object as argument. It is completely optional to use, but if you decide on it, this is how it's used:

const { Event, BaseEvent } = require('@sapphirejs/event')

class SendWelcomeEmail extends BaseEvent {
  listen(userId) {
    // send email to user with id=userId
  }
}

const event = new Event()
event.on('user.registered', new SendWelcomeEmail())
event.emit('user.registered', 7)

Notice how we don't pass the listen method on the event listener. It is automatically resolved when extending BaseEvent.

Event Names

Event names can be anything unique, so a string or constant comes to mind. However, if you think about it, class are also a very good way of having non-string event names. You can't misspell them, because the runtime or even your IDE will complain, so fewer bugs. And they're so easy to use:

class UserWasRegistered {}

event.on(UserWasRegistered, new SendWelcomeEmail())
event.on(UserWasRegistered, new SendConfirmationSms())

event.emit(UserWasRegistered)