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

@grund/events

v0.0.85

Published

This package contains two key concepts: `Emitter` and `EventService`. But before we go into these implementations, we will just define the difference between a `listener` and `hook` in grund,

Downloads

148

Readme

@grund/events

This package contains two key concepts: Emitter and EventService. But before we go into these implementations, we will just define the difference between a listener and hook in grund,

  • listener
    • A listener is a subscribing function that listens to a specific event. The keyword here is that is only listens to the emitter. It should not and cannot alter the outcome of the emitted process.
  • hook
    • A hook is something that is called in a specific lifecycle process, e.g. beforeCreate and afterUpdate. This usually takes in one or several objects that is the target of the function (e.g. the object that should be created) and mutates them or returns a formatted version of the object. In other words, a hook can alter the outcome of the emitted process.

Emitter

The emitter is an object that stores events and callbacks. When creating an emitter one should pass along an EventSchema which determines what events are available.

A bit simplified, this is the interface exposed by an emitter.

interface EventSchema {
    log: (...args: any[]) => any;
}

interface Emitter<EventSchema> {
    on: <Key extends EventSchema>(key: Key, cb: EventSchema[Key]) => any
    once: <Key extends EventSchema>(key: Key, cb: EventSchema[Key]) => any

    off: <Key extends EventSchema>(key: Key, cb: EventSchema[Key]) => any
    offAll: <Key extends EventSchema>(key: Key) => any

    emit: <Key extends EventSchema> => Promise<Array<ReturnType<EventSchema[Key]>>>
}

Event service

The event service is a helper class for other services to extend. The only thing it does is to create two emitters called listeners and hooks.

So whenever another service wants to implement listeners and hooks, it just extends the EventService and can then start using the events. This is used in e.g. LoggingService and many of the auth services.

Usage

import { Event } from '@grund/events';
import { EventSchema } from './events';

class AnyService extends EventService<EventSchema> {}

const service = new AnyService();

service.listeners.on('test', () => console.log('test fired'));
service.listeners.emit('test');